Skip to content
This repository was archived by the owner on Jan 21, 2023. It is now read-only.

Commit ab98585

Browse files
committed
Fix Shader reading. Close #720
1 parent 075d53a commit ab98585

File tree

2 files changed

+171
-37
lines changed

2 files changed

+171
-37
lines changed

AssetStudio/Classes/Shader.cs

+164-37
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ public class ConstantBuffer
394394
public VectorParameter[] m_VectorParams;
395395
public StructParameter[] m_StructParams;
396396
public int m_Size;
397+
public bool m_IsPartialCB;
397398

398399
public ConstantBuffer(ObjectReader reader)
399400
{
@@ -424,6 +425,16 @@ public ConstantBuffer(ObjectReader reader)
424425
}
425426
}
426427
m_Size = reader.ReadInt32();
428+
429+
if ((version[0] == 2020 && version[1] > 3) ||
430+
(version[0] == 2020 && version[1] == 3 && version[2] > 0) ||
431+
(version[0] == 2020 && version[1] == 3 && version[2] == 0 && version[3] >= 2) || //2020.3.0f2 to 2020.3.x
432+
(version[0] == 2021 && version[1] > 1) ||
433+
(version[0] == 2021 && version[1] == 1 && version[2] >= 4)) //2021.1.4f1 to 2021.1.x
434+
{
435+
m_IsPartialCB = reader.ReadBoolean();
436+
reader.AlignStream();
437+
}
427438
}
428439
}
429440

@@ -477,13 +488,8 @@ public enum ShaderGpuProgramType
477488
kShaderGpuProgramRayTracing = 31,
478489
};
479490

