Пример #1
0
        string WriteTextures(ShaderOutputContext ctx, TemplateGroup group)
        {
            List <Texture> textures = ctx.Textures;

            if (textures.Count == 0)
            {
                return("");
            }

            StringBuilder result = new StringBuilder();

            result.Append("///////////////// TEXTURES /////////////////// ");
            result.Append('\n');

            foreach (var texture in textures)
            {
                StringTemplate textureTemplate = group.GetInstanceOf("tex2d");
                textureTemplate.Add("name", texture.Name);
                textureTemplate.Add("reg", texture.Bindpoint.Reg);
                textureTemplate.Add("space", texture.Bindpoint.Space);
                result.Append(textureTemplate.Render() + '\n');
            }

            result.Append('\n');
            result.Append('\n');

            return(result.ToString());
        }
Пример #2
0
        string WriteRootConstants(ShaderOutputContext ctx, TemplateGroup group)
        {
            List <UniformConstant> rootConstants = ctx.UniformConstants;

            if (rootConstants.Count == 0)
            {
                return("");
            }

            StringBuilder result = new StringBuilder();

            result.Append("///////////////// ROOT CONSANTS /////////////////// ");
            result.Append('\n');

            foreach (var rootConstant in rootConstants)
            {
                StringTemplate cbufferTemplate = group.GetInstanceOf("cbuffer");
                cbufferTemplate.Add("name", "cb_" + rootConstant.Name);
                string member = rootConstant.Type + " " + rootConstant.Name + ";";
                cbufferTemplate.Add("members", member);
                cbufferTemplate.Add("reg", rootConstant.Bindpoint.Reg);
                cbufferTemplate.Add("space", rootConstant.Bindpoint.Space);
                result.Append(cbufferTemplate.Render() + '\n' + '\n');
            }

            result.Append('\n');
            result.Append('\n');

            return(result.ToString());
        }
Пример #3
0
        string WriteSamplers(ShaderOutputContext ctx, TemplateGroup group)
        {
            List <Sampler> samplers = ctx.Samplers;

            if (samplers.Count == 0)
            {
                return("");
            }

            StringBuilder result = new StringBuilder();

            result.Append("///////////////// SAMPLERS /////////////////// ");
            result.Append('\n');

            foreach (var sampler in samplers)
            {
                StringTemplate samplerTemplate = group.GetInstanceOf("sampler");
                samplerTemplate.Add("name", sampler.Name);
                samplerTemplate.Add("reg", sampler.Bindpoint.Reg);
                samplerTemplate.Add("space", sampler.Bindpoint.Space);
                result.Append(samplerTemplate.Render() + '\n');
            }

            result.Append('\n');
            result.Append('\n');

            return(result.ToString());
        }
Пример #4
0
        string WriteVertexLayout(ShaderOutputContext ctx, TemplateGroup group)
        {
            VertexLayout vLayout = ctx.VertLayout;

            StringBuilder result = new StringBuilder();

            result.Append("///////////////// VERTEX LAYOUT /////////////////// ");
            result.Append('\n');

            StringBuilder members = new StringBuilder();

            foreach (var member in vLayout.Members)
            {
                StringTemplate memberTemplate = group.GetInstanceOf("vlayoutvar");
                memberTemplate.Add("type", Variable.ConvertHlslType(member.Type));
                memberTemplate.Add("name", member.Name);
                string index = "";
                if (member.Semantic == "TEXCOORD" || member.Semantic == "COLOR")
                {
                    index = member.SemanticIndex.ToString();
                }
                memberTemplate.Add("semantic", member.Semantic + index);
                members.Append(memberTemplate.Render() + '\n');
            }
            StringTemplate structTemplate = group.GetInstanceOf("struct");

            structTemplate.Add("name", "vIn");
            structTemplate.Add("members", members);
            result.Append(structTemplate.Render() + '\n' + '\n');

            result.Append('\n');
            result.Append('\n');

            return(result.ToString());
        }
