Ejemplo n.º 1
0
        public void Test1D()
        {
            var textureData = new byte[256];
            for(int i = 0; i < textureData.Length; i++)
                textureData[i] = (byte)i;

            // -------------------------------------------------------
            // General test for a Texture1D
            // -------------------------------------------------------
            
            // Create Texture1D
            var texture = Texture1D.New(GraphicsDevice, textureData.Length, PixelFormat.R8.UNorm);

            // Check description against native description
            var d3d11Texture = (Direct3D11.Texture1D)texture;
            var d3d11SRV = (Direct3D11.ShaderResourceView)texture;

            Assert.AreEqual(d3d11Texture.Description, new Direct3D11.Texture1DDescription() {
                Width = textureData.Length,
                ArraySize = 1,
                BindFlags = BindFlags.ShaderResource,
                CpuAccessFlags = CpuAccessFlags.None,
                Format = DXGI.Format.R8_UNorm,
                MipLevels = 1,
                OptionFlags = ResourceOptionFlags.None,
                Usage = ResourceUsage.Default
            });

            // Check shader resource view.
            var srvDescription = d3d11SRV.Description;
            // Clear those fields that are garbage returned from ShaderResourceView.Description.
            srvDescription.Texture2DArray.ArraySize = 0;
            srvDescription.Texture2DArray.FirstArraySlice = 0;

            Assert.AreEqual(srvDescription, new Direct3D11.ShaderResourceViewDescription()
            {
                Dimension = ShaderResourceViewDimension.Texture1D,
                Format = DXGI.Format.R8_UNorm,
                Texture1D = { MipLevels = 1, MostDetailedMip = 0 },
            });

            // Check mipmap description
            var mipmapDescription = texture.GetMipMapDescription(0);
            var rowStride = textureData.Length * sizeof(byte);
            var refMipmapDescription = new MipMapDescription(textureData.Length, 1, 1, rowStride, rowStride, textureData.Length, 1);
            Assert.AreEqual(mipmapDescription, refMipmapDescription);

            // Check that getting the default SRV is the same as getting the first mip/array
            Assert.AreEqual(texture.ShaderResourceView[ViewType.Full, 0, 0].View, d3d11SRV);

            // Check GraphicsDevice.GetData/SetData data
            // Upload the textureData to the GPU
            texture.SetData(GraphicsDevice, textureData);

            // Read back data from the GPU
            var readBackData = texture.GetData<byte>();

            // Check that both content are equal
            Assert.True(Utilities.Compare(textureData, readBackData));

            // -------------------------------------------------------
            // Check with Texture1D.Clone and GraphicsDevice.Copy
            // -------------------------------------------------------
            using (var texture2 = texture.Clone<Texture1D>())
            {
                GraphicsDevice.Copy(texture, texture2);

                readBackData = texture2.GetData<byte>();

                // Check that both content are equal
                Assert.True(Utilities.Compare(textureData, readBackData));
            }

            // -------------------------------------------------------
            // Test SetData using a ResourceRegion
            // -------------------------------------------------------
            // Set the last 4 pixels in different orders
            var smallTextureDataRegion = new byte[] { 4, 3, 2, 1 };

            var region = new ResourceRegion(textureData.Length - 4, 0, 0, textureData.Length, 1, 1);
            texture.SetData(GraphicsDevice, smallTextureDataRegion, 0, 0, region);

            readBackData = texture.GetData<byte>();

            Array.Copy(smallTextureDataRegion, 0, textureData, textureData.Length - 4, 4);

            // Check that both content are equal
            Assert.True(Utilities.Compare(textureData, readBackData));

            // -------------------------------------------------------
            // Texture.Dispose()
            // -------------------------------------------------------
            // TODO check that Dispose is implemented correctly
            texture.Dispose();
        }
Ejemplo n.º 2
0
        internal static MipMapDescription[] CalculateMipMapDescription(ImageDescription metadata, PitchFlags cpFlags, out int nImages, out int pixelSize)
        {
            pixelSize = 0;
            nImages = 0;

            int w = metadata.Width;
            int h = metadata.Height;
            int d = metadata.Depth;

            var mipmaps = new MipMapDescription[metadata.MipLevels];

            for (int level = 0; level < metadata.MipLevels; ++level)
            {
                int rowPitch, slicePitch;
                int widthPacked;
                int heightPacked;
                ComputePitch(metadata.Format, w, h, out rowPitch, out slicePitch, out widthPacked, out heightPacked, PitchFlags.None);

                mipmaps[level] = new MipMapDescription(
                    w,
                    h,
                    d,
                    rowPitch,
                    slicePitch,
                    widthPacked,
                    heightPacked
                    );

                pixelSize += d * slicePitch;
                nImages += d;

                if (h > 1)
                    h >>= 1;

                if (w > 1)
                    w >>= 1;

                if (d > 1)
                    d >>= 1;
            }
            return mipmaps;
        }
