private string FormatMembers(int indent, string nameprefix, ShaderConstant[] vars)
        {
            string indentstr = new string(' ', indent*4);

            string ret = "";

            int i = 0;

            foreach (var v in vars)
            {
                if (v.type.members.Length > 0)
                {
                    if (i > 0)
                        ret += "\n";
                    ret += indentstr + "// struct " + v.type.Name + Environment.NewLine;
                    ret += indentstr + "{" + Environment.NewLine +
                                FormatMembers(indent + 1, v.name + "_", v.type.members) +
                           indentstr + "}" + Environment.NewLine;
                    if (i < vars.Length-1)
                        ret += Environment.NewLine;
                }
                else
                {
                    string arr = "";
                    if (v.type.descriptor.elements > 1)
                        arr = String.Format("[{0}]", v.type.descriptor.elements);
                    ret += indentstr + v.type.Name + " " + nameprefix + v.name + arr + ";" + Environment.NewLine;
                }

                i++;
            }

            return ret;
        }
        private void MakeShaderVariablesHLSL(bool cbufferContents, ShaderConstant[] vars, ref string struct_contents, ref string struct_defs)
        {
            var nl = Environment.NewLine;
            var nl2 = Environment.NewLine + Environment.NewLine;

            foreach (var v in vars)
            {
                if (v.type.members.Length > 0)
                {
                    string def = "struct " + v.type.descriptor.name + " {" + nl;

                    if(!struct_defs.Contains(def))
                    {
                        string contents = "";
                        MakeShaderVariablesHLSL(false, v.type.members, ref contents, ref struct_defs);

                        struct_defs += def + contents + "};" + nl2;
                    }
                }

                struct_contents += "\t" + v.type.descriptor.name + " " + v.name;

                char comp = 'x';
                if (v.reg.comp == 1) comp = 'y';
                if (v.reg.comp == 2) comp = 'z';
                if (v.reg.comp == 3) comp = 'w';

                if (cbufferContents) struct_contents += String.Format(" : packoffset(c{0}.{1});", v.reg.vec, comp);
                else struct_contents += ";";

                struct_contents += nl;
            }
        }
Exemplo n.º 3
0
        private string FriendlyName(string disasm, string stem, string prefix, ShaderConstant[] vars)
        {
            foreach (var v in vars)
            {
                if (v.type.descriptor.rows == 0 && v.type.descriptor.cols == 0 && v.type.members.Length > 0)
                {
                    string subPrefix = prefix + v.name + ".";

                    disasm = FriendlyName(disasm, stem, subPrefix, v.type.members);
                }
                else if (v.type.descriptor.rows > 0 && v.type.descriptor.cols > 0)
                {
                    uint numRegs = v.type.descriptor.rows * Math.Max(1, v.type.descriptor.elements);

                    for (uint r = 0; r < numRegs; r++)
                    {
                        var reg = string.Format("{0}[{1}]", stem, v.reg.vec + r);

                        int compStart = r == 0 ? (int)v.reg.comp : 0;
                        int compEnd = compStart + (int)v.type.descriptor.cols;

                        var comps = "xyzw".Substring(compStart, compEnd - compStart);

                        var regexp = string.Format(", (-|abs\\()?{0}\\.([{1}]*)([^xyzw])", Regex.Escape(reg), comps);

                        var match = Regex.Match(disasm, regexp);

                        while (match.Success)
                        {
                            var swizzle = match.Groups[2].Value.ToCharArray();

                            for (int c = 0; c < swizzle.Length; c++)
                            {
                                int val = "xyzw".IndexOf(swizzle[c]);
                                swizzle[c] = "xyzw"[val - compStart];
                            }

                            var name = numRegs == 1 ? v.name : string.Format("{0}[{1}]", v.name, r);

                            name = prefix + name;

                            var replacement = string.Format(", {0}{1}.{2}{3}",
                                                    match.Groups[1].Value, name, new string(swizzle), match.Groups[3].Value);

                            disasm = disasm.Remove(match.Index, match.Length);

                            disasm = disasm.Insert(match.Index, replacement);

                            match = Regex.Match(disasm, regexp);
                        }
                    }
                }
            }

            return disasm;
        }