コード例 #1
0
        public void TestMipDimensions()
        {
            // Define variables and constants
            const uint ORIGINAL_WIDTH_TX = 1 << 10;
            Texture1DArray <TexelFormat.RGBA32UInt> textureArray = TextureFactory.NewTexture1D <TexelFormat.RGBA32UInt>()
                                                                   .WithWidth(ORIGINAL_WIDTH_TX)
                                                                   .WithMipAllocation(true)
                                                                   .WithUsage(ResourceUsage.Write)
                                                                   .CreateArray(10);

            // Set up context


            // Execute
            for (int i = 0; ORIGINAL_WIDTH_TX >> i > 0; ++i)
            {
                Assert.AreEqual(ORIGINAL_WIDTH_TX >> i, textureArray.MipWidth((uint)i));
            }

#if !DEVELOPMENT && !RELEASE
            try {
                textureArray.MipWidth(10000U);
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
#endif

            // Assert outcome
            textureArray.Dispose();
        }
コード例 #2
0
        public void TestSubresIndex()
        {
            // Define variables and constants
            Texture1D <TexelFormat.RGBA32UInt> texture = TextureFactory.NewTexture1D <TexelFormat.RGBA32UInt>()
                                                         .WithWidth(1 << 5)
                                                         .WithMipAllocation(true)
                                                         .WithUsage(ResourceUsage.Write);

            // Set up context


            // Execute
            Assert.AreEqual(texture.GetSubresourceIndex(0U), 0U);
            Assert.AreEqual(texture.GetSubresourceIndex(1U), 1U);
            Assert.AreEqual(texture.GetSubresourceIndex(2U), 2U);

#if !DEVELOPMENT && !RELEASE
            try {
                texture.GetSubresourceIndex(10000U);
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
#endif

            // Assert outcome
            texture.Dispose();
        }
コード例 #3
0
        public void TestReadAndReadAll()
        {
            // Define variables and constants
            const uint WIDTH_TX = 512U;

            Texture1D <TexelFormat.RGBA32Int> srcTex = TextureFactory.NewTexture1D <TexelFormat.RGBA32Int>()
                                                       .WithInitialData(
                Enumerable.Range(0, (int)TextureUtils.GetSizeTexels(true, WIDTH_TX))
                .Select(i => new TexelFormat.RGBA32Int {
                R = i, G = i * 2, B = i * 3, A = i * 4
            })
                .ToArray()
                )
                                                       .WithMipAllocation(true)
                                                       .WithPermittedBindings(GPUBindings.None)
                                                       .WithUsage(ResourceUsage.StagingRead)
                                                       .WithWidth(WIDTH_TX);

            TexelFormat.RGBA32Int[] readAllData = srcTex.ReadAll();
            for (int i = 0, curMipIndex = 0; i < readAllData.Length; ++curMipIndex)
            {
                var readData = srcTex.Read((uint)curMipIndex);
                for (int j = 0; j < readData.Width; ++j, ++i)
                {
                    Assert.AreEqual(readData[j], readAllData[i]);
                }
            }

            srcTex.Dispose();
        }
コード例 #4
0
        public void TestGenerateMips()
        {
            LosgapSystem.InvokeOnMaster(() => {
                // Define variables and constants
                const uint WIDTH_TX  = 256U;
                const uint HEIGHT_TX = 128U;
                const uint ARR_LEN   = 4U;
                Texture2DArray <TexelFormat.RGBA8UNorm> sourceTex = TextureFactory.NewTexture2D <TexelFormat.RGBA8UNorm>()
                                                                    .WithWidth(WIDTH_TX)
                                                                    .WithHeight(HEIGHT_TX)
                                                                    .WithMipAllocation(true)
                                                                    .WithMipGenerationTarget(true)
                                                                    .WithPermittedBindings(GPUBindings.ReadableShaderResource | GPUBindings.RenderTarget)
                                                                    .WithUsage(ResourceUsage.Write)
                                                                    .CreateArray(ARR_LEN);

                Texture2DArray <TexelFormat.RGBA8UNorm> copyDestTex = sourceTex.Clone()
                                                                      .WithUsage(ResourceUsage.StagingRead)
                                                                      .WithPermittedBindings(GPUBindings.None)
                                                                      .WithMipGenerationTarget(false)
                                                                      .CreateArray(ARR_LEN);

                // Set up context
                for (uint u = 0U; u < ARR_LEN; u++)
                {
                    sourceTex[u].Write(
                        // ReSharper disable PossibleLossOfFraction No one cares
                        Enumerable.Range(0, (int)TextureUtils.GetSizeTexels(false, WIDTH_TX, HEIGHT_TX))
                        .Select(i => new TexelFormat.RGBA8UNorm {
                        R = 0f, G = 0.33f, B = 0.67f, A = 1f
                    }).ToArray(),
                        // ReSharper restore PossibleLossOfFraction
                        new SubresourceBox(0U, WIDTH_TX, 0U, HEIGHT_TX),
                        0U
                        );
                }

                // Execute
                sourceTex.GenerateMips();
                sourceTex.CopyTo(copyDestTex);

                // Assert outcome
                for (uint u = 0U; u < ARR_LEN; u++)
                {
                    for (uint i = 1U; i < TextureUtils.GetNumMips(WIDTH_TX, HEIGHT_TX); ++i)
                    {
                        IEnumerable <TexelFormat.RGBA8UNorm> outData = copyDestTex[u].Read(i);
                        // ReSharper disable CompareOfFloatsByEqualityOperator Exact equality is fine here
                        Assert.IsTrue(outData.Any(texel => texel.R != 0f || texel.G != 0f || texel.B != 0f || texel.A != 0f));
                        // ReSharper restore CompareOfFloatsByEqualityOperator
                    }
                }

                sourceTex.Dispose();
                copyDestTex.Dispose();
            });
        }
コード例 #5
0
        public void TestWrite()
        {
            LosgapSystem.InvokeOnMaster(() => {
                // Define variables and constants
                const uint WIDTH_TX  = 100U;
                const uint HEIGHT_TX = 100U;

                const uint WRITE_OFFSET_U = 30U;
                const uint WRITE_OFFSET_V = 30U;

                SubresourceBox writeTarget = new SubresourceBox(
                    WRITE_OFFSET_U, WIDTH_TX,
                    WRITE_OFFSET_V, HEIGHT_TX
                    );

                Texture2D <TexelFormat.RGBA8Int> srcTex = TextureFactory.NewTexture2D <TexelFormat.RGBA8Int>()
                                                          .WithUsage(ResourceUsage.Write)
                                                          .WithMultisampling(true)
                                                          .WithWidth(WIDTH_TX)
                                                          .WithHeight(HEIGHT_TX);

                // Set up context


                // Execute
                srcTex.Write(
                    Enumerable.Range(0, (int)writeTarget.Volume)
                    .Select(i => new TexelFormat.RGBA8Int {
                    R = (sbyte)i, G = (sbyte)(i * 2), B = (sbyte)(i * 3), A = (sbyte)(i * 4)
                })
                    .ToArray(),
                    writeTarget
                    );

                Texture2D <TexelFormat.RGBA8Int> dstTex = srcTex.Clone()
                                                          .WithUsage(ResourceUsage.StagingRead)
                                                          .WithPermittedBindings(GPUBindings.None);

                srcTex.CopyTo(dstTex);

                // Assert outcome
                TexelArray2D <TexelFormat.RGBA8Int> copiedData = dstTex.Read(0U);
                for (uint v = WRITE_OFFSET_V, value = 0U; v < HEIGHT_TX; ++v)
                {
                    for (uint u = WRITE_OFFSET_U; u < WIDTH_TX; ++u, ++value)
                    {
                        Assert.AreEqual((sbyte)value, copiedData[(int)u, (int)v].R);
                        Assert.AreEqual((sbyte)(value * 2U), copiedData[(int)u, (int)v].G);
                        Assert.AreEqual((sbyte)(value * 3U), copiedData[(int)u, (int)v].B);
                        Assert.AreEqual((sbyte)(value * 4U), copiedData[(int)u, (int)v].A);
                    }
                }

                srcTex.Dispose();
                dstTex.Dispose();
            });
        }
コード例 #6
0
        public void TestCopyTo()
        {
            LosgapSystem.InvokeOnMaster(() => {
                // Define variables and constants
                const uint WIDTH_TX = 512U;

                const uint NUM_TEXELS_TO_COPY  = 25U;
                const uint FIRST_TEXEL_TO_COPY = 25U;
                const uint SRC_MIP_INDEX       = 1U;
                const uint DST_MIP_INDEX       = 3U;
                const uint DST_WRITE_OFFSET    = 15U;
                const uint DATA_VALUE_START_R  = 512U + FIRST_TEXEL_TO_COPY;

                Texture1D <TexelFormat.RGBA32Int> srcTex = TextureFactory.NewTexture1D <TexelFormat.RGBA32Int>()
                                                           .WithDynamicDetail(false)
                                                           .WithInitialData(
                    Enumerable.Range(0, (int)TextureUtils.GetSizeTexels(true, WIDTH_TX))
                    .Select(i => new TexelFormat.RGBA32Int {
                    R = i, G = i * 2, B = i * 3, A = i * 4
                })
                    .ToArray()
                    )
                                                           .WithMipAllocation(true)
                                                           .WithMipGenerationTarget(false)
                                                           .WithPermittedBindings(GPUBindings.ReadableShaderResource)
                                                           .WithUsage(ResourceUsage.Immutable)
                                                           .WithWidth(WIDTH_TX);

                // Set up context


                // Execute
                Texture1D <TexelFormat.RGBA32Int> dstTex = srcTex.Clone()
                                                           .WithUsage(ResourceUsage.StagingRead)
                                                           .WithPermittedBindings(GPUBindings.None);
                srcTex.CopyTo(
                    dstTex,
                    new SubresourceBox(FIRST_TEXEL_TO_COPY, FIRST_TEXEL_TO_COPY + NUM_TEXELS_TO_COPY),
                    SRC_MIP_INDEX,
                    DST_MIP_INDEX,
                    DST_WRITE_OFFSET
                    );

                // Assert outcome
                TexelArray1D <TexelFormat.RGBA32Int> copiedData = dstTex.Read(DST_MIP_INDEX);
                for (int i = 0; i < NUM_TEXELS_TO_COPY; ++i)
                {
                    Assert.AreEqual(DATA_VALUE_START_R + i, copiedData[i + (int)DST_WRITE_OFFSET].R);
                    Assert.AreEqual((DATA_VALUE_START_R + i) * 2, copiedData[i + (int)DST_WRITE_OFFSET].G);
                    Assert.AreEqual((DATA_VALUE_START_R + i) * 3, copiedData[i + (int)DST_WRITE_OFFSET].B);
                    Assert.AreEqual((DATA_VALUE_START_R + i) * 4, copiedData[i + (int)DST_WRITE_OFFSET].A);
                }

                srcTex.Dispose();
                dstTex.Dispose();
            });
        }
コード例 #7
0
        public void TestCopyTo()
        {
            LosgapSystem.InvokeOnMaster(() => {
                // Define variables and constants
                const uint MIP0_WIDTH   = 512U;
                const uint ARRAY_LENGTH = 10U;
                Texture1DArray <TexelFormat.RGBA32UInt> srcArray = TextureFactory.NewTexture1D <TexelFormat.RGBA32UInt>()
                                                                   .WithDynamicDetail(false)
                                                                   .WithInitialData(
                    Enumerable.Repeat(
                        Enumerable.Range(0, (int)TextureUtils.GetSizeTexels(true, MIP0_WIDTH))
                        .Select(i => (uint)i)
                        .Select(i => new TexelFormat.RGBA32UInt {
                    R = i, G = i * 2, B = i * 3, A = i * 4
                }),
                        (int)ARRAY_LENGTH
                        )
                    .Flatten()
                    .ToArray()
                    )
                                                                   .WithMipAllocation(true)
                                                                   .WithMipGenerationTarget(false)
                                                                   .WithPermittedBindings(GPUBindings.ReadableShaderResource)
                                                                   .WithUsage(ResourceUsage.Immutable)
                                                                   .WithMipAllocation(true)
                                                                   .WithUsage(ResourceUsage.Write)
                                                                   .WithWidth(MIP0_WIDTH)
                                                                   .CreateArray(ARRAY_LENGTH);

                // Set up context
                Texture1DArray <TexelFormat.RGBA32UInt> dstArray = srcArray.Clone()
                                                                   .WithUsage(ResourceUsage.StagingRead)
                                                                   .WithPermittedBindings(GPUBindings.None)
                                                                   .CreateArray(srcArray.ArrayLength);

                srcArray.CopyTo(dstArray);

                var data = dstArray[4].ReadAll();

                // Execute
                for (int i = 0, curMipIndex = 0; i < data.Length; ++curMipIndex)
                {
                    for (int j = 0; j < dstArray.MipWidth((uint)curMipIndex); ++j, ++i)
                    {
                        Assert.AreEqual((uint)i, data[i].R);
                        Assert.AreEqual((uint)i * 2, data[i].G);
                        Assert.AreEqual((uint)i * 3, data[i].B);
                        Assert.AreEqual((uint)i * 4, data[i].A);
                    }
                }

                // Assert outcome
                srcArray.Dispose();
                dstArray.Dispose();
            });
        }
コード例 #8
0
        public void TestDiscardWrite()
        {
            LosgapSystem.InvokeOnMaster(() => {
                // Define variables and constants
                const uint WIDTH_TX  = 400U;
                const uint HEIGHT_TX = 200U;

                const uint WRITE_OFFSET_U = 190U;
                const uint WRITE_OFFSET_V = 10U;
                const int NUM_TX_TO_WRITE = 13555;

                Texture2D <TexelFormat.RGB32UInt> srcTex = TextureFactory.NewTexture2D <TexelFormat.RGB32UInt>()
                                                           .WithPermittedBindings(GPUBindings.ReadableShaderResource)
                                                           .WithUsage(ResourceUsage.DiscardWrite)
                                                           .WithWidth(WIDTH_TX)
                                                           .WithHeight(HEIGHT_TX);

                // Set up context


                // Execute
                srcTex.DiscardWrite(
                    Enumerable.Range(0, NUM_TX_TO_WRITE)
                    .Select(i => new TexelFormat.RGB32UInt {
                    R = (uint)i, G = (uint)i * 2, B = (uint)i * 3
                })
                    .ToArray(),
                    0U,
                    WRITE_OFFSET_U,
                    WRITE_OFFSET_V
                    );

                Texture2D <TexelFormat.RGB32UInt> dstTex = srcTex.Clone()
                                                           .WithUsage(ResourceUsage.StagingRead)
                                                           .WithPermittedBindings(GPUBindings.None);


                srcTex.CopyTo(dstTex);

                // Assert outcome
                TexelFormat.RGB32UInt[] copiedData = dstTex.Read(0U).Data;
                int offset = (int)(WRITE_OFFSET_V * WIDTH_TX + WRITE_OFFSET_U);
                for (int i = 0; i < NUM_TX_TO_WRITE; ++i)
                {
                    Assert.AreEqual((uint)i, copiedData[i + offset].R);
                    Assert.AreEqual((uint)i * 2U, copiedData[i + offset].G);
                    Assert.AreEqual((uint)i * 3U, copiedData[i + offset].B);
                }

                srcTex.Dispose();
                dstTex.Dispose();
            });
        }
コード例 #9
0
        public void TestReadWrite()
        {
            // Define variables and constants
            const uint WIDTH_TX               = 512U;
            const uint HEIGHT_TX              = 512U;
            const uint TARGET_MIP_INDEX       = 2U;
            const uint DATA_WRITE_OFFSET_U    = 10U;
            const uint DATA_WRITE_OFFSET_V    = 10U;
            const uint DATA_WRITE_SQUARE_SIZE = 80U;

            Texture2D <TexelFormat.RGBA32UInt> srcTex = TextureFactory.NewTexture2D <TexelFormat.RGBA32UInt>()
                                                        .WithInitialData(
                Enumerable.Range(0, (int)TextureUtils.GetSizeTexels(true, WIDTH_TX, HEIGHT_TX))
                .Select(i => (uint)i)
                .Select(i => new TexelFormat.RGBA32UInt {
                R = i, G = i * 2, B = i * 3, A = i * 4
            })
                .ToArray()
                )
                                                        .WithMipAllocation(true)
                                                        .WithPermittedBindings(GPUBindings.None)
                                                        .WithUsage(ResourceUsage.StagingReadWrite)
                                                        .WithWidth(WIDTH_TX)
                                                        .WithHeight(HEIGHT_TX);

            srcTex.ReadWrite(data => {
                for (uint u = DATA_WRITE_OFFSET_U; u < DATA_WRITE_OFFSET_U + DATA_WRITE_SQUARE_SIZE; ++u)
                {
                    for (uint v = DATA_WRITE_OFFSET_V; v < DATA_WRITE_OFFSET_V + DATA_WRITE_SQUARE_SIZE; ++v)
                    {
                        data[(int)u, (int)v] = new TexelFormat.RGBA32UInt {
                            R = u, G = v, B = u + v, A = u - v
                        };
                    }
                }
            },
                             TARGET_MIP_INDEX);

            var readData = srcTex.Read(TARGET_MIP_INDEX);

            for (uint u = DATA_WRITE_OFFSET_U; u < DATA_WRITE_OFFSET_U + DATA_WRITE_SQUARE_SIZE; ++u)
            {
                for (uint v = DATA_WRITE_OFFSET_V; v < DATA_WRITE_OFFSET_V + DATA_WRITE_SQUARE_SIZE; ++v)
                {
                    Assert.AreEqual(u, readData[(int)u, (int)v].R);
                    Assert.AreEqual(v, readData[(int)u, (int)v].G);
                    Assert.AreEqual(u + v, readData[(int)u, (int)v].B);
                    Assert.AreEqual(u - v, readData[(int)u, (int)v].A);
                }
            }

            srcTex.Dispose();
        }
コード例 #10
0
        public void TestWrite()
        {
            LosgapSystem.InvokeOnMaster(() => {
                // Define variables and constants
                const uint WIDTH_TX = 512U;

                const uint TARGET_MIP_INDEX = 1U;
                const uint WRITE_OFFSET     = 30U;
                const int NUM_TX_TO_WRITE   = 198;

                Texture1D <TexelFormat.RGBA32Int> srcTex = TextureFactory.NewTexture1D <TexelFormat.RGBA32Int>()
                                                           .WithPermittedBindings(GPUBindings.ReadableShaderResource)
                                                           .WithMipAllocation(true)
                                                           .WithDynamicDetail(true)
                                                           .WithUsage(ResourceUsage.Write)
                                                           .WithWidth(WIDTH_TX);

                // Set up context


                // Execute
                srcTex.Write(
                    Enumerable.Range(0, NUM_TX_TO_WRITE)
                    .Select(i => new TexelFormat.RGBA32Int {
                    R = i, G = i * 2, B = i * 3, A = i * 4
                })
                    .ToArray(),
                    TARGET_MIP_INDEX,
                    WRITE_OFFSET
                    );

                Texture1D <TexelFormat.RGBA32Int> dstTex = srcTex.Clone()
                                                           .WithDynamicDetail(false)
                                                           .WithUsage(ResourceUsage.StagingRead)
                                                           .WithPermittedBindings(GPUBindings.None);

                srcTex.CopyTo(dstTex);

                // Assert outcome
                TexelArray1D <TexelFormat.RGBA32Int> copiedData = dstTex.Read(TARGET_MIP_INDEX);
                for (int i = 0; i < NUM_TX_TO_WRITE; ++i)
                {
                    Assert.AreEqual(i, copiedData[i + (int)WRITE_OFFSET].R);
                    Assert.AreEqual(i * 2, copiedData[i + (int)WRITE_OFFSET].G);
                    Assert.AreEqual(i * 3, copiedData[i + (int)WRITE_OFFSET].B);
                    Assert.AreEqual(i * 4, copiedData[i + (int)WRITE_OFFSET].A);
                }

                srcTex.Dispose();
                dstTex.Dispose();
            });
        }
コード例 #11
0
        public void TestClone()
        {
            // Define variables and constants
            const uint WIDTH_TX  = 128U;
            const uint HEIGHT_TX = 256U;
            const uint DEPTH_TX  = 16U;
            Texture3D <TexelFormat.Int8> srcTex = TextureFactory.NewTexture3D <TexelFormat.Int8>()
                                                  .WithDynamicDetail(false)
                                                  .WithInitialData(
                Enumerable.Range(0, (int)TextureUtils.GetSizeTexels(true, WIDTH_TX, HEIGHT_TX, DEPTH_TX))
                .Select(i => new TexelFormat.Int8 {
                Value = (sbyte)i
            })
                .ToArray()
                )
                                                  .WithMipAllocation(true)
                                                  .WithMipGenerationTarget(false)
                                                  .WithPermittedBindings(GPUBindings.None)
                                                  .WithUsage(ResourceUsage.StagingRead)
                                                  .WithWidth(WIDTH_TX)
                                                  .WithHeight(HEIGHT_TX)
                                                  .WithDepth(DEPTH_TX);

            // Set up context


            // Execute
            Texture3D <TexelFormat.Int8> destNoCopy = srcTex.Clone(false);
            Texture3D <TexelFormat.Int8> destCopy   = srcTex.Clone(true);

            // Assert outcome
            Assert.AreEqual(srcTex.IsGlobalDetailTarget, destNoCopy.IsGlobalDetailTarget);
            Assert.AreEqual(srcTex.IsMipGenTarget, destNoCopy.IsMipGenTarget);
            Assert.AreEqual(srcTex.IsMipmapped, destNoCopy.IsMipmapped);
            Assert.AreEqual(srcTex.NumMips, destNoCopy.NumMips);
            Assert.AreEqual(srcTex.PermittedBindings, destNoCopy.PermittedBindings);
            Assert.AreEqual(srcTex.Size, destNoCopy.Size);
            Assert.AreEqual(srcTex.TexelFormat, destNoCopy.TexelFormat);
            Assert.AreEqual(srcTex.TexelSizeBytes, destNoCopy.TexelSizeBytes);
            Assert.AreEqual(srcTex.Usage, destNoCopy.Usage);
            Assert.AreEqual(srcTex.Width, destNoCopy.Width);

            TexelFormat.Int8[] copiedData = destCopy.ReadAll();
            for (int i = 0; i < copiedData.Length; ++i)
            {
                Assert.AreEqual((sbyte)i, copiedData[i].Value);
            }

            srcTex.Dispose();
            destNoCopy.Dispose();
            destCopy.Dispose();
        }
コード例 #12
0
        public void TestArrayElementsAreIdentical()
        {
            // Define variables and constants
            const uint MIP0_WIDTH   = 512U;
            const uint ARRAY_LENGTH = 10U;
            Texture1DArray <TexelFormat.RGBA32UInt> srcArray = TextureFactory.NewTexture1D <TexelFormat.RGBA32UInt>()
                                                               .WithDynamicDetail(true)
                                                               .WithInitialData(
                Enumerable.Repeat(
                    Enumerable.Range(0, (int)TextureUtils.GetSizeTexels(true, MIP0_WIDTH))
                    .Select(i => (uint)i)
                    .Select(i => new TexelFormat.RGBA32UInt {
                R = i, G = i * 2, B = i * 3, A = i * 4
            }),
                    (int)ARRAY_LENGTH
                    )
                .Flatten()
                .ToArray()
                )
                                                               .WithMipAllocation(true)
                                                               .WithMipGenerationTarget(false)
                                                               .WithPermittedBindings(GPUBindings.ReadableShaderResource)
                                                               .WithUsage(ResourceUsage.Immutable)
                                                               .WithMipAllocation(true)
                                                               .WithUsage(ResourceUsage.Write)
                                                               .WithWidth(MIP0_WIDTH)
                                                               .CreateArray(ARRAY_LENGTH);

            // Set up context


            // Execute


            // Assert outcome
            foreach (Texture1D <TexelFormat.RGBA32UInt> arrayElement in srcArray)
            {
                Assert.AreEqual(srcArray.IsGlobalDetailTarget, arrayElement.IsGlobalDetailTarget);
                Assert.AreEqual(srcArray.IsMipGenTarget, arrayElement.IsMipGenTarget);
                Assert.AreEqual(srcArray.IsMipmapped, arrayElement.IsMipmapped);
                Assert.AreEqual(srcArray.NumMips, arrayElement.NumMips);
                Assert.AreEqual(srcArray.PermittedBindings, arrayElement.PermittedBindings);
                Assert.AreEqual(srcArray.Size / srcArray.ArrayLength, arrayElement.Size);
                Assert.AreEqual(srcArray.TexelFormat, arrayElement.TexelFormat);
                Assert.AreEqual(srcArray.TexelSizeBytes, arrayElement.TexelSizeBytes);
                Assert.AreEqual(srcArray.Usage, arrayElement.Usage);
                Assert.AreEqual(srcArray.Width, arrayElement.Width);
            }

            srcArray.Dispose();
        }
コード例 #13
0
        public void TestClone()
        {
            // Define variables and constants
            const uint WIDTH_TX = 512U;
            Texture1D <TexelFormat.RGBA32Int> srcTex = TextureFactory.NewTexture1D <TexelFormat.RGBA32Int>()
                                                       .WithDynamicDetail(false)
                                                       .WithInitialData(
                Enumerable.Range(0, (int)TextureUtils.GetSizeTexels(true, WIDTH_TX))
                .Select(i => new TexelFormat.RGBA32Int {
                R = i, G = i * 2, B = i * 3, A = i * 4
            })
                .ToArray()
                )
                                                       .WithMipAllocation(true)
                                                       .WithMipGenerationTarget(false)
                                                       .WithPermittedBindings(GPUBindings.None)
                                                       .WithUsage(ResourceUsage.StagingRead)
                                                       .WithWidth(WIDTH_TX);

            // Set up context


            // Execute
            Texture1D <TexelFormat.RGBA32Int> destNoCopy = srcTex.Clone(false);
            Texture1D <TexelFormat.RGBA32Int> destCopy   = srcTex.Clone(true);

            // Assert outcome
            Assert.AreEqual(srcTex.IsGlobalDetailTarget, destNoCopy.IsGlobalDetailTarget);
            Assert.AreEqual(srcTex.IsMipGenTarget, destNoCopy.IsMipGenTarget);
            Assert.AreEqual(srcTex.IsMipmapped, destNoCopy.IsMipmapped);
            Assert.AreEqual(srcTex.NumMips, destNoCopy.NumMips);
            Assert.AreEqual(srcTex.PermittedBindings, destNoCopy.PermittedBindings);
            Assert.AreEqual(srcTex.Size, destNoCopy.Size);
            Assert.AreEqual(srcTex.TexelFormat, destNoCopy.TexelFormat);
            Assert.AreEqual(srcTex.TexelSizeBytes, destNoCopy.TexelSizeBytes);
            Assert.AreEqual(srcTex.Usage, destNoCopy.Usage);
            Assert.AreEqual(srcTex.Width, destNoCopy.Width);

            TexelFormat.RGBA32Int[] copiedData = destCopy.ReadAll();
            for (int i = 0; i < copiedData.Length; ++i)
            {
                Assert.AreEqual(i, copiedData[i].R);
                Assert.AreEqual(i * 2, copiedData[i].G);
                Assert.AreEqual(i * 3, copiedData[i].B);
                Assert.AreEqual(i * 4, copiedData[i].A);
            }

            srcTex.Dispose();
            destCopy.Dispose();
            destNoCopy.Dispose();
        }
コード例 #14
0
        public void TestCloneWithInitData()
        {
            // Define variables and constants
            const uint MIP0_WIDTH   = 512U;
            const uint ARRAY_LENGTH = 10U;
            Texture1DArray <TexelFormat.RGBA32UInt> srcArray = TextureFactory.NewTexture1D <TexelFormat.RGBA32UInt>()
                                                               .WithDynamicDetail(false)
                                                               .WithInitialData(
                Enumerable.Range(0, (int)TextureUtils.GetSizeTexels(true, MIP0_WIDTH) * (int)ARRAY_LENGTH)
                .Select(i => (uint)i)
                .Select(i => new TexelFormat.RGBA32UInt {
                R = i, G = i * 2, B = i * 3, A = i * 4
            })
                .ToArray()
                )
                                                               .WithMipAllocation(true)
                                                               .WithMipGenerationTarget(false)
                                                               .WithPermittedBindings(GPUBindings.None)
                                                               .WithUsage(ResourceUsage.StagingRead)
                                                               .WithWidth(MIP0_WIDTH)
                                                               .CreateArray(ARRAY_LENGTH);

            // Set up context


            // Execute
            var cloneArray = srcArray.Clone(true).CreateArray(ARRAY_LENGTH);

            // Assert outcome
            uint curVal = 0U;

            foreach (Texture1D <TexelFormat.RGBA32UInt> texture1D in cloneArray)
            {
                TexelFormat.RGBA32UInt[] texData = texture1D.ReadAll();
                for (int i = 0; i < texData.Length; i++)
                {
                    Assert.AreEqual(curVal, texData[i].R);
                    Assert.AreEqual(curVal * 2, texData[i].G);
                    Assert.AreEqual(curVal * 3, texData[i].B);
                    Assert.AreEqual(curVal * 4, texData[i].A);

                    ++curVal;
                }
            }

            cloneArray.Dispose();
            srcArray.Dispose();
        }
コード例 #15
0
        public void TestReadWrite()
        {
            // Define variables and constants
            const uint WIDTH_TX          = 512U;
            const uint TARGET_MIP_INDEX  = 4U;
            const uint DATA_WRITE_OFFSET = 10U;
            const uint DATA_WRITE_COUNT  = 10U;

            Texture1D <TexelFormat.RGBA32UInt> srcTex = TextureFactory.NewTexture1D <TexelFormat.RGBA32UInt>()
                                                        .WithInitialData(
                Enumerable.Range(0, (int)TextureUtils.GetSizeTexels(true, WIDTH_TX))
                .Select(i => (uint)i)
                .Select(i => new TexelFormat.RGBA32UInt {
                R = i, G = i * 2, B = i * 3, A = i * 4
            })
                .ToArray()
                )
                                                        .WithMipAllocation(true)
                                                        .WithPermittedBindings(GPUBindings.None)
                                                        .WithUsage(ResourceUsage.StagingReadWrite)
                                                        .WithWidth(WIDTH_TX);

            srcTex.ReadWrite(data => {
                for (uint i = DATA_WRITE_OFFSET; i < DATA_WRITE_OFFSET + DATA_WRITE_COUNT; ++i)
                {
                    data[(int)i] = new TexelFormat.RGBA32UInt {
                        R = i, G = i * 3, B = i * 6, A = i * 9
                    };
                }
            },
                             TARGET_MIP_INDEX);

            var readData = srcTex.Read(TARGET_MIP_INDEX);

            for (uint i = DATA_WRITE_OFFSET; i < DATA_WRITE_OFFSET + DATA_WRITE_COUNT; ++i)
            {
                Assert.AreEqual(i, readData[(int)i].R);
                Assert.AreEqual(i * 3, readData[(int)i].G);
                Assert.AreEqual(i * 6, readData[(int)i].B);
                Assert.AreEqual(i * 9, readData[(int)i].A);
            }

            srcTex.Dispose();
        }
コード例 #16
0
        public void TestCreationParameters()
        {
            var defaultBuilder        = TextureFactory.LoadTexture2D().WithUsage(ResourceUsage.Immutable).WithFilePath(@"Tests\potTex.bmp");
            var withStagingUsage      = defaultBuilder.WithUsage(ResourceUsage.StagingReadWrite).WithPermittedBindings(GPUBindings.None);
            var withReadWriteBindings = defaultBuilder.WithUsage(ResourceUsage.Write).WithPermittedBindings(GPUBindings.ReadableShaderResource | GPUBindings.WritableShaderResource);

            ITexture2D tex = withStagingUsage.Create();

            Assert.AreEqual(ResourceUsage.StagingReadWrite, tex.Usage);
            tex.Dispose();

            tex = withReadWriteBindings.Create();
            Assert.AreEqual(GPUBindings.ReadableShaderResource | GPUBindings.WritableShaderResource, tex.PermittedBindings);
            tex.Dispose();

#if !DEVELOPMENT && !RELEASE
            try {
                TextureFactory.LoadTexture2D()
                .WithUsage(ResourceUsage.Immutable)
                .Create();
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
            try {
                TextureFactory.LoadTexture2D()
                .WithFilePath(@"Tests\potTex.bmp")
                .WithUsage(ResourceUsage.DiscardWrite)
                .WithPermittedBindings(GPUBindings.None)
                .Create();
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
            try {
                TextureFactory.LoadTexture2D()
                .WithFilePath(@"Tests\potTex.bmp")
                .WithUsage(ResourceUsage.StagingRead)
                .WithPermittedBindings(GPUBindings.ReadableShaderResource)
                .Create();
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
#endif
        }
コード例 #17
0
        public void TestCopyTo()
        {
            LosgapSystem.InvokeOnMaster(() => {
                // Define variables and constants
                const uint WIDTH_TX = 512U;
                Texture1D <TexelFormat.RGBA32Int> srcTex = TextureFactory.NewTexture1D <TexelFormat.RGBA32Int>()
                                                           .WithDynamicDetail(false)
                                                           .WithInitialData(
                    Enumerable.Range(0, (int)TextureUtils.GetSizeTexels(true, WIDTH_TX))
                    .Select(i => new TexelFormat.RGBA32Int {
                    R = i, G = i * 2, B = i * 3, A = i * 4
                })
                    .ToArray()
                    )
                                                           .WithMipAllocation(true)
                                                           .WithMipGenerationTarget(false)
                                                           .WithPermittedBindings(GPUBindings.ReadableShaderResource)
                                                           .WithUsage(ResourceUsage.Immutable)
                                                           .WithWidth(WIDTH_TX);

                // Set up context


                // Execute
                Texture1D <TexelFormat.RGBA32Int> dstTex = srcTex.Clone()
                                                           .WithUsage(ResourceUsage.StagingRead)
                                                           .WithPermittedBindings(GPUBindings.None);
                srcTex.CopyTo(dstTex);

                // Assert outcome
                TexelFormat.RGBA32Int[] copiedData = dstTex.ReadAll();
                for (int i = 0; i < copiedData.Length; ++i)
                {
                    Assert.AreEqual(i, copiedData[i].R);
                    Assert.AreEqual(i * 2, copiedData[i].G);
                    Assert.AreEqual(i * 3, copiedData[i].B);
                    Assert.AreEqual(i * 4, copiedData[i].A);
                }

                srcTex.Dispose();
                dstTex.Dispose();
            });
        }
コード例 #18
0
        public void TestReadAndReadAll()
        {
            // Define variables and constants
            const uint WIDTH_TX  = 32U;
            const uint HEIGHT_TX = 16U;
            const uint DEPTH_TX  = 16U;

            Texture3D <TexelFormat.RGBA32Int> srcTex = TextureFactory.NewTexture3D <TexelFormat.RGBA32Int>()
                                                       .WithInitialData(
                Enumerable.Range(0, (int)TextureUtils.GetSizeTexels(true, WIDTH_TX, HEIGHT_TX, DEPTH_TX))
                .Select(i => new TexelFormat.RGBA32Int {
                R = i, G = i * 2, B = i * 3, A = i * 4
            })
                .ToArray()
                )
                                                       .WithMipAllocation(true)
                                                       .WithPermittedBindings(GPUBindings.None)
                                                       .WithUsage(ResourceUsage.StagingRead)
                                                       .WithWidth(WIDTH_TX)
                                                       .WithHeight(HEIGHT_TX)
                                                       .WithDepth(DEPTH_TX);

            TexelFormat.RGBA32Int[] readAllData = srcTex.ReadAll();
            for (int i = 0, curMipIndex = 0; i < readAllData.Length; ++curMipIndex)
            {
                var readData = srcTex.Read((uint)curMipIndex);
                for (int w = 0; w < readData.Depth; ++w)
                {
                    for (int v = 0; v < readData.Height; ++v)
                    {
                        for (int u = 0; u < readData.Width; ++u, ++i)
                        {
                            Assert.AreEqual(readData[u, v, w].R, readAllData[i].R);
                            Assert.AreEqual(readData[u, v, w].G, readAllData[i].G);
                            Assert.AreEqual(readData[u, v, w].B, readAllData[i].B);
                            Assert.AreEqual(readData[u, v, w].A, readAllData[i].A);
                        }
                    }
                }
            }

            srcTex.Dispose();
        }
コード例 #19
0
        public void TestRTVCreation()
        {
            Texture2D <TexelFormat.RenderTarget> testResource = TextureFactory.NewTexture2D <TexelFormat.RenderTarget>()
                                                                .WithDynamicDetail(false)
                                                                .WithMipAllocation(false)
                                                                .WithMipGenerationTarget(false)
                                                                .WithPermittedBindings(GPUBindings.RenderTarget)
                                                                .WithUsage(ResourceUsage.Write)
                                                                .WithWidth(800U)
                                                                .WithHeight(600U)
                                                                .WithMultisampling(true);
            RenderTargetView testRTV = testResource.CreateRenderTargetView(0U);

            Assert.IsFalse(testRTV.ResourceOrViewDisposed);
            Assert.AreEqual(testResource, testRTV.Resource);
            Assert.AreEqual(0U, testRTV.MipIndex);
            testResource.Dispose();
            Assert.IsTrue(testRTV.ResourceOrViewDisposed);
            testRTV.Dispose();
            Assert.IsTrue(testRTV.IsDisposed);

            Texture2DArray <TexelFormat.RenderTarget> testResourceArr = TextureFactory.NewTexture2D <TexelFormat.RenderTarget>()
                                                                        .WithDynamicDetail(false)
                                                                        .WithMipAllocation(false)
                                                                        .WithMipGenerationTarget(false)
                                                                        .WithPermittedBindings(GPUBindings.RenderTarget)
                                                                        .WithUsage(ResourceUsage.Write)
                                                                        .WithWidth(800U)
                                                                        .WithHeight(600U)
                                                                        .WithMultisampling(true)
                                                                        .CreateArray(10U);

            testRTV = testResourceArr[3].CreateRenderTargetView(0U);
            Assert.IsFalse(testRTV.ResourceOrViewDisposed);
            Assert.AreEqual(testResourceArr[3], testRTV.Resource);
            Assert.AreEqual(0U, testRTV.MipIndex);
            testResourceArr.Dispose();
            Assert.IsTrue(testRTV.ResourceOrViewDisposed);
            testRTV.Dispose();
            Assert.IsTrue(testRTV.IsDisposed);
        }
コード例 #20
0
        public void TestDSVCreation()
        {
            Texture2D <TexelFormat.DepthStencil> testResource = TextureFactory.NewTexture2D <TexelFormat.DepthStencil>()
                                                                .WithDynamicDetail(false)
                                                                .WithMipAllocation(true)
                                                                .WithMipGenerationTarget(false)
                                                                .WithPermittedBindings(GPUBindings.DepthStencilTarget)
                                                                .WithUsage(ResourceUsage.Write)
                                                                .WithWidth(512U)
                                                                .WithHeight(128U)
                                                                .WithMultisampling(false);
            DepthStencilView testDSV = testResource.CreateDepthStencilView(1U);

            Assert.IsFalse(testDSV.ResourceOrViewDisposed);
            Assert.AreEqual(testResource, testDSV.Resource);
            Assert.AreEqual(1U, testDSV.MipIndex);
            testResource.Dispose();
            Assert.IsTrue(testDSV.ResourceOrViewDisposed);
            testDSV.Dispose();
            Assert.IsTrue(testDSV.IsDisposed);

            Texture2DArray <TexelFormat.DepthStencil> testResourceArr = TextureFactory.NewTexture2D <TexelFormat.DepthStencil>()
                                                                        .WithDynamicDetail(false)
                                                                        .WithMipAllocation(true)
                                                                        .WithMipGenerationTarget(false)
                                                                        .WithPermittedBindings(GPUBindings.DepthStencilTarget)
                                                                        .WithUsage(ResourceUsage.Write)
                                                                        .WithWidth(512U)
                                                                        .WithHeight(128U)
                                                                        .WithMultisampling(false)
                                                                        .CreateArray(10U);

            testDSV = testResourceArr[3].CreateDepthStencilView(1U);
            Assert.IsFalse(testDSV.ResourceOrViewDisposed);
            Assert.AreEqual(testResourceArr[3], testDSV.Resource);
            Assert.AreEqual(1U, testDSV.MipIndex);
            testResourceArr.Dispose();
            Assert.IsTrue(testDSV.ResourceOrViewDisposed);
            testDSV.Dispose();
            Assert.IsTrue(testDSV.IsDisposed);
        }
コード例 #21
0
        public void TestClearRenderTarget()
        {
            Texture2D <TexelFormat.RenderTarget> renderTarget = TextureFactory.NewTexture2D <TexelFormat.RenderTarget>()
                                                                .WithWidth(800U)
                                                                .WithHeight(600U)
                                                                .WithDynamicDetail(false)
                                                                .WithMipAllocation(false)
                                                                .WithMipGenerationTarget(false)
                                                                .WithMultisampling(false)
                                                                .WithPermittedBindings(GPUBindings.RenderTarget)
                                                                .WithUsage(ResourceUsage.Write);

            RenderTargetView rtv = renderTarget.CreateRenderTargetView(0U);

            RenderCommand testCommand = RenderCommand.ClearRenderTarget(rtv);

            Assert.AreEqual(RenderCommandInstruction.ClearRenderTarget, testCommand.Instruction);
            Assert.AreEqual((RenderCommandArgument)(IntPtr)(ResourceViewHandle)rtv.ResourceViewHandle, testCommand.Arg1);

#if !DEVELOPMENT && !RELEASE
            try {
                RenderCommand.ClearRenderTarget(null as RenderTargetView);
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
#endif

            rtv.Dispose();
            renderTarget.Dispose();

#if !DEVELOPMENT && !RELEASE
            try {
                RenderCommand.ClearRenderTarget(rtv);
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
#endif
        }
コード例 #22
0
        public void TestClearDepthStencil()
        {
            Texture2D <TexelFormat.DepthStencil> depthStencilBuffer = TextureFactory.NewTexture2D <TexelFormat.DepthStencil>()
                                                                      .WithWidth(800U)
                                                                      .WithHeight(600U)
                                                                      .WithDynamicDetail(false)
                                                                      .WithMipAllocation(false)
                                                                      .WithMipGenerationTarget(false)
                                                                      .WithMultisampling(false)
                                                                      .WithPermittedBindings(GPUBindings.DepthStencilTarget)
                                                                      .WithUsage(ResourceUsage.Write);

            DepthStencilView dsv = depthStencilBuffer.CreateDepthStencilView(0U);

            RenderCommand testCommand = RenderCommand.ClearDepthStencil(dsv);

            Assert.AreEqual(RenderCommandInstruction.ClearDepthStencil, testCommand.Instruction);
            Assert.AreEqual((RenderCommandArgument)(IntPtr)(ResourceViewHandle)dsv.ResourceViewHandle, testCommand.Arg1);

#if !DEVELOPMENT && !RELEASE
            try {
                RenderCommand.ClearDepthStencil(null as DepthStencilView);
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
#endif

            dsv.Dispose();
            depthStencilBuffer.Dispose();

#if !DEVELOPMENT && !RELEASE
            try {
                RenderCommand.ClearDepthStencil(dsv);
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
#endif
        }
コード例 #23
0
        public void TestCopyTo()
        {
            LosgapSystem.InvokeOnMaster(() => {
                // Define variables and constants
                const uint WIDTH_TX  = 256U;
                const uint HEIGHT_TX = 32U;
                const uint DEPTH_TX  = 32U;

                const uint NUM_TEXELS_TO_COPY_PER_ROW = 25U;
                const uint FIRST_TEXEL_TO_COPY_IN_ROW = 25U;
                const uint NUM_ROWS_TO_COPY_PER_SLICE = 5U;
                const uint FIRST_ROW_TO_COPY_IN_SLICE = 1U;
                const uint NUM_SLICES_TO_COPY         = 10U;
                const uint FIRST_SLICE_TO_COPY        = 4U;
                const uint SRC_MIP_INDEX      = 1U;
                const uint DST_MIP_INDEX      = 1U;
                const uint DST_WRITE_OFFSET_X = 15U;
                const uint DST_WRITE_OFFSET_Y = 2U;
                const uint DST_WRITE_OFFSET_Z = 0U;

                const float DATA_VALUE_ADDITION_W = (float)(HEIGHT_TX >> 1) * (float)(WIDTH_TX >> 1);
                const float DATA_VALUE_ADDITION_V = (float)(WIDTH_TX >> 1);
                const float DATA_VALUE_START_R    =
                    WIDTH_TX * HEIGHT_TX * DEPTH_TX
                    + FIRST_TEXEL_TO_COPY_IN_ROW
                    + DATA_VALUE_ADDITION_V * FIRST_ROW_TO_COPY_IN_SLICE
                    + DATA_VALUE_ADDITION_W * FIRST_SLICE_TO_COPY;

                TexelFormat.RGBA32Float[] initialData = Enumerable.Range(0, (int)TextureUtils.GetSizeTexels(true, WIDTH_TX, HEIGHT_TX, DEPTH_TX))
                                                        .Select(i => new TexelFormat.RGBA32Float {
                    R = (float)i, G = (float)i * 2f, B = (float)i * 4f, A = (float)i * 8f
                })
                                                        .ToArray();

                Texture3D <TexelFormat.RGBA32Float> srcTex = TextureFactory.NewTexture3D <TexelFormat.RGBA32Float>()
                                                             .WithDynamicDetail(false)
                                                             .WithInitialData(initialData)
                                                             .WithMipAllocation(true)
                                                             .WithMipGenerationTarget(false)
                                                             .WithPermittedBindings(GPUBindings.ReadableShaderResource)
                                                             .WithUsage(ResourceUsage.Immutable)
                                                             .WithWidth(WIDTH_TX)
                                                             .WithHeight(HEIGHT_TX)
                                                             .WithDepth(DEPTH_TX);

                // Set up context

                // Execute
                Texture3D <TexelFormat.RGBA32Float> dstTex = srcTex.Clone()
                                                             .WithUsage(ResourceUsage.StagingRead)
                                                             .WithPermittedBindings(GPUBindings.None);
                SubresourceBox targetBox = new SubresourceBox(
                    FIRST_TEXEL_TO_COPY_IN_ROW, FIRST_TEXEL_TO_COPY_IN_ROW + NUM_TEXELS_TO_COPY_PER_ROW,
                    FIRST_ROW_TO_COPY_IN_SLICE, FIRST_ROW_TO_COPY_IN_SLICE + NUM_ROWS_TO_COPY_PER_SLICE,
                    FIRST_SLICE_TO_COPY, FIRST_SLICE_TO_COPY + NUM_SLICES_TO_COPY
                    );
                srcTex.CopyTo(
                    dstTex,
                    targetBox,
                    SRC_MIP_INDEX,
                    DST_MIP_INDEX,
                    DST_WRITE_OFFSET_X,
                    DST_WRITE_OFFSET_Y,
                    DST_WRITE_OFFSET_Z
                    );

                // Assert outcome
                TexelArray3D <TexelFormat.RGBA32Float> copiedData = dstTex.Read(DST_MIP_INDEX);
                for (int w = 0; w < NUM_SLICES_TO_COPY; ++w)
                {
                    for (int v = 0; v < NUM_ROWS_TO_COPY_PER_SLICE; ++v)
                    {
                        for (int u = 0; u < NUM_TEXELS_TO_COPY_PER_ROW; ++u)
                        {
                            var thisTexel = copiedData[u + (int)DST_WRITE_OFFSET_X, v + (int)DST_WRITE_OFFSET_Y, w + (int)DST_WRITE_OFFSET_Z];
                            Assert.AreEqual((float)(DATA_VALUE_START_R + u + v * DATA_VALUE_ADDITION_V + w * DATA_VALUE_ADDITION_W), thisTexel.R);
                            Assert.AreEqual((float)(DATA_VALUE_START_R + u + v * DATA_VALUE_ADDITION_V + w * DATA_VALUE_ADDITION_W) * 2f, thisTexel.G);
                            Assert.AreEqual((float)(DATA_VALUE_START_R + u + v * DATA_VALUE_ADDITION_V + w * DATA_VALUE_ADDITION_W) * 4f, thisTexel.B);
                            Assert.AreEqual((float)(DATA_VALUE_START_R + u + v * DATA_VALUE_ADDITION_V + w * DATA_VALUE_ADDITION_W) * 8f, thisTexel.A);
                        }
                    }
                }

                srcTex.Dispose();
                dstTex.Dispose();
            });
        }
コード例 #24
0
ファイル: Font.cs プロジェクト: dawsonzhangwei/EscapeLizards
        public static Font Load(string fontFile, FragmentShader textFS, uint?lineHeightPixels, int?kerningPixels)
        {
            Assure.NotNull(fontFile);
            Assure.NotNull(textFS);
            Assure.False(textFS.IsDisposed);
            if (!IOUtils.IsValidFilePath(fontFile) || !File.Exists(fontFile))
            {
                throw new FileNotFoundException("File '" + fontFile + "' not found: Could not load font.");
            }

            XDocument fontDocument  = XDocument.Load(fontFile, LoadOptions.None);
            XElement  root          = fontDocument.Root;
            XElement  commonElement = root.Element("common");

            if (commonElement == null)
            {
                throw new InvalidOperationException("Could not find common element in given font file.");
            }

            string name = Path.GetFileNameWithoutExtension(fontFile).CapitalizeFirst();
            uint   texWidth;
            uint   texHeight;

            try {
                texWidth  = uint.Parse(commonElement.Attribute("scaleW").Value);
                texHeight = uint.Parse(commonElement.Attribute("scaleH").Value);
                if (lineHeightPixels == null)
                {
                    lineHeightPixels = uint.Parse(commonElement.Attribute("lineHeight").Value) / 2U;
                }
            }
            catch (Exception e) {
                throw new InvalidOperationException("Could not read scaleW, scaleH, or lineHeight value!", e);
            }

            XElement pagesElement = root.Element("pages");
            IEnumerable <XElement> pageElements = pagesElement.Elements("page");

            ITexture2D[]         pageArray          = new ITexture2D[pageElements.Count()];
            ShaderResourceView[] characterPageViews = new ShaderResourceView[pageArray.Length];

            foreach (XElement pageElement in pageElements)
            {
                int    id;
                string filename;
                try {
                    id       = int.Parse(pageElement.Attribute("id").Value);
                    filename = pageElement.Attribute("file").Value;
                }
                catch (Exception e) {
                    throw new InvalidOperationException("Could not read page ID or filename for page " + pageElement + ".", e);
                }

                string fullFilename = Path.Combine(Path.GetDirectoryName(fontFile), filename);
                if (!IOUtils.IsValidFilePath(fullFilename) || !File.Exists(fullFilename))
                {
                    throw new InvalidOperationException("Page file '" + fullFilename + "' does not exist!");
                }
                if (id < 0 || id >= pageArray.Length || pageArray[id] != null)
                {
                    throw new InvalidOperationException("Invalid or duplicate page ID '" + id + "'.");
                }

                pageArray[id] = TextureFactory.LoadTexture2D()
                                .WithFilePath(fullFilename)
                                .WithPermittedBindings(GPUBindings.ReadableShaderResource)
                                .WithUsage(ResourceUsage.Immutable)
                                .Create();

                characterPageViews[id] = pageArray[id].CreateView();
            }

            GeometryCacheBuilder <DefaultVertex> characterCacheBuilder = new GeometryCacheBuilder <DefaultVertex>();
            Dictionary <char, FontCharacter>     charMap = new Dictionary <char, FontCharacter>();
            XElement charsElement = root.Element("chars");

            foreach (XElement charElement in charsElement.Elements("char"))
            {
                char unicodeValue;
                uint x, y, width, height;
                int  pageID, yOffset;

                try {
                    unicodeValue = (char)short.Parse(charElement.Attribute("id").Value);
                    x            = uint.Parse(charElement.Attribute("x").Value);
                    y            = uint.Parse(charElement.Attribute("y").Value);
                    width        = uint.Parse(charElement.Attribute("width").Value);
                    height       = uint.Parse(charElement.Attribute("height").Value);
                    pageID       = int.Parse(charElement.Attribute("page").Value);
                    yOffset      = int.Parse(charElement.Attribute("yoffset").Value);
                }
                catch (Exception e) {
                    throw new InvalidOperationException("Could not acquire character ID, page ID, or dimensions for char " + charElement + ".", e);
                }

                Rectangle charMapBoundary = new Rectangle(x, y, width, height);

                ModelHandle modelHandle = characterCacheBuilder.AddModel(
                    "Font_" + name + "_Character_" + unicodeValue,
                    new[] {
                    new DefaultVertex(
                        new Vector3(0f, 0f, 1f),
                        Vector3.BACKWARD, new Vector2(
                            charMapBoundary.GetCornerX(Rectangle.RectangleCorner.BottomLeft) / texWidth,
                            charMapBoundary.GetCornerY(Rectangle.RectangleCorner.BottomLeft) / texHeight
                            )),
                    new DefaultVertex(
                        new Vector3(charMapBoundary.Width, 0f, 1f),
                        Vector3.BACKWARD, new Vector2(
                            charMapBoundary.GetCornerX(Rectangle.RectangleCorner.BottomRight) / texWidth,
                            charMapBoundary.GetCornerY(Rectangle.RectangleCorner.BottomRight) / texHeight
                            )),
                    new DefaultVertex(
                        new Vector3(charMapBoundary.Width, -charMapBoundary.Height, 1f),
                        Vector3.BACKWARD, new Vector2(
                            charMapBoundary.GetCornerX(Rectangle.RectangleCorner.TopRight) / texWidth,
                            charMapBoundary.GetCornerY(Rectangle.RectangleCorner.TopRight) / texHeight
                            )),
                    new DefaultVertex(
                        new Vector3(0f, -charMapBoundary.Height, 1f),
                        Vector3.BACKWARD, new Vector2(
                            charMapBoundary.GetCornerX(Rectangle.RectangleCorner.TopLeft) / texWidth,
                            charMapBoundary.GetCornerY(Rectangle.RectangleCorner.TopLeft) / texHeight
                            )),
                },
                    new[] { 0U, 1U, 3U, 1U, 2U, 3U }
                    );

                //yOffset = 0;
                //if (unicodeValue == '.') yOffset = (int) (lineHeightPixels.Value * 0.9f);

                charMap.Add(
                    unicodeValue,
                    new FontCharacter(
                        unicodeValue,
                        charMapBoundary,
                        modelHandle,
                        textFS,
                        characterPageViews[pageID],
                        yOffset
                        )
                    );
            }

            if (kerningPixels == null)
            {
                kerningPixels = (int)(charMap.Values.Max(value => value.Boundary.Width) * 0.15f);
            }

            uint maxCharHeight = (uint)charMap.Values.Max(fc => fc.Boundary.Height);

            return(new Font(
                       name,
                       lineHeightPixels.Value,
                       kerningPixels.Value,
                       characterCacheBuilder.Build(),
                       pageArray,
                       characterPageViews,
                       charMap,
                       (ConstantBufferBinding)textFS.GetBindingByIdentifier(TEXT_COLOR_SHADER_CB_NAME),
                       maxCharHeight
                       ));
        }
コード例 #25
0
        public void TestCreateDefaultViewForTextures()
        {
            const uint         TEST_TEXTURE_WIDTH  = 128U;
            const uint         TEST_TEXTURE_HEIGHT = 64U;
            const uint         TEST_TEXTURE_DEPTH  = 32U;
            const uint         TEST_ARRAY_LEN      = 10U;
            IResource          testResource;
            ShaderResourceView testSRV;

            testResource = TextureFactory.NewTexture1D <TexelFormat.RGB32Float>()
                           .WithDynamicDetail(false)
                           .WithMipAllocation(true)
                           .WithMipGenerationTarget(false)
                           .WithPermittedBindings(GPUBindings.ReadableShaderResource)
                           .WithUsage(ResourceUsage.Write)
                           .WithWidth(TEST_TEXTURE_WIDTH)
                           .Create();
            testSRV = ((ITexture)testResource).CreateView();
            Assert.IsFalse(testSRV.ResourceOrViewDisposed);
            Assert.AreEqual(testResource, testSRV.Resource);
            Assert.AreEqual(0U, ((ShaderTextureResourceView)testSRV).FirstMipIndex);
            Assert.AreEqual(TextureUtils.GetNumMips(TEST_TEXTURE_WIDTH), ((ShaderTextureResourceView)testSRV).NumMips);
            testResource.Dispose();
            Assert.IsTrue(testSRV.ResourceOrViewDisposed);
            testSRV.Dispose();
            Assert.IsTrue(testSRV.IsDisposed);

            testResource = TextureFactory.NewTexture2D <TexelFormat.RGB32Float>()
                           .WithDynamicDetail(false)
                           .WithMipAllocation(true)
                           .WithMipGenerationTarget(false)
                           .WithPermittedBindings(GPUBindings.ReadableShaderResource)
                           .WithUsage(ResourceUsage.Write)
                           .WithWidth(TEST_TEXTURE_WIDTH)
                           .WithHeight(TEST_TEXTURE_HEIGHT)
                           .Create();
            testSRV = ((ITexture)testResource).CreateView();
            Assert.IsFalse(testSRV.ResourceOrViewDisposed);
            Assert.AreEqual(testResource, testSRV.Resource);
            Assert.AreEqual(0U, ((ShaderTextureResourceView)testSRV).FirstMipIndex);
            Assert.AreEqual(TextureUtils.GetNumMips(TEST_TEXTURE_WIDTH, TEST_TEXTURE_HEIGHT), ((ShaderTextureResourceView)testSRV).NumMips);
            testResource.Dispose();
            Assert.IsTrue(testSRV.ResourceOrViewDisposed);
            testSRV.Dispose();
            Assert.IsTrue(testSRV.IsDisposed);

            testResource = TextureFactory.NewTexture3D <TexelFormat.RGB32Float>()
                           .WithDynamicDetail(false)
                           .WithMipAllocation(true)
                           .WithMipGenerationTarget(false)
                           .WithPermittedBindings(GPUBindings.ReadableShaderResource)
                           .WithUsage(ResourceUsage.Write)
                           .WithWidth(TEST_TEXTURE_WIDTH)
                           .WithHeight(TEST_TEXTURE_HEIGHT)
                           .WithDepth(TEST_TEXTURE_DEPTH)
                           .Create();
            testSRV = ((ITexture)testResource).CreateView();
            Assert.IsFalse(testSRV.ResourceOrViewDisposed);
            Assert.AreEqual(testResource, testSRV.Resource);
            Assert.AreEqual(0U, ((ShaderTextureResourceView)testSRV).FirstMipIndex);
            Assert.AreEqual(TextureUtils.GetNumMips(TEST_TEXTURE_WIDTH, TEST_TEXTURE_HEIGHT, TEST_TEXTURE_DEPTH), ((ShaderTextureResourceView)testSRV).NumMips);
            testResource.Dispose();
            Assert.IsTrue(testSRV.ResourceOrViewDisposed);
            testSRV.Dispose();
            Assert.IsTrue(testSRV.IsDisposed);

            testResource = TextureFactory.NewTexture1D <TexelFormat.RGB32Float>()
                           .WithDynamicDetail(false)
                           .WithMipAllocation(true)
                           .WithMipGenerationTarget(false)
                           .WithPermittedBindings(GPUBindings.ReadableShaderResource)
                           .WithUsage(ResourceUsage.Write)
                           .WithWidth(TEST_TEXTURE_WIDTH)
                           .CreateArray(TEST_ARRAY_LEN);
            testSRV = ((ITextureArray)testResource).CreateView();
            Assert.IsFalse(testSRV.ResourceOrViewDisposed);
            Assert.AreEqual(testResource, testSRV.Resource);
            Assert.AreEqual(0U, ((ShaderTextureArrayResourceView)testSRV).FirstMipIndex);
            Assert.AreEqual(TextureUtils.GetNumMips(TEST_TEXTURE_WIDTH), ((ShaderTextureArrayResourceView)testSRV).NumMips);
            Assert.AreEqual(0U, ((ShaderTextureArrayResourceView)testSRV).FirstArrayElementIndex);
            Assert.AreEqual(TEST_ARRAY_LEN, ((ShaderTextureArrayResourceView)testSRV).NumArrayElements);
            testResource.Dispose();
            Assert.IsTrue(testSRV.ResourceOrViewDisposed);
            testSRV.Dispose();
            Assert.IsTrue(testSRV.IsDisposed);

            testResource = TextureFactory.NewTexture2D <TexelFormat.RGB32Float>()
                           .WithDynamicDetail(false)
                           .WithMipAllocation(true)
                           .WithMipGenerationTarget(false)
                           .WithPermittedBindings(GPUBindings.ReadableShaderResource)
                           .WithUsage(ResourceUsage.Write)
                           .WithWidth(TEST_TEXTURE_WIDTH)
                           .WithHeight(TEST_TEXTURE_HEIGHT)
                           .CreateArray(TEST_ARRAY_LEN);
            testSRV = ((ITextureArray)testResource).CreateView();
            Assert.IsFalse(testSRV.ResourceOrViewDisposed);
            Assert.AreEqual(testResource, testSRV.Resource);
            Assert.AreEqual(0U, ((ShaderTextureArrayResourceView)testSRV).FirstMipIndex);
            Assert.AreEqual(TextureUtils.GetNumMips(TEST_TEXTURE_WIDTH, TEST_TEXTURE_HEIGHT), ((ShaderTextureArrayResourceView)testSRV).NumMips);
            Assert.AreEqual(0U, ((ShaderTextureArrayResourceView)testSRV).FirstArrayElementIndex);
            Assert.AreEqual(TEST_ARRAY_LEN, ((ShaderTextureArrayResourceView)testSRV).NumArrayElements);
            testResource.Dispose();
            Assert.IsTrue(testSRV.ResourceOrViewDisposed);
            testSRV.Dispose();
            Assert.IsTrue(testSRV.IsDisposed);
        }
コード例 #26
0
        public unsafe void TestSetRenderTargets()
        {
            Texture2DArray <TexelFormat.RenderTarget> backBufferArray = TextureFactory.NewTexture2D <TexelFormat.RenderTarget>()
                                                                        .WithWidth(800U)
                                                                        .WithHeight(600U)
                                                                        .WithDynamicDetail(false)
                                                                        .WithMipAllocation(false)
                                                                        .WithMipGenerationTarget(false)
                                                                        .WithMultisampling(false)
                                                                        .WithPermittedBindings(GPUBindings.RenderTarget)
                                                                        .WithUsage(ResourceUsage.Write)
                                                                        .CreateArray(RenderCommand.MAX_RENDER_TARGETS + 1U);

            Texture2D <TexelFormat.DepthStencil> depthStencil = backBufferArray.Clone()
                                                                .WithTexelFormat <TexelFormat.DepthStencil>()
                                                                .WithPermittedBindings(GPUBindings.DepthStencilTarget);

            RenderTargetView[] rtvArr = backBufferArray.Select(tex => tex.CreateRenderTargetView(0U)).ToArray();
            DepthStencilView   dsv    = depthStencil.CreateDepthStencilView(0U);

            RenderCommand testCommand = RenderCommand.SetRenderTargets(dsv, rtvArr.Take((int)RenderCommand.MAX_RENDER_TARGETS).ToArray());

            Assert.AreEqual(RenderCommandInstruction.SetRenderTargets, testCommand.Instruction);
            RenderTargetViewHandle *rtvArrPtr
                = (RenderTargetViewHandle *)new IntPtr(UnsafeUtils.Reinterpret <RenderCommandArgument, long>(testCommand.Arg1, sizeof(long)));

            for (int i = 0; i < RenderCommand.MAX_RENDER_TARGETS; ++i)
            {
                Assert.AreEqual(rtvArr[i].ResourceViewHandle, rtvArrPtr[i]);
            }
            Assert.AreEqual(
                dsv.ResourceViewHandle,
                UnsafeUtils.Reinterpret <IntPtr, DepthStencilViewHandle>(new IntPtr(UnsafeUtils.Reinterpret <RenderCommandArgument, long>(testCommand.Arg2, sizeof(long))), sizeof(DepthStencilViewHandle))
                );
            Assert.AreEqual((RenderCommandArgument)RenderCommand.MAX_RENDER_TARGETS, testCommand.Arg3);

#if !DEVELOPMENT && !RELEASE
            try {
                RenderCommand.SetRenderTargets(null as DepthStencilView, rtvArr.Take((int)RenderCommand.MAX_RENDER_TARGETS).ToArray());
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }

            try {
                RenderCommand.SetRenderTargets(dsv, null);
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }

            try {
                RenderCommand.SetRenderTargets(dsv, rtvArr[0], rtvArr[1], null, rtvArr[2]);
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }

            try {
                RenderCommand.SetRenderTargets(dsv, rtvArr);
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
#endif

            rtvArr.ForEach(rtv => rtv.Dispose());
            dsv.Dispose();
            backBufferArray.Dispose();
            depthStencil.Dispose();

#if !DEVELOPMENT && !RELEASE
            try {
                RenderCommand.SetRenderTargets(dsv, rtvArr.Take((int)RenderCommand.MAX_RENDER_TARGETS).ToArray());
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
#endif
        }
コード例 #27
0
        public void TestCreationWithInitialData()
        {
            // Define variables and constants
            const uint TEXTURE_WIDTH  = 1 << 6;
            const uint TEXTURE_HEIGHT = 1 << 4;
            const uint TEXTURE_DEPTH  = 1 << 3;
            Texture3DBuilder <TexelFormat.RGBA8UInt> texBuilder = TextureFactory.NewTexture3D <TexelFormat.RGBA8UInt>()
                                                                  .WithUsage(ResourceUsage.StagingRead)
                                                                  .WithPermittedBindings(GPUBindings.None)
                                                                  .WithWidth(TEXTURE_WIDTH)
                                                                  .WithHeight(TEXTURE_HEIGHT)
                                                                  .WithDepth(TEXTURE_DEPTH);

            TexelFormat.RGBA8UInt[]           initialDataA = new TexelFormat.RGBA8UInt[TEXTURE_WIDTH * TEXTURE_HEIGHT * TEXTURE_DEPTH];
            TexelFormat.RGBA8UInt[]           initialDataB = new TexelFormat.RGBA8UInt[TextureUtils.GetSizeTexels(true, TEXTURE_WIDTH, TEXTURE_HEIGHT, TEXTURE_DEPTH)];
            Texture3D <TexelFormat.RGBA8UInt> testTextureA, testTextureB;

            TexelArray3D <TexelFormat.RGBA8UInt> texAData;

            TexelArray3D <TexelFormat.RGBA8UInt>[] texBData = new TexelArray3D <TexelFormat.RGBA8UInt> [TextureUtils.GetNumMips(TEXTURE_WIDTH, TEXTURE_HEIGHT, TEXTURE_DEPTH)];

            // Set up context
            for (uint i = 0; i < initialDataA.Length; ++i)
            {
                initialDataA[i].R = (byte)i;
                initialDataA[i].G = (byte)(i * 2);
                initialDataA[i].B = (byte)(i * 3);
                initialDataA[i].A = (byte)(i * 4);
            }
            testTextureA = texBuilder.WithInitialData(initialDataA).Create();

            uint mipWidth   = TEXTURE_WIDTH;
            uint mipHeight  = TEXTURE_HEIGHT;
            uint mipDepth   = TEXTURE_DEPTH;
            uint texelIndex = 0U;

            while (mipWidth > 1U || mipHeight > 1U || mipDepth > 1U)
            {
                for (uint w = 0; w < mipDepth; ++w)
                {
                    for (uint v = 0; v < mipHeight; ++v)
                    {
                        for (uint u = 0; u < mipWidth; ++u, ++texelIndex)
                        {
                            initialDataB[texelIndex].R = (byte)(u + mipWidth + v + mipHeight + w + mipDepth);
                            initialDataB[texelIndex].G = (byte)(u + mipWidth + v + mipHeight + w + mipDepth * 2);
                            initialDataB[texelIndex].B = (byte)(u + mipWidth + v + mipHeight + w + mipDepth * 3);
                            initialDataB[texelIndex].A = (byte)(u + mipWidth + v + mipHeight + w + mipDepth * 4);
                        }
                    }
                }
                mipWidth  = Math.Max(1U, mipWidth >> 1);
                mipHeight = Math.Max(1U, mipHeight >> 1);
                mipDepth  = Math.Max(1U, mipDepth >> 1);
            }
            initialDataB[initialDataB.Length - 1] = new TexelFormat.RGBA8UInt {
                R = 3, G = 4, B = 5, A = 6
            };
            testTextureB = texBuilder.WithMipAllocation(true).WithInitialData(initialDataB).Create();

            // Execute
            texAData = testTextureA.Read(0U);
            for (uint i = 0; i < texBData.Length; ++i)
            {
                texBData[i] = testTextureB.Read(i);
            }

            // Assert outcome
            for (uint i = 0; i < texAData.Width; ++i)
            {
                Assert.AreEqual((byte)i, initialDataA[i].R);
                Assert.AreEqual((byte)(i * 2), initialDataA[i].G);
                Assert.AreEqual((byte)(i * 3), initialDataA[i].B);
                Assert.AreEqual((byte)(i * 4), initialDataA[i].A);
            }

            for (uint mipIndex = 0U; mipIndex < testTextureB.NumMips; ++mipIndex)
            {
                for (uint w = 0; w < testTextureB.MipDepth(mipIndex); ++w)
                {
                    for (uint v = 0; v < testTextureB.MipHeight(mipIndex); ++v)
                    {
                        for (uint u = 0; u < testTextureB.MipWidth(mipIndex); ++u)
                        {
                            Assert.AreEqual((byte)(u + testTextureB.MipWidth(mipIndex) + v + testTextureB.MipHeight(mipIndex) + w + testTextureB.MipDepth(mipIndex)), texBData[mipIndex][u, v, w].R);
                            Assert.AreEqual((byte)(u + testTextureB.MipWidth(mipIndex) + v + testTextureB.MipHeight(mipIndex) + w + testTextureB.MipDepth(mipIndex) * 2), texBData[mipIndex][u, v, w].G);
                            Assert.AreEqual((byte)(u + testTextureB.MipWidth(mipIndex) + v + testTextureB.MipHeight(mipIndex) + w + testTextureB.MipDepth(mipIndex) * 3), texBData[mipIndex][u, v, w].B);
                            Assert.AreEqual((byte)(u + testTextureB.MipWidth(mipIndex) + v + testTextureB.MipHeight(mipIndex) + w + testTextureB.MipDepth(mipIndex) * 4), texBData[mipIndex][u, v, w].A);
                        }
                    }
                }
            }

            testTextureA.Dispose();
            testTextureB.Dispose();
        }
コード例 #28
0
        public void TestCreationParameters()
        {
            // Define variables and constants
            var defaultBuilder = TextureFactory.NewTexture3D <TexelFormat.RGBA32UInt>().WithUsage(ResourceUsage.DiscardWrite)
                                 .WithWidth(100).WithHeight(256).WithDepth(8);
            var withStagingUsage      = defaultBuilder.WithUsage(ResourceUsage.StagingReadWrite).WithPermittedBindings(GPUBindings.None);
            var withReadWriteBindings = defaultBuilder.WithUsage(ResourceUsage.Write).WithPermittedBindings(GPUBindings.ReadableShaderResource | GPUBindings.WritableShaderResource);
            var withDifferentFormat   = defaultBuilder.WithTexelFormat <TexelFormat.Int8>();
            var withWidth300          = defaultBuilder.WithWidth(300);
            var withMipAllocation     = defaultBuilder.WithUsage(ResourceUsage.Write).WithWidth(1 << 9).WithMipAllocation(true);
            var withDynDetail         = withMipAllocation.WithDynamicDetail(true);
            var withMipGen            = withMipAllocation
                                        .WithUsage(ResourceUsage.Write)
                                        .WithPermittedBindings(GPUBindings.RenderTarget | GPUBindings.ReadableShaderResource)
                                        .WithMipGenerationTarget(true)
                                        .WithTexelFormat <TexelFormat.RGBA8UNorm>();

            // Set up context

            // Execute

            // Assert outcome
            ITexture3D tex = withStagingUsage.Create();

            Assert.AreEqual(ResourceUsage.StagingReadWrite, tex.Usage);
            tex.Dispose();
            tex = withReadWriteBindings.Create();
            Assert.AreEqual(GPUBindings.ReadableShaderResource | GPUBindings.WritableShaderResource, tex.PermittedBindings);
            tex.Dispose();
            tex = withDifferentFormat.Create();
            Assert.AreEqual(typeof(TexelFormat.Int8), tex.TexelFormat);
            tex.Dispose();
            tex = withWidth300.Create();
            Assert.AreEqual(300U, tex.Width);
            tex.Dispose();
            tex = withMipAllocation.Create();
            Assert.AreEqual(TextureUtils.GetNumMips(1 << 9, 256), tex.NumMips);
            tex.Dispose();
            tex = withDynDetail.Create();
            Assert.AreEqual(true, tex.IsGlobalDetailTarget);
            tex.Dispose();
            tex = withMipGen.Create();
            Assert.AreEqual(true, tex.IsMipGenTarget);
            tex.Dispose();

#if !DEVELOPMENT && !RELEASE
            try {
                TextureFactory.NewTexture3D <TexelFormat.Float32>()
                .WithUsage(ResourceUsage.Immutable)
                .WithInitialData(null)
                .Create();
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
            try {
                TextureFactory.NewTexture3D <TexelFormat.Float32>()
                .WithUsage(ResourceUsage.DiscardWrite)
                .WithPermittedBindings(GPUBindings.None)
                .Create();
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
            try {
                TextureFactory.NewTexture3D <TexelFormat.Float32>()
                .WithUsage(ResourceUsage.StagingRead)
                .WithPermittedBindings(GPUBindings.ReadableShaderResource)
                .Create();
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
            try {
                TextureFactory.NewTexture3D <TexelFormat.Float32>()
                .WithWidth(0U)
                .Create();
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
            try {
                TextureFactory.NewTexture3D <TexelFormat.Float32>()
                .WithMipAllocation(true)
                .WithWidth(140)
                .WithHeight(1U)
                .Create();
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
            try {
                TextureFactory.NewTexture3D <TexelFormat.Float32>()
                .WithMipAllocation(true)
                .WithMipGenerationTarget(true)
                .WithWidth(1 << 4)
                .WithHeight(1 << 4)
                .WithUsage(ResourceUsage.Write)
                .WithPermittedBindings(GPUBindings.None)
                .Create();
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
            try {
                TextureFactory.NewTexture3D <TexelFormat.Float32>()
                .WithMipAllocation(false)
                .WithMipGenerationTarget(true)
                .WithWidth(1 << 4)
                .WithHeight(1 << 4)
                .WithUsage(ResourceUsage.Write)
                .WithPermittedBindings(GPUBindings.RenderTarget | GPUBindings.ReadableShaderResource)
                .Create();
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
            try {
                TextureFactory.NewTexture3D <TexelFormat.Float32>()
                .WithMipAllocation(true)
                .WithMipGenerationTarget(true)
                .WithWidth(1 << 4)
                .WithHeight(1 << 4)
                .WithUsage(ResourceUsage.Write)
                .WithPermittedBindings(GPUBindings.RenderTarget | GPUBindings.ReadableShaderResource)
                .WithInitialData(new TexelFormat.Float32[(1 << 5) - 1])
                .Create();
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
            try {
                TextureFactory.NewTexture3D <TexelFormat.Float32>()
                .WithMipAllocation(true)
                .WithWidth(1 << 4)
                .WithHeight(1 << 4)
                .WithUsage(ResourceUsage.Immutable)
                .WithPermittedBindings(GPUBindings.ReadableShaderResource)
                .WithInitialData(new TexelFormat.Float32[1 << 4])
                .Create();
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
            try {
                TextureFactory.NewTexture3D <TexelFormat.Float32>()
                .WithMipAllocation(false)
                .WithWidth(1 << 4)
                .WithHeight(1 << 4)
                .WithUsage(ResourceUsage.Immutable)
                .WithPermittedBindings(GPUBindings.ReadableShaderResource)
                .WithInitialData(new TexelFormat.Float32[(1 << 5) - 1])
                .Create();
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
#endif
        }
コード例 #29
0
        public unsafe void TestSetShaderResourceViews()
        {
            Texture2DBuilder <TexelFormat.RGBA32UInt> texBuilder = TextureFactory.NewTexture2D <TexelFormat.RGBA32UInt>()
                                                                   .WithWidth(100U)
                                                                   .WithHeight(100U)
                                                                   .WithUsage(ResourceUsage.DiscardWrite);
            Texture2D <TexelFormat.RGBA32UInt> tex0 = texBuilder.Create();
            Texture2D <TexelFormat.RGBA32UInt> tex2 = texBuilder.Create();

            BaseResourceView rv0 = tex0.CreateView();
            BaseResourceView rv2 = tex2.CreateView();

            Shader shader = new FragmentShader(
                @"Tests\SimpleFS.cso",
                new ResourceViewBinding(0U, "RV0"),
                new ResourceViewBinding(1U, "RV1"),
                new ResourceViewBinding(2U, "RV2")
                );

            Dictionary <ResourceViewBinding, BaseResourceView> rvDict = new Dictionary <ResourceViewBinding, BaseResourceView>();

            rvDict[shader.ResourceViewBindings[0]] = rv0;
            rvDict[shader.ResourceViewBindings[2]] = rv2;

            RenderCommand testCommand = RenderCommand.SetShaderResourceViews(shader, rvDict);

            Assert.AreEqual(RenderCommandInstruction.FSSetResources, testCommand.Instruction);
            ResourceViewHandle *resHandleArray = (ResourceViewHandle *)new IntPtr(UnsafeUtils.Reinterpret <RenderCommandArgument, long>(testCommand.Arg1, sizeof(long)));

            Assert.AreEqual(rv0.ResourceViewHandle, resHandleArray[0]);
            Assert.AreEqual(ResourceViewHandle.NULL, resHandleArray[1]);
            Assert.AreEqual(rv2.ResourceViewHandle, resHandleArray[2]);
            Assert.AreEqual((RenderCommandArgument)3U, testCommand.Arg2);

#if !DEVELOPMENT && !RELEASE
            try {
                RenderCommand.SetShaderResourceViews(null, rvDict);
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }

            try {
                RenderCommand.SetShaderResourceViews(shader, null);
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
#endif

            tex0.Dispose();
            tex2.Dispose();
            rv0.Dispose();
            rv2.Dispose();

#if !DEVELOPMENT && !RELEASE
            try {
                RenderCommand.SetShaderResourceViews(shader, rvDict);
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
#endif

            shader.Dispose();

#if !DEVELOPMENT && !RELEASE
            try {
                RenderCommand.SetShaderResourceViews(shader, new Dictionary <ResourceViewBinding, BaseResourceView>());
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
#endif
        }
コード例 #30
0
        public void TestDifferentFormats()
        {
            var defaultLoader = TextureFactory.LoadTexture2D()
                                .WithUsage(ResourceUsage.Immutable)
                                .WithPermittedBindings(GPUBindings.ReadableShaderResource);

            ITexture2D result;

            result = defaultLoader.WithFilePath(@"Tests\200x200.bmp").Create();
            Assert.AreEqual(200U, result.Width);
            Assert.AreEqual(200U, result.Height);
            Assert.AreEqual(typeof(Texture2D <TexelFormat.RGBA8UNorm>), result.GetType());
            Assert.AreEqual(0U, result.ArrayIndex);
            Assert.AreEqual(false, result.IsArrayTexture);
            Assert.AreEqual(false, result.IsGlobalDetailTarget);
            Assert.AreEqual(false, result.IsMipGenTarget);
            Assert.AreEqual(false, result.IsMipmapped);
            Assert.AreEqual(false, result.IsMultisampled);
            Assert.AreEqual(1U, result.NumMips);
            Assert.AreEqual(200U * 200U * 4U, result.Size);
            Assert.AreEqual(200U * 200U, result.SizeTexels);
            Assert.AreEqual(typeof(TexelFormat.RGBA8UNorm), result.TexelFormat);
            Assert.AreEqual(4U, result.TexelSizeBytes);

            LosgapSystem.InvokeOnMaster(() => {
                Texture2D <TexelFormat.RGBA8UNorm> dataCopyTex = (result as Texture2D <TexelFormat.RGBA8UNorm>).Clone()
                                                                 .WithUsage(ResourceUsage.StagingRead)
                                                                 .WithPermittedBindings(GPUBindings.None);

                result.CopyTo(dataCopyTex);

                Assert.AreEqual(UnsafeUtils.Reinterpret <uint, TexelFormat.RGBA8UNorm>(4294958703U, 4), dataCopyTex.Read(0U)[138, 99]);

                dataCopyTex.Dispose();
            });

            result.Dispose();

            result = defaultLoader.WithFilePath(@"Tests\200x200.png").Create();
            Assert.AreEqual(200U, result.Width);
            Assert.AreEqual(200U, result.Height);
            Assert.AreEqual(typeof(Texture2D <TexelFormat.RGBA8UNormSRGB>), result.GetType());
            Assert.AreEqual(0U, result.ArrayIndex);
            Assert.AreEqual(false, result.IsArrayTexture);
            Assert.AreEqual(false, result.IsGlobalDetailTarget);
            Assert.AreEqual(false, result.IsMipGenTarget);
            Assert.AreEqual(false, result.IsMipmapped);
            Assert.AreEqual(false, result.IsMultisampled);
            Assert.AreEqual(1U, result.NumMips);
            Assert.AreEqual(200U * 200U * 4U, result.Size);
            Assert.AreEqual(200U * 200U, result.SizeTexels);
            Assert.AreEqual(typeof(TexelFormat.RGBA8UNormSRGB), result.TexelFormat);
            Assert.AreEqual(4U, result.TexelSizeBytes);

            LosgapSystem.InvokeOnMaster(() => {
                Texture2D <TexelFormat.RGBA8UNormSRGB> dataCopyTex = (result as Texture2D <TexelFormat.RGBA8UNormSRGB>).Clone()
                                                                     .WithUsage(ResourceUsage.StagingRead)
                                                                     .WithPermittedBindings(GPUBindings.None);

                result.CopyTo(dataCopyTex);

                Assert.AreEqual(UnsafeUtils.Reinterpret <uint, TexelFormat.RGBA8UNormSRGB>(4294958703U, 4), dataCopyTex.Read(0U)[138, 99]);

                dataCopyTex.Dispose();
            });

            result.Dispose();

            result = defaultLoader.WithFilePath(@"Tests\200x200.gif").Create();
            Assert.AreEqual(200U, result.Width);
            Assert.AreEqual(200U, result.Height);
            Assert.AreEqual(typeof(Texture2D <TexelFormat.RGBA8UNorm>), result.GetType());
            Assert.AreEqual(0U, result.ArrayIndex);
            Assert.AreEqual(false, result.IsArrayTexture);
            Assert.AreEqual(false, result.IsGlobalDetailTarget);
            Assert.AreEqual(false, result.IsMipGenTarget);
            Assert.AreEqual(false, result.IsMipmapped);
            Assert.AreEqual(false, result.IsMultisampled);
            Assert.AreEqual(1U, result.NumMips);
            Assert.AreEqual(200U * 200U * 4U, result.Size);
            Assert.AreEqual(200U * 200U, result.SizeTexels);
            Assert.AreEqual(typeof(TexelFormat.RGBA8UNorm), result.TexelFormat);
            Assert.AreEqual(4U, result.TexelSizeBytes);

            LosgapSystem.InvokeOnMaster(() => {
                Texture2D <TexelFormat.RGBA8UNorm> dataCopyTex = (result as Texture2D <TexelFormat.RGBA8UNorm>).Clone()
                                                                 .WithUsage(ResourceUsage.StagingRead)
                                                                 .WithPermittedBindings(GPUBindings.None);

                result.CopyTo(dataCopyTex);

                Assert.AreEqual(UnsafeUtils.Reinterpret <uint, TexelFormat.RGBA8UNorm>(4294958702U, 4), dataCopyTex.Read(0U)[138, 99]);

                dataCopyTex.Dispose();
            });

            result.Dispose();

            result = defaultLoader.WithFilePath(@"Tests\200x200.jpg").Create();
            Assert.AreEqual(200U, result.Width);
            Assert.AreEqual(200U, result.Height);
            Assert.AreEqual(typeof(Texture2D <TexelFormat.RGBA8UNorm>), result.GetType());
            Assert.AreEqual(0U, result.ArrayIndex);
            Assert.AreEqual(false, result.IsArrayTexture);
            Assert.AreEqual(false, result.IsGlobalDetailTarget);
            Assert.AreEqual(false, result.IsMipGenTarget);
            Assert.AreEqual(false, result.IsMipmapped);
            Assert.AreEqual(false, result.IsMultisampled);
            Assert.AreEqual(1U, result.NumMips);
            Assert.AreEqual(200U * 200U * 4U, result.Size);
            Assert.AreEqual(200U * 200U, result.SizeTexels);
            Assert.AreEqual(typeof(TexelFormat.RGBA8UNorm), result.TexelFormat);
            Assert.AreEqual(4U, result.TexelSizeBytes);

            LosgapSystem.InvokeOnMaster(() => {
                Texture2D <TexelFormat.RGBA8UNorm> dataCopyTex = (result as Texture2D <TexelFormat.RGBA8UNorm>).Clone()
                                                                 .WithUsage(ResourceUsage.StagingRead)
                                                                 .WithPermittedBindings(GPUBindings.None);

                result.CopyTo(dataCopyTex);

                Assert.AreEqual(UnsafeUtils.Reinterpret <uint, TexelFormat.RGBA8UNorm>(4294955895U, 4), dataCopyTex.Read(0U)[138, 99]);

                dataCopyTex.Dispose();
            });

            result.Dispose();
        }