From 151d61e05f447080f3712d0750432bf9962f01d1 Mon Sep 17 00:00:00 2001 From: Pedro413 <44695680+Pedro413@users.noreply.github.com> Date: Thu, 23 May 2024 13:10:14 +1000 Subject: [PATCH] remove unused shader porting code --- .../Commands/Porting/PortTagCommand.Shader.cs | 1095 +---------------- TagTool/Commands/Porting/PortTagCommand.cs | 8 +- .../ShaderMatching/ShaderMatcherNew.cs | 107 -- 3 files changed, 5 insertions(+), 1205 deletions(-) diff --git a/TagTool/Commands/Porting/PortTagCommand.Shader.cs b/TagTool/Commands/Porting/PortTagCommand.Shader.cs index 28b89179..7fd1b2b4 100644 --- a/TagTool/Commands/Porting/PortTagCommand.Shader.cs +++ b/TagTool/Commands/Porting/PortTagCommand.Shader.cs @@ -1,18 +1,9 @@ using TagTool.Cache; -using TagTool.Commands.Common; using TagTool.Common; using TagTool.Tags.Definitions; -using System.Collections.Generic; using System.IO; -using TagTool.Shaders; using TagTool.Shaders.ShaderMatching; using System; -using System.Linq; -using TagTool.Shaders.ShaderFunctions; -using static TagTool.Tags.Definitions.RenderMethod; -using static TagTool.Tags.Definitions.RenderMethodOption; -using static TagTool.Tags.Definitions.RenderMethod.RenderMethodPostprocessBlock; -using TagTool.Shaders.ShaderGenerator; using TagTool.Shaders.ShaderConverter; namespace TagTool.Commands.Porting @@ -21,29 +12,6 @@ partial class PortTagCommand { public ShaderMatcherNew Matcher = new ShaderMatcherNew(); - private List emblemTagNames = new List - { - @"objects\characters\odst\shaders\mc_emblem", - @"objects\characters\odst_cine\shaders\mc_emblem", - @"objects\characters\odst_oni_op\shaders\mc_emblem", - @"objects\characters\elite\shaders\elite_emblem", - @"objects\characters\masterchief\shaders\mc_emblem" - }; - - private readonly List RealConstantToBoolConstant = new List - { - "use_material_texture", - }; - - private readonly List EffectRenderMethodTypes = new List - { - "decal", - "contrail", - "particle", - "beam", - "light_volume", - }; - private RasterizerGlobals ConvertRasterizerGlobals(RasterizerGlobals rasg) { if (BlamCache.Version == CacheVersion.Halo3ODST) @@ -126,7 +94,7 @@ private RenderMethod ConvertShaderInternal(Stream cacheStream, Stream blamCacheS return ConvertRenderMethod(cacheStream, blamCacheStream, definition, blamDefinition, blamShaderProperty.Template, blamTag); } - private CachedTag GetDefaultShader(Tag groupTag, CachedTag edTag) + private CachedTag GetDefaultShader(Tag groupTag) { CachedTag shaderTag; @@ -213,1067 +181,6 @@ private RenderMethod ConvertRenderMethod(Stream cacheStream, Stream blamCacheStr finalRm.PredictionAtomIndex = newRm.PredictionAtomIndex; return finalRm; - - var bmMaps = new List(); - var bmRealConstants = new List(); - var bmIntConstants = new List(); - var bmBoolConstants = new List(); - var edMaps = new List(); - var edRealConstants = new List(); - var edIntConstants = new List(); - var edBoolConstants = new List(); - - // Make a template of ShaderProperty, with the correct bitmaps and arguments counts. - var newShaderProperty = new RenderMethod.RenderMethodPostprocessBlock - { - TextureConstants = new List(), - RealConstants = new List(), - IntegerConstants = new List(), - BooleanConstants = 0 - }; - - RenderMethod originalRm = finalRm; - - // convert filter mode - if (BlamCache.Version == CacheVersion.Halo3ODST || BlamCache.Version >= CacheVersion.HaloReach) - { - foreach (var textureConstant in originalRm.ShaderProperties[0].TextureConstants) - textureConstant.FilterMode = textureConstant.FilterModePacked.FilterMode; - } - - // Get a simple list of bitmaps and arguments names - var bmRmt2Instance = blamRmt2; - var bmRmt2 = BlamCache.Deserialize(blamCacheStream, bmRmt2Instance); - - // Get a simple list of H3 bitmaps and arguments names - foreach (var a in bmRmt2.TextureParameterNames) - bmMaps.Add(BlamCache.StringTable.GetString(a.Name)); - foreach (var a in bmRmt2.RealParameterNames) - bmRealConstants.Add(BlamCache.StringTable.GetString(a.Name)); - foreach (var a in bmRmt2.IntegerParameterNames) - bmIntConstants.Add(BlamCache.StringTable.GetString(a.Name)); - foreach (var a in bmRmt2.BooleanParameterNames) - bmBoolConstants.Add(BlamCache.StringTable.GetString(a.Name)); - - // get template previously matched from porttag rmt2 - CachedTag edRmt2Instance = finalRm.ShaderProperties[0].Template; - - if (edRmt2Instance == null) - { - throw new Exception($"Failed to find HO rmt2 for this RenderMethod instance"); - } - - // create blam rmt2 descriptor - ShaderMatcherNew.Rmt2Descriptor.TryParse(bmRmt2Instance.Name, out ShaderMatcherNew.Rmt2Descriptor blamRmt2Descriptor); - // create ed rmt2 descriptor - ShaderMatcherNew.Rmt2Descriptor.TryParse(edRmt2Instance.Name, out ShaderMatcherNew.Rmt2Descriptor edRmt2Descriptor); - - // get relevant rmdf - CachedTag rmdfInstance = Matcher.FindRmdf(edRmt2Instance); - if (rmdfInstance == null) // shader matching will fail without an rmdf -- throw an exception - throw new Exception($"Unable to find valid \"{edRmt2Descriptor.Type}\" rmdf for rmt2"); - - finalRm.BaseRenderMethod = rmdfInstance; - - // build new rm option indices - finalRm.Options = BuildRenderMethodOptionIndices(edRmt2Descriptor); - - // black has no options, skip conversion - if (edRmt2Descriptor.Type == "black") - return finalRm; - - var edRmt2 = CacheContext.Deserialize(cacheStream, edRmt2Instance); - - foreach (var a in edRmt2.TextureParameterNames) - edMaps.Add(CacheContext.StringTable.GetString(a.Name)); - foreach (var a in edRmt2.RealParameterNames) - edRealConstants.Add(CacheContext.StringTable.GetString(a.Name)); - foreach (var a in edRmt2.IntegerParameterNames) - edIntConstants.Add(CacheContext.StringTable.GetString(a.Name)); - foreach (var a in edRmt2.BooleanParameterNames) - edBoolConstants.Add(CacheContext.StringTable.GetString(a.Name)); - - RenderMethodDefinition rmdf = CacheContext.Deserialize(cacheStream, rmdfInstance); - - // dictionaries for fast lookup - //var optionParameters = Matcher.GetOptionParameters(rmt2Descriptor.Options.ToList(), renderMethodDefinition); - var optionBlocks = Matcher.GetOptionBlocks(edRmt2Descriptor.Options.ToList(), rmdf); - var optionBitmaps = Matcher.GetOptionBitmaps(edRmt2Descriptor.Options.ToList(), rmdf); - - List methodNames = new List(); - foreach (var method in rmdf.Categories) - methodNames.Add(CacheContext.StringTable.GetString(method.Name)); - - foreach (var a in edRealConstants) - newShaderProperty.RealConstants.Add(GetDefaultRealConstant(a, edRmt2Descriptor.Type, methodNames, optionBlocks)); - - foreach (var a in edIntConstants) - newShaderProperty.IntegerConstants.Add(GetDefaultValue(a, edRmt2Descriptor.Type, methodNames, optionBlocks)); - - foreach (var a in edMaps) - newShaderProperty.TextureConstants.Add(GetDefaultTextureConstant(a, edRmt2Descriptor, optionBitmaps)); - - // if we have bits enabled by default, this actually disables boolean args. fixes a few visual issues - for (int a = 0; a < edRmt2.BooleanParameterNames.Count; a++) - newShaderProperty.BooleanConstants |= (1u << a); - // newShaderProperty.BooleanConstants |= (GetDefaultValue(CacheContext.StringTable.GetString(edRmt2.BooleanParameterNames[a].Name), edRmt2Descriptor.Type, methodNames, optionBlocks) << a); - - // apply option->option conversion where applicable - ApplyDefaultOptionFixups(newShaderProperty, originalRm.ShaderProperties[0], blamRmt2Descriptor, edRmt2Descriptor, edRmt2, bmRmt2, rmdf); - - // Reorder blam bitmaps to match the HO rmt2 order - // Reorder blam real constants to match the HO rmt2 order - // Reorder blam int constants to match the HO rmt2 order - // Reorder blam bool constants to match the HO rmt2 order - foreach (var eM in edMaps) - foreach (var bM in bmMaps) - if (eM == bM) - newShaderProperty.TextureConstants[edMaps.IndexOf(eM)] = finalRm.ShaderProperties[0].TextureConstants[bmMaps.IndexOf(bM)]; - - foreach (var eA in edRealConstants) - { - bool found = false; - foreach (var bA in bmRealConstants) - { - if (eA == bA) - { - newShaderProperty.RealConstants[edRealConstants.IndexOf(eA)] = finalRm.ShaderProperties[0].RealConstants[bmRealConstants.IndexOf(bA)]; - found = true; - break; - } - } - - if (!found) // search for bool - { - foreach (var bA in bmBoolConstants) - { - if (eA == bA) - { - float boolconst = (finalRm.ShaderProperties[0].BooleanConstants & (1u << bmBoolConstants.IndexOf(bA))) == 0 ? 0.0f : 1.0f; - newShaderProperty.RealConstants[edRealConstants.IndexOf(eA)] = new RealConstant { Arg0 = boolconst }; - found = true; - break; - } - } - } - - if (!found) // search for int - { - foreach (var bA in bmIntConstants) - { - if (eA == bA) - { - float intConst = (float)finalRm.ShaderProperties[0].IntegerConstants[bmIntConstants.IndexOf(bA)]; - newShaderProperty.RealConstants[edRealConstants.IndexOf(eA)] = new RealConstant { Arg0 = intConst }; - found = true; - break; - } - } - } - } - - foreach (var eA in edIntConstants) - foreach (var bA in bmIntConstants) - if (eA == bA) - newShaderProperty.IntegerConstants[edIntConstants.IndexOf(eA)] = finalRm.ShaderProperties[0].IntegerConstants[bmIntConstants.IndexOf(bA)]; - - foreach (var eA in edBoolConstants) - { - if (RealConstantToBoolConstant.Contains(eA) && bmRealConstants.Contains(eA)) - { - if (finalRm.ShaderProperties[0].RealConstants[bmRealConstants.IndexOf(eA)].Arg0 == 0.0f) - newShaderProperty.BooleanConstants &= ~(1u << edBoolConstants.IndexOf(eA)); - } - else - { - foreach (var bA in bmBoolConstants) - if (eA == bA) - { - if ((finalRm.ShaderProperties[0].BooleanConstants & (1u << bmBoolConstants.IndexOf(bA))) == 0) - newShaderProperty.BooleanConstants &= ~(1u << edBoolConstants.IndexOf(eA)); - } - } - } - - newShaderProperty.BlendMode = finalRm.ShaderProperties[0].BlendMode; - newShaderProperty.Flags = finalRm.ShaderProperties[0].Flags; - - // We now disable ATOC completely, later games have conversion issues - - //// Check for ATOC materials and flag accordingly -- - //// ATOC materials changed in ODST. We use a base of H3, so we need to re-enable the ATOC flag where necessary. - //if (rmdf.ContainsCategory(CacheContext, "alpha_test") && - // !finalRm.CategoryOptionSelected(CacheContext, rmdf, "alpha_test", "none") && - // !finalRm.CategoryOptionSelected(CacheContext, rmdf, "material_model", "cook_torrance") && - // !finalRm.CategoryOptionSelected(CacheContext, rmdf, "material_model", "two_lobe_phong") && - // !finalRm.CategoryOptionSelected(CacheContext, rmdf, "material_model", "default_skin") && - // !finalRm.CategoryOptionSelected(CacheContext, rmdf, "material_model", "glass") && - // !finalRm.CategoryOptionSelected(CacheContext, rmdf, "material_model", "organism")) - // newShaderProperty.Flags |= RenderMethodPostprocessFlags.EnableAlphaTest; - //else - newShaderProperty.Flags &= ~RenderMethodPostprocessFlags.EnableAlphaTest; - - // Flag single pass accordingly (this should already be correct) - if (rmdf.ContainsCategory(CacheContext, "misc") && - (finalRm.CategoryOptionSelected(CacheContext, rmdf, "misc", "first_person_always") || - finalRm.CategoryOptionSelected(CacheContext, rmdf, "misc", "first_person_never"))) - newShaderProperty.Flags |= RenderMethodPostprocessFlags.ForceSinglePass; - else - newShaderProperty.Flags &= ~RenderMethodPostprocessFlags.ForceSinglePass; - - // apply post option->options fixups - ApplyPostOptionFixups(newShaderProperty, originalRm.ShaderProperties[0], blamRmt2Descriptor, edRmt2Descriptor, edRmt2, bmRmt2, rmdf); - - // hackfix for cook_torrance -> cook_torrance_rim_fresnel (ms23) - // in rim_fresnel material parameters are multiplied against the material texture, we want to negate this behaviour - // with new odst cook_torrance this can be removed in shader code - if (BlamCache.Version != CacheVersion.Halo3ODST && - edRmt2Descriptor.Type == "shader" && - blamRmt2Descriptor.Options[4] == 1 && - edRmt2Descriptor.Options[4] == 1) - { - bool using_material_texture = false; - for (int i = 0; i < bmRmt2.BooleanParameterNames.Count; i++) - { - if (BlamCache.StringTable.GetString(bmRmt2.BooleanParameterNames[i].Name) == "use_material_texture") - { - if ((originalRm.ShaderProperties[0].BooleanConstants & (1 << i)) == 1) - using_material_texture = true; - } - } - for (int i = 0; i < bmRmt2.RealParameterNames.Count; i++) - { - if (BlamCache.StringTable.GetString(bmRmt2.RealParameterNames[i].Name) == "use_material_texture") - { - if (originalRm.ShaderProperties[0].RealConstants[i].Arg0 != 0.0f) - using_material_texture = true; - } - } - - if (using_material_texture) - { - for (int i = 0; i < edRmt2.RealParameterNames.Count; i++) - { - switch (CacheContext.StringTable.GetString(edRmt2.RealParameterNames[i].Name)) - { - case "specular_coefficient": - case "environment_map_specular_contribution": - case "albedo_blend": - case "roughness": - newShaderProperty.RealConstants[i].Arg0 = 1.0f; - break; - } - } - } - } - - finalRm.Parameters = new List(); // most likely not used - finalRm.ShaderProperties[0].Template = edRmt2Instance; - finalRm.ShaderProperties[0].TextureConstants = newShaderProperty.TextureConstants; - finalRm.ShaderProperties[0].RealConstants = newShaderProperty.RealConstants; - finalRm.ShaderProperties[0].IntegerConstants = newShaderProperty.IntegerConstants; - finalRm.ShaderProperties[0].BooleanConstants = newShaderProperty.BooleanConstants; - finalRm.ShaderProperties[0].BlendMode = newShaderProperty.BlendMode; - finalRm.ShaderProperties[0].Flags = newShaderProperty.Flags; - - // fixup runtime queryable properties - if (BlamCache.Version < CacheVersion.HaloReach) - { - for (int i = 0; i < finalRm.ShaderProperties[0].QueryableProperties.Length; i++) - { - if (finalRm.ShaderProperties[0].QueryableProperties[i] == -1) - continue; - - switch (i) - { - case 0: - case 1: - case 2: - case 3: - case 5: - finalRm.ShaderProperties[0].QueryableProperties[i] = (short)edMaps.IndexOf(bmMaps[finalRm.ShaderProperties[0].QueryableProperties[i]]); - break; - case 4: - finalRm.ShaderProperties[0].QueryableProperties[i] = (short)edRealConstants.IndexOf(bmRealConstants[finalRm.ShaderProperties[0].QueryableProperties[i]]); - break; - default: - finalRm.ShaderProperties[0].QueryableProperties[i] = -1; - break; - } - } - } - else if (BlamCache.Version >= CacheVersion.HaloReach) - { - finalRm.ShaderProperties[0].QueryableProperties = new short[8]; - - for (int i = 0; i < finalRm.ShaderProperties[0].QueryableProperties.Length; i++) - { - if (finalRm.ShaderProperties[0].QueryablePropertiesReach[i] == -1) - { - finalRm.ShaderProperties[0].QueryableProperties[i] = -1; - continue; - } - - switch (i) - { - case 0: - case 1: - case 2: - case 3: - case 5: - finalRm.ShaderProperties[0].QueryableProperties[i] = (short)edMaps.IndexOf(bmMaps[finalRm.ShaderProperties[0].QueryablePropertiesReach[i]]); - break; - case 4: - finalRm.ShaderProperties[0].QueryableProperties[i] = (short)edRealConstants.IndexOf(bmRealConstants[finalRm.ShaderProperties[0].QueryablePropertiesReach[i]]); - break; - default: - finalRm.ShaderProperties[0].QueryableProperties[i] = -1; - break; - } - } - } - - // fixup xform arguments; - foreach (var tex in finalRm.ShaderProperties[0].TextureConstants) - { - if (tex.TextureTransformConstantIndex != -1) - tex.TextureTransformConstantIndex = (sbyte)edRealConstants.IndexOf(bmRealConstants[tex.TextureTransformConstantIndex]); - } - - // fixup rm animations - if (finalRm.ShaderProperties[0].Functions.Count > 0) - { - RebuildRenderMethodAnimationsFromRmt2(finalRm, blamRm, edRmt2, bmRmt2); - } - - return finalRm; - } - - private List> MatchPixelRegisters(PixelShader externalPixl, PixelShader basePixl, GlobalPixelShader externalGlps, GlobalPixelShader baseGlps, byte[] bmOptions, byte[] edOptions) - { - List> pixelRegisters = new List>(); - - for (int entryPoint = 0; entryPoint < externalPixl.EntryPointShaders.Count; entryPoint++) - { - Dictionary entryPointRegisters = new Dictionary(); - - if (basePixl.EntryPointShaders.Count <= entryPoint || baseGlps.EntryPoints.Count <= entryPoint) - { - new TagToolWarning($"Pixel entrypoint does not match up with external ({((EntryPoint)entryPoint).ToString()})"); - break; - } - - // glps first - int bmGlpsIndex = -1; - int edGlpsIndex = -1; - - if (externalGlps.EntryPoints[entryPoint].CategoryDependency.Count > 0 && baseGlps.EntryPoints[entryPoint].CategoryDependency.Count > 0) - { - bmGlpsIndex = externalGlps.EntryPoints[entryPoint].CategoryDependency[0].OptionDependency[bmOptions[externalGlps.EntryPoints[entryPoint].CategoryDependency[0].DefinitionCategoryIndex]].CompiledShaderIndex; - edGlpsIndex = baseGlps.EntryPoints[entryPoint].CategoryDependency[0].OptionDependency[edOptions[baseGlps.EntryPoints[entryPoint].CategoryDependency[0].DefinitionCategoryIndex]].CompiledShaderIndex; - } - else if (externalGlps.EntryPoints[entryPoint].DefaultCompiledShaderIndex > -1 && baseGlps.EntryPoints[entryPoint].DefaultCompiledShaderIndex > -1) - { - bmGlpsIndex = externalGlps.EntryPoints[entryPoint].DefaultCompiledShaderIndex; - edGlpsIndex = baseGlps.EntryPoints[entryPoint].DefaultCompiledShaderIndex; - } - - if (bmGlpsIndex != -1 && edGlpsIndex != -1) - { - foreach (var xboxParameter in externalGlps.Shaders[bmGlpsIndex].GetConstantTable(BlamCache.Version, BlamCache.Platform).Constants) - { - RegisterID registerID = new RegisterID(xboxParameter.RegisterIndex, xboxParameter.RegisterType); - - if (entryPointRegisters.ContainsKey(registerID)) - continue; - - string xboxParameterName = BlamCache.StringTable.GetString(xboxParameter.ParameterName); - - foreach (var pcParameter in baseGlps.Shaders[edGlpsIndex].GetConstantTable(CacheContext.Version, CacheContext.Platform).Constants) - { - string pcParameterName = CacheContext.StringTable.GetString(pcParameter.ParameterName); - - if (xboxParameterName == pcParameterName && xboxParameter.RegisterType == pcParameter.RegisterType) - { - entryPointRegisters.Add(registerID, pcParameter.RegisterIndex); - break; - } - } - } - } - - // pixl - for (int i = externalPixl.EntryPointShaders[entryPoint].Offset; i < externalPixl.EntryPointShaders[entryPoint].Offset + externalPixl.EntryPointShaders[entryPoint].Count; i++) - { - foreach (var xboxParameter in externalPixl.Shaders[i].GetConstantTable(BlamCache.Version, BlamCache.Platform).Constants) - { - RegisterID registerID = new RegisterID(xboxParameter.RegisterIndex, xboxParameter.RegisterType); - - if (entryPointRegisters.ContainsKey(registerID)) - continue; - - string xboxParameterName = BlamCache.StringTable.GetString(xboxParameter.ParameterName); - - for (int j = basePixl.EntryPointShaders[entryPoint].Offset; j < basePixl.EntryPointShaders[entryPoint].Offset + basePixl.EntryPointShaders[entryPoint].Count; j++) - { - bool parameterFound = false; - - foreach (var pcParameter in basePixl.Shaders[j].GetConstantTable(CacheContext.Version, CacheContext.Platform).Constants) - { - string pcParameterName = CacheContext.StringTable.GetString(pcParameter.ParameterName); - - if (xboxParameterName == pcParameterName && xboxParameter.RegisterType == pcParameter.RegisterType) - { - parameterFound = true; - entryPointRegisters.Add(registerID, pcParameter.RegisterIndex); - break; - } - } - - if (parameterFound) - break; - } - } - } - - pixelRegisters.Add(entryPointRegisters); - } - - return pixelRegisters; - } - - private List> MatchVertexRegisters(GlobalVertexShader externalGlvs, GlobalVertexShader baseGlvs, List validVertexTypes) - { - List> vertexRegisters = new List>(); - - int entryPointCount = externalGlvs.VertexTypes[0].EntryPoints.Count; - - while (vertexRegisters.Count != entryPointCount) - vertexRegisters.Add(new Dictionary()); - - foreach (var validVertexType in validVertexTypes) - { - for (int i = 0; i < entryPointCount; i++) - { - if (externalGlvs.VertexTypes[validVertexType].EntryPoints[i].ShaderIndex == -1) - continue; - if (baseGlvs.VertexTypes[validVertexType].EntryPoints[i].ShaderIndex == -1) - { - new TagToolWarning($"Invalid vertex shader index \"{((TagTool.Geometry.VertexType)validVertexType).ToString()}, {((EntryPoint)i).ToString()}\""); - continue; - } - - foreach (var xboxParameter in externalGlvs.Shaders[externalGlvs.VertexTypes[validVertexType].EntryPoints[i].ShaderIndex].GetConstantTable(BlamCache.Version, BlamCache.Platform).Constants) - { - RegisterID registerID = new RegisterID(xboxParameter.RegisterIndex, xboxParameter.RegisterType); - - if (vertexRegisters[i].ContainsKey(registerID)) - continue; - - string xboxParameterName = BlamCache.StringTable.GetString(xboxParameter.ParameterName); - - foreach (var pcParameter in baseGlvs.Shaders[baseGlvs.VertexTypes[validVertexType].EntryPoints[i].ShaderIndex].GetConstantTable(CacheContext.Version, CacheContext.Platform).Constants) - { - string pcParameterName = CacheContext.StringTable.GetString(pcParameter.ParameterName); - - if (xboxParameterName == pcParameterName && xboxParameter.RegisterType == pcParameter.RegisterType) - { - vertexRegisters[i].Add(registerID, pcParameter.RegisterIndex); - break; - } - } - } - } - } - - return vertexRegisters; - } - - private bool TableParameterAlreadyExists(TagBlockIndex tableInteger, List tableParameters, RenderMethodRoutingInfoBlock parameter) - { - for (int i = tableInteger.Offset; i < tableInteger.Offset + tableInteger.Count; i++) - if (tableParameters[i].RegisterIndex == parameter.RegisterIndex && - tableParameters[i].FunctionIndex == parameter.FunctionIndex && - tableParameters[i].SourceIndex == parameter.SourceIndex) - return true; - - return false; - } - - private void RebuildRenderMethodAnimations(Stream cacheStream, Stream blamCacheStream, RenderMethod finalRm, RenderMethodTemplate edRmt2, RenderMethodTemplate bmRmt2, CachedTag rmdfTag, byte[] bmOptions, byte[] edOptions) - { - // - // Store shader definitions - // - - CachedTag blamRmdfTag = BlamCache.TagCache.GetTag(rmdfTag.Name.Replace("ms30\\", "") + ".rmdf"); - - RenderMethodDefinition blamRmdf = BlamCache.Deserialize(blamCacheStream, blamRmdfTag); - GlobalVertexShader blamGlvs = BlamCache.Deserialize(blamCacheStream, blamRmdf.GlobalVertexShader); - GlobalPixelShader blamGlps = BlamCache.Deserialize(blamCacheStream, blamRmdf.GlobalPixelShader); - PixelShader blamPixl = BlamCache.Deserialize(blamCacheStream, bmRmt2.PixelShader); - - RenderMethodDefinition rmdf = CacheContext.Deserialize(cacheStream, rmdfTag); - GlobalVertexShader glvs = CacheContext.Deserialize(cacheStream, rmdf.GlobalVertexShader); - GlobalPixelShader glps = CacheContext.Deserialize(cacheStream, rmdf.GlobalPixelShader); - PixelShader pixl = CacheContext.Deserialize(cacheStream, edRmt2.PixelShader); - - // get valid vertex types - List validVertexTypes = new List(); - foreach (var vertex in blamRmdf.VertexTypes) - if (!validVertexTypes.Contains((int)vertex.VertexType)) - validVertexTypes.Add((int)vertex.VertexType); - - // EntryPoints<> - List> vertexRegisters = MatchVertexRegisters(blamGlvs, glvs, validVertexTypes); - List> pixelRegisters = MatchPixelRegisters(blamPixl, pixl, blamGlps, glps, bmOptions, edOptions); - - // - Dictionary newRealConstantIndices = new Dictionary(); - Dictionary newTextureConstantIndices = new Dictionary(); - - for (int i = 0; i < bmRmt2.RealParameterNames.Count; i++) - for (int j = 0; j < edRmt2.RealParameterNames.Count; j++) - if (BlamCache.StringTable.GetString(bmRmt2.RealParameterNames[i].Name) == CacheContext.StringTable.GetString(edRmt2.RealParameterNames[j].Name)) - { - newRealConstantIndices.Add(i, j); - break; - } - - for (int i = 0; i < bmRmt2.TextureParameterNames.Count; i++) - for (int j = 0; j < edRmt2.TextureParameterNames.Count; j++) - if (BlamCache.StringTable.GetString(bmRmt2.TextureParameterNames[i].Name) == CacheContext.StringTable.GetString(edRmt2.TextureParameterNames[j].Name)) - { - newTextureConstantIndices.Add(i, j); - break; - } - - // - // Collect parameters per entry point - // - - // EntryPoints> - List> tableUsedSamplerParameters = new List>(); - List> tableUsedVertexParameters = new List>(); - List> tableUsedPixelParameters = new List>(); - - foreach (var entryPoint in finalRm.ShaderProperties[0].EntryPoints) - { - List usedSamplerParameters = new List(); - List usedVertexParameters = new List(); - List usedPixelParameters = new List(); - - for (int tableIndex = entryPoint.Offset; tableIndex < entryPoint.Offset + entryPoint.Count; tableIndex++) - { - var table = finalRm.ShaderProperties[0].Passes[tableIndex]; - - for (int i = table.Texture.Offset; i < table.Texture.Offset + table.Texture.Count; i++) - usedSamplerParameters.Add(finalRm.ShaderProperties[0].RoutingInfo[i]); - - for (int i = table.RealVertex.Offset; i < table.RealVertex.Offset + table.RealVertex.Count; i++) - usedVertexParameters.Add(finalRm.ShaderProperties[0].RoutingInfo[i]); - - for (int i = table.RealPixel.Offset; i < table.RealPixel.Offset + table.RealPixel.Count; i++) - usedPixelParameters.Add(finalRm.ShaderProperties[0].RoutingInfo[i]); - } - - tableUsedSamplerParameters.Add(usedSamplerParameters); - tableUsedVertexParameters.Add(usedVertexParameters); - tableUsedPixelParameters.Add(usedPixelParameters); - } - - // - // Build parameters - // - - // TODO: fix this code with new knowledge of ms30 entry points - - // collect new entry point indices (saber crap) - List newEntryPoints = new List(); - switch (rmdfTag.Name) - { - // ms30 custom should be here too but their templates werent converted properly... - case @"shaders\shader": - case @"ms30\shaders\shader": - case @"ms30\shaders\halogram": - newEntryPoints = new List { 18, 19 }; - break; - case @"ms30\shaders\water": - newEntryPoints = new List { 16, 17, 18 }; - break; - } - - finalRm.ShaderProperties[0].EntryPoints.Clear(); - finalRm.ShaderProperties[0].Passes.Clear(); - finalRm.ShaderProperties[0].RoutingInfo.Clear(); - - while (finalRm.ShaderProperties[0].EntryPoints.Count != edRmt2.EntryPoints.Count) - finalRm.ShaderProperties[0].EntryPoints.Add(new TagBlockIndex()); - while (finalRm.ShaderProperties[0].Passes.Count != edRmt2.Passes.Count) - finalRm.ShaderProperties[0].Passes.Add(new RenderMethodPostprocessPassBlock()); - - int skipInt = 0; - - for (int epIndex = 0; epIndex < edRmt2.EntryPoints.Count; epIndex++) - { - finalRm.ShaderProperties[0].EntryPoints[epIndex] = edRmt2.EntryPoints[epIndex]; - - if (newEntryPoints.Contains(epIndex)) - { - skipInt++; - continue; - } - - // the loops ran before dont account for new entry points, this corrects the index - int listIndex = epIndex - skipInt; - - var samplerParameters = tableUsedSamplerParameters[listIndex]; - var vertexParameters = tableUsedVertexParameters[listIndex]; - var pixelParameters = tableUsedPixelParameters[listIndex]; - - int tableCount = finalRm.ShaderProperties[0].EntryPoints[epIndex].Offset + finalRm.ShaderProperties[0].EntryPoints[epIndex].Count; - - for (int tableIndex = finalRm.ShaderProperties[0].EntryPoints[epIndex].Offset; tableIndex < tableCount; tableIndex++) - { - if (samplerParameters.Count > 0) - { - finalRm.ShaderProperties[0].Passes[tableIndex].Texture.Offset = (ushort)finalRm.ShaderProperties[0].RoutingInfo.Count; - - foreach (var samplerParameter in samplerParameters) - { - // adjust sampler (pixl) register and source index (texture constant) - - RegisterID registerID = new RegisterID(samplerParameter.RegisterIndex, ShaderParameter.RType.Sampler); - if (!pixelRegisters[epIndex].ContainsKey(registerID) || !newTextureConstantIndices.ContainsKey(samplerParameter.SourceIndex)) - continue; - - samplerParameter.RegisterIndex = (short)pixelRegisters[epIndex][registerID]; - samplerParameter.SourceIndex = (byte)newTextureConstantIndices[samplerParameter.SourceIndex]; - - if (!TableParameterAlreadyExists(finalRm.ShaderProperties[0].Passes[tableIndex].Texture, finalRm.ShaderProperties[0].RoutingInfo, samplerParameter)) - { - finalRm.ShaderProperties[0].RoutingInfo.Add(samplerParameter); - finalRm.ShaderProperties[0].Passes[tableIndex].Texture.Count++; - } - } - } - - if (vertexParameters.Count > 0) - { - finalRm.ShaderProperties[0].Passes[tableIndex].RealVertex.Offset = (ushort)finalRm.ShaderProperties[0].RoutingInfo.Count; - - foreach (var vertexParameter in vertexParameters) - { - // adjust vertex register and source index (real constant) - - RegisterID registerID = new RegisterID(vertexParameter.RegisterIndex, ShaderParameter.RType.Vector); - if (!vertexRegisters[epIndex].ContainsKey(registerID) || !newRealConstantIndices.ContainsKey(vertexParameter.SourceIndex)) - continue; - - vertexParameter.RegisterIndex = (short)vertexRegisters[epIndex][registerID]; - vertexParameter.SourceIndex = (byte)newRealConstantIndices[vertexParameter.SourceIndex]; - - if (!TableParameterAlreadyExists(finalRm.ShaderProperties[0].Passes[tableIndex].RealVertex, finalRm.ShaderProperties[0].RoutingInfo, vertexParameter)) - { - finalRm.ShaderProperties[0].RoutingInfo.Add(vertexParameter); - finalRm.ShaderProperties[0].Passes[tableIndex].RealVertex.Count++; - } - } - } - - if (pixelParameters.Count > 0) - { - finalRm.ShaderProperties[0].Passes[tableIndex].RealPixel.Offset = (ushort)finalRm.ShaderProperties[0].RoutingInfo.Count; - - foreach (var pixelParameter in pixelParameters) - { - // adjust pixel register and source index (real constant) - - RegisterID registerID = new RegisterID(pixelParameter.RegisterIndex, ShaderParameter.RType.Vector); - if (!pixelRegisters[epIndex].ContainsKey(registerID) || !newRealConstantIndices.ContainsKey(pixelParameter.SourceIndex)) - continue; - - pixelParameter.RegisterIndex = (short)pixelRegisters[epIndex][registerID]; - pixelParameter.SourceIndex = (byte)newRealConstantIndices[pixelParameter.SourceIndex]; - - if (!TableParameterAlreadyExists(finalRm.ShaderProperties[0].Passes[tableIndex].RealPixel, finalRm.ShaderProperties[0].RoutingInfo, pixelParameter)) - { - finalRm.ShaderProperties[0].RoutingInfo.Add(pixelParameter); - finalRm.ShaderProperties[0].Passes[tableIndex].RealPixel.Count++; - } - } - } - } - } - } - - /// - /// Same as RebuildRenderMethodAnimations(), however matches parameters via rmt2 instead of register info. - /// - private void RebuildRenderMethodAnimationsFromRmt2(RenderMethod finalRm, RenderMethod blamRm, RenderMethodTemplate edRmt2, RenderMethodTemplate bmRmt2) - { - var animatedParameters = ShaderFunctionHelper.GetAnimatedParameters(BlamCache, blamRm, bmRmt2); - - ShaderFunctionHelper.BuildAnimatedParameters(CacheContext, finalRm, edRmt2, animatedParameters); - } - - private TextureConstant GetDefaultTextureConstant(string parameter, ShaderMatcherNew.Rmt2Descriptor rmt2Descriptor, Dictionary optionBitmaps) - { - TextureConstant textureConstant = new TextureConstant { TextureTransformConstantIndex = -1 }; - - if (rmt2Descriptor.Type == "particle") - { - textureConstant.SamplerAddressMode = new TextureConstant.PackedSamplerAddressMode - { - AddressU = TextureConstant.SamplerAddressModeEnum.Clamp, - AddressV = TextureConstant.SamplerAddressModeEnum.Clamp - }; - } - - // get default bitmap from dictionary - if (!optionBitmaps.TryGetValue(CacheContext.StringTable.GetStringId(parameter), out textureConstant.Bitmap) || textureConstant.Bitmap == null) - { - // null bitmap causes bad rendering, use default_detail in these cases - if (rmt2Descriptor.IsMs30) - textureConstant.Bitmap = CacheContext.TagCache.GetTag(@"ms30\shaders\default_bitmaps\bitmaps\default_detail.bitm"); - else - textureConstant.Bitmap = CacheContext.TagCache.GetTag(@"shaders\default_bitmaps\bitmaps\default_detail.bitm"); - } - - return textureConstant; - } - - private uint GetDefaultValue(string parameter, string type, List methodNames, Dictionary optionBlocks) - { - if (!optionBlocks.TryGetValue(CacheContext.StringTable.GetStringId(parameter), out RenderMethodOption.ParameterBlock optionBlock)) - { - if (!methodNames.Contains(parameter)) - { - new TagToolWarning($"No type found for {type} parameter \"{parameter}\""); - - optionBlock = new RenderMethodOption.ParameterBlock(); - } - } - - return optionBlock.DefaultIntBoolArgument; - } - - private RealConstant GetDefaultRealConstant(string parameter, string type, List methodNames, Dictionary optionBlocks) - { - if (!optionBlocks.TryGetValue(CacheContext.StringTable.GetStringId(parameter), out RenderMethodOption.ParameterBlock optionBlock)) - { - // TODO: verify, very rarely some arg names show up - if (!methodNames.Contains(parameter)) - { - new TagToolWarning($"No type found for {type} parameter \"{parameter}\""); - - optionBlock = new RenderMethodOption.ParameterBlock - { - Type = RenderMethodOption.ParameterBlock.OptionDataType.Real, - DefaultFloatArgument = 0.0f - }; - } - - else // particles, contrails, ltvl - optionBlock = new RenderMethodOption.ParameterBlock - { - Type = RenderMethodOption.ParameterBlock.OptionDataType.Bitmap, - DefaultBitmapScale = 1.0f - }; - } - - if (type == "terrain" && parameter.StartsWith("specular_tint_m_")) // prevent purple terrain - return new RealConstant { Arg0 = 0.0f, Arg1 = 0.0f, Arg2 = 0.0f, Arg3 = 0.0f }; - - // use color if one is set - if (optionBlock.DefaultColor.GetValue() != 0) - optionBlock.Type = RenderMethodOption.ParameterBlock.OptionDataType.ArgbColor; - - // uses 1.0 as default bitmap scale for effect RMs - if (EffectRenderMethodTypes.Contains(type) && optionBlock.Type == RenderMethodOption.ParameterBlock.OptionDataType.Bitmap && optionBlock.DefaultBitmapScale == 0.0f) - optionBlock.DefaultBitmapScale = 1.0f; - - switch (optionBlock.Type) - { - case RenderMethodOption.ParameterBlock.OptionDataType.Bitmap: - return new RealConstant { Arg0 = optionBlock.DefaultBitmapScale, Arg1 = optionBlock.DefaultBitmapScale, Arg2 = 0.0f, Arg3 = 0.0f }; - - case RenderMethodOption.ParameterBlock.OptionDataType.Real: - case RenderMethodOption.ParameterBlock.OptionDataType.Color: - return new RealConstant { Arg0 = optionBlock.DefaultFloatArgument, Arg1 = optionBlock.DefaultFloatArgument, Arg2 = optionBlock.DefaultFloatArgument, Arg3 = optionBlock.DefaultFloatArgument }; - - // convert ARGB to RealRGBA - case RenderMethodOption.ParameterBlock.OptionDataType.ArgbColor: - return new RealConstant { Arg0 = optionBlock.DefaultColor.Red / 255.0f, Arg1 = optionBlock.DefaultColor.Blue / 255.0f, Arg2 = optionBlock.DefaultColor.Green / 255.0f, Arg3 = optionBlock.DefaultColor.Alpha / 255.0f }; - - default: - return new RealConstant { Arg0 = 0.0f, Arg1 = 0.0f, Arg2 = 0.0f, Arg3 = 0.0f }; - } - } - - private List BuildRenderMethodOptionIndices(ShaderMatcherNew.Rmt2Descriptor rmt2Descriptor) - { - List newRmIndices = new List(); - - foreach (var option in rmt2Descriptor.Options) - { - RenderMethodOptionIndex optionIndex = new RenderMethodOptionIndex(); - optionIndex.OptionIndex = option; - newRmIndices.Add(optionIndex); - } - - return newRmIndices; - } - - private bool OptionChanged(string rmt2Type, string methodName, out int edOptionIndex, ShaderMatcherNew.Rmt2Descriptor blamRmt2Descriptor, ShaderMatcherNew.Rmt2Descriptor edRmt2Descriptor, RenderMethodDefinition rmdf) - { - // - // This compares the original rmt2 with the matched rmt2 from the matching process and checks if the specified option has changed. - // - - edOptionIndex = -1; - - if (edRmt2Descriptor.Type == rmt2Type) - { - for (int i = 0; i < rmdf.Categories.Count; i++) - { - // find name, and compare the options at that index. maybe need to loop blam rmdf too? - if (CacheContext.StringTable.GetString(rmdf.Categories[i].Name) == methodName && blamRmt2Descriptor.Options[i] != edRmt2Descriptor.Options[i]) - { - edOptionIndex = i; - return true; - } - } - } - - return false; - } - - private void ApplyDefaultOptionFixups(RenderMethodPostprocessBlock edShaderProperty, RenderMethodPostprocessBlock bmShaderProperty, ShaderMatcherNew.Rmt2Descriptor blamRmt2Descriptor, ShaderMatcherNew.Rmt2Descriptor edRmt2Descriptor, RenderMethodTemplate edRmt2, RenderMethodTemplate bmRmt2, RenderMethodDefinition rmdf) - { - // - // This applies manual option->option fixups for matching. This concept should not be used for rm creation/generation. - // - - // special case, has no options. - if (blamRmt2Descriptor.Type == "black" || edRmt2Descriptor.Type == "black") - return; - - // TODO: cleaner way of doing this - - var realConstants = edShaderProperty.RealConstants; - var textureConstants = edShaderProperty.TextureConstants; - var bmRealConstants = bmShaderProperty.RealConstants; - var bmTextureConstants = bmShaderProperty.TextureConstants; - - int optionIndex = -1; - - if (OptionChanged("halogram", "overlay", out optionIndex, blamRmt2Descriptor, edRmt2Descriptor, rmdf)) - { - // if overlay is new to the shader, set its intensity to 0 - if (blamRmt2Descriptor.Options[optionIndex] == 0 && edRmt2Descriptor.Options[optionIndex] != 0) - { - for (int i = 0; i < edRmt2.RealParameterNames.Count; i++) - if (CacheContext.StringTable.GetString(edRmt2.RealParameterNames[i].Name) == "overlay_intensity") - { - realConstants[i] = new RealConstant { Arg0 = 0.0f, Arg1 = 0.0f, Arg2 = 0.0f, Arg3 = 0.0f }; - break; - } - } - // overlay_detail_map to 0 - for (int i = 0; i < edRmt2.RealParameterNames.Count; i++) - if (CacheContext.StringTable.GetString(edRmt2.RealParameterNames[i].Name) == "overlay_detail_map") - { - realConstants[i] = new RealConstant { Arg0 = 0.0f, Arg1 = 0.0f, Arg2 = 0.0f, Arg3 = 0.0f }; - break; - } - } - if (OptionChanged("halogram", "edge_fade", out optionIndex, blamRmt2Descriptor, edRmt2Descriptor, rmdf)) - { - // prevent purple edge_fade - if (blamRmt2Descriptor.Options[optionIndex] == 0 && edRmt2Descriptor.Options[optionIndex] == 1) - { - for (int i = 0; i < edRmt2.RealParameterNames.Count; i++) - if (CacheContext.StringTable.GetString(edRmt2.RealParameterNames[i].Name) == "edge_fade_center_tint") - { - realConstants[i] = new RealConstant { Arg0 = 1.0f, Arg1 = 1.0f, Arg2 = 1.0f, Arg3 = 1.0f }; - break; - } - } - } - if (OptionChanged("water", "bankalpha", out optionIndex, blamRmt2Descriptor, edRmt2Descriptor, rmdf)) - { - // if the water shader uses bankalpha.depth when it didnt previously, set the depth to 0 to prevent transparent puddles - if (blamRmt2Descriptor.Options[optionIndex] != 1 && edRmt2Descriptor.Options[optionIndex] == 1) - { - for (int i = 0; i < edRmt2.RealParameterNames.Count; i++) - if (CacheContext.StringTable.GetString(edRmt2.RealParameterNames[i].Name) == "bankalpha_infuence_depth") - { - realConstants[i] = new RealConstant { Arg0 = 0.0f, Arg1 = 0.0f, Arg2 = 0.0f, Arg3 = 0.0f }; - break; - } - } - } - if (OptionChanged("shader", "self_illumination", out optionIndex, blamRmt2Descriptor, edRmt2Descriptor, rmdf)) - { - // if self_illumination is new to the shader, set self_illum_intensity to 0 - if (blamRmt2Descriptor.Options[optionIndex] == 0 && edRmt2Descriptor.Options[optionIndex] != 0) - { - for (int i = 0; i < edRmt2.RealParameterNames.Count; i++) - if (CacheContext.StringTable.GetString(edRmt2.RealParameterNames[i].Name) == "self_illum_intensity") - { - realConstants[i] = new RealConstant { Arg0 = 0.0f, Arg1 = 0.0f, Arg2 = 0.0f, Arg3 = 0.0f }; - break; - } - } - } - if (OptionChanged("terrain", "material_3", out optionIndex, blamRmt2Descriptor, edRmt2Descriptor, rmdf) && BlamCache.Version < CacheVersion.HaloReach) - { - // if the fourth material is new, set its bitmaps to be that of the first - if (blamRmt2Descriptor.Options[optionIndex] == 0 && edRmt2Descriptor.Options[optionIndex] != 0) - { - bool baseIsSet = false, detailIsSet = false, bumpIsSet = false, detailBumpIsSet = false; - - for (int i = 0; i < edRmt2.TextureParameterNames.Count; i++) - { - if (!baseIsSet && CacheContext.StringTable.GetString(edRmt2.TextureParameterNames[i].Name) == "base_map_m_3") - for (int j = 0; j < bmRmt2.TextureParameterNames.Count; j++) - if (BlamCache.StringTable.GetString(bmRmt2.TextureParameterNames[j].Name) == "base_map_m_0") - { - baseIsSet = true; - textureConstants[i].Bitmap = CacheContext.TagCache.GetTag(bmTextureConstants[j].Bitmap.Name + ".bitm"); - textureConstants[i].BitmapIndex = bmTextureConstants[j].BitmapIndex; - break; - } - if (!detailIsSet && CacheContext.StringTable.GetString(edRmt2.TextureParameterNames[i].Name) == "detail_map_m_3") - for (int j = 0; j < bmRmt2.TextureParameterNames.Count; j++) - if (BlamCache.StringTable.GetString(bmRmt2.TextureParameterNames[j].Name) == "detail_map_m_0") - { - detailIsSet = true; - textureConstants[i].Bitmap = CacheContext.TagCache.GetTag(bmTextureConstants[j].Bitmap.Name + ".bitm"); - textureConstants[i].BitmapIndex = bmTextureConstants[j].BitmapIndex; - break; - } - if (!bumpIsSet && CacheContext.StringTable.GetString(edRmt2.TextureParameterNames[i].Name) == "bump_map_m_3") - for (int j = 0; j < bmRmt2.TextureParameterNames.Count; j++) - if (BlamCache.StringTable.GetString(bmRmt2.TextureParameterNames[j].Name) == "bump_map_m_0") - { - bumpIsSet = true; - textureConstants[i].Bitmap = CacheContext.TagCache.GetTag(bmTextureConstants[j].Bitmap.Name + ".bitm"); - textureConstants[i].BitmapIndex = bmTextureConstants[j].BitmapIndex; - break; - } - if (!detailBumpIsSet && CacheContext.StringTable.GetString(edRmt2.TextureParameterNames[i].Name) == "detail_bump_m_3") - for (int j = 0; j < bmRmt2.TextureParameterNames.Count; j++) - if (BlamCache.StringTable.GetString(bmRmt2.TextureParameterNames[j].Name) == "detail_bump_m_0") - { - detailBumpIsSet = true; - textureConstants[i].Bitmap = CacheContext.TagCache.GetTag(bmTextureConstants[j].Bitmap.Name + ".bitm"); - textureConstants[i].BitmapIndex = bmTextureConstants[j].BitmapIndex; - break; - } - } - - baseIsSet = false; detailIsSet = false; bumpIsSet = false; detailBumpIsSet = false; - - for (int i = 0; i < edRmt2.RealParameterNames.Count; i++) - { - if (!baseIsSet && CacheContext.StringTable.GetString(edRmt2.RealParameterNames[i].Name) == "base_map_m_3") - for (int j = 0; j < bmRmt2.RealParameterNames.Count; j++) - if (BlamCache.StringTable.GetString(bmRmt2.RealParameterNames[j].Name) == "base_map_m_0") - { - baseIsSet = true; - realConstants[i] = bmRealConstants[j]; - break; - } - if (!detailIsSet && CacheContext.StringTable.GetString(edRmt2.RealParameterNames[i].Name) == "detail_map_m_3") - for (int j = 0; j < bmRmt2.RealParameterNames.Count; j++) - if (BlamCache.StringTable.GetString(bmRmt2.RealParameterNames[j].Name) == "detail_map_m_0") - { - detailIsSet = true; - realConstants[i] = bmRealConstants[j]; - break; - } - if (!bumpIsSet && CacheContext.StringTable.GetString(edRmt2.RealParameterNames[i].Name) == "bump_map_m_3") - for (int j = 0; j < bmRmt2.RealParameterNames.Count; j++) - if (BlamCache.StringTable.GetString(bmRmt2.RealParameterNames[j].Name) == "bump_map_m_0") - { - bumpIsSet = true; - realConstants[i] = bmRealConstants[j]; - break; - } - if (!detailBumpIsSet && CacheContext.StringTable.GetString(edRmt2.RealParameterNames[i].Name) == "detail_bump_m_3") - for (int j = 0; j < bmRmt2.RealParameterNames.Count; j++) - if (BlamCache.StringTable.GetString(bmRmt2.RealParameterNames[j].Name) == "detail_bump_m_0") - { - detailBumpIsSet = true; - realConstants[i] = bmRealConstants[j]; - break; - } - } - } - } - if (OptionChanged("decal", "tinting", out optionIndex, blamRmt2Descriptor, edRmt2Descriptor, rmdf)) - { - // if decal tinting is new, ensure its colour is white - if (blamRmt2Descriptor.Options[optionIndex] == 0 && edRmt2Descriptor.Options[optionIndex] != 0) - { - for (int i = 0; i < edRmt2.RealParameterNames.Count; i++) - if (CacheContext.StringTable.GetString(edRmt2.RealParameterNames[i].Name) == "tint_color") - { - realConstants[i] = new RealConstant { Arg0 = 1.0f, Arg1 = 1.0f, Arg2 = 1.0f, Arg3 = 1.0f }; - break; - } - } - } - if (OptionChanged("shader", "parallax", out optionIndex, blamRmt2Descriptor, edRmt2Descriptor, rmdf)) - { - if (blamRmt2Descriptor.Options[optionIndex] == 0 && edRmt2Descriptor.Options[optionIndex] != 0) - { - for (int i = 0; i < edRmt2.RealParameterNames.Count; i++) - { - // prevent "smoothing" - if (CacheContext.StringTable.GetString(edRmt2.RealParameterNames[i].Name) == "height_map") - realConstants[i] = new RealConstant { Arg0 = 1.0f, Arg1 = 1.0f, Arg2 = 0.0f, Arg3 = 0.0f }; - if (CacheContext.StringTable.GetString(edRmt2.RealParameterNames[i].Name) == "height_scale") - realConstants[i] = new RealConstant { Arg0 = 0.0f, Arg1 = 0.0f, Arg2 = 0.0f, Arg3 = 0.0f }; - } - } - } - } - - private void ApplyPostOptionFixups(RenderMethodPostprocessBlock edShaderProperty, RenderMethodPostprocessBlock bmShaderProperty, ShaderMatcherNew.Rmt2Descriptor blamRmt2Descriptor, ShaderMatcherNew.Rmt2Descriptor edRmt2Descriptor, RenderMethodTemplate edRmt2, RenderMethodTemplate bmRmt2, RenderMethodDefinition rmdf) - { - // - // This applies manual option->option fixups for matching. This concept should not be used for rm creation/generation. - // - - // special case, has no options. - if (blamRmt2Descriptor.Type == "black" || edRmt2Descriptor.Type == "black") - return; - - // TODO: cleaner way of doing this - - var realConstants = edShaderProperty.RealConstants; - var textureConstants = edShaderProperty.TextureConstants; - var bmRealConstants = bmShaderProperty.RealConstants; - var bmTextureConstants = bmShaderProperty.TextureConstants; - - int optionIndex = -1; - - if (OptionChanged("shader", "specular_mask", out optionIndex, blamRmt2Descriptor, edRmt2Descriptor, rmdf)) - { - // if specular mask/texture is no longer used, 0 the specular coefficients (prevents blinding/bad env reflections) - if (blamRmt2Descriptor.Options[optionIndex] > 1 && edRmt2Descriptor.Options[optionIndex] == 0) - { - for (int i = 0; i < edRmt2.RealParameterNames.Count; i++) - if (CacheContext.StringTable.GetString(edRmt2.RealParameterNames[i].Name) == "specular_coefficient") - { - realConstants[i] = new RealConstant { Arg0 = 0.0f, Arg1 = 0.0f, Arg2 = 0.0f, Arg3 = 0.0f }; - break; - } - } - } } } } \ No newline at end of file diff --git a/TagTool/Commands/Porting/PortTagCommand.cs b/TagTool/Commands/Porting/PortTagCommand.cs index eddca33f..f0cf8d83 100644 --- a/TagTool/Commands/Porting/PortTagCommand.cs +++ b/TagTool/Commands/Porting/PortTagCommand.cs @@ -261,7 +261,7 @@ private bool TagIsValid(CachedTag blamTag, Stream blamCacheStream, out CachedTag { case "rmcs": case "rmgl": - resultTag = GetDefaultShader(blamTag.Group.Tag, resultTag); + resultTag = GetDefaultShader(blamTag.Group.Tag); return false; } } @@ -282,7 +282,7 @@ private bool TagIsValid(CachedTag blamTag, Stream blamCacheStream, out CachedTag // TODO: add code for "!MatchShaders" -- if a perfect match isnt found a null tag will be left in the cache // "ConvertTagInternal" isnt called so the default shader needs to be set here - resultTag = GetDefaultShader(blamTag.Group.Tag, resultTag); + resultTag = GetDefaultShader(blamTag.Group.Tag); return false; } else if (blamTag.Group.Tag == "glvs" || blamTag.Group.Tag == "glps" || blamTag.Group.Tag == "rmdf") @@ -1387,7 +1387,7 @@ public CachedTag ConvertTagInternal(Stream cacheStream, Stream blamCacheStream, case BeamSystem beam: case ShaderCortana rmct: if (!FlagIsSet(PortingFlags.MatchShaders)) - return GetDefaultShader(blamTag.Group.Tag, edTag); + return GetDefaultShader(blamTag.Group.Tag); else { // Verify that the ShaderMatcher is ready to use @@ -1396,7 +1396,7 @@ public CachedTag ConvertTagInternal(Stream cacheStream, Stream blamCacheStream, blamDefinition = ConvertShader(cacheStream, blamCacheStream, blamDefinition, blamTag, BlamCache.Deserialize(blamCacheStream, blamTag)); if (blamDefinition == null) // convert shader failed - return GetDefaultShader(blamTag.Group.Tag, edTag); + return GetDefaultShader(blamTag.Group.Tag); } break; } diff --git a/TagTool/Shaders/ShaderMatching/ShaderMatcherNew.cs b/TagTool/Shaders/ShaderMatching/ShaderMatcherNew.cs index 4aaf74df..f53a2df4 100644 --- a/TagTool/Shaders/ShaderMatching/ShaderMatcherNew.cs +++ b/TagTool/Shaders/ShaderMatching/ShaderMatcherNew.cs @@ -7,9 +7,6 @@ using TagTool.Commands.Common; using TagTool.Common; using TagTool.Tags.Definitions; -using HaloShaderGenerator.Shader; -using static TagTool.Tags.Definitions.RenderMethodTemplate; -using TagTool.Cache.HaloOnline; namespace TagTool.Shaders.ShaderMatching { @@ -19,10 +16,7 @@ public class ShaderMatcherNew private GameCache PortingCache; private Stream BaseCacheStream; private Stream PortingCacheStream; - private Dictionary _rmt2Cache; - private List UpdatedRmdf; - public static string DefaultTemplate => @"shaders\shader_templates\_0_0_0_0_0_0_0_0_0_0_0.rmt2"; public bool IsInitialized { get; private set; } = false; public bool UseMs30 { get; set; } = false; public bool PerfectMatchesOnly { get; set; } = false; @@ -33,8 +27,6 @@ public class ShaderMatcherNew ["shader"] = new int[] { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 } }; - - public ShaderMatcherNew() { } @@ -47,8 +39,6 @@ public void Init(GameCache baseCache, GameCache portingCache, Stream baseCacheSt PortingCache = portingCache; BaseCacheStream = baseCacheStream; PortingCacheStream = portingCacheStream; - _rmt2Cache = new Dictionary(); - UpdatedRmdf = new List(); IsInitialized = true; } @@ -60,7 +50,6 @@ public void DeInit() PortingCache = null; BaseCacheStream = null; PortingCacheStream = null; - UpdatedRmdf = null; IsInitialized = false; } @@ -373,15 +362,6 @@ private CachedTag GetBestTag(SortingInterface sortingInterface, Dictionary - /// Modifies the input render method to make it work using the matchedTemplate - /// - private RenderMethod MatchRenderMethods(RenderMethod renderMethod, RenderMethodTemplate matchedTemplate, RenderMethodTemplate originalTemplate) - { - - return renderMethod; - } - /// /// Rebuilds an rmt2's options in memory so indices match up with the base cache /// @@ -530,31 +510,6 @@ private Rmt2Descriptor RebuildRmt2Options(Rmt2Descriptor srcRmt2Descriptor, Stre return srcRmt2Descriptor; } - private Rmt2ParameterMatch MatchParameterBlocks(List sourceBlock, List destBlock) - { - var result = new Rmt2ParameterMatch(); - - var destNames = destBlock.Select(x => BaseCache.StringTable.GetString(x.Name)); - var sourceNames = sourceBlock.Select(x => PortingCache.StringTable.GetString(x.Name)); - - result.SourceCount = sourceNames.Count(); - result.DestCount = destNames.Count(); - result.MissingFromDest = sourceNames.Except(destNames).Count(); - result.MissingFromSource = destNames.Except(sourceNames).Count(); - result.Common = destNames.Intersect(sourceNames).Count(); - - return result; - } - - private RenderMethodTemplate GetTemplate(CachedTag tag) - { - RenderMethodTemplate template; - if (!_rmt2Cache.TryGetValue(tag, out template)) - template = _rmt2Cache[tag] = BaseCache.Deserialize(BaseCacheStream, tag); - - return template; - } - public class Rmt2Pairing { public Rmt2ParameterMatch RealParams; @@ -695,67 +650,5 @@ public CachedTag FindRmdf(CachedTag matchedRmt2Tag) return BaseCache.TagCache.GetTag(rmdfName, "rmdf"); } - - - private List GetRenderMethodDefinitionMethods(RenderMethodDefinition rmdf, GameCache cache) - { - var result = new List(); - foreach(var method in rmdf.Categories) - { - var str = cache.StringTable.GetString(method.Name); - result.Add(str); - } - return result; - } - - private List GetMethodOptions(RenderMethodDefinition rmdf, int methodIndex, GameCache cache) - { - var result = new List(); - var method = rmdf.Categories[methodIndex]; - foreach(var option in method.ShaderOptions) - { - result.Add(cache.StringTable.GetString(option.Name)); - } - return result; - } - - private void FindClosestShaderTemplate(CachedTag sourceRmt2) - { - // somehow build a list of rmt2 of the same type - List candidateTemplates = new List(); - - // defined method search order, ignore last method from ms30 - List methodOrder = new List {0, 2, 3, 1, 6, 4, 5, 7, 8, 9, 10}; - - Dictionary matchLevelDictionary = new Dictionary(); - Rmt2Descriptor sourceRmt2Desc; - if (!Rmt2Descriptor.TryParse(sourceRmt2.Name, out sourceRmt2Desc)) - return; - - while (candidateTemplates.Count != 0) - { - var template = candidateTemplates.Last(); - Rmt2Descriptor destRmt2Desc; - if (!Rmt2Descriptor.TryParse(template.Name, out destRmt2Desc)) - { - candidateTemplates.Remove(template); - matchLevelDictionary[template] = 0; - } - else - { - var matchLevel = 0; - for(int i = 0; i < methodOrder.Count; i++) - { - var methodIndex = methodOrder[i]; - // we need to define a ordering on the method options, so that there is a single best rmt2 - if (sourceRmt2Desc.Options[methodIndex] == destRmt2Desc.Options[methodIndex]) - matchLevel++; - else - break; - } - matchLevelDictionary[template] = matchLevel; - } - } - } } }