Skip to content

Shader export has no programs when they are stored in m_PlayerSubPrograms #389

Open

Description

@nichinichisou0609

In the class database UnityPy ships, SerializedProgram has a field m_PlayerSubPrograms next to m_SubPrograms from Unity 2021.3.10f1, 2022.1.13f1 and 2022.2 on: a list of lists of SerializedPlayerSubProgram, each with m_BlobIndex, m_GpuProgramType, m_KeywordIndices and m_ShaderRequirements. ConvertSerializedPass writes a Program block only for the entries of m_SubPrograms.

In a player build made with Unity 6000.3.12f1, m_SubPrograms is empty in every pass of the shaders I looked at and the programs are listed in m_PlayerSubPrograms, so Shader.export() returns the passes with their state and no program at all. For TextMeshPro/Distance Field:

UnityPy 1.25.4
TextMeshPro/Distance Field 6000.3.12f1
  subshader 0 pass 0 progVertex: m_SubPrograms 0, m_PlayerSubPrograms [0, 0, 0, 8]
  export: 0 Program blocks, 0 SubProgram blocks
Script
import sys

import UnityPy

print("UnityPy", UnityPy.__version__)
STAGES = ("progVertex", "progFragment", "progGeometry", "progHull", "progDomain")

env = UnityPy.load(sys.argv[1])
for obj in env.objects:
    if obj.type.name != "Shader":
        continue
    shader = obj.read()
    print(shader.m_ParsedForm.m_Name, obj.assets_file.unity_version)
    for si, subshader in enumerate(shader.m_ParsedForm.m_SubShaders):
        for pi, shader_pass in enumerate(subshader.m_Passes):
            for stage in STAGES:
                program = getattr(shader_pass, stage)
                player = [len(entries) for entries in program.m_PlayerSubPrograms or []]
                if program.m_SubPrograms or any(player):
                    print(f"  subshader {si} pass {pi} {stage}: m_SubPrograms {len(program.m_SubPrograms)}, m_PlayerSubPrograms {player}")
    text = shader.export()
    print("  export:", text.count('Program "'), "Program blocks,", text.count('SubProgram "'), "SubProgram blocks")

The blob itself reads fine: passing the entries of m_PlayerSubPrograms to ConvertSerializedSubPrograms, with the index of the outer list in place of m_ShaderHardwareTier, puts the 8 subprograms of this shader into the export, the four gles3 ones with their keywords and GLSL code and the four vulkan ones with their keywords and empty code. What I do not know is what the outer list index stands for, and whether the parameters referenced by m_ParameterBlobIndices should be written out as well. If that mapping is right, I can send a PR.

Tested with UnityPy 417d595 (1.25.4) and CPython 3.11.13 on Windows x86_64.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions