コード例 #1
0
        public void TestCustomSRVForBuffers()
        {
            const uint         TEST_BUFFER_LEN = 300U;
            const uint         TEST_VIEW_START = 100U;
            const uint         TEST_VIEW_LEN   = 50U;
            IGeneralBuffer     testResource;
            ShaderResourceView testSRV;

            testResource = BufferFactory.NewBuffer <int>().WithLength(TEST_BUFFER_LEN).WithUsage(ResourceUsage.DiscardWrite).Create();
            testSRV      = testResource.CreateView(TEST_VIEW_START, TEST_VIEW_LEN);
            Assert.IsFalse(testSRV.ResourceOrViewDisposed);
            Assert.AreEqual(testResource, testSRV.Resource);
            Assert.AreEqual(TEST_VIEW_START, ((ShaderBufferResourceView)testSRV).FirstElementIndex);
            Assert.AreEqual(TEST_VIEW_LEN, ((ShaderBufferResourceView)testSRV).NumElements);
            testResource.Dispose();
            Assert.IsTrue(testSRV.ResourceOrViewDisposed);
            testSRV.Dispose();
            Assert.IsTrue(testSRV.IsDisposed);

            testResource = BufferFactory.NewBuffer <Matrix>().WithLength(TEST_BUFFER_LEN).WithUsage(ResourceUsage.DiscardWrite).Create();
            testSRV      = testResource.CreateView(TEST_VIEW_START, TEST_VIEW_LEN);
            Assert.IsFalse(testSRV.ResourceOrViewDisposed);
            Assert.AreEqual(testResource, testSRV.Resource);
            Assert.AreEqual(TEST_VIEW_START, ((ShaderBufferResourceView)testSRV).FirstElementIndex);
            Assert.AreEqual(TEST_VIEW_LEN, ((ShaderBufferResourceView)testSRV).NumElements);
            testResource.Dispose();
            Assert.IsTrue(testSRV.ResourceOrViewDisposed);
            testSRV.Dispose();
            Assert.IsTrue(testSRV.IsDisposed);
        }
コード例 #2
0
        public void TestCreateDefaultViewForBuffers()
        {
            const uint         TEST_BUFFER_LEN = 300U;
            IGeneralBuffer     testResource;
            ShaderResourceView testSRV;

            testResource = BufferFactory.NewBuffer <int>().WithLength(TEST_BUFFER_LEN)
                           .WithUsage(ResourceUsage.DiscardWrite).WithPermittedBindings(GPUBindings.ReadableShaderResource).Create();
            testSRV = testResource.CreateView();
            Assert.IsFalse(testSRV.ResourceOrViewDisposed);
            Assert.AreEqual(testResource, testSRV.Resource);
            Assert.AreEqual(0U, ((ShaderBufferResourceView)testSRV).FirstElementIndex);
            Assert.AreEqual(TEST_BUFFER_LEN, ((ShaderBufferResourceView)testSRV).NumElements);
            testResource.Dispose();
            Assert.IsTrue(testSRV.ResourceOrViewDisposed);
            testSRV.Dispose();
            Assert.IsTrue(testSRV.IsDisposed);

            testResource = BufferFactory.NewBuffer <Matrix>().WithLength(TEST_BUFFER_LEN)
                           .WithUsage(ResourceUsage.DiscardWrite).WithPermittedBindings(GPUBindings.ReadableShaderResource).Create();
            testSRV = testResource.CreateView();
            Assert.IsFalse(testSRV.ResourceOrViewDisposed);
            Assert.AreEqual(testResource, testSRV.Resource);
            Assert.AreEqual(0U, ((ShaderBufferResourceView)testSRV).FirstElementIndex);
            Assert.AreEqual(TEST_BUFFER_LEN, ((ShaderBufferResourceView)testSRV).NumElements);
            testResource.Dispose();
            Assert.IsTrue(testSRV.ResourceOrViewDisposed);
            testSRV.Dispose();
            Assert.IsTrue(testSRV.IsDisposed);
        }
コード例 #3
0
        public void TestWrite()
        {
            LosgapSystem.InvokeOnMaster(() => {
                // Define variables and constants
                IBuffer res = BufferFactory.NewBuffer <int>()
                              .WithUsage(ResourceUsage.Write)
                              .WithLength(100)
                              .Create();

                IBuffer copyDest = (res as Buffer <int>).Clone()
                                   .WithUsage(ResourceUsage.StagingRead)
                                   .WithPermittedBindings(GPUBindings.None)
                                   .Create();

                // Set up context


                // Execute
                Assert.IsTrue(res.CanWrite);
                res.Write(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 10U);
                res.CopyTo(copyDest);

                // Assert outcome
                byte[] readData = copyDest.Read();
                for (int i = 10; i < 20; ++i)
                {
                    Assert.AreEqual(i - 9, readData[i]);
                }

                res.Dispose();
                copyDest.Dispose();
            });
        }
コード例 #4
0
        public void TestCreation()
        {
            // Define variables and constants
            VertexBufferBuilder <int>  defaultBuilder           = BufferFactory.NewVertexBuffer <int>().WithLength(1);
            VertexBufferBuilder <int>  builderWithFrequentWrite = defaultBuilder.WithUsage(ResourceUsage.DiscardWrite);
            VertexBufferBuilder <int>  builderWith300Verts      = builderWithFrequentWrite.WithLength(300);
            VertexBufferBuilder <int>  builderWithInitData      = defaultBuilder.WithInitialData(Enumerable.Range(0, 10).ToArray());
            VertexBufferBuilder <long> builderWith200LongVert   = builderWithFrequentWrite.WithVertexType <long>().WithLength(200);

            // Set up context


            // Execute
            VertexBuffer <int>  withFreqWriteBuffer = builderWithFrequentWrite;
            VertexBuffer <int>  with300VertBuffer   = builderWith300Verts;
            VertexBuffer <int>  withInitDataBuffer  = builderWithInitData;
            VertexBuffer <long> with200LongBuffer   = builderWith200LongVert;

            // Assert outcome
            Assert.AreEqual(ResourceUsage.DiscardWrite, withFreqWriteBuffer.Usage);
            Assert.AreEqual(300U, with300VertBuffer.Length);
            Assert.AreEqual(10U, withInitDataBuffer.Length);
            Assert.AreEqual(200U * sizeof(long), with200LongBuffer.Size.InBytes);

            try {
                builderWithInitData.WithUsage(ResourceUsage.StagingRead);
                Assert.Fail();
            }
            catch (ArgumentException) { }

            withFreqWriteBuffer.Dispose();
            with300VertBuffer.Dispose();
            withInitDataBuffer.Dispose();
            with200LongBuffer.Dispose();
        }
