Ejemplo n.º 1
0
 public override ByteSize GetSize(uint mipIndex)
 {
     return(TextureUtils.GetSize(TexelSizeBytes, false, MipWidth(mipIndex)));
 }
Ejemplo n.º 2
0
 public override uint GetSizeTexels(uint mipIndex)
 {
     return(TextureUtils.GetSizeTexels(false, MipWidth(mipIndex)));
 }
Ejemplo n.º 3
0
        public unsafe ITexture2D Create()
        {
            Assure.NotNull(filePath, "You must supply a file path to load a texture.");
            string fullFilePath = Path.Combine(Path.GetFullPath(LosgapSystem.InstallationDirectory.ToString()), filePath);

            Assure.True(IOUtils.IsValidFilePath(fullFilePath), "Invalid file path: " + fullFilePath);
            Assure.True(File.Exists(fullFilePath), "File does not exist: " + fullFilePath);
            Assure.False(
                (usage == ResourceUsage.Immutable || usage == ResourceUsage.DiscardWrite) && permittedBindings == GPUBindings.None,
                "An immutable or discard-write resource with no permitted bindings is useless."
                );
            Assure.False(
                usage.GetUsage() == 0x3 && permittedBindings != GPUBindings.None,
                "Staging resources can not be bound to the pipeline."
                );
            Assure.False((usage == ResourceUsage.DiscardWrite || usage == ResourceUsage.Immutable) &&
                         ((permittedBindings & GPUBindings.RenderTarget) > 0 ||
                          (permittedBindings & GPUBindings.DepthStencilTarget) > 0 ||
                          (permittedBindings & GPUBindings.WritableShaderResource) > 0),
                         "Can not bind an immutable or discard-write texture as a render target or depth stencil target, or as a GPU-writeable shader resource."
                         );

            Texture2DResourceHandle outResHandle;
            uint           outWidth;
            uint           outHeight;
            ResourceFormat outFormat;


            InteropUtils.CallNative(NativeMethods.ResourceFactory_LoadTexture2D,
                                    RenderingModule.Device,
                                    fullFilePath,
                                    (InteropBool)mipAllocation,
                                    usage.GetUsage(),
                                    usage.GetCPUUsage(),
                                    (PipelineBindings)permittedBindings,
                                    (InteropBool)mipGenerationTarget,
                                    (InteropBool)dynamicDetail,
                                    (IntPtr)(&outResHandle),
                                    (IntPtr)(&outWidth),
                                    (IntPtr)(&outHeight),
                                    (IntPtr)(&outFormat)
                                    ).ThrowOnFailure();

            Type texelFormat = null;

            foreach (Type texelType in TexelFormat.AllFormats.Keys.Except(typeof(TexelFormat.RenderTarget), typeof(TexelFormat.DepthStencil)))
            {
                if ((ResourceFormat)texelType.GetCustomAttribute <TexelFormatMetadataAttribute>().ResourceFormatIndex == outFormat)
                {
                    texelFormat = texelType;
                    break;
                }
            }
            if (texelFormat == null)
            {
                throw new InvalidOperationException("The format for the loaded texture (" + outFormat + ") is not supported.");
            }

            Type tex2DType      = typeof(Texture2D <>).MakeGenericType(texelFormat);
            uint texelSizeBytes = texelFormat.GetCustomAttribute <TexelFormatMetadataAttribute>().FormatSizeBytes;

            try {
                return(Activator.CreateInstance(
                           tex2DType, BindingFlags.NonPublic | BindingFlags.Instance, null,
                           new object[] {
                    (ResourceHandle)outResHandle,
                    usage,
                    (ByteSize)(outWidth * outHeight * texelSizeBytes),
                    permittedBindings,
                    false,
                    mipGenerationTarget,
                    dynamicDetail,
                    outWidth,
                    outHeight,
                    texelSizeBytes,
                    mipAllocation,
                    mipAllocation ? TextureUtils.GetNumMips(outWidth, outHeight) : 1U,
                    0U,
                    false
                },
                           CultureInfo.InvariantCulture
                           ) as ITexture2D);
            }
            catch (Exception e) {
                throw new InvalidOperationException("Could not load texture as a " + texelFormat.Name + " due to an instantiation error.", e);
            }
        }