Пример #5
0
        public void WriteHeaders(ShaderOutputContext ctx, string filename)
        {
            TemplateGroup group = new Antlr4.StringTemplate.TemplateGroupFile(Program.TemplatesDir + "/cppTemplate.stg");

            string constantBuffers     = WriteConstantBuffers(ctx, group);
            string constantBufferNames = WriteConstantBuffersNames(ctx, group);
            string constants           = WriteRootConstants(ctx, group);
            string textureSets         = WriteTextureSets(ctx, group);
            string bindings            = WriteBindings(ctx, group);
            string vertexLayouts       = WriteVertexLayouts(ctx, group);
            string programNames        = WriteProgramNames(ctx, group);
            string structs             = WriteStructures(group, ctx.Structures);

            structs += WriteStructures(group, ctx.ConstantBuffers);

            StringTemplate headerTemplate = group.GetInstanceOf("header");

            headerTemplate.Add("name", filename);
            headerTemplate.Add("structs", structs);
            headerTemplate.Add("cbuffers", constantBuffers);
            headerTemplate.Add("cbNames", constantBufferNames);
            headerTemplate.Add("constants", constants);
            headerTemplate.Add("texSets", textureSets);
            headerTemplate.Add("shaderProgs", bindings);
            headerTemplate.Add("vertexLayout", vertexLayouts);
            headerTemplate.Add("shaderProgNames", programNames);
            headerTemplate.Add("shaderPath", "Shaders/" + filename + ".hlsl");

            string outDirHlsl  = Program.CppOutputDir + "/sInp/";
            string filenameOut = outDirHlsl + filename + ".h";

            System.IO.File.WriteAllText(filenameOut, headerTemplate.Render());
        }
Пример #6
0
        public void WriteFactory(string[] files)
        {
            TemplateGroup cppGroup = new Antlr4.StringTemplate.TemplateGroupFile(Program.TemplatesDir + "/factoryTemplateCpp.stg");

            StringBuilder mapping  = new StringBuilder();
            StringBuilder includes = new StringBuilder();

            foreach (var filepath in files)
            {
                string         filenameWithoutExt = Path.GetFileNameWithoutExtension(filepath);
                StringTemplate mapTemplate        = cppGroup.GetInstanceOf("shaderMap");
                mapTemplate.Add("key", filenameWithoutExt);
                mapping.Append(mapTemplate.Render() + '\n');

                StringTemplate inclTemplate = cppGroup.GetInstanceOf("include");
                inclTemplate.Add("filename", filenameWithoutExt);
                includes.Append(inclTemplate.Render() + '\n');
            }
            StringTemplate factoryCppTemplate = cppGroup.GetInstanceOf("factory");

            factoryCppTemplate.Add("includes", includes.ToString());
            factoryCppTemplate.Add("mapping", mapping.ToString());

            string filename = Program.CppOutputDir + "/KiotoShaders";

            System.IO.File.WriteAllText(filename + ".cpp", factoryCppTemplate.Render());

            TemplateGroup  hGroup           = new Antlr4.StringTemplate.TemplateGroupFile(Program.TemplatesDir + "/factoryTemplateH.stg");
            StringTemplate factoryHTemplate = hGroup.GetInstanceOf("factory");

            System.IO.File.WriteAllText(filename + ".h", factoryHTemplate.Render());
        }
        public void StringTemplateRenderExtraElementDataTest()
        {
            target = new StringTemplate("<test> two two");

            target.Add("test", "one");
            target.Add("testing", "two");

            Assert.AreEqual("one two two", target.Render());
        }
        public void StringTemplateRenderTwoElementTemplateTest()
        {
            target = new StringTemplate("<test1> <test2> two");

            target.Add("test1", "one");
            target.Add("test2", "two");

            Assert.AreEqual("one two two", target.Render());
        }
        public void StringTemplateAddTwoDifferentValuesTest()
        {
            target = new StringTemplate(String.Empty);

            target.Add("test", "one");
            target.Add("testing", "two");

            Assert.AreEqual(2, target.ElementData.Count);
            Assert.IsTrue(target.ElementData.ContainsKey("test"));
            Assert.AreEqual("one", target.ElementData["test"]);
            Assert.IsTrue(target.ElementData.ContainsKey("testing"));
            Assert.AreEqual("two", target.ElementData["testing"]);
        }
