Esempio n. 1
0
        public VBIB(IKeyValueCollection data) : this()
        {
            var vertexBuffers = data.GetArray("m_vertexBuffers");

            foreach (var vb in vertexBuffers)
            {
                var vertexBuffer = BufferDataFromDATA(vb);

                var decompressedSize = vertexBuffer.ElementCount * vertexBuffer.ElementSizeInBytes;
                if (vertexBuffer.Data.Length != decompressedSize)
                {
                    vertexBuffer.Data = MeshOptimizerVertexDecoder.DecodeVertexBuffer((int)vertexBuffer.ElementCount, (int)vertexBuffer.ElementSizeInBytes, vertexBuffer.Data);
                }
                VertexBuffers.Add(vertexBuffer);
            }
            var indexBuffers = data.GetArray("m_indexBuffers");

            foreach (var ib in indexBuffers)
            {
                var indexBuffer = BufferDataFromDATA(ib);

                var decompressedSize = indexBuffer.ElementCount * indexBuffer.ElementSizeInBytes;
                if (indexBuffer.Data.Length != decompressedSize)
                {
                    indexBuffer.Data = MeshOptimizerIndexDecoder.DecodeIndexBuffer((int)indexBuffer.ElementCount, (int)indexBuffer.ElementSizeInBytes, indexBuffer.Data);
                }

                IndexBuffers.Add(indexBuffer);
            }
        }
Esempio n. 2
0
        // Find apropriate vertex buffer and create it if doesn't exist
        public NVRVertexBuffer GetVertexBuffer(int vertexToAddCount, NVRVertexType type)
        {
            foreach (NVRVertexBuffer buffer in VertexBuffers)
            {
                if (buffer.Type == type && buffer.Vertices.Count < (UInt16.MaxValue - vertexToAddCount))
                {
                    return(buffer);
                }
            }
            NVRVertexBuffer created = new NVRVertexBuffer(type);

            VertexBuffers.Add(created);
            return(created);
        }
Esempio n. 3
0
        bool ReadVertexBufferHack(Blam.CacheFile c,
                                  geometry_block_info_struct gbi)
        {
            var rsrc_cache = Program.Halo2.FromLocation(c as Halo2.CacheFile, gbi.GetBlockOffset());

            // the shared cache isn't loaded, break
            if (rsrc_cache == null)
            {
                return(false);
            }

            // seek to the actual data
            var er = rsrc_cache.InputStream;

            er.Seek(gbi.GetBlockOffset().Offset + 8);

            VertexBuffers.Add();
            VertexBuffers.Read(er);

            return(true);
        }
