virtual public ShaderModel ReadShader() { int dxbc = ReadInt32(); System.Diagnostics.Debug.Assert(dxbc == FourCC.Make("DXBC")); ReadBytes(16); // checksum ReadInt32(); // 1 ReadInt32(); // totalSize int chunkCount = ReadInt32(); int[] chunkOffsets = new int[chunkCount]; for (int i = 0; i < chunkCount; i++) { chunkOffsets[i] = ReadInt32(); } ShaderModel shader = null; foreach (int chunkOffset in chunkOffsets) { BaseStream.Position = chunkOffset; string chunkType = FourCC.Decode(ReadInt32()); if (chunkType == "RDEF") { ReadBytes(20); byte majorVersion = ReadByte(); byte minorVersion = ReadByte(); ShaderType shaderType = (ShaderType)ReadUInt16(); shader = new ShaderModel(minorVersion, majorVersion, shaderType); } else if (chunkType == "OSGN") { } else if (chunkType == "SHDR") { ReadBytes(8); int chunkSize = ReadInt32() * 4; long chunkEnd = BaseStream.Position + chunkSize - 8; while (BaseStream.Position < chunkEnd) { D3D10Instruction instruction = ReadInstruction(); InstructionVerifier.Verify(instruction); shader.Instructions.Add(instruction); } } } return(shader); }
private D3D10Instruction ReadInstruction() { uint opcodeToken = ReadUInt32(); D3D10Opcode opcode = (D3D10Opcode)(opcodeToken & 0x7FF); int operandCount = (int)((opcodeToken >> 24) & 0x7F) - 1; bool isExtended = (opcodeToken & 0x80000000) != 0; if (isExtended) { throw new NotImplementedException(); } uint[] operandTokens = new uint[operandCount]; for (int i = 0; i < operandCount; i++) { operandTokens[i] = ReadUInt32(); } var instruction = new D3D10Instruction(opcode, operandTokens); return(instruction); }
private static string GetSourceName(D3D10Instruction instruction, int srcIndex) { var operandType = instruction.GetOperandType(srcIndex); if (operandType == OperandType.Immediate32) { var componentSelection = instruction.GetOperandComponentSelection(srcIndex); if (componentSelection == D3D10OperandNumComponents.Operand1Component) { string immediate; if (instruction.Opcode == D3D10Opcode.Discard) { immediate = instruction.GetParamInt(srcIndex).ToString(); } else { immediate = ConstantFormatter.Format(instruction.GetParamSingle(srcIndex)); } return($"l({immediate})"); } else { string immediate0 = ConstantFormatter.Format(instruction.GetParamSingle(srcIndex, 0)); string immediate1 = ConstantFormatter.Format(instruction.GetParamSingle(srcIndex, 1)); string immediate2 = ConstantFormatter.Format(instruction.GetParamSingle(srcIndex, 2)); string immediate3 = ConstantFormatter.Format(instruction.GetParamSingle(srcIndex, 3)); return($"l({immediate0}, {immediate1}, {immediate2}, {immediate3})"); } } string sourceName = instruction.GetParamRegisterName(srcIndex); sourceName += instruction.GetSourceSwizzleName(srcIndex); sourceName = ApplyModifier(instruction.GetOperandModifier(srcIndex), sourceName); return(sourceName); }
private void WriteD3D10Instruction(D3D10Instruction instruction) { switch (instruction.Opcode) { case D3D10Opcode.Add: WriteLine("add {0}, {1}, {2}", GetDestinationName(instruction), GetSourceName(instruction, 1), GetSourceName(instruction, 2)); break; case D3D10Opcode.DclInputPS: WriteLine("dcl_input_ps {0} {1}", instruction.GetInterpolationModeName(), GetDestinationName(instruction)); break; case D3D10Opcode.DclInput: WriteLine("dcl_input {0}", GetDestinationName(instruction)); break; case D3D10Opcode.DclOutput: WriteLine("dcl_output {0}", GetDestinationName(instruction)); break; case D3D10Opcode.DclTemps: WriteLine("dcl_temps {0}", instruction.GetParamInt(0)); break; case D3D10Opcode.Discard: WriteLine("discard_nz {0}", GetSourceName(instruction, 0)); break; case D3D10Opcode.Dp2: WriteLine("dp2 {0}, {1}, {2}", GetDestinationName(instruction), GetSourceName(instruction, 1), GetSourceName(instruction, 2)); break; case D3D10Opcode.Dp3: WriteLine("dp3 {0}, {1}, {2}", GetDestinationName(instruction), GetSourceName(instruction, 1), GetSourceName(instruction, 2)); break; case D3D10Opcode.Dp4: WriteLine("dp4 {0}, {1}, {2}", GetDestinationName(instruction), GetSourceName(instruction, 1), GetSourceName(instruction, 2)); break; case D3D10Opcode.Mad: WriteLine("mad {0}, {1}, {2}, {3}", GetDestinationName(instruction), GetSourceName(instruction, 1), GetSourceName(instruction, 2), GetSourceName(instruction, 3)); break; case D3D10Opcode.Mov: WriteLine("mov {0}, {1}", GetDestinationName(instruction), GetSourceName(instruction, 1)); break; case D3D10Opcode.Mul: WriteLine("mul {0}, {1}, {2}", GetDestinationName(instruction), GetSourceName(instruction, 1), GetSourceName(instruction, 2)); break; case D3D10Opcode.Ret: WriteLine("ret"); break; case D3D10Opcode.Rsq: WriteLine("rsq {0}, {1}", GetDestinationName(instruction), GetSourceName(instruction, 1)); break; default: WriteLine(instruction.Opcode.ToString()); Console.WriteLine(instruction.Opcode); //throw new NotImplementedException(); break; } }