Пример #10
0
        string WriteProgramNames(ShaderOutputContext ctx, TemplateGroup group)
        {
            StringBuilder        res         = new StringBuilder();
            List <ShaderBinding> shaderBinds = ctx.ShaderBinding.Bindings;

            for (int i = 0; i < shaderBinds.Count; ++i)
            {
                StringTemplate nameTemplate = group.GetInstanceOf("programNamePair");
                nameTemplate.Add("shaderProg", m_shaderStagesToKioto[shaderBinds[i].ShaderType]);
                nameTemplate.Add("name", shaderBinds[i].EntryPointName);
                res.Append(nameTemplate.Render() + '\n');
            }
            return(res.ToString());
        }
        public void StringTemplatStringAngleBracketsTest()
        {
            target = new StringTemplate("<hello> <world>");
            target.Add("hello", "hello");
            target.Add("<world>", "world");

            Assert.AreEqual(2, target.ElementData.Count);
            Assert.IsTrue(target.ElementData.ContainsKey("hello"));
            Assert.AreEqual("hello", target.ElementData["hello"]);
            Assert.IsTrue(target.ElementData.ContainsKey("world"));
            Assert.AreEqual("world", target.ElementData["world"]);

            Assert.AreEqual("hello world", target.Render());
        }
Пример #12
0
        string WriteVertexLayouts(ShaderOutputContext ctx, TemplateGroup group)
        {
            List <VertexLayoutMember> vertexLayoutMembers = ctx.VertLayout.Members;
            StringBuilder             res = new StringBuilder();

            foreach (var vlm in vertexLayoutMembers)
            {
                StringTemplate vertexLayoutMemberTemplate = group.GetInstanceOf("vlayoutmember");
                vertexLayoutMemberTemplate.Add("semantic", m_semanticToKiotoSemantic[vlm.Semantic]);
                vertexLayoutMemberTemplate.Add("index", vlm.SemanticIndex);
                vertexLayoutMemberTemplate.Add("format", m_typeToKiotoFormat[vlm.Type]);
                res.Append(vertexLayoutMemberTemplate.Render() + '\n');
            }
            return(res.ToString());
        }
Пример #13
0
        string WriteRootConstants(ShaderOutputContext ctx, TemplateGroup group)
        {
            StringBuilder          res    = new StringBuilder();
            List <UniformConstant> consts = ctx.UniformConstants;

            foreach (var c in consts)
            {
                StringTemplate cBufferTemplate = group.GetInstanceOf("uniformConstant");
                cBufferTemplate.Add("name", c.Name);
                cBufferTemplate.Add("reg", c.Bindpoint.Reg.ToString());
                cBufferTemplate.Add("space", c.Bindpoint.Space.ToString());
                res.Append(cBufferTemplate.Render() + '\n');
            }
            return(res.ToString());
        }
Пример #14
0
        static void Basic()
        {
            StringTemplate stringTemplate = new StringTemplate("hello <data>");
            stringTemplate.Add("data", "world");

            Console.WriteLine(stringTemplate.Render());
        }
        public void StringTemplateRenderOneElementRepeatedTemplateTest()
        {
            target = new StringTemplate("one <test> <test>");

            target.Add("test", "two");

            Assert.AreEqual("one two two", target.Render());
        }
Пример #16
0
        static void Basic()
        {
            StringTemplate stringTemplate = new StringTemplate("hello <data>");

            stringTemplate.Add("data", "world");

            Console.WriteLine(stringTemplate.Render());
        }
        public void StringTemplateRenderCaseInsensitiveTest2()
        {
            target = new StringTemplate("<TEST> two two");

            target.Add("test", "one");

            Assert.AreEqual("one two two", target.Render());
        }
        public void StringTemplateRenderNoDataForElementTest()
        {
            target = new StringTemplate("<test> two two");

            target.Add("testing", "one");

            Assert.AreEqual("<test> two two", target.Render());
        }
