示例#1
0
        public void TestTexture1DMipMap()
        {
            var device = GraphicsDevice.New();

            // Check texture creation with an array of data, with usage default to later allow SetData
            var data = new byte[256];

            data[0]  = 255;
            data[31] = 1;
            var texture = Texture1D.New(device, 256, true, PixelFormat.R8_UNorm, TextureFlags.ShaderResource | TextureFlags.RenderTarget);

            // Verify the number of mipmap levels
            Assert.That(texture.Description.MipLevels, Is.EqualTo(Math.Log(data.Length, 2) + 1));

            // Get a render target on the mipmap 1 (128) with value 1 and get back the data
            var renderTarget1 = texture.ToRenderTarget(ViewType.Single, 0, 1);

            device.Clear(renderTarget1, new Color4(0xFF000001));
            var data1 = texture.GetData <byte>(0, 1);

            Assert.That(data1.Length, Is.EqualTo(128));
            Assert.That(data1[0], Is.EqualTo(1));
            renderTarget1.Dispose();

            // Get a render target on the mipmap 2 (128) with value 2 and get back the data
            var renderTarget2 = texture.ToRenderTarget(ViewType.Single, 0, 2);

            device.Clear(renderTarget2, new Color4(0xFF000002));
            var data2 = texture.GetData <byte>(0, 2);

            Assert.That(data2.Length, Is.EqualTo(64));
            Assert.That(data2[0], Is.EqualTo(2));
            renderTarget2.Dispose();

            // Get a render target on the mipmap 3 (128) with value 3 and get back the data
            var renderTarget3 = texture.ToRenderTarget(ViewType.Single, 0, 3);

            device.Clear(renderTarget3, new Color4(0xFF000003));
            var data3 = texture.GetData <byte>(0, 3);

            Assert.That(data3.Length, Is.EqualTo(32));
            Assert.That(data3[0], Is.EqualTo(3));
            renderTarget3.Dispose();

            // Release the texture
            texture.Dispose();

            device.Dispose();
        }
示例#2
0
        public void TestTexture1D()
        {
            var device = GraphicsDevice.New();

            // Check texture creation with an array of data, with usage default to later allow SetData
            var data = new byte[256];

            data[0]  = 255;
            data[31] = 1;
            var texture = Texture1D.New(device, 256, PixelFormat.R8_UNorm, data, usage: GraphicsResourceUsage.Default);

            // Perform texture op
            CheckTexture(texture, data);

            // Release the texture
            texture.Dispose();

            device.Dispose();
        }
示例#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();
        }