Ejemplo n.º 4
0
 public uint MipWidth(uint mipIndex)
 {
     Assure.LessThan(mipIndex, NumMips, "Mip index out of bounds.");
     return(TextureUtils.GetDimensionForMipLevel(width, mipIndex));
 }
Ejemplo n.º 5
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);
        }
        private unsafe Texture1DResourceHandle CreateTexture1D(uint arrayLen, ArraySlice <TTexel>?initialData)
        {
            Assure.True(Usage != ResourceUsage.Immutable || initialData != null, "You must supply initial data to an immutable resource.");
            Assure.False(
                (Usage == ResourceUsage.Immutable || Usage == ResourceUsage.DiscardWrite) && permittedBindings == GPUBindings.None,
                "An immutable or discard-write resource with no permitted bindings is useless."
                );
            Assure.False(
                Usage.GetUsage() == 0x3 && permittedBindings != GPUBindings.None,
                "Staging resources can not be bound to the pipeline."
                );
            Assure.False((Usage == ResourceUsage.DiscardWrite || Usage == ResourceUsage.Immutable) &&
                         ((permittedBindings & GPUBindings.RenderTarget) > 0 ||
                          (permittedBindings & GPUBindings.DepthStencilTarget) > 0 ||
                          (permittedBindings & GPUBindings.WritableShaderResource) > 0),
                         "Can not bind an immutable or discard-write texture as a render target or depth stencil target, or as a GPU-writeable shader resource."
                         );

            Assure.GreaterThan(arrayLen, 0U, "Can not create an array of 0 textures.");

            Assure.GreaterThan(width, 0U, "Please specify a width for the texture.");

            Assure.False(
                mipAllocation && !MathUtils.IsPowerOfTwo(width),
                "Can not create mipmapped texture with any non-power-of-two (NPOT) dimension. Dimensions: " + width + "."
                );
            Assure.False(
                mipAllocation && Usage == ResourceUsage.DiscardWrite,
                "Can not allocate mips on a discard-write texture."
                );
            Assure.False(
                mipGenerationTarget && !mipAllocation,
                "Can not generate mips without allocating space for them."
                );
            Assure.False(
                mipGenerationTarget &&
                ((permittedBindings & GPUBindings.RenderTarget) == 0x0 || (permittedBindings & GPUBindings.ReadableShaderResource) == 0x0),
                "To make a texture a viable mip generation target, it must be created with the RenderTarget and ReadableShaderResource GPU bindings."
                );
            Assure.False(
                mipGenerationTarget && initialData != null,
                "Can not supply initial data to a mip generation target."
                );
            Assure.True(
                initialData == null ||
                (initialData.Value.Length == TextureUtils.GetSizeTexels(mipAllocation, width) * arrayLen),
                "Initial data is of incorrect length (" + (initialData != null ? initialData.Value.Length : 0) + ") for this resource. " +
                "It should have a length of: " + TextureUtils.GetSizeTexels(mipAllocation, width) * arrayLen + "."
                );
            Assure.False(dynamicDetail && Usage.GetUsage() == 0x3, "Can not create a dynamic-detail staging resource.");
            Assure.False(
                (permittedBindings & GPUBindings.DepthStencilTarget) == GPUBindings.DepthStencilTarget &&
                texelFormat != TexelFormat.DSV_FORMAT_CODE,
                "Can not create a depth-stencil target with any texel format other than " + typeof(TexelFormat.DepthStencil).Name + "."
                );

            Texture1DResourceHandle outResourceHandle;
            GCHandle?pinnedInitData   = null;
            GCHandle?pinnedDataHandle = null;
            IntPtr   initialDataPtr   = IntPtr.Zero;

            InitialResourceDataDesc[] dataArr = null;

            if (initialData != null)
            {
                pinnedInitData = GCHandle.Alloc(initialData.Value.ContainingArray, GCHandleType.Pinned);

                dataArr = InitialResourceDataDesc.CreateDataArr(
                    pinnedInitData.Value.AddrOfPinnedObject() + (int)(initialData.Value.Offset * texelSizeBytes),
                    arrayLen,
                    numMips,
                    width,
                    1U,
                    1U,
                    texelSizeBytes
                    );

                pinnedDataHandle = GCHandle.Alloc(dataArr, GCHandleType.Pinned);
                initialDataPtr   = pinnedDataHandle.Value.AddrOfPinnedObject();
            }

            try {
                InteropUtils.CallNative(NativeMethods.ResourceFactory_CreateTexture1DArray,
                                        RenderingModule.Device,
                                        width,
                                        arrayLen,
                                        (InteropBool)mipAllocation,
                                        texelFormat,
                                        Usage.GetUsage(),
                                        Usage.GetCPUUsage(),
                                        (PipelineBindings)permittedBindings,
                                        (InteropBool)mipGenerationTarget,
                                        (InteropBool)dynamicDetail,
                                        initialDataPtr,
                                        dataArr != null ? (uint)dataArr.Length : 0U,
                                        (IntPtr)(&outResourceHandle)
                                        ).ThrowOnFailure();
            }
            finally {
                if (pinnedDataHandle != null)
                {
                    pinnedDataHandle.Value.Free();
                }
                if (pinnedInitData != null)
                {
                    pinnedInitData.Value.Free();
                }
            }

            return(outResourceHandle);
        }
        public void TestCreationWithInitialData()
        {
            // Define variables and constants
            const uint TEXTURE_WIDTH = 1 << 6;
            Texture1DBuilder <TexelFormat.RGBA8UInt> texBuilder = TextureFactory.NewTexture1D <TexelFormat.RGBA8UInt>()
                                                                  .WithUsage(ResourceUsage.StagingRead)
                                                                  .WithPermittedBindings(GPUBindings.None)
                                                                  .WithWidth(TEXTURE_WIDTH);

            TexelFormat.RGBA8UInt[]           initialDataA = new TexelFormat.RGBA8UInt[TEXTURE_WIDTH];
            TexelFormat.RGBA8UInt[]           initialDataB = new TexelFormat.RGBA8UInt[(TEXTURE_WIDTH << 1) - 1];
            Texture1D <TexelFormat.RGBA8UInt> testTextureA, testTextureB;

            TexelArray1D <TexelFormat.RGBA8UInt> texAData;

            TexelArray1D <TexelFormat.RGBA8UInt>[] texBData = new TexelArray1D <TexelFormat.RGBA8UInt> [TextureUtils.GetNumMips(TEXTURE_WIDTH)];

            // 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 texelIndex = 0U;

            while (mipWidth > 0)
            {
                for (uint i = 0; i < mipWidth; ++i, ++texelIndex)
                {
                    initialDataB[texelIndex].R = (byte)(i + mipWidth);
                    initialDataB[texelIndex].G = (byte)(i * 2 + mipWidth);
                    initialDataB[texelIndex].B = (byte)(i * 3 + mipWidth);
                    initialDataB[texelIndex].A = (byte)(i * 4 + mipWidth);
                }
                mipWidth >>= 1;
            }
            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);
            }

            mipWidth   = TEXTURE_WIDTH;
            texelIndex = 0U;
            while (mipWidth > 0)
            {
                for (uint i = 0; i < mipWidth; ++i, ++texelIndex)
                {
                    Assert.AreEqual((byte)(i + mipWidth), initialDataB[texelIndex].R);
                    Assert.AreEqual((byte)(i * 2 + mipWidth), initialDataB[texelIndex].G);
                    Assert.AreEqual((byte)(i * 3 + mipWidth), initialDataB[texelIndex].B);
                    Assert.AreEqual((byte)(i * 4 + mipWidth), initialDataB[texelIndex].A);
                }
                mipWidth >>= 1;
            }

            testTextureA.Dispose();
            testTextureB.Dispose();
        }
        public void TestCreationParameters()
        {
            // Define variables and constants
            var defaultBuilder        = TextureFactory.NewTexture1D <TexelFormat.RGBA32UInt>().WithUsage(ResourceUsage.DiscardWrite).WithWidth(100);
            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
            ITexture1D 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), tex.NumMips);
            tex.Dispose();
            tex = withDynDetail.Create();
            Assert.AreEqual(true, tex.IsGlobalDetailTarget);
            tex.Dispose();
            tex = withMipGen.Create();
            Assert.AreEqual(true, tex.IsMipGenTarget);
            tex.Dispose();

            ITexture1DArray ta = withStagingUsage.CreateArray(10);

            Assert.AreEqual(ResourceUsage.StagingReadWrite, ta.Usage);
            ta.Dispose();
            ta = withReadWriteBindings.CreateArray(10);
            Assert.AreEqual(GPUBindings.ReadableShaderResource | GPUBindings.WritableShaderResource, ta.PermittedBindings);
            ta.Dispose();
            ta = withDifferentFormat.WithUsage(ResourceUsage.Write).CreateArray(10);
            Assert.AreEqual(typeof(TexelFormat.Int8), ta.TexelFormat);
            ta.Dispose();
            ta = withWidth300.WithUsage(ResourceUsage.Write).CreateArray(10);
            Assert.AreEqual(300U, ta.Width);
            ta.Dispose();
            ta = withMipAllocation.CreateArray(10);
            Assert.AreEqual(TextureUtils.GetNumMips(1 << 9), ta.NumMips);
            ta.Dispose();
            ta = withDynDetail.CreateArray(10);
            Assert.AreEqual(true, ta.IsGlobalDetailTarget);
            ta.Dispose();
            ta = withMipGen.CreateArray(10);
            Assert.AreEqual(true, ta.IsMipGenTarget);
            ta.Dispose();
            ta = defaultBuilder.WithUsage(ResourceUsage.Immutable).WithInitialData(new TexelFormat.RGBA32UInt[1000]).CreateArray(10);
            Assert.AreEqual(10U, ta.ArrayLength);
            ta.Dispose();