Пример #19
0
        string WriteTextureSets(ShaderOutputContext ctx, TemplateGroup group)
        {
            List <Texture> textures = ctx.Textures;

            StringBuilder res = new StringBuilder();

            foreach (var t in textures)
            {
                StringTemplate textureTemplate = group.GetInstanceOf("texture");
                textureTemplate.Add("name", t.Name);
                textureTemplate.Add("reg", t.Bindpoint.Reg);
                res.Append(textureTemplate.Render() + '\n');
            }
            StringTemplate textureSetTemplate = group.GetInstanceOf("texSet");

            textureSetTemplate.Add("addParams", res.ToString());

            return(textureSetTemplate.Render());
        }
        public void StringTemplateAddOneTest()
        {
            target = new StringTemplate(String.Empty);

            target.Add("test", "one");

            Assert.AreEqual(1, target.ElementData.Count);
            Assert.IsTrue(target.ElementData.ContainsKey("test"));
            Assert.AreEqual("one", target.ElementData["test"]);
        }
Пример #21
0
        string WriteConstantBuffers(ShaderOutputContext ctx, TemplateGroup group)
        {
            StringBuilder         res = new StringBuilder();
            List <ConstantBuffer> cbs = ctx.ConstantBuffers;

            foreach (var cb in cbs)
            {
                StringTemplate cBufferTemplate = group.GetInstanceOf("cbuffer");
                cBufferTemplate.Add("cbname", cb.Name);
                cBufferTemplate.Add("reg", cb.Bindpoint.Reg.ToString());
                cBufferTemplate.Add("space", cb.Bindpoint.Space.ToString());
                cBufferTemplate.Add("typename", cb.Typename);
                if (cb.Size > 1)
                {
                    cBufferTemplate.Add("size", cb.Size);
                }
                res.Append(cBufferTemplate.Render() + '\n');
            }
            return(res.ToString());
        }
        public void StringTemplatAddDuplicateCaseInsensitiveTest()
        {
            target = new StringTemplate(String.Empty);

            target.Add("test", "one");

            try
            {
                target.Add("TEST", "one");

                Assert.Fail("No exception thrown");
            }
            catch (ArgumentException exception)
            {
                Assert.AreEqual("Element has already been added.\r\nParameter name: element", exception.Message);
                Assert.AreEqual("element", exception.ParamName);
            }
            catch
            {
                Assert.Fail("ArgumentException not thrown");
            }
        }
        public void StringTemplatAddDuplicateSameValuesTest()
        {
            target = new StringTemplate(String.Empty);

            target.Add("test", "one");

            try
            {
                target.Add("test", "one");

                Assert.Fail("No exception thrown");
            }
            catch (ArgumentException exception)
            {
                Assert.AreEqual("Element has already been added.\r\nParameter name: element", exception.Message);
                Assert.AreEqual("element", exception.ParamName);
            }
            catch
            {
                Assert.Fail("ArgumentException not thrown");
            }
        }
Пример #24
0
        string WriteConstantBuffersNames(ShaderOutputContext ctx, TemplateGroup group)
        {
            StringBuilder         res = new StringBuilder();
            List <ConstantBuffer> cbs = ctx.ConstantBuffers;

            foreach (var cb in cbs)
            {
                StringTemplate cBufferTemplate = group.GetInstanceOf("cbName");
                cBufferTemplate.Add("name", cb.Name);
                res.Append(cBufferTemplate.Render() + '\n');
            }
            return(res.ToString());
        }
