Пример #1
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();
        }
Пример #2
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();
        }
        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();
        }
Пример #4
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();
        }
Пример #5
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();
        }
Пример #6
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
        }
Пример #7
0
        public void TestSetShader()
        {
            ConstantBuffer <Matrix> vpMat = BufferFactory.NewConstantBuffer <Matrix>().WithUsage(ResourceUsage.DiscardWrite);
            VertexShader            vs    = VertexShader.NewDefaultShader(vpMat);

            ConstantBuffer <Vector4> colorVec = BufferFactory.NewConstantBuffer <Vector4>().WithUsage(ResourceUsage.DiscardWrite);
            FragmentShader           fs       = new FragmentShader(@"Tests\SimpleFS.cso", new ConstantBufferBinding(0U, "MaterialProperties", colorVec));

            RenderCommand testCommand = RenderCommand.SetShader(vs);

            Assert.AreEqual(RenderCommandInstruction.VSSetShader, testCommand.Instruction);
            Assert.AreEqual((RenderCommandArgument)(IntPtr)vs.Handle, testCommand.Arg1);

            testCommand = RenderCommand.SetShader(fs);
            Assert.AreEqual(RenderCommandInstruction.FSSetShader, testCommand.Instruction);
            Assert.AreEqual((RenderCommandArgument)(IntPtr)fs.Handle, testCommand.Arg1);

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

            vs.Dispose();
            fs.Dispose();
            vpMat.Dispose();
            colorVec.Dispose();

#if !DEVELOPMENT && !RELEASE
            try {
                RenderCommand.SetShader(fs);
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
#endif
        }
Пример #8
0
        public void TestAllowsDuplicateSlotsForDifferentBindingTypes()
        {
            Shader shader = new FragmentShader(@"Tests\SimpleFS.cso", new TextureSamplerBinding(0U, "A"), new ResourceViewBinding(0U, "B"));

            shader.Dispose();
        }
Пример #9
0
        public unsafe void TestSetShaderResourceViews()
        {
            Texture2DBuilder <TexelFormat.RGBA32UInt> texBuilder = TextureFactory.NewTexture2D <TexelFormat.RGBA32UInt>()
                                                                   .WithWidth(100U)
                                                                   .WithHeight(100U)
                                                                   .WithUsage(ResourceUsage.DiscardWrite);
            Texture2D <TexelFormat.RGBA32UInt> tex0 = texBuilder.Create();
            Texture2D <TexelFormat.RGBA32UInt> tex2 = texBuilder.Create();

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

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

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

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

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

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

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

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

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

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

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

            shader.Dispose();

#if !DEVELOPMENT && !RELEASE
            try {
                RenderCommand.SetShaderResourceViews(shader, new Dictionary <ResourceViewBinding, BaseResourceView>());
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
#endif
        }
Пример #10
0
        public unsafe void TestSetShaderTextureSamplers()
        {
            TextureSampler ts0 = new TextureSampler(TextureFilterType.Anisotropic, TextureWrapMode.Border, AnisotropicFilteringLevel.EightTimes);
            TextureSampler ts2 = new TextureSampler(TextureFilterType.Anisotropic, TextureWrapMode.Border, AnisotropicFilteringLevel.EightTimes);

            Shader shader = new FragmentShader(
                @"Tests\SimpleFS.cso",
                new TextureSamplerBinding(0U, "TS0"),
                new TextureSamplerBinding(1U, "TS1"),
                new TextureSamplerBinding(2U, "TS2")
                );

            Dictionary <TextureSamplerBinding, TextureSampler> tsDict = new Dictionary <TextureSamplerBinding, TextureSampler>();

            tsDict[shader.TextureSamplerBindings[0]] = ts0;
            tsDict[shader.TextureSamplerBindings[2]] = ts2;

            RenderCommand testCommand = RenderCommand.SetShaderTextureSamplers(shader, tsDict);

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

            Assert.AreEqual(ts0.ResourceHandle, resHandleArray[0]);
            Assert.AreEqual(ResourceHandle.NULL, resHandleArray[1]);
            Assert.AreEqual(ts2.ResourceHandle, resHandleArray[2]);
            Assert.AreEqual((RenderCommandArgument)3U, testCommand.Arg2);

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

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

            ts0.Dispose();
            ts2.Dispose();

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

            shader.Dispose();

#if !DEVELOPMENT && !RELEASE
            try {
                RenderCommand.SetShaderTextureSamplers(shader, new Dictionary <TextureSamplerBinding, TextureSampler>());
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
#endif
        }
        public unsafe void TestCreateAndDestroyInstance()
        {
            // Define variables and constants
            const int NUM_INSTANCES = 1000;

            var gcb = new GeometryCacheBuilder <TestVertex>();

            gcb.AddModel("TCADI_a", new[] { new TestVertex(Vector3.ONE), new TestVertex(Vector3.LEFT), }, new[] { 0U, 1U, 1U, 0U, 1U, 0U });
            gcb.AddModel("TCADI_b", new[] { new TestVertex(Vector3.RIGHT), new TestVertex(Vector3.UP), }, new[] { 0U, 1U, 1U, 0U, 1U, 0U });
            gcb.AddModel("TCADI_c", new[] { new TestVertex(Vector3.ZERO), new TestVertex(Vector3.DOWN), }, new[] { 0U, 1U, 1U, 0U, 1U, 0U });
            GeometryCache testCache = gcb.Build();

            SceneLayer testLayerA = Scene.CreateLayer("Test Layer A");
            SceneLayer testLayerB = Scene.CreateLayer("Test Layer B");
            ConstantBuffer <Vector4> fsColorBuffer = BufferFactory.NewConstantBuffer <Vector4>().WithUsage(ResourceUsage.DiscardWrite);
            FragmentShader           fs            = new FragmentShader(@"Tests\SimpleFS.cso", new ConstantBufferBinding(0U, "MaterialProperties", fsColorBuffer));
            Material testMatA = new Material("Brick", fs);
            Material testMatB = new Material("Wood", fs);

            // Set up context


            // Execute
            ModelInstanceHandle[] instanceArr = new ModelInstanceHandle[NUM_INSTANCES];
            for (int i = 0; i < NUM_INSTANCES; ++i)
            {
                Transform transform = new Transform(
                    Vector3.ONE * i,
                    Quaternion.FromAxialRotation(Vector3.UP, i),
                    Vector3.ONE * -i
                    );
                if (i % 5 == 0)
                {
                    instanceArr[i] = testLayerA.CreateModelInstance(new ModelHandle(testCache.ID, (uint)(i % 3)), (i % 2 == 0 ? testMatA : testMatB), transform);
                }
                else
                {
                    instanceArr[i] = testLayerB.CreateModelInstance(new ModelHandle(testCache.ID, (uint)(i % 3)), (i % 2 == 0 ? testMatA : testMatB), transform);
                }
            }

            // Assert outcome
            RenderingModule.RenderStateBarrier.FreezeMutations();             // Cheeky, but we have to on debug mode (and it's re-entrant for now, so no problem)
            var instanceData = testCache.GetModelInstanceData();

            for (int i = 0; i < NUM_INSTANCES; ++i)
            {
                Material instanceMaterial = Material.GetMaterialByIndex(instanceArr[i].MaterialIndex);
                ModelInstanceManager.MIDArray materialDataArray = instanceData.First(kvp => kvp.Key == instanceMaterial).Value;

                Assert.AreEqual((i % 2 == 0 ? testMatA : testMatB), instanceMaterial);
                Assert.AreEqual((i % 5 == 0 ? testLayerA : testLayerB), Scene.GetLayerByIndex(materialDataArray.Data[GetMIHInstanceIndex(instanceArr[i])].SceneLayerIndex));
                Assert.IsTrue(materialDataArray.Data[GetMIHInstanceIndex(instanceArr[i])].InUse);
                Assert.AreEqual((uint)(i % 3), materialDataArray.Data[GetMIHInstanceIndex(instanceArr[i])].ModelIndex);
                Assert.AreEqual(
                    new Transform(
                        Vector3.ONE * i,
                        Quaternion.FromAxialRotation(Vector3.UP, i),
                        Vector3.ONE * -i
                        ),
                    instanceArr[i].Transform
                    );
                Assert.AreEqual(instanceArr[i].Transform, materialDataArray.Data[GetMIHInstanceIndex(instanceArr[i])].Transform);

                instanceArr[i].Dispose();
                Assert.IsFalse(materialDataArray.Data[GetMIHInstanceIndex(instanceArr[i])].InUse);
            }

            RenderingModule.RenderStateBarrier.UnfreezeMutations();

            testCache.Dispose();
            testMatA.Dispose();
            testMatB.Dispose();
            testLayerA.Dispose();
            testLayerB.Dispose();
            fs.Dispose();
            fsColorBuffer.Dispose();
        }
        public void TestAllMethods()
        {
            // Define variables and constants
            ModelInstanceManager testMIM          = new ModelInstanceManager();
            const int            NUM_ALLOCATIONS  = 3000;
            const int            NUM_MODELS       = 7;
            const int            NUM_MATERIALS    = 11;
            const int            NUM_SCENE_LAYERS = 3;

            ConstantBuffer <Vector4> fsCBuffer = BufferFactory.NewConstantBuffer <Vector4>().WithUsage(ResourceUsage.DiscardWrite);
            FragmentShader           testFS    = new FragmentShader(@"Tests\SimpleFS.cso", new ConstantBufferBinding(0U, "MaterialProperties", fsCBuffer));

            Material[] materials = new Material[NUM_MATERIALS];
            for (int i = 0; i < NUM_MATERIALS; ++i)
            {
                materials[i] = new Material(i.ToString(), testFS);
            }

            // Set up context
            ModelInstanceHandle[] instances = new ModelInstanceHandle[NUM_ALLOCATIONS];

            // Execute
            for (int i = 0; i < NUM_ALLOCATIONS; i++)
            {
                Transform initialTransform = new Transform(
                    Vector3.ONE * i,
                    Quaternion.FromAxialRotation(Vector3.ONE * (i + 1), MathUtils.PI),
                    Vector3.ONE * -i
                    );
                instances[i] = testMIM.AllocateInstance(materials[i % NUM_MATERIALS].Index, (uint)i % NUM_MODELS, (uint)i % NUM_SCENE_LAYERS, initialTransform);
            }

            for (int i = 0; i < NUM_ALLOCATIONS; i += 2)
            {
                instances[i].Transform = instances[i].Transform.With(scale: Vector3.FORWARD * i);
            }

            // Assert outcome
            RenderingModule.RenderStateBarrier.FreezeMutations();
            ArraySlice <KeyValuePair <Material, ModelInstanceManager.MIDArray> > midData = testMIM.GetModelInstanceData();

            RenderingModule.RenderStateBarrier.UnfreezeMutations();
            Assert.AreEqual(NUM_ALLOCATIONS, midData.Sum(kvp => {
                unsafe {
                    int val = 0;
                    for (int i = 0; i < kvp.Value.Length; ++i)
                    {
                        if (kvp.Value.Data[i].InUse)
                        {
                            ++val;
                        }
                    }
                    return(val);
                }
            }));

            unsafe {
                foreach (KeyValuePair <Material, ModelInstanceManager.MIDArray> kvp in midData)
                {
                    Assert.IsTrue(materials.Contains(kvp.Key));
                    for (uint i = 0U; i < kvp.Value.Length; ++i)
                    {
                        if (!kvp.Value.Data[i].InUse)
                        {
                            continue;
                        }
                        Assert.AreEqual(1, instances.Count(mih => mih.Transform == kvp.Value.Data[i].Transform));
                    }
                }
            }

            materials.ForEach(mat => mat.Dispose());
            testFS.Dispose();
            fsCBuffer.Dispose();
        }