public ExpressionHLSLWriter(ShaderModel shader, string expressionName) { Shader = shader; ExpressionName = expressionName; Ctab = shader.ConstantTable; Cli = shader.Cli; }
private string FormatOperand(ConstantTable ctab, CliToken cli, FxlcOperandType type, uint index) { var elementIndex = index / 4; var componentIndex = index % 4; var component = FormatComponent(componentIndex, ComponentCount); switch (type) { case FxlcOperandType.Literal: var literal = string.Join(", ", Enumerable.Repeat(cli.GetLiteral(index), (int)ComponentCount)); return(string.Format("({0})", literal)); case FxlcOperandType.Temp: return(string.Format("r{0}{1}", elementIndex, component)); case FxlcOperandType.Variable: return(string.Format("c{0}{1}", elementIndex, component)); case FxlcOperandType.Expr: return(string.Format("c{0}{1}", elementIndex, component)); default: return(string.Format("unknown{0}{1}", elementIndex, component)); } }
public string ToString(ConstantTable ctab, CliToken cli) { var operands = string.Join(", ", Operands.Select(o => o.FormatOperand(ctab, cli))); return(string.Format("{0} {1}", Opcode.ToString().ToLowerInvariant(), operands)); }
/// <summary> /// Format operand for FX9 preshaders /// </summary> /// <param name="ctab"></param> /// <param name="cli"></param> /// <returns></returns> public string FormatOperand(ConstantTable ctab, CliToken cli) { if (IsArray == 0) { return(FormatOperand(ctab, cli, OpType, OpIndex)); } else { return(string.Format("{0}[{1}]", FormatOperand(ctab, cli, ArrayType, ArrayIndex), FormatOperand(ctab, cli, OpType, OpIndex))); } }
Token ReadInstruction(BytecodeReader reader) { uint instructionToken = reader.ReadUInt32(); Opcode opcode = (Opcode)(instructionToken & 0xffff); int size; if (opcode == Opcode.Comment) { size = (int)((instructionToken >> 16) & 0x7FFF); } else { size = (int)((instructionToken >> 24) & 0x0f); } Token token = null; if (opcode == Opcode.Comment) { var fourCC = reader.ReadUInt32(); if (KnownCommentTypes.ContainsKey(fourCC)) { var commentReader = reader.CopyAtCurrentPosition(); reader.ReadBytes(size * 4 - 4); switch (KnownCommentTypes[fourCC]) { case CommentType.CTAB: ConstantTable = ConstantTable.Parse(commentReader); return(null); case CommentType.C**T: Cli = CliToken.Parse(commentReader); return(null); case CommentType.FXLC: Fxlc = FxlcBlock.Parse(commentReader); return(null); case CommentType.PRES: Preshader = Preshader.Parse(commentReader); return(null); case CommentType.PRSI: Prsi = PrsiToken.Parse(commentReader); return(null); } } token = new CommentToken(opcode, size, this); token.Data[0] = fourCC; for (int i = 1; i < size; i++) { token.Data[i] = reader.ReadUInt32(); } } else { token = new InstructionToken(opcode, size, this); var inst = token as InstructionToken; for (int i = 0; i < size; i++) { token.Data[i] = reader.ReadUInt32(); if (opcode == Opcode.Def || opcode == Opcode.DefB || opcode == Opcode.DefI) { if (i == 0) { inst.Operands.Add(new DestinationOperand(token.Data[i])); } } else if (opcode == Opcode.Dcl) { if (i == 0) { inst.Operands.Add(new DeclarationOperand(token.Data[i])); } else { inst.Operands.Add(new DestinationOperand(token.Data[i])); } } else if (i == 0 && opcode.HasDestination()) { if ((token.Data[i] & (1 << 13)) != 0) { //Relative Address mode token.Data[i + 1] = reader.ReadUInt32(); inst.Operands.Add(new DestinationOperand(token.Data[i], token.Data[i + 1])); i++; } else { inst.Operands.Add(new DestinationOperand(token.Data[i])); } } else if ((token.Data[i] & (1 << 13)) != 0) { //Relative Address mode token.Data[i + 1] = reader.ReadUInt32(); inst.Operands.Add(new SourceOperand(token.Data[i], token.Data[i + 1])); i++; } else { inst.Operands.Add(new SourceOperand(token.Data[i])); } } if (opcode != Opcode.Comment) { token.Modifier = (int)((instructionToken >> 16) & 0xff); token.Predicated = (instructionToken & 0x10000000) != 0; token.CoIssue = (instructionToken & 0x40000000) != 0; Debug.Assert((instructionToken & 0xA0000000) == 0, $"Instruction has unexpected bits set {instructionToken & 0xE0000000}"); } } return(token); }