Esempio n. 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();
        }
Esempio n. 2
0
        public Material CreateNewColorableMaterial()
        {
            Material result = new Material("Colorable Font Char " + UnicodeValue, FragmentShader);

            result.SetMaterialResource((ResourceViewBinding)FragmentShader.GetBindingByIdentifier(Font.CHAR_MAP_SHADER_RES_NAME), FontMapResourceView);
            return(result);
        }
Esempio n. 3
0
        public HUDTexture(FragmentShader fragmentShader, SceneLayer targetLayer, SceneViewport targetViewport)
        {
            lock (staticMutationLock) {
                if (hudTextureModel == null)
                {
                    throw new InvalidOperationException("HUD Texture Model must be set before creating any HUDTexture objects.");
                }
            }
            this.targetViewport            = targetViewport;
            this.material                  = new Material("HUDTexture Mat", fragmentShader);
            this.curModelInstance          = targetLayer.CreateModelInstance(hudTextureModel.Value, material, Transform.DEFAULT_TRANSFORM);
            this.fsTexColorMultiplyBinding = (ConstantBufferBinding)fragmentShader.GetBindingByIdentifier(HUDFS_TEX_PROPS_CBB_NAME);
            this.fsTexBinding              = (ResourceViewBinding)fragmentShader.GetBindingByIdentifier(HUDFS_TEX_BINDING_NAME);
            material.SetMaterialConstantValue(fsTexColorMultiplyBinding, color);
            targetViewport.TargetWindow.WindowResized += WindowResize;

            this.fragmentShader = fragmentShader;
            this.sceneLayer     = targetLayer;
        }
Esempio n. 4
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();
        }
Esempio n. 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();
        }
 public void SetLensProperties(float focalDistance, float maxBlurDistance)
 {
     lock (InstanceMutationLock) {
         Vector4 lensProps = new Vector4(
             this.geometryPass.Output.NearPlaneDist,
             this.geometryPass.Output.FarPlaneDist,
             focalDistance,
             maxBlurDistance
             );
         unsafe {
             ((ConstantBufferBinding)dofShader.GetBindingByIdentifier("LensProperties")).SetValue((byte *)(&lensProps));
         }
     }
 }
Esempio n. 7
0
        public static Font Load(string fontFile, FragmentShader textFS, uint?lineHeightPixels, int?kerningPixels)
        {
            Assure.NotNull(fontFile);
            Assure.NotNull(textFS);
            Assure.False(textFS.IsDisposed);
            if (!IOUtils.IsValidFilePath(fontFile) || !File.Exists(fontFile))
            {
                throw new FileNotFoundException("File '" + fontFile + "' not found: Could not load font.");
            }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            return(new Font(
                       name,
                       lineHeightPixels.Value,
                       kerningPixels.Value,
                       characterCacheBuilder.Build(),
                       pageArray,
                       characterPageViews,
                       charMap,
                       (ConstantBufferBinding)textFS.GetBindingByIdentifier(TEXT_COLOR_SHADER_CB_NAME),
                       maxCharHeight
                       ));
        }