Esempio n. 4
0
        public override void Read(BinaryReader reader, Resource resource)
        {
            reader.BaseStream.Position = Offset;

            var vertexBufferOffset = reader.ReadUInt32();
            var vertexBufferCount  = reader.ReadUInt32();
            var indexBufferOffset  = reader.ReadUInt32();
            var indexBufferCount   = reader.ReadUInt32();

            reader.BaseStream.Position = Offset + vertexBufferOffset;
            for (var i = 0; i < vertexBufferCount; i++)
            {
                var vertexBuffer = ReadOnDiskBufferData(reader);

                var decompressedSize = vertexBuffer.ElementCount * vertexBuffer.ElementSizeInBytes;
                if (vertexBuffer.Data.Length != decompressedSize)
                {
                    vertexBuffer.Data = MeshOptimizerVertexDecoder.DecodeVertexBuffer((int)vertexBuffer.ElementCount, (int)vertexBuffer.ElementSizeInBytes, vertexBuffer.Data);
                }

                VertexBuffers.Add(vertexBuffer);
            }

            reader.BaseStream.Position = Offset + 8 + indexBufferOffset; //8 to take into account vertexOffset / count
            for (var i = 0; i < indexBufferCount; i++)
            {
                var indexBuffer = ReadOnDiskBufferData(reader);

                var decompressedSize = indexBuffer.ElementCount * indexBuffer.ElementSizeInBytes;
                if (indexBuffer.Data.Length != decompressedSize)
                {
                    indexBuffer.Data = MeshOptimizerIndexDecoder.DecodeIndexBuffer((int)indexBuffer.ElementCount, (int)indexBuffer.ElementSizeInBytes, indexBuffer.Data);
                }

                IndexBuffers.Add(indexBuffer);
            }
        }
        private void CommitCurrentBuffers()
        {
            if (!_inBuffer)
            {
                return;
            }

            _device.Unmap(_currentVertexBuffer);
            _device.Unmap(_currentIndexBuffer);

            var numInd  = _currentIndirectArguments.Count;
            var indSize = Unsafe.SizeOf <IndirectDrawIndexedArguments>();

            if (numInd == 0)
            {
                // No indirect arguments, this buffer is empty.
                _currentVertexBuffer.Dispose();
                _currentIndexBuffer.Dispose();

                _currentVertexMap         = _currentIndexMap = default(MappedResource);
                _currentIndexBuffer       = _currentVertexBuffer = null;
                _currentVertexOffset      = _currentIndexOffset = 0;
                _currentIndirectArguments = null;
                _inBuffer = false;
                return;
            }

            var  bufferGroups   = new List <BufferGroup>();
            var  indirectBuffer = new IndirectIndirectBuffer(_device, numInd * indSize);
            uint indirOffset    = 0;

            foreach (var g in _currentIndirectArguments.GroupBy(x => new { x.Pipeline, x.Camera, x.HasTransparency, x.Binding }))
            {
                if (g.Key.HasTransparency)
                {
                    foreach (var ia in g)
                    {
                        indirectBuffer.Update(indirOffset * indSize, ia.Arguments);
                        var bg = new BufferGroup(ia.Pipeline, ia.Camera, ia.Location, ia.Binding, indirOffset, 1);
                        bufferGroups.Add(bg);
                        indirOffset += 1;
                    }
                }
                else
                {
                    var indir      = g.Select(x => x.Arguments).ToArray();
                    var indirCount = (uint)indir.Length;

                    indirectBuffer.Update(indirOffset * indSize, indir);

                    var bg = new BufferGroup(g.Key.Pipeline, g.Key.Camera, g.Key.Binding, indirOffset, indirCount);
                    bufferGroups.Add(bg);

                    indirOffset += indirCount;
                }
            }

            VertexBuffers.Add(_currentVertexBuffer);
            IndexBuffers.Add(_currentIndexBuffer);
            IndirectBuffers.Add(indirectBuffer);
            IndirectBufferGroups.Add(bufferGroups);

#if DEBUG
            AllocationInformation.Add(new BufferAllocation(_currentVertexBuffer.SizeInBytes, _currentVertexOffset, _currentIndexBuffer.SizeInBytes, _currentIndexOffset));
#endif

            _currentVertexMap         = _currentIndexMap = default(MappedResource);
            _currentIndexBuffer       = _currentVertexBuffer = null;
            _currentVertexOffset      = _currentIndexOffset = 0;
            _currentIndirectArguments = null;
            _inBuffer = false;
        }
Esempio n. 6
0
        public override void Read(BinaryReader reader, Resource resource)
        {
            reader.BaseStream.Position = Offset;

            var vertexOffset = reader.ReadUInt32();
            var vertexCount  = reader.ReadUInt32();
            var indexOffset  = reader.ReadUInt32();
            var indexCount   = reader.ReadUInt32();

            reader.BaseStream.Position = Offset + vertexOffset;
            for (var i = 0; i < vertexCount; i++)
            {
                var vertexBuffer = default(VertexBuffer);

                vertexBuffer.Count = reader.ReadUInt32();            //0
                vertexBuffer.Size  = reader.ReadUInt32();            //4
                var decompressedSize = vertexBuffer.Count * vertexBuffer.Size;

                var refA            = reader.BaseStream.Position;
                var attributeOffset = reader.ReadUInt32();  //8
                var attributeCount  = reader.ReadUInt32();  //12

                //TODO: Read attributes in the future
                var refB       = reader.BaseStream.Position;
                var dataOffset = reader.ReadUInt32();       //16
                var totalSize  = reader.ReadUInt32();       //20

                // TODO: underlords has compressed buffers
                if (totalSize != decompressedSize)
                {
                    throw new NotImplementedException($"Vertex buffer totalSize ({totalSize}) != decompressedSize ({decompressedSize})");
                }

                vertexBuffer.Attributes = new List <VertexAttribute>();

                reader.BaseStream.Position = refA + attributeOffset;
                for (var j = 0; j < attributeCount; j++)
                {
                    var previousPosition = reader.BaseStream.Position;

                    var attribute = default(VertexAttribute);

                    attribute.Name = reader.ReadNullTermString(Encoding.UTF8);

                    // Offset is always 40 bytes from the start
                    reader.BaseStream.Position = previousPosition + 36;

                    attribute.Type   = (DXGI_FORMAT)reader.ReadUInt32();
                    attribute.Offset = reader.ReadUInt32();

                    // There's unusual amount of padding in attributes
                    reader.BaseStream.Position = previousPosition + 56;

                    vertexBuffer.Attributes.Add(attribute);
                }

                reader.BaseStream.Position = refB + dataOffset;

                vertexBuffer.Buffer = reader.ReadBytes((int)totalSize);
                VertexBuffers.Add(vertexBuffer);

                reader.BaseStream.Position = refB + 4 + 4; //Go back to the vertex array to read the next iteration
            }

            reader.BaseStream.Position = Offset + 8 + indexOffset; //8 to take into account vertexOffset / count
            for (var i = 0; i < indexCount; i++)
            {
                var indexBuffer = default(IndexBuffer);

                indexBuffer.Count = reader.ReadUInt32();        //0
                indexBuffer.Size  = reader.ReadUInt32();        //4
                var decompressedSize = indexBuffer.Count * indexBuffer.Size;

                var unknown1 = reader.ReadUInt32();     //8
                var unknown2 = reader.ReadUInt32();     //12

                var refC       = reader.BaseStream.Position;
                var dataOffset = reader.ReadUInt32();   //16
                var dataSize   = reader.ReadUInt32();   //20

                if (dataSize != decompressedSize)
                {
                    throw new NotImplementedException($"Index buffer dataSize ({dataSize}) != decompressedSize ({decompressedSize})");
                }

                reader.BaseStream.Position = refC + dataOffset;

                indexBuffer.Buffer = reader.ReadBytes((int)dataSize);
                IndexBuffers.Add(indexBuffer);

                reader.BaseStream.Position = refC + 4 + 4; //Go back to the index array to read the next iteration.
            }
        }
