void ExportSerializedProgram(SerializedProgram serializedProgram, ShaderWriter writer, ShaderType type) { if (serializedProgram.SubPrograms.Count > 0) { writer.WriteIndent(3); writer.Write("Program \"{0}\" {{\n", type.ToProgramTypeString()); HashSet <ShaderGpuProgramType> isTierLookup = GetIsTierLookup(serializedProgram.SubPrograms); var subprograms = serializedProgram.SubPrograms; var best = serializedProgram.SubPrograms .OrderByDescending(subProgram => { GPUPlatform platform = subProgram.GpuProgramType.ToGPUPlatform(writer.Platform); int index = writer.Shader.Platforms.IndexOf(platform); ShaderSubProgramBlob blob = writer.Shader.SubProgramBlobs[index]; var sp = blob.SubPrograms[(int)subProgram.BlobIndex]; return(sp.ProgramData.Count); //return sp.GlobalKeywords.Sum(keyword => keyword.Contains("ON") ? 2 : 1); }).FirstOrDefault(); if (FilterSubPrograms) { subprograms = new SerializedSubProgram[] { best }; } foreach (SerializedSubProgram subProgram in subprograms) { Platform uplatform = writer.Platform; GPUPlatform platform = subProgram.GpuProgramType.ToGPUPlatform(uplatform); int index = writer.Shader.Platforms.IndexOf(platform); ShaderSubProgramBlob blob = writer.Shader.SubProgramBlobs[index]; bool isTier = isTierLookup.Contains(subProgram.GpuProgramType); ExportSerializedSubProgram(subProgram, writer, blob, type, isTier, best.BlobIndex == subProgram.BlobIndex); } writer.WriteIndent(3); writer.Write("}\n"); } }
public static void FixShaderSubProgram(ShaderSubProgram shaderSubProgram, SerializedSubProgram serializedSubProgram) { /* Note: NameIndex isn't set on ShaderSubProgram parameters, Name is not set on * SerializedSubProgram's ConstantBuffer and parameters * Is NameIndex even needed? TODO: delete this later */ var bindingFlags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance; var contantBuffers = (ConstantBuffer[])typeof(ShaderSubProgram) .GetField("m_constantBuffers", bindingFlags) .GetValue(shaderSubProgram); for (int i = 0; i < contantBuffers.Length; i++) { { object boxed = contantBuffers[i]; var nameIndex = serializedSubProgram.ConstantBuffers[i].NameIndex; typeof(ConstantBuffer) .GetProperty("NameIndex", bindingFlags) .SetValue(boxed, nameIndex); contantBuffers[i] = (ConstantBuffer)boxed; } var constantBuffer = contantBuffers[i]; var matrixParams = (MatrixParameter[])typeof(ConstantBuffer) .GetField("m_matrixParams", bindingFlags) .GetValue(constantBuffer); var vectorParams = (VectorParameter[])typeof(ConstantBuffer) .GetField("m_vectorParams", bindingFlags) .GetValue(constantBuffer); for (int j = 0; j < matrixParams.Length; j++) { var serializedParam = serializedSubProgram.ConstantBuffers[i].MatrixParams[j]; object boxed = matrixParams[j]; var nameIndex = serializedParam.NameIndex; typeof(MatrixParameter) .GetProperty("NameIndex", bindingFlags) .SetValue(boxed, nameIndex); matrixParams[j] = (MatrixParameter)boxed; var shaderParam = matrixParams[j]; if (shaderParam.Name == null) throw new Exception("Name is null"); if (shaderParam.Index < 0) throw new Exception("Index is out of bounds"); if (shaderParam.NameIndex < 0) throw new Exception("NameIndex is out of bounds"); } for (int j = 0; j < constantBuffer.VectorParams.Count; j++) { var serializedParam = serializedSubProgram.ConstantBuffers[i].VectorParams[j]; object boxed = vectorParams[j]; var nameIndex = serializedParam.NameIndex; typeof(VectorParameter) .GetProperty("NameIndex", bindingFlags) .SetValue(boxed, nameIndex); vectorParams[j] = (VectorParameter)boxed; var shaderParam = vectorParams[j]; if (shaderParam.Name == null) throw new Exception("Name is null"); if (shaderParam.Index < 0) throw new Exception("Index is out of bounds"); if (shaderParam.NameIndex < 0) throw new Exception("NameIndex is out of bounds"); } } }
public void Export(ShaderWriter writer, ShaderType type) { writer.WriteIndent(3); writer.Write("Program \"{0}\" {{\n", type.ToProgramTypeString()); int tierCount = GetTierCount(SubPrograms); for (int i = 0; i < SubPrograms.Length; i++) { SerializedSubProgram subProgram = SubPrograms[i]; Platform uplatform = writer.Platform; GPUPlatform platform = subProgram.GpuProgramType.ToGPUPlatform(uplatform); int index = writer.Shader.Platforms.IndexOf(platform); ref ShaderSubProgramBlob blob = ref writer.Shader.SubProgramBlobs[index]; subProgram.Export(writer, ref blob, type, tierCount > 1); }
void ExportSerializedSubProgram(SerializedSubProgram subProgram, ShaderWriter writer, ShaderSubProgramBlob blob, ShaderType type, bool isTier) { writer.WriteIndent(4); writer.Write("SubProgram \"{0} ", subProgram.GpuProgramType.ToGPUPlatform(writer.Platform)); if (isTier) { writer.Write("hw_tier{0} ", subProgram.ShaderHardwareTier.ToString("00")); } writer.Write("\" {\n"); writer.WriteIndent(5); var shaderSubProgram = blob.SubPrograms[(int)subProgram.BlobIndex]; string hash = Hash(shaderSubProgram.ProgramData.ToArray()); var filesteam = writer.BaseStream as FileStream; var folder = Path.GetDirectoryName(filesteam.Name); ExportShaderSubProgram(shaderSubProgram, writer, type); writer.Write('\n'); writer.WriteIndent(4); writer.Write("}\n"); }