From ade2fcee61c43755ae42365aa68020afdd06ba58 Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Sat, 23 Sep 2023 09:26:04 -0400 Subject: [PATCH] Begin sphk article --- content/format/shpk.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 content/format/shpk.md diff --git a/content/format/shpk.md b/content/format/shpk.md new file mode 100644 index 0000000..d6b99db --- /dev/null +++ b/content/format/shpk.md @@ -0,0 +1,28 @@ +--- +title: "SHPK" +--- + +These are "shader packages" or a collection of vertex and pixel shaders. A good example is _"character.shpk"_ which contains - you guessed it - character shaders. + +# Header Structure + +| Offset | Type | Purpose | +| ------ | ----- | ------ | +| 0x00 | 4 character ASCII string | Magic, should read "ShPk". | +| 0x04 | Unknown 4 byte. | Unknown. | +| 0x08 | 4 character ASCII string | The format of the shader, e.g. "DX11". | +| 0x0C | 32-bit integer | File length in bytes. | +| 0x10 | 32-bit integer | Offset in bytes to the shader bytecode. | +| 0x14 | 32-bit integer | Offset in bytes to the parameter list. | +| 0x18 | 32-bit integer | Number of vertex shaders. | +| 0x1C | 32-bit integer | Number of pixel shaders. | +| 0x20 | 32-bit integer | Number of scalar parameters. | +| 0x24 | 32-bit integer | Number of resource parameters. | + +# Reading shader bytecode + +Getting the shader bytecode from a shader package is easy, it begins at the shader bytecode offset in the header. Then, move your cursor up 12 bytes (to skip the initial `DXBC` ASCII, and some garbage data). + +Then, collect a WORD (4 bytes) at a time and putting them into a buffer. Keep doing this until you hit the next `DXBC` ASCII and you found a complete shader bytecode. Rinse and repeat and you should have `N` shaders where `N = number of vertex shaders + number of pixel shaders`. + +The format of this shader bytecode is nothing special, it's simply DXBC (the bytecode format used by DirectX before DXIL). If you want to reverse this into something usable, like SPIR-V, HLSL or GLSL then you need to find a decompiler.