Exemplo n.º 1
0
        public void TestCreationParameters()
        {
            var defaultBuilder        = TextureFactory.LoadTexture2D().WithUsage(ResourceUsage.Immutable).WithFilePath(@"Tests\potTex.bmp");
            var withStagingUsage      = defaultBuilder.WithUsage(ResourceUsage.StagingReadWrite).WithPermittedBindings(GPUBindings.None);
            var withReadWriteBindings = defaultBuilder.WithUsage(ResourceUsage.Write).WithPermittedBindings(GPUBindings.ReadableShaderResource | GPUBindings.WritableShaderResource);

            ITexture2D tex = withStagingUsage.Create();

            Assert.AreEqual(ResourceUsage.StagingReadWrite, tex.Usage);
            tex.Dispose();

            tex = withReadWriteBindings.Create();
            Assert.AreEqual(GPUBindings.ReadableShaderResource | GPUBindings.WritableShaderResource, tex.PermittedBindings);
            tex.Dispose();

#if !DEVELOPMENT && !RELEASE
            try {
                TextureFactory.LoadTexture2D()
                .WithUsage(ResourceUsage.Immutable)
                .Create();
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
            try {
                TextureFactory.LoadTexture2D()
                .WithFilePath(@"Tests\potTex.bmp")
                .WithUsage(ResourceUsage.DiscardWrite)
                .WithPermittedBindings(GPUBindings.None)
                .Create();
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
            try {
                TextureFactory.LoadTexture2D()
                .WithFilePath(@"Tests\potTex.bmp")
                .WithUsage(ResourceUsage.StagingRead)
                .WithPermittedBindings(GPUBindings.ReadableShaderResource)
                .Create();
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
#endif
        }
Exemplo n.º 2
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
                       ));
        }
