Ejemplo n.º 1
0
        public void TestWrite()
        {
            // Define variables and constants
            IndexBuffer testBuffer = BufferFactory.NewIndexBuffer()
                                     .WithUsage(ResourceUsage.Write)
                                     .WithLength(300);

            // Set up context


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

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

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

            testBuffer.Dispose();
        }
Ejemplo n.º 2
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();
        }
Ejemplo n.º 3
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();
        }
Ejemplo n.º 4
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
        }
Ejemplo n.º 5
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();
        }
Ejemplo n.º 6
0
        public void TestCopyTo()
        {
            // Define variables and constants
            IndexBuffer srcBuffer = BufferFactory.NewIndexBuffer()
                                    .WithUsage(ResourceUsage.Immutable)
                                    .WithInitialData(Enumerable.Range(0, 3000).Select(@int => (uint)@int).ToArray());
            IndexBuffer 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();
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Builds the <see cref="GeometryCache"/> with all the models that have previously been added with <see cref="AddModel"/>.
        /// This method may only be called once per GeometryCacheBuilder.
        /// </summary>
        /// <returns>A new <see cref="GeometryCache"/> containing 'baked' resource data for all the loaded models and an
        /// "instance pool", allowing model instances to be created and loaded in to the <see cref="Scene"/>.</returns>
        public unsafe GeometryCache Build()
        {
            lock (instanceMutationLock) {
                if (isBuilt)
                {
                    throw new InvalidOperationException("Cache has already been built!");
                }

                FieldInfo[] vertexComponents = typeof(TVertex).GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                if (vertexComponents.Length > VertexShader.MAX_VS_INPUT_BINDINGS)
                {
                    throw new InvalidOperationException("Vertex type '" + typeof(TVertex).Name + "' has too many components. Maximum " +
                                                        "permissible is " + VertexShader.MAX_VS_INPUT_BINDINGS + ", but the vertex type has " + vertexComponents.Length + " fields.");
                }
                else if (vertexComponents.Length == 0)
                {
                    throw new InvalidOperationException("Vertex type '" + typeof(TVertex).Name + "' has no components. Please choose a type with " +
                                                        "at least one field.");
                }

                if (vertexCounts.Count == 0)
                {
                    throw new InvalidOperationException("Can not build empty geometry cache: Please add at least one model first!");
                }

                if (modelNames.AnyDuplicates())
                {
                    throw new InvalidOperationException("No two models should have the same name.");
                }
                if (GeometryCache.CheckForModelNameClashes(modelNames))
                {
                    throw new InvalidOperationException("One or more model names added to this builder have already been used in other active geometry caches.");
                }



                IVertexBuffer[]  vertexComponentBuffers   = new IVertexBuffer[vertexComponents.Length];
                string[]         vertexComponentSemantics = new string[vertexComponents.Length];
                ResourceFormat[] vertexComponentFormats   = new ResourceFormat[vertexComponents.Length];

                for (int i = 0; i < vertexComponents.Length; ++i)
                {
                    FieldInfo component = vertexComponents[i];
                    if (!component.FieldType.IsBlittable())
                    {
                        throw new InvalidOperationException("Invalid vertex component type: '" + component.FieldType.Name + "'.");
                    }
                    if (!component.HasCustomAttribute <VertexComponentAttribute>())
                    {
                        throw new InvalidOperationException("Every field on given vertex type (" + typeof(TVertex).Name + ") must be annoted with " +
                                                            "a " + typeof(VertexComponentAttribute).Name + "!");
                    }

                    typeof(GeometryCacheBuilder <TVertex>)
                    .GetMethod("FillComponentBuffer", BindingFlags.NonPublic | BindingFlags.Instance)                             // TODO replace with nameof() operator when C#6 is released
                    .MakeGenericMethod(component.FieldType)
                    .Invoke(this, new object[] { component, i, vertexComponentBuffers, vertexComponentSemantics, vertexComponentFormats });
                }

                IndexBuffer indexBuffer = BufferFactory.NewIndexBuffer().WithInitialData(indices.ToArray()).WithUsage(ResourceUsage.Immutable);

                Assure.Equal(vertexCounts.Count, indexCounts.Count);
                AlignedAllocation <uint> componentStartPointsAlloc = AlignedAllocation <uint> .AllocArray(
                    START_POINT_ARRAY_ALIGNMENT,
                    (uint)vertexCounts.Count + 1U                      // Extra one so we can set the last value to one-past-the-end (performance improvement later on)
                    );

                AlignedAllocation <uint> indexStartPointsAlloc = AlignedAllocation <uint> .AllocArray(
                    START_POINT_ARRAY_ALIGNMENT,
                    (uint)vertexCounts.Count + 1U                      // Extra one so we can set the last value to one-past-the-end (performance improvement later on)
                    );

                uint *componentStartPtr = (uint *)componentStartPointsAlloc.AlignedPointer;
                uint *indexStartPtr     = (uint *)indexStartPointsAlloc.AlignedPointer;
                uint  vbCounter         = 0U;
                uint  ibCounter         = 0U;
                for (int i = 0; i < vertexCounts.Count; ++i)
                {
                    componentStartPtr[i] = vbCounter;
                    indexStartPtr[i]     = ibCounter;
                    vbCounter           += vertexCounts[i];
                    ibCounter           += indexCounts[i];
                }

                // Set the last two elements of each start-point array to one-past-the-last, so we don't have to test
                // for being the 'last' count later on
                componentStartPtr[vertexCounts.Count] = (uint)vertices.Count;
                indexStartPtr[vertexCounts.Count]     = (uint)indices.Count;

                Dictionary <string, ModelHandle> modelNameToHandleMap = new Dictionary <string, ModelHandle>();
                for (uint i = 0U; i < modelNames.Count; ++i)
                {
                    modelNameToHandleMap.Add(modelNames[(int)i], new ModelHandle(cacheID, i));
                }

                isBuilt = true;

                return(new GeometryCache(
                           vertexComponentBuffers,
                           vertexComponentSemantics,
                           vertexComponentFormats,
                           indexBuffer,
                           componentStartPointsAlloc,
                           indexStartPointsAlloc,
                           (uint)vertexCounts.Count,
                           typeof(TVertex),
                           cacheID,
                           modelNameToHandleMap,
                           orderFirst
                           ));
            }
        }
Ejemplo n.º 8
0
        public unsafe void TestGetModelBufferValues()
        {
            // Define variables and constants
            const int FAKE_CACHE_ID = 1351616;

            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;

            uint outVBStartIndex, outIBStartIndex, outVBCount, outIBCount;

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

            // Execute
            cache.GetModelBufferValues(0U, out outVBStartIndex, out outIBStartIndex, out outVBCount, out outIBCount);

            // Assert outcome
            Assert.AreEqual(0U, outVBStartIndex);
            Assert.AreEqual(0U, outIBStartIndex);
            Assert.AreEqual(3U, outVBCount);
            Assert.AreEqual(9U, outIBCount);

            cache.GetModelBufferValues(1U, out outVBStartIndex, out outIBStartIndex, out outVBCount, out outIBCount);

            Assert.AreEqual(3U, outVBStartIndex);
            Assert.AreEqual(9U, outIBStartIndex);
            Assert.AreEqual(3U, outVBCount);
            Assert.AreEqual(12U, outIBCount);

#if !DEVELOPMENT && !RELEASE
            try {
                cache.GetModelBufferValues(2U, out outVBStartIndex, out outIBStartIndex, out outVBCount, out outIBCount);
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
#endif

            cache.Dispose();
        }
Ejemplo n.º 9
0
        public unsafe void TestGetInputLayout()
        {
            // Define variables and constants
            const int FAKE_CACHE_ID = 1351618;

            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 <float>()
                         .WithInitialData(new[] { 0f, 1f, 2f, 3f, 4f, 5f })
                         .WithUsage(ResourceUsage.Immutable)
                         .Create();
            string[]         semantics = { "POSITION", "RANDOM_FLOATS" };
            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(Vector4), FAKE_CACHE_ID, modelNameMap
                );

            ConstantBuffer <Matrix> instanceCBuffer = BufferFactory.NewConstantBuffer <Matrix>().WithUsage(ResourceUsage.DiscardWrite);
            VertexShader            vs1             = new VertexShader(
                @"Tests\SimpleVS.cso",
                new VertexInputBinding(0U, "INSTANCE_TRANSFORM"),
                new ConstantBufferBinding(0U, "CameraTransform", instanceCBuffer),
                new VertexInputBinding(1U, "POSITION")
                );
            VertexShader vs2 = new VertexShader(
                @"Tests\SimpleVS.cso",
                new VertexInputBinding(0U, "INSTANCE_TRANSFORM"),
                new ConstantBufferBinding(0U, "CameraTransform", instanceCBuffer),
                new VertexInputBinding(1U, "RANDOM_FLOATS"),
                new VertexInputBinding(2U, "POSITION")
                );
            VertexShader vs3 = new VertexShader(
                @"Tests\SimpleVS.cso",
                new VertexInputBinding(0U, "INSTANCE"),
                new ConstantBufferBinding(0U, "VPTransform", instanceCBuffer),
                new VertexInputBinding(1U, "TEXCOORD")
                );

            // Execute
            GeometryInputLayout inputLayout1 = cache.GetInputLayout(vs1);
            GeometryInputLayout inputLayout2 = cache.GetInputLayout(vs2);

            try {
                cache.GetInputLayout(vs3);
                Assert.Fail();
            }
            catch (InvalidOperationException) { }

            // Assert outcome
            Assert.AreEqual(cache, inputLayout1.AssociatedCache);
            Assert.AreEqual(cache, inputLayout2.AssociatedCache);

            Assert.AreEqual(vs1, inputLayout1.AssociatedShader);
            Assert.AreEqual(vs2, inputLayout2.AssociatedShader);

            KeyValuePair <VertexInputBinding, IVertexBuffer>[] boundCompBuffers = inputLayout1.BoundComponentBuffers;
            Assert.AreEqual(buffers[0], boundCompBuffers.First(kvp => kvp.Key == vs1.GetBindingByIdentifier("POSITION")).Value);
            Assert.IsFalse(boundCompBuffers.Any(kvp => kvp.Value == buffers[1]));

            boundCompBuffers = inputLayout2.BoundComponentBuffers;
            Assert.AreEqual(buffers[0], boundCompBuffers.First(kvp => kvp.Key == vs2.GetBindingByIdentifier("POSITION")).Value);
            Assert.AreEqual(buffers[1], boundCompBuffers.First(kvp => kvp.Key == vs2.GetBindingByIdentifier("RANDOM_FLOATS")).Value);

            Assert.AreEqual(inputLayout1, cache.GetInputLayout(vs1));
            Assert.AreEqual(inputLayout2, cache.GetInputLayout(vs2));

            cache.Dispose();
            inputLayout2.Dispose();
            inputLayout1.Dispose();
            vs1.Dispose();
            vs2.Dispose();
            vs3.Dispose();
            instanceCBuffer.Dispose();
            indices.Dispose();
            buffers[1].Dispose();
            buffers[0].Dispose();
        }