コード例 #1
0
ファイル: Effect.cs プロジェクト: davidfoxhu/G3D
        private void ReadShader(Effect effect, XElement child, bool isHLSL)
        {
            Effect.Shader shader = new Effect.Shader();
            shader.isHLSL = isHLSL;

            if (!isHLSL)
            {
                shader.isVertex = ((string)child.Attribute("type") == "vertex");
            }
            else
            {
                shader.target   = (string)child.Attribute("target");
                shader.entry    = (string)child.Attribute("entry");
                shader.isVertex = (shader.target[0] == 'v');
            }
            shader.generate = (string)child.Attribute("generate");
            if (shader.generate == null)
            {
                shader.generate = "";
            }

            string filename = mgr.MakeAbsolute((string)child.Attribute("filename"), parent.filename);

            if (filename != null)
            {
                if (!File.Exists(filename))
                {
                    mgr.Error("failed to load shader: " + filename);
                }
                else
                {
                    shader.source = File.ReadAllText(filename);
                }
            }
            for (var item = child.FirstNode; item != null; item = item.NextNode)
            {
                if (item.NodeType == XmlNodeType.Text)
                {
                    shader.source += ((XText)item).Value;
                }
                else if (item.NodeType == XmlNodeType.Element)
                {
                    string name  = (string)((XElement)item).Attribute("name");
                    string value = (string)((XElement)item).Attribute("value");
                    if (value == null)
                    {
                        value = "";
                    }
                    if (name != null)
                    {
                        shader.defines[name] = value;
                    }
                }
            }

            effect.shaders.Add(shader);
        }
コード例 #2
0
ファイル: Effect.cs プロジェクト: davidfoxhu/G3D
        private void WriteShader(Effect.Shader shader, string filename)
        {
            string output = "";

            foreach (KeyValuePair <string, string> pair in shader.defines)
            {
                output += string.Format("#define {0} {1}\n", pair.Key, pair.Value);
            }
            output += shader.source;
            File.WriteAllText(filename, output);
        }
コード例 #3
0
ファイル: Effect.cs プロジェクト: davidfoxhu/G3D
        private void Compile(Effect.Shader shader, string type)
        {
            Dictionary <string, string> args = new Dictionary <string, string>();

            args["temp"] = Path.GetTempPath();
            if (shader.entry != null)
            {
                args["entry"] = shader.entry;
            }
            if (shader.target != null)
            {
                args["target"] = shader.target;
            }
            args["in"]  = Path.Combine(args["temp"], "in.source");
            args["out"] = Path.Combine(args["temp"], "out.binary");

            WriteShader(shader, args["in"]);

            if (shader.isVertex)
            {
                mgr.RunTool(type, "vertex", args);
            }
            else
            {
                mgr.RunTool(type, "pixel", args);
            }

            try {
                shader.binary[type] = File.ReadAllBytes(args["out"]);
            } catch (Exception) { }

            try {
                File.Delete(args["in"]);
            } catch (Exception) { }
            try {
                File.Delete(args["out"]);
            } catch (Exception) { }
        }