Esempio n. 7
0
        public override void Read(BinaryReader reader, Resource resource)
        {
            reader.BaseStream.Position = Offset;

            var vertexBufferOffset = reader.ReadUInt32();
            var vertexBufferCount  = reader.ReadUInt32();
            var indexBufferOffset  = reader.ReadUInt32();
            var indexBufferCount   = reader.ReadUInt32();

            reader.BaseStream.Position = Offset + vertexBufferOffset;
            for (var i = 0; i < vertexBufferCount; i++)
            {
                var vertexBuffer = default(VertexBuffer);

                vertexBuffer.Count = reader.ReadUInt32();            //0
                vertexBuffer.Size  = reader.ReadUInt32();            //4
                var decompressedSize = vertexBuffer.Count * vertexBuffer.Size;

                var refA            = reader.BaseStream.Position;
                var attributeOffset = reader.ReadUInt32();  //8
                var attributeCount  = reader.ReadUInt32();  //12

                //TODO: Read attributes in the future
                var refB       = reader.BaseStream.Position;
                var dataOffset = reader.ReadUInt32();       //16
                var totalSize  = reader.ReadUInt32();       //20

                vertexBuffer.Attributes = new List <VertexAttribute>();

                reader.BaseStream.Position = refA + attributeOffset;
                for (var j = 0; j < attributeCount; j++)
                {
                    var previousPosition = reader.BaseStream.Position;

                    var attribute = default(VertexAttribute);

                    attribute.Name = reader.ReadNullTermString(Encoding.UTF8).ToUpperInvariant();

                    // Offset is always 40 bytes from the start
                    reader.BaseStream.Position = previousPosition + 36;

                    attribute.Type   = (DXGI_FORMAT)reader.ReadUInt32();
                    attribute.Offset = reader.ReadUInt32();

                    // There's unusual amount of padding in attributes
                    reader.BaseStream.Position = previousPosition + 56;

                    vertexBuffer.Attributes.Add(attribute);
                }

                reader.BaseStream.Position = refB + dataOffset;

                var vertexBufferBytes = reader.ReadBytes((int)totalSize);
                if (totalSize == decompressedSize)
                {
                    vertexBuffer.Buffer = vertexBufferBytes;
                }
                else
                {
                    vertexBuffer.Buffer = MeshOptimizerVertexDecoder.DecodeVertexBuffer((int)vertexBuffer.Count, (int)vertexBuffer.Size, vertexBufferBytes);
                }

                VertexBuffers.Add(vertexBuffer);

                reader.BaseStream.Position = refB + 4 + 4; //Go back to the vertex array to read the next iteration
            }

            reader.BaseStream.Position = Offset + 8 + indexBufferOffset; //8 to take into account vertexOffset / count
            for (var i = 0; i < indexBufferCount; i++)
            {
                var indexBuffer = default(IndexBuffer);

                indexBuffer.Count = reader.ReadUInt32();        //0
                indexBuffer.Size  = reader.ReadUInt32();        //4
                var decompressedSize = indexBuffer.Count * indexBuffer.Size;

                var unknown1 = reader.ReadUInt32();     //8
                var unknown2 = reader.ReadUInt32();     //12

                var refC       = reader.BaseStream.Position;
                var dataOffset = reader.ReadUInt32();   //16
                var dataSize   = reader.ReadUInt32();   //20

                reader.BaseStream.Position = refC + dataOffset;

                if (dataSize == decompressedSize)
                {
                    indexBuffer.Buffer = reader.ReadBytes((int)dataSize);
                }
                else
                {
                    indexBuffer.Buffer = MeshOptimizerIndexDecoder.DecodeIndexBuffer((int)indexBuffer.Count, (int)indexBuffer.Size, reader.ReadBytes((int)dataSize));
                }

                IndexBuffers.Add(indexBuffer);

                reader.BaseStream.Position = refC + 4 + 4; //Go back to the index array to read the next iteration.
            }
        }