480-
public class SerializedSubProgram
491+
public class SerializedProgramParameters
481492
{
482-
public uint m_BlobIndex;
483-
public ParserBindChannels m_Channels;
484-
public ushort[] m_KeywordIndices;
485-
public sbyte m_ShaderHardwareTier;
486-
public ShaderGpuProgramType m_GpuProgramType;
487493
public VectorParameter[] m_VectorParams;
488494
public MatrixParameter[] m_MatrixParams;
489495
public TextureParameter[] m_TextureParams;
@@ -493,33 +499,8 @@ public class SerializedSubProgram
493499
public UAVParameter[] m_UAVParams;
494500
public SamplerParameter[] m_Samplers;
495501

496-
public SerializedSubProgram(ObjectReader reader)
502+
public SerializedProgramParameters(ObjectReader reader)
497503
{
498-
var version = reader.version;
499-
500-
m_BlobIndex = reader.ReadUInt32();
501-
m_Channels = new ParserBindChannels(reader);
502-
503-
if (version[0] >= 2019) //2019 and up
504-
{
505-
var m_GlobalKeywordIndices = reader.ReadUInt16Array();
506-
reader.AlignStream();
507-
var m_LocalKeywordIndices = reader.ReadUInt16Array();
508-
reader.AlignStream();
509-
}
510-
else
511-
{
512-
m_KeywordIndices = reader.ReadUInt16Array();
513-
if (version[0] >= 2017) //2017 and up
514-
{
515-
reader.AlignStream();
516-
}
517-
}
518-
519-
m_ShaderHardwareTier = reader.ReadSByte();
520-
m_GpuProgramType = (ShaderGpuProgramType)reader.ReadSByte();
521-
reader.AlignStream();
522-
523504
int numVectorParams = reader.ReadInt32();
524505
m_VectorParams = new VectorParameter[numVectorParams];
525506
for (int i = 0; i < numVectorParams; i++)
@@ -569,15 +550,129 @@ public SerializedSubProgram(ObjectReader reader)
569550
m_UAVParams[i] = new UAVParameter(reader);
570551
}
571552

572-
if (version[0] >= 2017) //2017 and up
553+
int numSamplers = reader.ReadInt32();
554+
m_Samplers = new SamplerParameter[numSamplers];
555+
for (int i = 0; i < numSamplers; i++)
556+
{
557+
m_Samplers[i] = new SamplerParameter(reader);
558+
}
559+
}
560+
}
561+
562+
public class SerializedSubProgram
563+
{
564+
public uint m_BlobIndex;
565+
public ParserBindChannels m_Channels;
566+
public ushort[] m_KeywordIndices;
567+
public sbyte m_ShaderHardwareTier;
568+
public ShaderGpuProgramType m_GpuProgramType;
569+
public SerializedProgramParameters m_Parameters;
570+
public VectorParameter[] m_VectorParams;
571+
public MatrixParameter[] m_MatrixParams;
572+
public TextureParameter[] m_TextureParams;
573+
public BufferBinding[] m_BufferParams;
574+
public ConstantBuffer[] m_ConstantBuffers;
575+
public BufferBinding[] m_ConstantBufferBindings;
576+
public UAVParameter[] m_UAVParams;
577+
public SamplerParameter[] m_Samplers;
578+
579+
public SerializedSubProgram(ObjectReader reader)
580+
{
581+
var version = reader.version;
582+
583+
m_BlobIndex = reader.ReadUInt32();
584+
m_Channels = new ParserBindChannels(reader);
585+
586+
if (version[0] >= 2019) //2019 and up
587+
{
588+
var m_GlobalKeywordIndices = reader.ReadUInt16Array();
589+
reader.AlignStream();
590+
var m_LocalKeywordIndices = reader.ReadUInt16Array();
591+
reader.AlignStream();
592+
}
593+
else
594+
{
595+
m_KeywordIndices = reader.ReadUInt16Array();
596+
if (version[0] >= 2017) //2017 and up
597+
{
598+
reader.AlignStream();
599+
}
600+
}
601+
602+
m_ShaderHardwareTier = reader.ReadSByte();
603+
m_GpuProgramType = (ShaderGpuProgramType)reader.ReadSByte();
604+
reader.AlignStream();
605+
606+
if ((version[0] == 2020 && version[1] > 3) ||
607+
(version[0] == 2020 && version[1] == 3 && version[2] > 0) ||
608+
(version[0] == 2020 && version[1] == 3 && version[2] == 0 && version[3] >= 2) || //2020.3.0f2 to 2020.3.x
609+
(version[0] == 2021 && version[1] > 1) ||
610+
(version[0] == 2021 && version[1] == 1 && version[2] >= 4)) //2021.1.4f1 to 2021.1.x
573611
{
574-
int numSamplers = reader.ReadInt32();
575-
m_Samplers = new SamplerParameter[numSamplers];
576-
for (int i = 0; i < numSamplers; i++)
612+
m_Parameters = new SerializedProgramParameters(reader);
613+
}
614+
else
615+
{
616+
int numVectorParams = reader.ReadInt32();
617+
m_VectorParams = new VectorParameter[numVectorParams];
618+
for (int i = 0; i < numVectorParams; i++)
619+
{
620+
m_VectorParams[i] = new VectorParameter(reader);
621+
}
622+
623+
int numMatrixParams = reader.ReadInt32();
624+
m_MatrixParams = new MatrixParameter[numMatrixParams];
625+
for (int i = 0; i < numMatrixParams; i++)
626+
{
627+
m_MatrixParams[i] = new MatrixParameter(reader);
628+
}
629+
630+
int numTextureParams = reader.ReadInt32();
631+
m_TextureParams = new TextureParameter[numTextureParams];
632+
for (int i = 0; i < numTextureParams; i++)
577633
{
578-
m_Samplers[i] = new SamplerParameter(reader);
634+
m_TextureParams[i] = new TextureParameter(reader);
635+
}
636+
637+
int numBufferParams = reader.ReadInt32();
638+
m_BufferParams = new BufferBinding[numBufferParams];
639+
for (int i = 0; i < numBufferParams; i++)
640+
{
641+
m_BufferParams[i] = new BufferBinding(reader);
642+
}
643+
644+
int numConstantBuffers = reader.ReadInt32();
645+
m_ConstantBuffers = new ConstantBuffer[numConstantBuffers];
646+
for (int i = 0; i < numConstantBuffers; i++)
647+
{
648+
m_ConstantBuffers[i] = new ConstantBuffer(reader);
649+
}
650+
651+
int numConstantBufferBindings = reader.ReadInt32();
652+
m_ConstantBufferBindings = new BufferBinding[numConstantBufferBindings];
653+
for (int i = 0; i < numConstantBufferBindings; i++)
654+
{
655+
m_ConstantBufferBindings[i] = new BufferBinding(reader);
656+
}
657+
658+
int numUAVParams = reader.ReadInt32();
659+
m_UAVParams = new UAVParameter[numUAVParams];
660+
for (int i = 0; i < numUAVParams; i++)
661+
{
662+
m_UAVParams[i] = new UAVParameter(reader);
663+
}
664+
665+
if (version[0] >= 2017) //2017 and up
666+
{
667+
int numSamplers = reader.ReadInt32();
668+
m_Samplers = new SamplerParameter[numSamplers];
669+
for (int i = 0; i < numSamplers; i++)
670+
{
671+
m_Samplers[i] = new SamplerParameter(reader);
672+
}
579673
}
580674
}
675+
581676
if (version[0] > 2017 || (version[0] == 2017 && version[1] >= 2)) //2017.2 and up
582677
{
583678
if (version[0] >= 2021) //2021.1 and up
@@ -595,15 +690,27 @@ public SerializedSubProgram(ObjectReader reader)
595690
public class SerializedProgram
596691
{
597692
public SerializedSubProgram[] m_SubPrograms;
693+
public SerializedProgramParameters m_CommonParameters;
598694

599695
public SerializedProgram(ObjectReader reader)
600696
{
697+
var version = reader.version;
698+
601699
int numSubPrograms = reader.ReadInt32();
602700
m_SubPrograms = new SerializedSubProgram[numSubPrograms];
603701
for (int i = 0; i < numSubPrograms; i++)
604702
{
605703
m_SubPrograms[i] = new SerializedSubProgram(reader);
606704
}
705+
706+
if ((version[0] == 2020 && version[1] > 3) ||
707+
(version[0] == 2020 && version[1] == 3 && version[2] > 0) ||
708+
(version[0] == 2020 && version[1] == 3 && version[2] == 0 && version[3] >= 2) || //2020.3.0f2 to 2020.3.x
709+
(version[0] == 2021 && version[1] > 1) ||
710+
(version[0] == 2021 && version[1] == 1 && version[2] >= 4)) //2021.1.4f1 to 2021.1.x
711+
{
712+
m_CommonParameters = new SerializedProgramParameters(reader);
713+
}
607714
}
608715
}
609716

@@ -861,6 +968,26 @@ public Shader(ObjectReader reader) : base(reader)
861968
decompressedLengths = reader.ReadUInt32Array();
862969
}
863970
compressedBlob = reader.ReadUInt8Array();
971+
reader.AlignStream();
972+
973+
var m_DependenciesCount = reader.ReadInt32();
974+
for (int i = 0; i < m_DependenciesCount; i++)
975+
{
976+
new PPtr<Shader>(reader);
977+
}
978+
979+
if (version[0] >= 2018)
980+
{
981+
var m_NonModifiableTexturesCount = reader.ReadInt32();
982+
for (int i = 0; i < m_NonModifiableTexturesCount; i++)
983+
{
984+
var first = reader.ReadAlignedString();
985+
new PPtr<Texture>(reader);
986+
}
987+
}
988+
989+
var m_ShaderIsBaked = reader.ReadBoolean();
990+
reader.AlignStream();
864991
}
865992
else
866993
{

AssetStudioUtility/ShaderConverter.cs

+7
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,13 @@ private static string ConvertSerializedPass(SerializedPass m_Passe, ShaderCompil
167167
sb.Append(ConvertSerializedSubPrograms(m_Passe.progDomain.m_SubPrograms, platforms, shaderPrograms));
168168
sb.Append("}\n");
169169
}
170+
171+
if (m_Passe.progRayTracing?.m_SubPrograms.Length > 0)
172+
{
173+
sb.Append("Program \"rtp\" {\n");
174+
sb.Append(ConvertSerializedSubPrograms(m_Passe.progRayTracing.m_SubPrograms, platforms, shaderPrograms));
175+
sb.Append("}\n");
176+
}
170177
}
171178
sb.Append("}\n");
172179
}

0 commit comments

Comments
 (0)