#if !DEVELOPMENT && !RELEASE
            try {
                TextureFactory.NewTexture1D <TexelFormat.Float32>()
                .WithUsage(ResourceUsage.Immutable)
                .WithInitialData(null)
                .Create();
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
            try {
                TextureFactory.NewTexture1D <TexelFormat.Float32>()
                .WithUsage(ResourceUsage.DiscardWrite)
                .WithPermittedBindings(GPUBindings.None)
                .Create();
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
            try {
                TextureFactory.NewTexture1D <TexelFormat.Float32>()
                .WithUsage(ResourceUsage.StagingRead)
                .WithPermittedBindings(GPUBindings.ReadableShaderResource)
                .Create();
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
            try {
                TextureFactory.NewTexture1D <TexelFormat.Float32>()
                .WithWidth(0U)
                .Create();
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
            try {
                TextureFactory.NewTexture1D <TexelFormat.Float32>()
                .WithMipAllocation(true)
                .WithWidth(140)
                .Create();
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
            try {
                TextureFactory.NewTexture1D <TexelFormat.Float32>()
                .WithMipAllocation(true)
                .WithMipGenerationTarget(true)
                .WithWidth(1 << 4)
                .WithUsage(ResourceUsage.Write)
                .WithPermittedBindings(GPUBindings.None)
                .Create();
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
            try {
                TextureFactory.NewTexture1D <TexelFormat.Float32>()
                .WithMipAllocation(false)
                .WithMipGenerationTarget(true)
                .WithWidth(1 << 4)
                .WithUsage(ResourceUsage.Write)
                .WithPermittedBindings(GPUBindings.RenderTarget | GPUBindings.ReadableShaderResource)
                .Create();
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
            try {
                TextureFactory.NewTexture1D <TexelFormat.Float32>()
                .WithMipAllocation(true)
                .WithMipGenerationTarget(true)
                .WithWidth(1 << 4)
                .WithUsage(ResourceUsage.Write)
                .WithPermittedBindings(GPUBindings.RenderTarget | GPUBindings.ReadableShaderResource)
                .WithInitialData(new TexelFormat.Float32[(1 << 5) - 1])
                .Create();
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
            try {
                TextureFactory.NewTexture1D <TexelFormat.Float32>()
                .WithMipAllocation(true)
                .WithWidth(1 << 4)
                .WithUsage(ResourceUsage.Immutable)
                .WithPermittedBindings(GPUBindings.ReadableShaderResource)
                .WithInitialData(new TexelFormat.Float32[1 << 4])
                .Create();
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
            try {
                TextureFactory.NewTexture1D <TexelFormat.Float32>()
                .WithMipAllocation(false)
                .WithWidth(1 << 4)
                .WithUsage(ResourceUsage.Immutable)
                .WithPermittedBindings(GPUBindings.ReadableShaderResource)
                .WithInitialData(new TexelFormat.Float32[(1 << 5) - 1])
                .Create();
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
#endif
        }
Ejemplo n.º 9
0
        public void TestCreationWithInitialData()
        {
            // Define variables and constants
            const uint TEXTURE_WIDTH  = 1 << 6;
            const uint TEXTURE_HEIGHT = 1 << 4;
            Texture2DBuilder <TexelFormat.RGBA8UInt> texBuilder = TextureFactory.NewTexture2D <TexelFormat.RGBA8UInt>()
                                                                  .WithUsage(ResourceUsage.StagingRead)
                                                                  .WithPermittedBindings(GPUBindings.None)
                                                                  .WithWidth(TEXTURE_WIDTH)
                                                                  .WithHeight(TEXTURE_HEIGHT);

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

            TexelArray2D <TexelFormat.RGBA8UInt> texAData;

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

            // 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 texelIndex = 0U;

            while (mipWidth > 1U || mipHeight > 1U)
            {
                for (uint v = 0; v < mipHeight; ++v)
                {
                    for (uint u = 0; u < mipWidth; ++u, ++texelIndex)
                    {
                        initialDataB[texelIndex].R = (byte)(u + mipWidth + v + mipHeight);
                        initialDataB[texelIndex].G = (byte)(u + mipWidth + v + mipHeight * 2);
                        initialDataB[texelIndex].B = (byte)(u + mipWidth + v + mipHeight * 3);
                        initialDataB[texelIndex].A = (byte)(u + mipWidth + v + mipHeight * 4);
                    }
                }
                mipWidth  = Math.Max(1U, mipWidth >> 1);
                mipHeight = Math.Max(1U, mipHeight >> 1);
            }
            initialDataB[initialDataB.Length - 1] = new TexelFormat.RGBA8UInt {
                R = 2, G = 3, B = 4, A = 5
            };
            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 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)), texBData[mipIndex][u, v].R);
                        Assert.AreEqual((byte)(u + testTextureB.MipWidth(mipIndex) + v + testTextureB.MipHeight(mipIndex) * 2), texBData[mipIndex][u, v].G);
                        Assert.AreEqual((byte)(u + testTextureB.MipWidth(mipIndex) + v + testTextureB.MipHeight(mipIndex) * 3), texBData[mipIndex][u, v].B);
                        Assert.AreEqual((byte)(u + testTextureB.MipWidth(mipIndex) + v + testTextureB.MipHeight(mipIndex) * 4), texBData[mipIndex][u, v].A);
                    }
                }
            }

            testTextureA.Dispose();
            testTextureB.Dispose();
        }