Пример #25
0
        public static string WriteStructures(TemplateGroup group, IEnumerable <IStructureType> structures)
        {
            if (structures.Count() == 0)
            {
                return("");
            }

            StringBuilder result = new StringBuilder();

            foreach (var structure in structures)
            {
                if (structure.Members == null) // [a_vorontcov] For example templated cb.
                {
                    continue;
                }

                StringBuilder members = new StringBuilder();
                foreach (var member in structure.Members)
                {
                    StringTemplate memberTemplate = group.GetInstanceOf("var");
                    memberTemplate.Add("type", Variable.ConvertHlslType(member.Type));
                    memberTemplate.Add("name", member.Name);
                    if (member.Dimension > 0)
                    {
                        memberTemplate.Add("dim", member.Dimension);
                    }
                    members.Append(memberTemplate.Render() + '\n');
                }
                StringTemplate structTemplate = group.GetInstanceOf("struct");
                structTemplate.Add("name", structure.Typename);
                structTemplate.Add("members", members);
                result.Append(structTemplate.Render() + '\n' + '\n');
            }

            return(result.ToString());
        }
        public void StringTemplateAddValueNullTest()
        {
            target = new StringTemplate("test");

            try
            {
                target.Add("test", null);

                Assert.Fail("No exception thrown");
            }
            catch (ArgumentNullException exception)
            {
                Assert.AreEqual("value", exception.ParamName);
            }
            catch
            {
                Assert.Fail("ArgumentNullException not thrown");
            }
        }
        public void StringTemplateAddElementEmptyTest()
        {
            target = new StringTemplate("test");

            try
            {
                target.Add(String.Empty, new Object());

                Assert.Fail("No exception thrown");
            }
            catch (ArgumentException exception)
            {
                Assert.AreEqual("element", exception.ParamName);
            }
            catch
            {
                Assert.Fail("ArgumentException not thrown");
            }
        }
        public void StringTemplateAddElementWhitespaceTest()
        {
            target = new StringTemplate("test");

            try
            {
                target.Add(" ", new Object());

                Assert.Fail("No exception thrown");
            }
            catch (ArgumentException exception)
            {
                Assert.AreEqual("The parameter 'element' cannot be an empty string.\r\nParameter name: element", exception.Message);
                Assert.AreEqual("element", exception.ParamName);
            }
            catch
            {
                Assert.Fail("ArgumentException not thrown");
            }
        }
        public void WriteHeaders(ShaderOutputGlobalContext ctx)
        {
            TemplateGroup  structCppGroup          = new Antlr4.StringTemplate.TemplateGroupFile(Program.TemplatesDir + "/cppTemplate.stg");
            TemplateGroup  commonGroup             = new Antlr4.StringTemplate.TemplateGroupFile(Program.TemplatesDir + "/commonStructuresTemplate.stg");
            var            structWithoutDuplicates = RemoveDuplicates(ctx.Structures);
            string         cppStructures           = CppHeaderWriter.WriteStructures(structCppGroup, structWithoutDuplicates);
            StringTemplate commonTemplate          = commonGroup.GetInstanceOf("cppHeader");

            commonTemplate.Add("structs", cppStructures);

            string cppFilename = Program.CppOutputDir + "/CommonStructures.h";

            System.IO.File.WriteAllText(cppFilename, commonTemplate.Render());

            TemplateGroup  structHlslGroup    = new Antlr4.StringTemplate.TemplateGroupFile(Program.TemplatesDir + "/hlslTemplate.stg");
            string         hlslStructures     = HlslHeadersWriter.WriteStructures(structHlslGroup, structWithoutDuplicates);
            StringTemplate commonHlslTemplate = commonGroup.GetInstanceOf("hlslHeader");

            commonHlslTemplate.Add("structs", hlslStructures);

            string hlslFilename = Program.HlslOutputDir + "/CommonStructures.hlsl";

            System.IO.File.WriteAllText(hlslFilename, commonHlslTemplate.Render());
        }