コード例 #5
0
        // ReSharper disable once UnusedMember.Local Is used through reflection
        private void FillComponentBuffer <TComponent>(FieldInfo component, int componentIndex,
                                                      IBuffer[] vertexComponentBuffers, string[] vertexComponentSemantics, ResourceFormat[] vertexComponentFormats) where TComponent : struct
        {
            TComponent[] initialData = new TComponent[vertices.Count];

            for (int i = 0; i < vertices.Count; ++i)
            {
                initialData[i] = (TComponent)component.GetValue(vertices[i]);
            }

            VertexBuffer <TComponent> componentBuffer = BufferFactory.NewVertexBuffer <TComponent>()
                                                        .WithInitialData(initialData)
                                                        .WithUsage(ResourceUsage.Immutable);

            vertexComponentBuffers[componentIndex]   = componentBuffer;
            vertexComponentSemantics[componentIndex] = component.GetCustomAttribute <VertexComponentAttribute>().SemanticName;
            ResourceFormat componentFormat = (ResourceFormat)component.GetCustomAttribute <VertexComponentAttribute>().D3DResourceFormat;

            if (componentFormat == ResourceFormat.Unknown)
            {
                componentFormat = BaseResource.GetFormatForType(typeof(TComponent));
                if (componentFormat == ResourceFormat.Unknown)
                {
                    throw new InvalidOperationException("Could not discern resource format for type '" + typeof(TComponent).Name + "' " +
                                                        "with semantic '" + vertexComponentSemantics[componentIndex] + "', " +
                                                        "please specify a resource format using the \"D3DResourceFormat\" field of the VertexComponent attribute.");
                }
            }
            vertexComponentFormats[componentIndex] = componentFormat;
        }
コード例 #6
0
        public void TestProperties()
        {
            ConstantBuffer <Matrix> testBuffer = BufferFactory.NewConstantBuffer <Matrix>().WithUsage(ResourceUsage.DiscardWrite);
            Shader shader = new FragmentShader(
                @"Tests\SimpleFS.cso",
                new TextureSamplerBinding(0U, "TS0"),
                new TextureSamplerBinding(1U, "TS1"),
                new TextureSamplerBinding(5U, "TS5"),
                new ResourceViewBinding(0U, "RV0"),
                new ResourceViewBinding(2U, "RV2"),
                new ConstantBufferBinding(3U, "CB3", testBuffer)
                );

            Assert.AreEqual("SimpleFS", shader.Name);
            Assert.AreEqual(6, shader.ResourceBindings.Length);
            Assert.IsTrue(((TextureSamplerBinding)shader.ResourceBindings.Single(bind => bind.Identifier == "TS0")).SlotIndex == 0U);
            Assert.IsTrue(((TextureSamplerBinding)shader.ResourceBindings.Single(bind => bind.Identifier == "TS1")).SlotIndex == 1U);
            Assert.IsTrue(((TextureSamplerBinding)shader.ResourceBindings.Single(bind => bind.Identifier == "TS5")).SlotIndex == 5U);
            Assert.IsTrue(((ResourceViewBinding)shader.ResourceBindings.Single(bind => bind.Identifier == "RV0")).SlotIndex == 0U);
            Assert.IsTrue(((ResourceViewBinding)shader.ResourceBindings.Single(bind => bind.Identifier == "RV2")).SlotIndex == 2U);
            Assert.IsTrue(((ConstantBufferBinding)shader.ResourceBindings.Single(bind => bind.Identifier == "CB3")).SlotIndex == 3U);

            shader.Dispose();
            testBuffer.Dispose();
        }
