Beispiel #1
0
        private byte[] LoadTile(SharpDX.Direct3D11.TiledResourceCoordinate coordinate)
        {
            var subresourceInFile = (coordinate.Subresource / _subresourcesPerFaceInResource) * _subresourcesPerFaceInFile
                                    + coordinate.Subresource % _subresourcesPerFaceInResource;

            var fileOffset = (_subresourceTileOffsets[subresourceInFile] + (coordinate.Y * _tilingInfo[coordinate.Subresource].WidthInTiles + coordinate.X)) * SampleSettings.TileSizeInBytes;

            //var data = new byte[SampleSettings.TileSizeInBytes];
            var data = GetOrCreateBuffer();

            using (var stream = File.Open(_filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                stream.Seek(fileOffset, SeekOrigin.Begin);
                var bytesRead = stream.Read(data, 0, data.Length);
                Debug.Assert(bytesRead == data.Length);
            }

            // set a border around the loaded tile with a color depending on the mip level
            if (_border)
            {
                const int blockRows   = 256 / 4;
                const int blockCols   = 512 / 4;
                const int borderWidth = 2;

                var colorSelect = (17 - subresourceInFile % 15);

                for (var blockRow = 0; blockRow < blockRows; blockRow++)
                {
                    for (var blockCol = 0; blockCol < blockCols; blockCol++)
                    {
                        var shouldAddBorder = blockRow < borderWidth ||
                                              blockRow >= (blockRows - borderWidth) ||
                                              blockCol <borderWidth ||
                                                        blockCol> (blockCols - borderWidth);

                        if (shouldAddBorder)
                        {
                            var color = colors[colorSelect % colors.Length];

                            var baseIndex = (blockRow * blockCols + blockCol) * 8;

                            data[baseIndex]     = (byte)((color & 0x00FF));
                            data[baseIndex + 1] = (byte)((color & 0xFF00) >> 8);

                            for (var i = 1; i < 4; i++)
                            {
                                data[baseIndex + i * sizeof(ushort)]     = 0;
                                data[baseIndex + i * sizeof(ushort) + 1] = 0;
                            }
                        }
                    }
                }
            }

            return(data);
        }
Beispiel #2
0
        /// <summary>
        /// Process decoded samples to determine which tiles are brought into view and need to be loaded
        /// </summary>
        /// <param name="samples">The list of the samples</param>
        public void EnqueueSamples(List <DecodedSample> samples)
        {
            foreach (var sample in samples)
            {
                // iterate trough each managed resource for each sample
                foreach (var resource in _managedTiledResources)
                {
                    var desc = resource.Texture.Description;

                    var mipCount = desc.Format == SharpDX.Toolkit.Graphics.PixelFormat.BC1.UNorm
                                       ? SampleSettings.TerrainAssets.Diffuse.UnpackedMipCount
                                       : SampleSettings.TerrainAssets.Normal.UnpackedMipCount;

                    // determine the available mip level
                    var actualMip = Math.Max(0, Math.Min(mipCount - 1, sample.Mip));

                    // process all mip levels from the current one to the least detailed
                    for (var mip = actualMip; mip < desc.MipLevels; mip++)
                    {
                        var coordinate = new SharpDX.Direct3D11.TiledResourceCoordinate();
                        coordinate.Subresource = mip + sample.Face * desc.MipLevels; // compute subresource index

                        var widthInTiles  = resource.SubresourceTilings[coordinate.Subresource].WidthInTiles;
                        var heightInTiles = resource.SubresourceTilings[coordinate.Subresource].HeightInTiles;

                        // compute tile coordinate in the face texture for the current mip level
                        coordinate.X = (int)Math.Min(widthInTiles - 1, Math.Max(0, widthInTiles * sample.U));
                        coordinate.Y = (int)Math.Min(heightInTiles - 1, Math.Max(0, heightInTiles * sample.V));

                        var         key = new TileKey(coordinate, resource.Texture);
                        TrackedTile trackedTile;
                        // if the tile has not been tracked yet - mark it as seen
                        if (!_trackedTiles.TryGetValue(key, out trackedTile))
                        {
                            trackedTile = new TrackedTile(resource, coordinate, (short)mip, sample.Face)
                            {
                                lastSeen = _frame,
                                State    = TileState.Seen
                            };

                            _trackedTiles[key] = trackedTile;
                            _seenTiles.Add(trackedTile);
                        }
                        else // ... otherwise just update its last seen time
                        {
                            trackedTile.lastSeen = _frame;
                        }
                    }
                }
            }

            _frame++;
        }
Beispiel #3
0
        /// <summary>
        /// Loads all packed mips.
        /// </summary>
        /// <returns>The async loading task</returns>
        public Task InitializeManagedResourcesAsync()
        {
            Reset();

            var tileLoadTasks = new List <Task>();

            var context = _graphicsDeviceManager.GetContext2();

            foreach (var resource in _managedTiledResources)
            {
                var r = resource;

                if (r.PackedMipDescription.PackedMipCount == 0)
                {
                    continue;
                }

                var coordinate = new SharpDX.Direct3D11.TiledResourceCoordinate();

                for (var face = 0; face < 6; face++)
                {
                    for (var i = 0; i < r.PackedMipDescription.PackedMipCount; i++)
                    {
                        var mip = r.Texture.Description.MipLevels - i - 1;
                        coordinate.Subresource = face * r.Texture.Description.MipLevels + mip;

                        var c = coordinate;
                        tileLoadTasks.Add(r.Loader.LoadTileAsync(c)
                                          .ContinueWith(t =>
                        {
                            var pitch = GetPitch(r.TileShape.WidthInTexels, r.Texture.Description.Format);
                            unsafe
                            {
                                fixed(void *pdata = &t.Result[0])
                                {
                                    context.UpdateSubresource(r.Texture,
                                                              c.Subresource,
                                                              null,
                                                              new IntPtr(pdata),
                                                              pitch,
                                                              0);
                                }
                            }

                            r.Loader.CompleteLoading(t.Result);
                        }));
                    }
                }
            }

            return(Task.Factory.StartNew(() => Task.WaitAll(tileLoadTasks.ToArray())));
        }
Beispiel #4
0
 /// <summary>
 /// Launches tile loading as async task.
 /// </summary>
 /// <param name="coordinate">The coordinate of the tile to load.</param>
 /// <returns>A task which will load the tile data asyncronously</returns>
 public Task <byte[]> LoadTileAsync(SharpDX.Direct3D11.TiledResourceCoordinate coordinate)
 {
     return(Task.Factory.StartNew(() => LoadTile(coordinate)));
 }