Ejemplo n.º 3
0
        public void Test1D()
        {
            var textureData = new byte[256];

            for (int i = 0; i < textureData.Length; i++)
            {
                textureData[i] = (byte)i;
            }

            // -------------------------------------------------------
            // General test for a Texture1D
            // -------------------------------------------------------

            // Create Texture1D
            var texture = Texture1D.New(GraphicsDevice, textureData.Length, PixelFormat.R8.UNorm);

            // Check description against native description
            var d3d11Texture = (Direct3D11.Texture1D)texture;
            var d3d11SRV     = (Direct3D11.ShaderResourceView)texture;

            Assert.AreEqual(d3d11Texture.Description, new Direct3D11.Texture1DDescription()
            {
                Width          = textureData.Length,
                ArraySize      = 1,
                BindFlags      = BindFlags.ShaderResource,
                CpuAccessFlags = CpuAccessFlags.None,
                Format         = DXGI.Format.R8_UNorm,
                MipLevels      = 1,
                OptionFlags    = ResourceOptionFlags.None,
                Usage          = ResourceUsage.Default
            });

            // Check shader resource view.
            var srvDescription = d3d11SRV.Description;

            // Clear those fields that are garbage returned from ShaderResourceView.Description.
            srvDescription.Texture2DArray.ArraySize       = 0;
            srvDescription.Texture2DArray.FirstArraySlice = 0;

            Assert.AreEqual(srvDescription, new Direct3D11.ShaderResourceViewDescription()
            {
                Dimension = ShaderResourceViewDimension.Texture1D,
                Format    = DXGI.Format.R8_UNorm,
                Texture1D = { MipLevels = 1, MostDetailedMip = 0 },
            });

            // Check mipmap description
            var mipmapDescription    = texture.GetMipMapDescription(0);
            var rowStride            = textureData.Length * sizeof(byte);
            var refMipmapDescription = new MipMapDescription(textureData.Length, 1, 1, rowStride, rowStride, textureData.Length, 1);

            Assert.AreEqual(mipmapDescription, refMipmapDescription);

            // Check that getting the default SRV is the same as getting the first mip/array
            Assert.AreEqual(texture.ShaderResourceView[ViewType.Full, 0, 0].View, d3d11SRV);

            // Check GraphicsDevice.GetData/SetData data
            // Upload the textureData to the GPU
            texture.SetData(GraphicsDevice, textureData);

            // Read back data from the GPU
            var readBackData = texture.GetData <byte>();

            // Check that both content are equal
            Assert.True(Utilities.Compare(textureData, readBackData));

            // -------------------------------------------------------
            // Check with Texture1D.Clone and GraphicsDevice.Copy
            // -------------------------------------------------------
            using (var texture2 = texture.Clone <Texture1D>())
            {
                GraphicsDevice.Copy(texture, texture2);

                readBackData = texture2.GetData <byte>();

                // Check that both content are equal
                Assert.True(Utilities.Compare(textureData, readBackData));
            }

            // -------------------------------------------------------
            // Test SetData using a ResourceRegion
            // -------------------------------------------------------
            // Set the last 4 pixels in different orders
            var smallTextureDataRegion = new byte[] { 4, 3, 2, 1 };

            var region = new ResourceRegion(textureData.Length - 4, 0, 0, textureData.Length, 1, 1);

            texture.SetData(GraphicsDevice, smallTextureDataRegion, 0, 0, region);

            readBackData = texture.GetData <byte>();

            Array.Copy(smallTextureDataRegion, 0, textureData, textureData.Length - 4, 4);

            // Check that both content are equal
            Assert.True(Utilities.Compare(textureData, readBackData));

            // -------------------------------------------------------
            // Texture.Dispose()
            // -------------------------------------------------------
            // TODO check that Dispose is implemented correctly
            texture.Dispose();
        }