internal SdkMeshIndexBuffer(D3D11Device device, SdkMeshRawFile rawFile, SdkMeshRawMesh rawMesh)
        {
            int index = rawMesh.IndexBuffer;
            SdkMeshRawIndexBufferHeader header = rawFile.IndexBufferHeaders[index];

            byte[] bytes = rawFile.IndexBufferBytes[index];

            this.NumIndices = (int)header.NumIndices;
            this.SizeBytes  = (uint)header.SizeBytes;

            switch (header.IndexType)
            {
            case SdkMeshIndexType.IndexType16Bit:
                this.IndexFormat = DxgiFormat.R16UInt;
                break;

            case SdkMeshIndexType.IndexType32Bit:
                this.IndexFormat = DxgiFormat.R32UInt;
                break;

            default:
                this.IndexFormat = DxgiFormat.R16UInt;
                break;
            }

            var desc = new D3D11BufferDesc((uint)header.SizeBytes, D3D11BindOptions.IndexBuffer);
            var data = new D3D11SubResourceData(bytes, 0, 0);

            this.Buffer = device.CreateBuffer(desc, data);
        }
        internal SdkMeshVertexBuffer(D3D11Device device, SdkMeshRawFile rawFile, SdkMeshRawMesh rawMesh, int i)
        {
            int index = rawMesh.VertexBuffers[i];
            SdkMeshRawVertexBufferHeader header = rawFile.VertexBufferHeaders[index];

            byte[] bytes = rawFile.VertexBufferBytes[index];

            this.NumVertices = (int)header.NumVertices;
            this.SizeBytes   = (uint)header.SizeBytes;
            this.StrideBytes = (uint)header.StrideBytes;
            this.Decl        = header.Decl.ToArray();

            var desc = new D3D11BufferDesc((uint)header.SizeBytes, D3D11BindOptions.VertexBuffer);
            var data = new D3D11SubResourceData(bytes, 0, 0);

            this.Buffer = device.CreateBuffer(desc, data);
        }
        private static bool FillInitData(
            int width,
            int height,
            int depth,
            int mipCount,
            int arraySize,
            DxgiFormat format,
            int maxSize,
            byte[] bitData,
            out int twidth,
            out int theight,
            out int tdepth,
            out int skipMip,
            out D3D11SubResourceData[] initData)
        {
            skipMip  = 0;
            twidth   = 0;
            theight  = 0;
            tdepth   = 0;
            initData = new D3D11SubResourceData[mipCount * arraySize];

            int pSrcBits = 0;
            int index    = 0;

            for (int j = 0; j < arraySize; j++)
            {
                int w = width;
                int h = height;
                int d = depth;

                for (int i = 0; i < mipCount; i++)
                {
                    DdsHelpers.GetSurfaceInfo(w, h, (DdsFormat)format, out int NumBytes, out int RowBytes, out _);

                    if ((mipCount <= 1) || maxSize == 0 || (w <= maxSize && h <= maxSize && d <= maxSize))
                    {
                        if (twidth == 0)
                        {
                            twidth  = w;
                            theight = h;
                            tdepth  = d;
                        }

                        int dataLength = NumBytes * d;
                        var data       = new byte[dataLength];
                        Array.Copy(bitData, pSrcBits, data, 0, dataLength);

                        initData[index] = new D3D11SubResourceData(data, (uint)RowBytes, (uint)NumBytes);
                        index++;
                    }
                    else if (j == 0)
                    {
                        // Count number of skipped mipmaps (first item only)
                        skipMip++;
                    }

                    pSrcBits += NumBytes * d;

                    w >>= 1;
                    h >>= 1;
                    d >>= 1;

                    if (w == 0)
                    {
                        w = 1;
                    }

                    if (h == 0)
                    {
                        h = 1;
                    }

                    if (d == 0)
                    {
                        d = 1;
                    }
                }
            }

            return(index > 0);
        }
        private void CreateTextures(OptFile opt)
        {
            foreach (var textureKey in opt.Textures)
            {
                var optTextureName = textureKey.Key;
                var optTexture     = textureKey.Value;

                int mipLevels = optTexture.MipmapsCount;

                if (mipLevels == 0)
                {
                    continue;
                }

                D3D11SubResourceData[] textureSubResData = new D3D11SubResourceData[mipLevels];

                int bpp = optTexture.BitsPerPixel;

                if (bpp == 8)
                {
                    List <XMUInt3> palette = Enumerable.Range(0, 256)
                                             .Select(i =>
                    {
                        ushort c = BitConverter.ToUInt16(optTexture.Palette, 8 * 512 + i * 2);

                        byte r = (byte)((c & 0xF800) >> 11);
                        byte g = (byte)((c & 0x7E0) >> 5);
                        byte b = (byte)(c & 0x1F);

                        r = (byte)((r * (0xffU * 2) + 0x1fU) / (0x1fU * 2));
                        g = (byte)((g * (0xffU * 2) + 0x3fU) / (0x3fU * 2));
                        b = (byte)((b * (0xffU * 2) + 0x1fU) / (0x1fU * 2));

                        return(new XMUInt3(r, g, b));
                    })
                                             .ToList();

                    for (int level = 0; level < mipLevels; level++)
                    {
                        byte[] imageData = optTexture.GetMipmapImageData(level, out int width, out int height);
                        byte[] alphaData = optTexture.GetMipmapAlphaData(level, out int w, out int h);

                        int    size        = width * height;
                        byte[] textureData = new byte[size * 4];

                        for (int i = 0; i < size; i++)
                        {
                            int     colorIndex = imageData[i];
                            XMUInt3 color      = palette[colorIndex];

                            textureData[i * 4 + 0] = (byte)color.Z;
                            textureData[i * 4 + 1] = (byte)color.Y;
                            textureData[i * 4 + 2] = (byte)color.X;
                            textureData[i * 4 + 3] = alphaData != null ? alphaData[i] : (byte)255;
                        }

                        textureSubResData[level] = new D3D11SubResourceData(textureData, (uint)width * 4);
                    }
                }
                else if (bpp == 32)
                {
                    for (int level = 0; level < mipLevels; level++)
                    {
                        byte[] imageData = optTexture.GetMipmapImageData(level, out int width, out int height);

                        textureSubResData[level] = new D3D11SubResourceData(imageData, (uint)width * 4);
                    }
                }
                else
                {
                    continue;
                }

                D3D11Texture2DDesc textureDesc = new D3D11Texture2DDesc(DxgiFormat.B8G8R8A8UNorm, (uint)optTexture.Width, (uint)optTexture.Height, 1, (uint)textureSubResData.Length);

                using (var texture = this.deviceResources.D3DDevice.CreateTexture2D(textureDesc, textureSubResData))
                {
                    D3D11ShaderResourceViewDesc textureViewDesc = new D3D11ShaderResourceViewDesc
                    {
                        Format        = textureDesc.Format,
                        ViewDimension = D3D11SrvDimension.Texture2D,
                        Texture2D     = new D3D11Texture2DSrv
                        {
                            MipLevels       = textureDesc.MipLevels,
                            MostDetailedMip = 0
                        }
                    };

                    this.textureViews.Add(optTextureName, this.deviceResources.D3DDevice.CreateShaderResourceView(texture, textureViewDesc));
                }
            }
        }