Exemplo n.º 3
0
        public void TestDifferentFormats()
        {
            var defaultLoader = TextureFactory.LoadTexture2D()
                                .WithUsage(ResourceUsage.Immutable)
                                .WithPermittedBindings(GPUBindings.ReadableShaderResource);

            ITexture2D result;

            result = defaultLoader.WithFilePath(@"Tests\200x200.bmp").Create();
            Assert.AreEqual(200U, result.Width);
            Assert.AreEqual(200U, result.Height);
            Assert.AreEqual(typeof(Texture2D <TexelFormat.RGBA8UNorm>), result.GetType());
            Assert.AreEqual(0U, result.ArrayIndex);
            Assert.AreEqual(false, result.IsArrayTexture);
            Assert.AreEqual(false, result.IsGlobalDetailTarget);
            Assert.AreEqual(false, result.IsMipGenTarget);
            Assert.AreEqual(false, result.IsMipmapped);
            Assert.AreEqual(false, result.IsMultisampled);
            Assert.AreEqual(1U, result.NumMips);
            Assert.AreEqual(200U * 200U * 4U, result.Size);
            Assert.AreEqual(200U * 200U, result.SizeTexels);
            Assert.AreEqual(typeof(TexelFormat.RGBA8UNorm), result.TexelFormat);
            Assert.AreEqual(4U, result.TexelSizeBytes);

            LosgapSystem.InvokeOnMaster(() => {
                Texture2D <TexelFormat.RGBA8UNorm> dataCopyTex = (result as Texture2D <TexelFormat.RGBA8UNorm>).Clone()
                                                                 .WithUsage(ResourceUsage.StagingRead)
                                                                 .WithPermittedBindings(GPUBindings.None);

                result.CopyTo(dataCopyTex);

                Assert.AreEqual(UnsafeUtils.Reinterpret <uint, TexelFormat.RGBA8UNorm>(4294958703U, 4), dataCopyTex.Read(0U)[138, 99]);

                dataCopyTex.Dispose();
            });

            result.Dispose();

            result = defaultLoader.WithFilePath(@"Tests\200x200.png").Create();
            Assert.AreEqual(200U, result.Width);
            Assert.AreEqual(200U, result.Height);
            Assert.AreEqual(typeof(Texture2D <TexelFormat.RGBA8UNormSRGB>), result.GetType());
            Assert.AreEqual(0U, result.ArrayIndex);
            Assert.AreEqual(false, result.IsArrayTexture);
            Assert.AreEqual(false, result.IsGlobalDetailTarget);
            Assert.AreEqual(false, result.IsMipGenTarget);
            Assert.AreEqual(false, result.IsMipmapped);
            Assert.AreEqual(false, result.IsMultisampled);
            Assert.AreEqual(1U, result.NumMips);
            Assert.AreEqual(200U * 200U * 4U, result.Size);
            Assert.AreEqual(200U * 200U, result.SizeTexels);
            Assert.AreEqual(typeof(TexelFormat.RGBA8UNormSRGB), result.TexelFormat);
            Assert.AreEqual(4U, result.TexelSizeBytes);

            LosgapSystem.InvokeOnMaster(() => {
                Texture2D <TexelFormat.RGBA8UNormSRGB> dataCopyTex = (result as Texture2D <TexelFormat.RGBA8UNormSRGB>).Clone()
                                                                     .WithUsage(ResourceUsage.StagingRead)
                                                                     .WithPermittedBindings(GPUBindings.None);

                result.CopyTo(dataCopyTex);

                Assert.AreEqual(UnsafeUtils.Reinterpret <uint, TexelFormat.RGBA8UNormSRGB>(4294958703U, 4), dataCopyTex.Read(0U)[138, 99]);

                dataCopyTex.Dispose();
            });

            result.Dispose();

            result = defaultLoader.WithFilePath(@"Tests\200x200.gif").Create();
            Assert.AreEqual(200U, result.Width);
            Assert.AreEqual(200U, result.Height);
            Assert.AreEqual(typeof(Texture2D <TexelFormat.RGBA8UNorm>), result.GetType());
            Assert.AreEqual(0U, result.ArrayIndex);
            Assert.AreEqual(false, result.IsArrayTexture);
            Assert.AreEqual(false, result.IsGlobalDetailTarget);
            Assert.AreEqual(false, result.IsMipGenTarget);
            Assert.AreEqual(false, result.IsMipmapped);
            Assert.AreEqual(false, result.IsMultisampled);
            Assert.AreEqual(1U, result.NumMips);
            Assert.AreEqual(200U * 200U * 4U, result.Size);
            Assert.AreEqual(200U * 200U, result.SizeTexels);
            Assert.AreEqual(typeof(TexelFormat.RGBA8UNorm), result.TexelFormat);
            Assert.AreEqual(4U, result.TexelSizeBytes);

            LosgapSystem.InvokeOnMaster(() => {
                Texture2D <TexelFormat.RGBA8UNorm> dataCopyTex = (result as Texture2D <TexelFormat.RGBA8UNorm>).Clone()
                                                                 .WithUsage(ResourceUsage.StagingRead)
                                                                 .WithPermittedBindings(GPUBindings.None);

                result.CopyTo(dataCopyTex);

                Assert.AreEqual(UnsafeUtils.Reinterpret <uint, TexelFormat.RGBA8UNorm>(4294958702U, 4), dataCopyTex.Read(0U)[138, 99]);

                dataCopyTex.Dispose();
            });

            result.Dispose();

            result = defaultLoader.WithFilePath(@"Tests\200x200.jpg").Create();
            Assert.AreEqual(200U, result.Width);
            Assert.AreEqual(200U, result.Height);
            Assert.AreEqual(typeof(Texture2D <TexelFormat.RGBA8UNorm>), result.GetType());
            Assert.AreEqual(0U, result.ArrayIndex);
            Assert.AreEqual(false, result.IsArrayTexture);
            Assert.AreEqual(false, result.IsGlobalDetailTarget);
            Assert.AreEqual(false, result.IsMipGenTarget);
            Assert.AreEqual(false, result.IsMipmapped);
            Assert.AreEqual(false, result.IsMultisampled);
            Assert.AreEqual(1U, result.NumMips);
            Assert.AreEqual(200U * 200U * 4U, result.Size);
            Assert.AreEqual(200U * 200U, result.SizeTexels);
            Assert.AreEqual(typeof(TexelFormat.RGBA8UNorm), result.TexelFormat);
            Assert.AreEqual(4U, result.TexelSizeBytes);

            LosgapSystem.InvokeOnMaster(() => {
                Texture2D <TexelFormat.RGBA8UNorm> dataCopyTex = (result as Texture2D <TexelFormat.RGBA8UNorm>).Clone()
                                                                 .WithUsage(ResourceUsage.StagingRead)
                                                                 .WithPermittedBindings(GPUBindings.None);

                result.CopyTo(dataCopyTex);

                Assert.AreEqual(UnsafeUtils.Reinterpret <uint, TexelFormat.RGBA8UNorm>(4294955895U, 4), dataCopyTex.Read(0U)[138, 99]);

                dataCopyTex.Dispose();
            });

            result.Dispose();
        }