Esempio n. 8
0
        public override void Read(BinaryReader reader, Resource resource)
        {
            reader.BaseStream.Position = Offset;

            var vertexOffset = reader.ReadUInt32();
            var vertexCount  = reader.ReadUInt32();

            reader.BaseStream.Position = Offset + vertexOffset;
            for (var i = 0; i < vertexCount; i++)
            {
                var vertexBuffer = default(VertexBuffer);

                vertexBuffer.Count = reader.ReadUInt32();            //0
                vertexBuffer.Size  = reader.ReadUInt32();            //4

                var refA            = reader.BaseStream.Position;
                var attributeOffset = reader.ReadUInt32();  //8
                var attributeCount  = reader.ReadUInt32();  //12

                //TODO: Read attributes in the future
                var refB       = reader.BaseStream.Position;
                var dataOffset = reader.ReadUInt32();       //16
                var totalSize  = reader.ReadUInt32();       //20

                vertexBuffer.Attributes = new List <VertexAttribute>();

                reader.BaseStream.Position = refA + attributeOffset;
                for (var j = 0; j < attributeCount; j++)
                {
                    var previousPosition = reader.BaseStream.Position;

                    var attribute = default(VertexAttribute);

                    attribute.Name = reader.ReadNullTermString(Encoding.UTF8);

                    // Offset is always 40 bytes from the start
                    reader.BaseStream.Position = previousPosition + 36;

                    attribute.Type   = (DXGI_FORMAT)reader.ReadUInt32();
                    attribute.Offset = reader.ReadUInt32();

                    // There's unusual amount of padding in attributes
                    reader.BaseStream.Position = previousPosition + 56;

                    vertexBuffer.Attributes.Add(attribute);
                }

                reader.BaseStream.Position = refB + dataOffset;

                vertexBuffer.Buffer = reader.ReadBytes((int)vertexBuffer.Count * (int)vertexBuffer.Size);
                VertexBuffers.Add(vertexBuffer);

                reader.BaseStream.Position = refB + 4 + 4; //Go back to the vertex array to read the next iteration

                //if(i > 0)break; // TODO: Read only first buffer
            }

            reader.BaseStream.Position = Offset + 4 + 4; //We are back at the header.

            var indexOffset = reader.ReadUInt32();
            var indexCount  = reader.ReadUInt32();

            reader.BaseStream.Position = Offset + 8 + indexOffset; //8 to take into account vertexOffset / count
            for (var i = 0; i < indexCount; i++)
            {
                var indexBuffer = default(IndexBuffer);

                indexBuffer.Count = reader.ReadUInt32(); //0
                indexBuffer.Size  = reader.ReadUInt32(); //4

                var unknown1 = reader.ReadUInt32();      //8
                var unknown2 = reader.ReadUInt32();      //12

                var refC       = reader.BaseStream.Position;
                var dataOffset = reader.ReadUInt32();   //16
                var dataSize   = reader.ReadUInt32();   //20

                reader.BaseStream.Position = refC + dataOffset;

                indexBuffer.Buffer = reader.ReadBytes((int)indexBuffer.Count * (int)indexBuffer.Size);
                IndexBuffers.Add(indexBuffer);

                reader.BaseStream.Position = refC + 4 + 4; //Go back to the index array to read the next iteration.

                //if(i > 0)break; // TODO: Read only first buffer
            }
        }