コード例 #7
0
        public void TestWrite()
        {
            // Define variables and constants
            VertexBuffer <int> testBuffer = BufferFactory.NewVertexBuffer <int>()
                                            .WithUsage(ResourceUsage.Write)
                                            .WithLength(300);

            // Set up context


            // Execute
            testBuffer.Write(Enumerable.Range(0, 300).ToArray(), 0);
            testBuffer.Write(Enumerable.Range(0, 200).ToArray(), 100);
            testBuffer.Write(new ArraySlice <int>(Enumerable.Range(0, 200).ToArray(), 50, 50), 24);


#if !DEVELOPMENT && !RELEASE
            try {
                testBuffer.Write(new ArraySlice <int>(Enumerable.Range(0, 200).ToArray(), 50), 250);
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
#endif

            // Assert outcome
            Assert.IsTrue(testBuffer.CanWrite);

            testBuffer.Dispose();
        }
コード例 #8
0
        public void TestClone()
        {
            // Define variables and constants
            const ResourceUsage USAGE       = ResourceUsage.Write;
            const uint          NUM_INDICES = 21515;
            IndexBuffer         firstBuffer = BufferFactory.NewIndexBuffer().WithUsage(USAGE).WithLength(NUM_INDICES);

            // Set up context


            // Execute
            IndexBuffer secondBuffer = firstBuffer.Clone();
            IndexBuffer thirdBuffer  = secondBuffer.Clone().WithLength(NUM_INDICES * 2U);

            // Assert outcome
            Assert.AreEqual(USAGE, secondBuffer.Usage);
            Assert.AreEqual(firstBuffer.Usage, secondBuffer.Usage);
            Assert.AreEqual(NUM_INDICES, secondBuffer.Length);
            Assert.AreEqual(firstBuffer.Length, secondBuffer.Length);

            Assert.AreEqual(USAGE, thirdBuffer.Usage);
            Assert.AreEqual(secondBuffer.Length * 2U, thirdBuffer.Length);

            firstBuffer.Dispose();
            secondBuffer.Dispose();
            thirdBuffer.Dispose();
        }
コード例 #9
0
        public void TestBindingMethods()
        {
            ConstantBuffer <Matrix> testBuffer = BufferFactory.NewConstantBuffer <Matrix>().WithUsage(ResourceUsage.DiscardWrite);

            IShaderResourceBinding[] bindingArr =
            {
                new TextureSamplerBinding(0U, "TS0"),
                new TextureSamplerBinding(1U, "TS1"),
                new TextureSamplerBinding(5U, "TS5"),
                new ResourceViewBinding(0U,   "RV0"),
                new ResourceViewBinding(2U,   "RV2"),
                new ConstantBufferBinding(3U, "CB3", testBuffer)
            };

            Shader shader = new FragmentShader(@"Tests\SimpleFS.cso", bindingArr);

            Assert.AreEqual(bindingArr[0], shader.GetBindingByIdentifier("TS0"));
            Assert.AreEqual(bindingArr[1], shader.GetBindingByIdentifier("TS1"));
            Assert.AreEqual(bindingArr[2], shader.GetBindingByIdentifier("TS5"));
            Assert.AreEqual(bindingArr[3], shader.GetBindingByIdentifier("RV0"));
            Assert.AreEqual(bindingArr[4], shader.GetBindingByIdentifier("RV2"));
            Assert.AreEqual(bindingArr[5], shader.GetBindingByIdentifier("CB3"));

            Assert.IsTrue(shader.ContainsBinding("TS0"));
            Assert.IsTrue(shader.ContainsBinding("TS1"));
            Assert.IsFalse(shader.ContainsBinding("TS2"));

            Assert.IsTrue(shader.ContainsBinding(bindingArr[0]));
            Assert.IsTrue(shader.ContainsBinding(bindingArr[1]));
            Assert.IsFalse(shader.ContainsBinding(new TextureSamplerBinding(0U, "TS0")));

            shader.Dispose();
            testBuffer.Dispose();
        }
コード例 #10
0
        public void TestIsValid()
        {
            SceneLayer testLayer  = Scene.CreateLayer("Test layer");
            Window     testWindow = new Window("Test window");
            ConstantBuffer <Matrix> instanceBuffer = BufferFactory.NewConstantBuffer <Matrix>().WithUsage(ResourceUsage.DiscardWrite);
            VertexShader            testShader     = VertexShader.NewDefaultShader(instanceBuffer);
            HUDPass testPass = new HUDPass("Test pass");

            Assert.IsFalse(testPass.IsValid);
            testPass.Input = new Camera();
            testPass.AddLayer(testLayer);
            testPass.Output       = testWindow;
            testPass.VertexShader = testShader;
            Assert.IsFalse(testPass.IsValid);
            testPass.RasterizerState   = new RasterizerState(true, TriangleCullMode.FrontfaceCulling, false);
            testPass.DepthStencilState = new DepthStencilState();
            Assert.IsTrue(testPass.IsValid);
            testPass.RasterizerState.Dispose();
            Assert.IsFalse(testPass.IsValid);

            testPass.DepthStencilState.Dispose();
            testPass.Dispose();
            testShader.Dispose();
            instanceBuffer.Dispose();
            testWindow.Close();
            testLayer.Dispose();
        }
コード例 #11
0
        public void TestTransform()
        {
            // Define variables and constants
            ModelInstanceManager     mim   = new ModelInstanceManager();
            ConstantBuffer <Vector4> fsCB  = BufferFactory.NewConstantBuffer <Vector4>().WithUsage(ResourceUsage.DiscardWrite);
            FragmentShader           fs    = new FragmentShader(@"Tests\SimpleFS.cso", new ConstantBufferBinding(0U, "MaterialProperties", fsCB));
            Material            testMat    = new Material("TestMat", fs);
            SceneLayer          testLayer  = Scene.CreateLayer("TestLayer");
            ModelInstanceHandle testHandle = mim.AllocateInstance(testMat.Index, 0U, testLayer.Index, Transform.DEFAULT_TRANSFORM);

            // Set up context


            // Execute
            testHandle.Transform = new Transform(Vector3.ONE * 4f, Quaternion.IDENTITY, Vector3.ONE * -15f);

            // Assert outcome
            Assert.AreEqual(new Transform(Vector3.ONE * 4f, Quaternion.IDENTITY, Vector3.ONE * -15f), testHandle.Transform);
            testHandle.Dispose();
            testLayer.Dispose();
            mim.Dispose();
            testMat.Dispose();
            fs.Dispose();
            fsCB.Dispose();
        }
コード例 #12
0
        public void TestCreation()
        {
            // Define variables and constants
            ConstantBuffer <SixteenByteStruct> defaultBuffer   = BufferFactory.NewConstantBuffer <SixteenByteStruct>();
            ConstantBuffer <SixteenByteStruct> freqWriteBuffer = defaultBuffer.Clone().WithUsage(ResourceUsage.DiscardWrite);
            ConstantBuffer <SixteenByteStruct> initDataBuffer  = defaultBuffer.Clone().WithInitialData(new SixteenByteStruct(1f, 2f, 3f, 4f));

            // Set up context

            // Execute

            // Assert outcome
            Assert.AreEqual(ResourceUsage.Immutable, defaultBuffer.Usage);
            Assert.AreEqual(ResourceUsage.DiscardWrite, freqWriteBuffer.Usage);
            Assert.AreEqual(defaultBuffer.Usage, initDataBuffer.Usage);
            Assert.AreEqual(16L, initDataBuffer.Size.InBytes);

            try {
                freqWriteBuffer.Clone().WithUsage(ResourceUsage.StagingRead);
                Assert.Fail();
            }
            catch (ArgumentException) { }

            defaultBuffer.Dispose();
            freqWriteBuffer.Dispose();
            initDataBuffer.Dispose();
        }
コード例 #13
0
        public void TestCreation()
        {
            // Define variables and constants
            IndexBufferBuilder defaultBuilder           = BufferFactory.NewIndexBuffer().WithLength(1);
            IndexBufferBuilder builderWithFrequentWrite = defaultBuilder.WithUsage(ResourceUsage.DiscardWrite);
            IndexBufferBuilder builderWith300Indices    = builderWithFrequentWrite.WithLength(300);
            IndexBufferBuilder builderWithInitData      = defaultBuilder
                                                          .WithInitialData(Enumerable.Range(0, 10).Select(@int => (uint)@int).ToArray());

            // Set up context


            // Execute
            IndexBuffer withFreqWriteBuffer = builderWithFrequentWrite;
            IndexBuffer with300IndexBuffer  = builderWith300Indices;
            IndexBuffer withInitDataBuffer  = builderWithInitData;

            // Assert outcome
            Assert.AreEqual(ResourceUsage.DiscardWrite, withFreqWriteBuffer.Usage);
            Assert.AreEqual(300U, with300IndexBuffer.Length);
            Assert.AreEqual(10U, withInitDataBuffer.Length);

            try {
                builderWithInitData.WithUsage(ResourceUsage.StagingRead);
                Assert.Fail();
            }
            catch (ArgumentException) { }

            withFreqWriteBuffer.Dispose();
            with300IndexBuffer.Dispose();
            withInitDataBuffer.Dispose();
        }
コード例 #14
0
        public void TestReadWrite()
        {
            // Define variables and constants
            Buffer <long> testBuffer = BufferFactory.NewBuffer <long>()
                                       .WithLength(300)
                                       .WithUsage(ResourceUsage.StagingReadWrite)
                                       .WithPermittedBindings(GPUBindings.None);

            // Set up context
            testBuffer.Write(Enumerable.Range(0, 300).Select(i => (long)i).ToArray(), 0);

            // Execute
            testBuffer.ReadWrite(dataView => {
                for (int i = 0; i < dataView.Width; ++i)
                {
                    dataView[i] *= 2L;
                }
            });

            // Assert outcome
            long[] data = testBuffer.Read();
            for (int i = 0; i < data.Length; ++i)
            {
                Assert.AreEqual(i * 2, data[i]);
            }

            testBuffer.Dispose();
        }
コード例 #15
0
        public void TestCopyTo()
        {
            // Define variables and constants
            ConstantBuffer <decimal> srcBuffer = BufferFactory.NewConstantBuffer <decimal>()
                                                 .WithUsage(ResourceUsage.Immutable)
                                                 .WithInitialData(13515m);
            ConstantBuffer <decimal> dstBuffer = srcBuffer.Clone()
                                                 .WithUsage(ResourceUsage.DiscardWrite);

            // Set up context


            // Execute
            srcBuffer.CopyTo(dstBuffer);
            srcBuffer.CopyTo(dstBuffer);

            try {
                dstBuffer.CopyTo(srcBuffer);
                Assert.Fail();
            }
            catch (ResourceOperationUnavailableException) { }

            // Assert outcome
            Assert.IsFalse(srcBuffer.CanBeCopyDestination);
            Assert.IsTrue(dstBuffer.CanBeCopyDestination);

            srcBuffer.Dispose();
            dstBuffer.Dispose();
        }
コード例 #16
0
        public unsafe void TestShaderResourcePackage()
        {
            ConstantBuffer <Vector4> matColorBuffer = BufferFactory.NewConstantBuffer <Vector4>().WithUsage(ResourceUsage.DiscardWrite);
            TextureSampler           textureSampler = new TextureSampler(TextureFilterType.Anisotropic, TextureWrapMode.Border, AnisotropicFilteringLevel.EightTimes);
            FragmentShader           testFS         = new FragmentShader(
                @"Tests\SimpleFS.cso",
                new ConstantBufferBinding(0U, "MaterialColor", matColorBuffer),
                new TextureSamplerBinding(0U, "DefaultSampler")
                );

            Material testMaterial = new Material("Test Material", testFS);

            testMaterial.SetMaterialConstantValue((ConstantBufferBinding)testFS.GetBindingByIdentifier("MaterialColor"), Vector4.ONE);
            testMaterial.SetMaterialResource((TextureSamplerBinding)testFS.GetBindingByIdentifier("DefaultSampler"), textureSampler);

            ShaderResourcePackage shaderResourcePackage = testMaterial.FragmentShaderResourcePackage;

            Assert.AreEqual(Vector4.ONE, *((Vector4 *)shaderResourcePackage.GetValue((ConstantBufferBinding)testFS.GetBindingByIdentifier("MaterialColor"))));
            Assert.AreEqual(textureSampler, shaderResourcePackage.GetValue((TextureSamplerBinding)testFS.GetBindingByIdentifier("DefaultSampler")));

            testFS.Dispose();
            matColorBuffer.Dispose();
            testMaterial.Dispose();
            textureSampler.Dispose();
        }
コード例 #17
0
        public void TestSetIndexBuffer()
        {
            IndexBuffer ib = BufferFactory.NewIndexBuffer().WithLength(300U).WithUsage(ResourceUsage.DiscardWrite);

            RenderCommand testCommand = RenderCommand.SetIndexBuffer(ib);

            Assert.AreEqual(RenderCommandInstruction.SetIndexBuffer, testCommand.Instruction);
            Assert.AreEqual((RenderCommandArgument)(IntPtr)ib.ResourceHandle, testCommand.Arg1);

            ib.Dispose();

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

            try {
                RenderCommand.SetIndexBuffer(ib);
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
#endif
        }
コード例 #18
0
        public void TestSetInstanceBuffer()
        {
            const uint            INPUT_SLOT     = 3U;
            VertexBuffer <Matrix> instanceBuffer = BufferFactory.NewVertexBuffer <Matrix>()
                                                   .WithLength(10U)
                                                   .WithUsage(ResourceUsage.DiscardWrite);

            RenderCommand testCommand = RenderCommand.SetInstanceBuffer(instanceBuffer, INPUT_SLOT);

            Assert.AreEqual(RenderCommandInstruction.SetInstanceBuffer, testCommand.Instruction);
            Assert.AreEqual((RenderCommandArgument)(IntPtr)instanceBuffer.ResourceHandle, testCommand.Arg1);
            Assert.AreEqual((RenderCommandArgument)INPUT_SLOT, testCommand.Arg2);

            instanceBuffer.Dispose();

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

            try {
                RenderCommand.SetInstanceBuffer(instanceBuffer, INPUT_SLOT);
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
#endif
        }
コード例 #19
0
        public void TestClone()
        {
            // Define variables and constants
            const ResourceUsage USAGE        = ResourceUsage.Write;
            const uint          NUM_VERTICES = 21515;
            VertexBuffer <int>  firstBuffer  = BufferFactory.NewVertexBuffer <int>().WithUsage(USAGE).WithLength(NUM_VERTICES);

            // Set up context


            // Execute
            VertexBuffer <int>  secondBuffer = firstBuffer.Clone();
            VertexBuffer <long> thirdBuffer  = secondBuffer.Clone().WithVertexType <long>();

            // Assert outcome
            Assert.AreEqual(USAGE, secondBuffer.Usage);
            Assert.AreEqual(firstBuffer.Usage, secondBuffer.Usage);
            Assert.AreEqual(NUM_VERTICES, secondBuffer.Length);
            Assert.AreEqual(firstBuffer.Length, secondBuffer.Length);

            Assert.AreEqual(USAGE, thirdBuffer.Usage);
            Assert.AreEqual(secondBuffer.Length, thirdBuffer.Length);

            firstBuffer.Dispose();
            secondBuffer.Dispose();
            thirdBuffer.Dispose();
        }
コード例 #20
0
        public unsafe void TestProperties()
        {
            // Define variables and constants
            const int FAKE_CACHE_ID = 1351617;

            IVertexBuffer[] buffers = new IVertexBuffer[2];
            buffers[0] = BufferFactory.NewVertexBuffer <Vector3>()
                         .WithInitialData(new[] { Vector3.ONE * 0f, Vector3.ONE * 1f, Vector3.ONE * 2f, Vector3.ONE * 3f, Vector3.ONE * 4f, Vector3.ONE * 5f })
                         .WithUsage(ResourceUsage.Immutable)
                         .Create();
            buffers[1] = BufferFactory.NewVertexBuffer <Vector2>()
                         .WithInitialData(new[] { Vector2.ONE * 0f, Vector2.ONE * 1f, Vector2.ONE * 2f, Vector2.ONE * 3f, Vector2.ONE * 4f, Vector2.ONE * 5f })
                         .WithUsage(ResourceUsage.Immutable)
                         .Create();
            string[]         semantics = { "POSITION", "TEXCOORD" };
            ResourceFormat[] formats   = { ResourceFormat.R32G32B32Float, ResourceFormat.R32G32Float };
            IndexBuffer      indices   = BufferFactory.NewIndexBuffer()
                                         .WithInitialData(new uint[] { 0, 1, 2, 1, 2, 0, 2, 1, 0, 3, 4, 5, 4, 5, 3, 5, 3, 4, 5, 3, 4 })
                                         .WithUsage(ResourceUsage.Immutable);
            AlignedAllocation <uint> componentStartPointsAlloc = AlignedAllocation <uint> .AllocArray(7L, 3);

            *(((uint *)componentStartPointsAlloc.AlignedPointer) + 0) = 0U;
            *(((uint *)componentStartPointsAlloc.AlignedPointer) + 1) = 3U;
            *(((uint *)componentStartPointsAlloc.AlignedPointer) + 2) = 6U;
            AlignedAllocation <uint> indexStartPointsAlloc = AlignedAllocation <uint> .AllocArray(7L, 3);

            *(((uint *)indexStartPointsAlloc.AlignedPointer) + 0) = 0U;
            *(((uint *)indexStartPointsAlloc.AlignedPointer) + 1) = 9U;
            *(((uint *)indexStartPointsAlloc.AlignedPointer) + 2) = 21U;
            Dictionary <string, ModelHandle> modelNameMap = new Dictionary <string, ModelHandle>()
            {
                { FAKE_CACHE_ID + "A", new ModelHandle(FAKE_CACHE_ID, 0U) },
                { FAKE_CACHE_ID + "B", new ModelHandle(FAKE_CACHE_ID, 1U) }
            };
            const uint NUM_MODELS = 2;

            // Set up context
            GeometryCache cache = new GeometryCache(
                buffers, semantics, formats,
                indices, componentStartPointsAlloc, indexStartPointsAlloc,
                NUM_MODELS, typeof(FakeVertex), FAKE_CACHE_ID, modelNameMap
                );


            // Execute


            // Assert outcome
            Assert.AreEqual(buffers[0], cache.VertexBuffers[0]);
            Assert.AreEqual(buffers[1], cache.VertexBuffers[1]);
            Assert.AreEqual(semantics[0], cache.VertexSemantics[0]);
            Assert.AreEqual(semantics[1], cache.VertexSemantics[1]);
            Assert.AreEqual(formats[0], cache.VertexFormats[0]);
            Assert.AreEqual(formats[1], cache.VertexFormats[1]);
            Assert.AreEqual(indices, cache.IndexBuffer);

            cache.Dispose();
        }
コード例 #21
0
        public void TestCreation()
        {
            // Define variables and constants
            BufferBuilder <int> defaultBufferBuilder    = BufferFactory.NewBuffer <int>().WithLength(1);
            Buffer <int>        bufferWithFrequentWrite = defaultBufferBuilder.WithUsage(ResourceUsage.DiscardWrite);
            Buffer <int>        bufferWith300Ele        = bufferWithFrequentWrite.Clone().WithLength(300);
            Buffer <int>        bufferWithInitData      = defaultBufferBuilder.WithInitialData(Enumerable.Range(0, 10).ToArray());
            Buffer <long>       bufferWith200LongEle    = bufferWithFrequentWrite.Clone().WithElementType <long>().WithLength(200);
            Buffer <int>        bufferWithNoGPUBindings = bufferWithFrequentWrite.Clone()
                                                          .WithPermittedBindings(GPUBindings.None)
                                                          .WithUsage(ResourceUsage.StagingRead);

            // Set up context

            // Execute

            // Assert outcome
            Assert.AreEqual(ResourceUsage.DiscardWrite, bufferWithFrequentWrite.Usage);
            Assert.AreEqual(300L, bufferWith300Ele.Length);
            Assert.AreEqual(10L, bufferWithInitData.Length);
            Assert.AreEqual(200L, bufferWith200LongEle.Length);
            Assert.IsFalse(bufferWithInitData.IsStructured);
            Assert.IsTrue(bufferWith200LongEle.IsStructured);
            Assert.AreEqual(GPUBindings.None, bufferWithNoGPUBindings.PermittedBindings);

            Assert.AreNotEqual(bufferWithNoGPUBindings.PermittedBindings, bufferWithFrequentWrite.PermittedBindings);

#if !DEVELOPMENT && !RELEASE
            try {
                defaultBufferBuilder.WithUsage(ResourceUsage.StagingRead).WithPermittedBindings(GPUBindings.ReadableShaderResource).Create();
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
#endif

            try {
                defaultBufferBuilder.WithPermittedBindings(GPUBindings.RenderTarget);
                Assert.Fail();
            }
            catch (ArgumentException) { }

            try {
                defaultBufferBuilder.WithPermittedBindings(GPUBindings.DepthStencilTarget);
                Assert.Fail();
            }
            catch (ArgumentException) { }

            bufferWithFrequentWrite.Dispose();
            bufferWith300Ele.Dispose();
            bufferWithInitData.Dispose();
            bufferWith200LongEle.Dispose();
            bufferWithNoGPUBindings.Dispose();
        }
コード例 #22
0
        public void TestClone()
        {
            // Define variables and constants
            const ResourceUsage USAGE        = ResourceUsage.StagingReadWrite;
            const uint          LENGTH       = 21515;
            const GPUBindings   GPU_BINDINGS = GPUBindings.None;

            int[] initialData = new int[LENGTH];

            // Set up context
            for (int i = 0; i < initialData.Length; i++)
            {
                initialData[i] = i * 3;
            }

            Buffer <int> firstBuffer = BufferFactory.NewBuffer <int>()
                                       .WithUsage(USAGE)
                                       .WithLength(LENGTH)
                                       .WithPermittedBindings(GPU_BINDINGS)
                                       .WithInitialData(initialData);

            // Execute
            Buffer <int>  secondBuffer = firstBuffer.Clone();
            Buffer <long> thirdBuffer  = secondBuffer.Clone().WithElementType <long>();
            Buffer <int>  fourthBuffer = firstBuffer.Clone(true);

            // Assert outcome
            Assert.AreEqual(USAGE, secondBuffer.Usage);
            Assert.AreEqual(firstBuffer.Usage, secondBuffer.Usage);
            Assert.AreEqual(LENGTH, secondBuffer.Length);
            Assert.AreEqual(firstBuffer.Length, secondBuffer.Length);
            Assert.AreEqual(GPU_BINDINGS, secondBuffer.PermittedBindings);
            Assert.AreEqual(firstBuffer.PermittedBindings, secondBuffer.PermittedBindings);
            Assert.AreEqual(false, secondBuffer.IsStructured);
            Assert.AreEqual(firstBuffer.IsStructured, secondBuffer.IsStructured);

            Assert.AreEqual(USAGE, thirdBuffer.Usage);
            Assert.AreEqual(secondBuffer.Length, thirdBuffer.Length);
            Assert.AreEqual(secondBuffer.Size * 2U, thirdBuffer.Size);

            int[] firstBufferData  = firstBuffer.Read();
            int[] fourthBufferData = fourthBuffer.Read();
            for (int i = 0; i < initialData.Length; i++)
            {
                Assert.AreEqual(initialData[i], firstBufferData[i]);
                Assert.AreEqual(firstBufferData[i], fourthBufferData[i]);
            }

            firstBuffer.Dispose();
            secondBuffer.Dispose();
            thirdBuffer.Dispose();
            fourthBuffer.Dispose();
        }
コード例 #23
0
        public void TestDiscardWrite()
        {
            // Define variables and constants
            ConstantBuffer <decimal> testBuffer = BufferFactory.NewConstantBuffer <decimal>()
                                                  .WithUsage(ResourceUsage.DiscardWrite);

            // Set up context


            // Execute
            testBuffer.DiscardWrite(15m);

            // Assert outcome
            Assert.IsTrue(testBuffer.CanDiscardWrite);

            testBuffer.Dispose();
        }
コード例 #24
0
        public void TestCopyTo()
        {
            // Define variables and constants
            VertexBuffer <float> srcBuffer = BufferFactory.NewVertexBuffer <float>()
                                             .WithUsage(ResourceUsage.Immutable)
                                             .WithInitialData(Enumerable.Range(0, 3000).Select(@int => (float)@int).ToArray());
            VertexBuffer <float> dstBuffer = srcBuffer.Clone()
                                             .WithUsage(ResourceUsage.DiscardWrite);

            // Set up context


            // Execute
            srcBuffer.CopyTo(dstBuffer);
            srcBuffer.CopyTo(dstBuffer, 100, 2000, 500);

            try {
                dstBuffer.CopyTo(srcBuffer);
                Assert.Fail();
            }
            catch (ResourceOperationUnavailableException) { }

#if !DEVELOPMENT && !RELEASE
            try {
                srcBuffer.CopyTo(dstBuffer, 1600, 2000, 500);
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }

            try {
                srcBuffer.CopyTo(dstBuffer, 0, 2000, 2000);
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
#endif

            // Assert outcome
            Assert.IsFalse(srcBuffer.CanBeCopyDestination);
            Assert.IsTrue(dstBuffer.CanBeCopyDestination);

            srcBuffer.Dispose();
            dstBuffer.Dispose();
        }
コード例 #25
0
        public void TestDiscardWrite()
        {
            LosgapSystem.InvokeOnMaster(() => {
                // Define variables and constants
                Buffer <decimal> testBuffer = BufferFactory.NewBuffer <decimal>()
                                              .WithUsage(ResourceUsage.DiscardWrite)
                                              .WithLength(300);
                Buffer <decimal> copyDestBuffer = testBuffer.Clone()
                                                  .WithUsage(ResourceUsage.StagingRead)
                                                  .WithPermittedBindings(GPUBindings.None);

                // Set up context


                // Execute
                testBuffer.DiscardWrite(Enumerable.Range(0, 300).Select(i => (decimal)i).ToArray(), 0);
                testBuffer.DiscardWrite(Enumerable.Range(0, 200).Select(i => (decimal)i).ToArray(), 100);
                testBuffer.DiscardWrite(new ArraySlice <decimal>(Enumerable.Range(0, 200).Select(i => (decimal)i).ToArray(), 50, 50), 24);

#if !DEVELOPMENT && !RELEASE
                try {
                    testBuffer.DiscardWrite(new ArraySlice <decimal>(Enumerable.Range(0, 200).Select(i => (decimal)i).ToArray(), 50), 250);
                    Assert.Fail();
                }
                catch (AssuranceFailedException) { }
#endif

                testBuffer.CopyTo(copyDestBuffer);
                decimal[] testData = copyDestBuffer.Read();
                for (int i = 24; i < 24 + 50; ++i)
                {
                    Assert.AreEqual((decimal)(50 + (i - 24)), testData[i]);
                }

                // Assert outcome
                Assert.IsTrue(testBuffer.CanDiscardWrite);
                Assert.IsFalse(copyDestBuffer.CanDiscardWrite);

                testBuffer.Dispose();
                copyDestBuffer.Dispose();
            });
        }
コード例 #26
0
        public void TestSetInputLayout()
        {
            GeometryCacheBuilder <DefaultVertex> gcb = new GeometryCacheBuilder <DefaultVertex>();

            gcb.AddModel("TSIL_a", new DefaultVertex[] { new DefaultVertex(Vector3.ONE) }, new uint[] { 0U });
            GeometryCache           cache  = gcb.Build();
            ConstantBuffer <Matrix> vpMat  = BufferFactory.NewConstantBuffer <Matrix>().WithUsage(ResourceUsage.DiscardWrite);
            VertexShader            shader = new VertexShader(
                @"Tests\SimpleVS.cso",
                new VertexInputBinding(0U, "INSTANCE_TRANSFORM"),
                new ConstantBufferBinding(0U, "CameraTransform", vpMat),
                new VertexInputBinding(1U, "POSITION")
                );

            GeometryInputLayout gil = cache.GetInputLayout(shader);

            RenderCommand testCommand = RenderCommand.SetInputLayout(gil);

            Assert.AreEqual(RenderCommandInstruction.SetInputLayout, testCommand.Instruction);
            Assert.AreEqual((RenderCommandArgument)(IntPtr)gil.ResourceHandle, testCommand.Arg1);

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

            shader.Dispose();
            vpMat.Dispose();
            cache.Dispose();
            gil.Dispose();

#if !DEVELOPMENT && !RELEASE
            try {
                RenderCommand.SetInputLayout(gil);
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
#endif
        }
コード例 #27
0
        public unsafe void TestDiscardWriteShaderConstantBuffer()
        {
            ConstantBuffer <Vector4> cb  = BufferFactory.NewConstantBuffer <Vector4>().WithUsage(ResourceUsage.DiscardWrite);
            ConstantBufferBinding    cbb = new ConstantBufferBinding(0U, "CB0", cb);
            Vector4 initialValue         = Vector4.FORWARD;

            cbb.SetValue((byte *)(&initialValue));

            RenderCommand testCommand = RenderCommand.DiscardWriteShaderConstantBuffer(cbb, cbb.CurValuePtr);

            Assert.AreEqual(RenderCommandInstruction.CBDiscardWrite, testCommand.Instruction);
            Assert.AreEqual((RenderCommandArgument)(IntPtr)cbb.GetBoundResource().ResourceHandle, testCommand.Arg1);
            Assert.AreEqual(*((Vector4 *)cbb.CurValuePtr), *((Vector4 *)new IntPtr(UnsafeUtils.Reinterpret <RenderCommandArgument, long>(testCommand.Arg2, sizeof(long)))));
            Assert.AreEqual((RenderCommandArgument)cbb.BufferSizeBytes, testCommand.Arg3);

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

            try {
                RenderCommand.DiscardWriteShaderConstantBuffer(cbb, IntPtr.Zero);
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
#endif

            (cbb as IDisposable).Dispose();

#if !DEVELOPMENT && !RELEASE
            try {
                RenderCommand.DiscardWriteShaderConstantBuffer(cbb, cbb.CurValuePtr);
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
#endif

            cb.Dispose();
        }
コード例 #28
0
        public void TestCustomUAVForBuffers()
        {
            const uint          TEST_BUFFER_LEN      = 300U;
            const uint          TEST_VIEW_START      = 100U;
            const uint          TEST_VIEW_LEN        = 50U;
            const bool          TEST_APPEND_CONSUME  = true;
            const bool          TEST_INCLUDE_COUNTER = false;
            IGeneralBuffer      testResource;
            UnorderedAccessView testUAV;

            testResource = BufferFactory.NewBuffer <int>().WithLength(TEST_BUFFER_LEN)
                           .WithUsage(ResourceUsage.Write).WithPermittedBindings(GPUBindings.WritableShaderResource).Create();
            testUAV = testResource.CreateUnorderedAccessView(TEST_VIEW_START, TEST_VIEW_LEN, false, false);
            Assert.IsFalse(testUAV.ResourceOrViewDisposed);
            Assert.AreEqual(testResource, testUAV.Resource);
            Assert.AreEqual(TEST_VIEW_START, ((UnorderedBufferAccessView)testUAV).FirstElementIndex);
            Assert.AreEqual(true, ((UnorderedBufferAccessView)testUAV).IsRawAccessView);
            Assert.AreEqual(TEST_VIEW_LEN, ((UnorderedBufferAccessView)testUAV).NumElements);
            Assert.AreEqual(false, ((UnorderedBufferAccessView)testUAV).AppendConsumeSupport);
            Assert.AreEqual(false, ((UnorderedBufferAccessView)testUAV).IncludesCounter);
            testResource.Dispose();
            Assert.IsTrue(testUAV.ResourceOrViewDisposed);
            testUAV.Dispose();
            Assert.IsTrue(testUAV.IsDisposed);

            testResource = BufferFactory.NewBuffer <Matrix>().WithLength(TEST_BUFFER_LEN)
                           .WithUsage(ResourceUsage.Write).WithPermittedBindings(GPUBindings.WritableShaderResource).Create();
            testUAV = testResource.CreateUnorderedAccessView(TEST_VIEW_START, TEST_VIEW_LEN, TEST_APPEND_CONSUME, TEST_INCLUDE_COUNTER);
            Assert.IsFalse(testUAV.ResourceOrViewDisposed);
            Assert.AreEqual(testResource, testUAV.Resource);
            Assert.AreEqual(TEST_VIEW_START, ((UnorderedBufferAccessView)testUAV).FirstElementIndex);
            Assert.AreEqual(false, ((UnorderedBufferAccessView)testUAV).IsRawAccessView);
            Assert.AreEqual(TEST_VIEW_LEN, ((UnorderedBufferAccessView)testUAV).NumElements);
            Assert.AreEqual(TEST_APPEND_CONSUME, ((UnorderedBufferAccessView)testUAV).AppendConsumeSupport);
            Assert.AreEqual(TEST_INCLUDE_COUNTER, ((UnorderedBufferAccessView)testUAV).IncludesCounter);
            testResource.Dispose();
            Assert.IsTrue(testUAV.ResourceOrViewDisposed);
            testUAV.Dispose();
            Assert.IsTrue(testUAV.IsDisposed);
        }
コード例 #29
0
        public unsafe void SetShaderConstantBuffers()
        {
            ConstantBuffer <Vector4> cb0 = BufferFactory.NewConstantBuffer <Vector4>().WithUsage(ResourceUsage.DiscardWrite);
            ConstantBuffer <Matrix>  cb1 = BufferFactory.NewConstantBuffer <Matrix>().WithUsage(ResourceUsage.DiscardWrite);

            Shader shader = new FragmentShader(
                @"Tests\SimpleFS.cso",
                new ConstantBufferBinding(0U, "CB0", cb0),
                new ConstantBufferBinding(1U, "CB1", cb1)
                );

            RenderCommand testCommand = RenderCommand.SetShaderConstantBuffers(shader);

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

            Assert.AreEqual(cb0.ResourceHandle, resHandleArray[0]);
            Assert.AreEqual(cb1.ResourceHandle, resHandleArray[1]);
            Assert.AreEqual((RenderCommandArgument)shader.NumConstantBufferSlots, testCommand.Arg2);

            shader.Dispose();
            cb1.Dispose();
            cb0.Dispose();

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

            try {
                RenderCommand.SetShaderConstantBuffers(shader);
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
#endif
        }
コード例 #30
0
        public void TestSettingMaterialProperties()
        {
            ConstantBuffer <Vector4> matColorBuffer = BufferFactory.NewConstantBuffer <Vector4>().WithUsage(ResourceUsage.DiscardWrite);
            TextureSampler           textureSampler = new TextureSampler(TextureFilterType.Anisotropic, TextureWrapMode.Border, AnisotropicFilteringLevel.EightTimes);
            FragmentShader           testFS         = new FragmentShader(
                @"Tests\SimpleFS.cso",
                new ConstantBufferBinding(0U, "MaterialColor", matColorBuffer),
                new TextureSamplerBinding(0U, "DefaultSampler")
                );

            Material testMaterial = new Material("Test Material", testFS);

            testMaterial.SetMaterialConstantValue((ConstantBufferBinding)testFS.GetBindingByIdentifier("MaterialColor"), Vector4.ONE);
            testMaterial.SetMaterialResource((TextureSamplerBinding)testFS.GetBindingByIdentifier("DefaultSampler"), textureSampler);

#if !DEVELOPMENT && !RELEASE
            ConstantBufferBinding cb = new ConstantBufferBinding(1U, "Test", matColorBuffer);
            try {
                testMaterial.SetMaterialConstantValue(cb, Vector4.RIGHT);
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
            finally {
                (cb as IDisposable).Dispose();
            }

            try {
                testMaterial.SetMaterialConstantValue((ConstantBufferBinding)testFS.GetBindingByIdentifier("MaterialColor"), Matrix.IDENTITY);
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
#endif

            testFS.Dispose();
            matColorBuffer.Dispose();
            testMaterial.Dispose();
            textureSampler.Dispose();
        }