Пример #30
0
        string WriteConstantBuffers(ShaderOutputContext ctx, TemplateGroup group)
        {
            List <ConstantBuffer> constantBuffers = ctx.ConstantBuffers;

            if (constantBuffers.Count == 0)
            {
                return("");
            }

            StringBuilder result = new StringBuilder();

            result.Append("///////////////// CONSTANT BUFFERS /////////////////// ");
            result.Append('\n');

            foreach (var constantBuffer in constantBuffers)
            {
                StringTemplate cbufferTemplate = group.GetInstanceOf("cbufferTempl");
                cbufferTemplate.Add("name", constantBuffer.Name);
                cbufferTemplate.Add("typename", constantBuffer.Typename);
                cbufferTemplate.Add("reg", constantBuffer.Bindpoint.Reg);
                cbufferTemplate.Add("space", constantBuffer.Bindpoint.Space);
                if (constantBuffer.Size != 0)
                {
                    cbufferTemplate.Add("isArray", "true");
                }
                if (constantBuffer.Size > 0)
                {
                    cbufferTemplate.Add("size", constantBuffer.Size);
                }
                result.Append(cbufferTemplate.Render() + '\n' + '\n');
            }

            result.Append('\n');
            result.Append('\n');

            return(result.ToString());
        }
        public void StringTemplateAddElementWhitespaceTest()
        {
            target = new StringTemplate("test");

            try
            {
                target.Add(" ", new Object());

                Assert.Fail("No exception thrown");
            }
            catch (ArgumentException exception)
            {
                Assert.AreEqual("The parameter 'element' cannot be an empty string.\r\nParameter name: element", exception.Message);
                Assert.AreEqual("element", exception.ParamName);
            }
            catch
            {
                Assert.Fail("ArgumentException not thrown");
            }
        }
        public void StringTemplateAddElementEmptyTest()
        {
            target = new StringTemplate("test");

            try
            {
                target.Add(String.Empty, new Object());

                Assert.Fail("No exception thrown");
            }
            catch (ArgumentException exception)
            {
                Assert.AreEqual("element", exception.ParamName);
            }
            catch
            {
                Assert.Fail("ArgumentException not thrown");
            }
        }
        public void StringTemplatStringAngleBracketsTest()
        {
            target = new StringTemplate("<hello> <world>");
            target.Add("hello", "hello");
            target.Add("<world>", "world");

            Assert.AreEqual(2, target.ElementData.Count);
            Assert.IsTrue(target.ElementData.ContainsKey("hello"));
            Assert.AreEqual("hello", target.ElementData["hello"]);
            Assert.IsTrue(target.ElementData.ContainsKey("world"));
            Assert.AreEqual("world", target.ElementData["world"]);

            Assert.AreEqual("hello world", target.Render());
        }
        public void StringTemplateRenderTwoElementTemplateTest()
        {
            target = new StringTemplate("<test1> <test2> two");

            target.Add("test1", "one");
            target.Add("test2", "two");

            Assert.AreEqual("one two two", target.Render());
        }
        public void StringTemplateRenderOneElementRepeatedTemplateTest()
        {
            target = new StringTemplate("one <test> <test>");

            target.Add("test", "two");

            Assert.AreEqual("one two two", target.Render());
        }
        public void StringTemplateRenderNoDataForElementTest()
        {
            target = new StringTemplate("<test> two two");

            target.Add("testing", "one");

            Assert.AreEqual("<test> two two", target.Render());
        }
        public void StringTemplateRenderExtraElementDataTest()
        {
            target = new StringTemplate("<test> two two");

            target.Add("test", "one");
            target.Add("testing", "two");

            Assert.AreEqual("one two two", target.Render());
        }
        public void StringTemplateRenderCaseInsensitiveTest2()
        {
            target = new StringTemplate("<TEST> two two");

            target.Add("test", "one");

            Assert.AreEqual("one two two", target.Render());
        }
        public void StringTemplateAddValueNullTest()
        {
            target = new StringTemplate("test");

            try
            {
                target.Add("test", null);

                Assert.Fail("No exception thrown");
            }
            catch (ArgumentNullException exception)
            {
                Assert.AreEqual("value", exception.ParamName);
            }
            catch
            {
                Assert.Fail("ArgumentNullException not thrown");
            }
        }
        public void StringTemplateAddTwoSameValuesTest()
        {
            target = new StringTemplate(String.Empty);

            target.Add("test", "one");
            target.Add("testing", "one");

            Assert.AreEqual(2, target.ElementData.Count);
            Assert.IsTrue(target.ElementData.ContainsKey("test"));
            Assert.AreEqual("one", target.ElementData["test"]);
            Assert.IsTrue(target.ElementData.ContainsKey("testing"));
            Assert.AreEqual("one", target.ElementData["testing"]);
        }