AGL > Binary Shader Archive (SHARCFB)

A SHARCFB file contains compiled shaders. This page describes version 8 of the file format.

In little endian mode, the whole header is swapped (even the magic number).

OffsetSizeDescription
0x04Magic number: SHAB
0x44Version number
0x84Filesize
0xC4Endianness (0=big, 1=little)
0x104Always 0
0x144Length of filename string (including null terminator)
0x18Filename string (null-terminated)

Shader Binary Section

This section contains the compiled shader binaries.

OffsetSizeDescription
0x04Size of shader binary section
0x44Number of shader binaries
0x8Shader binaries

Shader Binary

OffsetSizeDescription
0x04Size (or offset to next shader binary)
0x44Type:
0 = GX2VertexShader
1 = GX2PixelShader
2 = GX2GeometryShader
0x84Offset to shader data
0xC4Size of shader data
0x10Shader data. This uses the same format as the GX2 structures, but pointers are stored as offsets relative to the start of the shader data.

Shader Program Section

This section contains named shader programs that reference shaders from the shader binary section.

OffsetSizeDescription
0x04Size of shader program section
0x44Number of shader programs
0x8Shader programs

Shader Program

OffsetSizeDescription
0x04Size (or offset to next shader program)
0x44Length of name string (including null terminator)
0x84A bitfield defining the kind of shaders in this program:
0x1: Vertex shader
0x2: Pixel shader
0x4: Geometry shader

A shader program must contain a vertex and a pixel shader and may optionally contain a geometry shader.
0xC4Index of first shader binary (base_index)
0x10Null-terminated name string
Shader variation section (possible values)
Shader variation section (symbols / default values)
Shader symbol section (uniform vars)
Shader symbol section (uniform blocks)
Shader symbol section (sampler vars)
Shader symbol section (attrib vars)

Shader Variation Section

A single shader program may have different variations. Variations are defined by a set of macros, each of which can be set to a predefined set of values. Every possible combination of macro values has its own shader binaries, so the number of shader binaries in a program grows exponentially with its number of variation macros. This is why some sharcfb files are much bigger than others.

Each shader program has two shader variation sections. The first one defines the list of possible values for each shader macro. The second section only has one value per macro: its default value.

OffsetSizeDescription
0x04Size of this section
0x44Number of variation macros
0x8Variations macros

The following algorithm can be used to find the shader binary index of a specific variation:

def get_variation_index(base_index, shader_program, settings):
    index = 0
    for macro in shader_program.variation_macros:
        index *= len(macro.possible_values)
        index += macro.possible_values.index(settings[macro.name])
    
    if shader_program.has_geometry_shader:
        return base_index + index * 3
    return base_index + index * 2

Now load the vertex shader from variation_index, the pixel shader from variation_index + 1 and the geometry shader from variation_index + 2 (if it exists).

Variation Macro

OffsetSizeDescription
0x04Size (or offset to next variation macro)
0x44Length of macro name
0x84Number of values
0xC4Length of symbol name
0x10Variation macro name (null-terminated)
List of values. These are null-terminated strings stored directly after each other (without padding)
Symbol name (null-terminated)

Shader Symbol Section

OffsetSizeDescription
0x04Size of this section
0x44Number of shader symbols
0x8Shader symbols

Shader Symbol

OffsetSizeDescription
0x04Size (or offset to next shader symbol)
0x44Shader variable size
0x84Length of variable name
0xC4Length of symbol name
0x104Size of default value
0x144Number of shader variations
0x18Variable name (null-terminated)
Symbol name (null-terminated)
Default value
For each shader variation, a bool (one byte) indicating whether this variable is used by that variation