示例#4
0
        public void Test1DArrayAndMipmaps()
        {
            // -----------------------------------------------------------------------------------------
            // Check for a Texture1D as an array of 6 texture with (8+1) mipmaps each, with UAV support
            // -----------------------------------------------------------------------------------------
            var texture  = Texture1D.New(GraphicsDevice, 256, true, PixelFormat.R8.UNorm, TextureFlags.UnorderedAccess, 6);
            var mipcount = (int)Math.Log(256, 2) + 1;

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

            Assert.AreEqual(d3d11Texture.Description, new Direct3D11.Texture1DDescription()
            {
                Width          = 256,
                ArraySize      = 6,
                BindFlags      = BindFlags.ShaderResource | BindFlags.UnorderedAccess,
                CpuAccessFlags = CpuAccessFlags.None,
                Format         = DXGI.Format.R8_UNorm,
                MipLevels      = mipcount,
                OptionFlags    = ResourceOptionFlags.None,
                Usage          = ResourceUsage.Default
            });

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

            Assert.AreEqual(srvDescription, new Direct3D11.ShaderResourceViewDescription()
            {
                Dimension      = ShaderResourceViewDimension.Texture1DArray,
                Format         = DXGI.Format.R8_UNorm,
                Texture1DArray = { MipLevels = mipcount, MostDetailedMip = 0, ArraySize = 6, FirstArraySlice = 0 },
            });

            // Simplified view of mip (mip up to 9) and array (array up to 6)
            //        Array0 Array1 Array2
            //       ______________________
            //  Mip0 |      |      |      |
            //       |------+------+------|
            //  Mip1 |   X  |  X   |  X   |
            //       |------+------+------|
            //  Mip2 |      |      |      |
            //       ----------------------
            srvDescription = ((ShaderResourceView)texture.ShaderResourceView[ViewType.MipBand, 0, 1]).Description;
            Assert.AreEqual(srvDescription, new Direct3D11.ShaderResourceViewDescription()
            {
                Dimension      = ShaderResourceViewDimension.Texture1DArray,
                Format         = DXGI.Format.R8_UNorm,
                Texture1DArray = { MipLevels = 1, MostDetailedMip = 1, ArraySize = 6, FirstArraySlice = 0 },
            });

            // Simplified view of mip (mip up to 9) and array (array up to 6)
            //        Array0 Array1 Array2
            //       ______________________
            //  Mip0 |      |  X   |      |
            //       |------+------+------|
            //  Mip1 |      |  X   |      |
            //       |------+------+------|
            //  Mip2 |      |  X   |      |
            //       ----------------------
            srvDescription = ((ShaderResourceView)texture.ShaderResourceView[ViewType.ArrayBand, 1, 0]).Description;
            Assert.AreEqual(srvDescription, new Direct3D11.ShaderResourceViewDescription()
            {
                Dimension      = ShaderResourceViewDimension.Texture1DArray,
                Format         = DXGI.Format.R8_UNorm,
                Texture1DArray = { MipLevels = mipcount, MostDetailedMip = 0, ArraySize = 1, FirstArraySlice = 1 },
            });

            // Simplified view of mip (mip up to 9) and array (array up to 6)
            //        Array0 Array1 Array2
            //       ______________________
            //  Mip0 |      |      |      |
            //       |------+------+------|
            //  Mip1 |      |  X   |      |
            //       |------+------+------|
            //  Mip2 |      |      |      |
            //       ----------------------
            srvDescription = ((ShaderResourceView)texture.ShaderResourceView[ViewType.Single, 1, 1]).Description;
            Assert.AreEqual(srvDescription, new Direct3D11.ShaderResourceViewDescription()
            {
                Dimension      = ShaderResourceViewDimension.Texture1DArray,
                Format         = DXGI.Format.R8_UNorm,
                Texture1DArray = { MipLevels = 1, MostDetailedMip = 1, ArraySize = 1, FirstArraySlice = 1 },
            });

            // -------------------------------------------------------
            // Test SetData on last mipmap on 2nd array
            // -------------------------------------------------------
            var textureData = new byte[] { 255 };

            texture.SetData(GraphicsDevice, textureData, 1, 8);

            var readbackData = texture.GetData <byte>(1, 8);

            Assert.AreEqual(textureData.Length, readbackData.Length);
            Assert.AreEqual(textureData[0], readbackData[0]);

            // -------------------------------------------------------
            // Clear the content of the texture using UAV
            // -------------------------------------------------------
            var uav            = texture.UnorderedAccessView[1, 8];
            var uavDescription = uav.Description;

            Assert.AreEqual(uavDescription, new Direct3D11.UnorderedAccessViewDescription()
            {
                Dimension      = UnorderedAccessViewDimension.Texture1DArray,
                Format         = DXGI.Format.R8_UNorm,
                Texture1DArray = { ArraySize = 1, FirstArraySlice = 1, MipSlice = 8 }
            });

            // Set the value == 1
            GraphicsDevice.Clear(uav, new Int4(1, 0, 0, 0));
            readbackData = texture.GetData <byte>(1, 8);
            Assert.AreEqual(readbackData.Length, 1);

            //Assert.AreEqual(readbackData[0], 1); // TODO: Check why this test is not working on AMD

            texture.Dispose();
        }