Skip to content

Commit

Permalink
Make Shader Converter use shader resources and IO signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
DeltaDesigns committed Dec 29, 2023
1 parent 44bcfa9 commit cf115dd
Show file tree
Hide file tree
Showing 5 changed files with 255 additions and 238 deletions.
104 changes: 71 additions & 33 deletions Charm/MaterialView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,52 @@ public void Load(FileHash hash)

if (material.VertexShader is not null)
{
VertexShader.Text = material.Decompile(material.VertexShader.GetBytecode(), $"ps{material.VertexShader.Hash}");
VertexShader.Text = material.Decompile(material.VertexShader.GetBytecode(), $"vs{material.VertexShader.Hash}");
VS_CBufferList.ItemsSource = GetCBufferDetails(material, true);

var vs_test = material.VertexShader?.Resources;
Console.WriteLine($"----Vertex----");
foreach (var vsr in vs_test)
{
Console.WriteLine($"Type: {vsr.ResourceType} | Index:{vsr.Index} | Count: {vsr.Count}");
}

var vs_in = material.VertexShader.InputSignatures;
foreach (var b in vs_in)
{
Console.WriteLine($"v{b.RegisterIndex}: {b.ToString()}{b.SemanticIndex}.{b.Mask}");
}

var vs_out = material.VertexShader.OutputSignatures;
foreach (var b in vs_out)
{
Console.WriteLine($"o{b.RegisterIndex}: {b.ToString()}{b.SemanticIndex}.{b.Mask}");
}
}

if (material.PixelShader is not null)
{
PixelShader.Text = material.Decompile(material.PixelShader.GetBytecode(), $"ps{material.PixelShader.Hash}");
PS_CBufferList.ItemsSource = GetCBufferDetails(material);
PS_CBufferList.ItemsSource = GetCBufferDetails(material);

var ps_test = material.PixelShader?.Resources;
Console.WriteLine($"----Pixel----");
foreach (var a in ps_test)
{
Console.WriteLine($"Type: {a.ResourceType} | Index:{a.Index} | Count: {a.Count}");
}

var ps_in = material.PixelShader.InputSignatures;
foreach (var b in ps_in)
{
Console.WriteLine($"v{b.RegisterIndex}: {b.ToString()}{b.SemanticIndex}.{b.Mask}");
}

var ps_out = material.PixelShader.OutputSignatures;
foreach (var b in ps_out)
{
Console.WriteLine($"o{b.RegisterIndex}: {b.ToString()}{b.SemanticIndex}.{b.Mask}");
}
}
}

Expand Down Expand Up @@ -202,7 +240,7 @@ await Task.Run(() =>
Dispatcher.Invoke(() =>
{
TfxBytecodeInterpreter bytecode = new(TfxBytecodeOp.ParseAll(dc.Stage == CBufferDetail.Shader.Pixel ? Material.PS_TFX_Bytecode : Material.VS_TFX_Bytecode));
var bytecode_hlsl = bytecode.Evaluate(dc.Stage == CBufferDetail.Shader.Pixel ? Material.PS_TFX_Bytecode_Constants : Material.VS_TFX_Bytecode_Constants);
var bytecode_hlsl = bytecode.Evaluate(dc.Stage == CBufferDetail.Shader.Pixel ? Material.PS_TFX_Bytecode_Constants : Material.VS_TFX_Bytecode_Constants, true);
ConcurrentBag<CBufferDataDetail> items = new ConcurrentBag<CBufferDataDetail>();
for (int i = 0; i < dc.Data.Count; i++)
Expand Down Expand Up @@ -243,36 +281,36 @@ public BitmapImage LoadTexture(Texture textureHeader)
return bitmapImage;
}

public static List<Cbuffer> GetCBuffers(IMaterial material, bool isVertexShader = false)
{
StringReader reader = new(material.Decompile((isVertexShader ? material.VertexShader : material.PixelShader).GetBytecode(),
$"{(isVertexShader ? $"vs{material.VertexShader.Hash}" : $"ps{material.PixelShader.Hash}")}"));

List<Cbuffer> buffers = new();

string line = string.Empty;
do
{
line = reader.ReadLine();
if (line != null)
{
if (line.Contains("cbuffer"))
{
reader.ReadLine();
line = reader.ReadLine();
Cbuffer cbuffer = new Cbuffer();
cbuffer.Variable = "cb" + line.Split("cb")[1].Split("[")[0];
cbuffer.Index = Int32.TryParse(new string(cbuffer.Variable.Skip(2).ToArray()), out int index) ? index : -1;
cbuffer.Count = Int32.TryParse(new string(line.Split("[")[1].Split("]")[0]), out int count) ? count : -1;
cbuffer.Type = line.Split("cb")[0].Trim();
buffers.Add(cbuffer);
}
}

} while (line != null);

return buffers;
}
//public static List<Cbuffer> GetCBuffers(IMaterial material, bool isVertexShader = false)
//{
// StringReader reader = new(material.Decompile((isVertexShader ? material.VertexShader : material.PixelShader).GetBytecode(),
// $"{(isVertexShader ? $"vs{material.VertexShader.Hash}" : $"ps{material.PixelShader.Hash}")}"));

// List<Cbuffer> buffers = new();

// string line = string.Empty;
// do
// {
// line = reader.ReadLine();
// if (line != null)
// {
// if (line.Contains("cbuffer"))
// {
// reader.ReadLine();
// line = reader.ReadLine();
// Cbuffer cbuffer = new Cbuffer();
// cbuffer.Variable = "cb" + line.Split("cb")[1].Split("[")[0];
// cbuffer.Index = Int32.TryParse(new string(cbuffer.Variable.Skip(2).ToArray()), out int index) ? index : -1;
// cbuffer.Count = Int32.TryParse(new string(line.Split("[")[1].Split("]")[0]), out int count) ? count : -1;
// cbuffer.Type = line.Split("cb")[0].Trim();
// buffers.Add(cbuffer);
// }
// }

// } while (line != null);

// return buffers;
//}

private void Texture_OnClick(object sender, RoutedEventArgs e)
{
Expand Down
43 changes: 27 additions & 16 deletions Tiger/Exporters/Source2Handler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,27 +203,38 @@ public static void SaveVMAT(string savePath, string hash, IMaterial materialHead
vmat.AppendLine($"\t\tcb0_{entry.Key} \"{entry.Value}\"");
}

vmat.AppendLine($"\t\tcb2_0 \"float4(0,1,1,1)\"");
vmat.AppendLine($"\t\tcb2_1 \"float4(0,1,1,1)\"");

for (int i = 0; i < 37; i++)
foreach(var resource in materialHeader.PixelShader.Resources)
{
if (i < 5)
vmat.AppendLine($"\t\tcb8_{i} \"float4(0,0,0,0)\"");
else
vmat.AppendLine($"\t\tcb8_{i} \"float4(1,1,1,1)\"");
if (resource.ResourceType == Schema.ResourceType.CBuffer)
{
switch (resource.Index)
{
case 2: //Transparent scope
for(int i = 0; i < resource.Count; i++)
{
vmat.AppendLine($"\t\tcb2_{i} \"float4(0,1,1,1)\"");
}
break;
case 8: //??? scope
for (int i = 0; i < resource.Count; i++)
{
if (i < 5)
vmat.AppendLine($"\t\tcb8_{i} \"float4(0,0,0,0)\"");
else
vmat.AppendLine($"\t\tcb8_{i} \"float4(1,1,1,1)\"");
}
break;
case 13: //Frame scope
vmat.AppendLine($"\t\tcb13_0 \"Time\"");
vmat.AppendLine($"\t\tcb13_1 \"float4(1,1,1,1)\"");
break;
}
}
}

vmat.AppendLine($"\t\tcb12_4 \"float4(1,0,0,0)\"");
vmat.AppendLine($"\t\tcb12_5 \"float4(0,1,0,0)\"");
vmat.AppendLine($"\t\tcb12_6 \"float4(0,0,1,0)\"");

vmat.AppendLine($"\t\tcb13_0 \"Time\"");
vmat.AppendLine($"\t\tcb13_1 \"float4(1,1,1,1)\"");

vmat.AppendLine($"\t}}");

vmat.AppendLine("}");

try
{
File.WriteAllText($"{savePath}/{hash}.vmat", vmat.ToString());
Expand Down
39 changes: 0 additions & 39 deletions Tiger/Schema/Shaders/Material.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,6 @@

namespace Tiger.Schema.Shaders
{
public struct TextureView
{
public string Dimension;
public string Type;
public string Variable;
public int Index;
}

public struct Buffer
{
public string Variable;
public string Type;
public int Index;
}

public struct Cbuffer
{
public string Variable;
public string Type;
public int Count;
public int Index;
}

public struct Input
{
public string Variable;
public string Type;
public int Index;
public string Semantic;
}

public struct Output
{
public string Variable;
public string Type;
public int Index;
public string Semantic;
}

public enum MaterialType
{
Opaque,
Expand Down
Loading

0 comments on commit cf115dd

Please sign in to comment.