Search by

1:1 PHP bindings of glslang's C API: GLSL and HLSL compiled to SPIR-V

Package info

github.com/php-io-extensions/glslang

Language:C

Type:php-ext

Ext name:ext-glslang

pkg:composer/php-io-extensions/glslang

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v0.11.0 2026-12-05 05:13 UTC

This package is auto-updated.

Last update: 2026-12-05 05:16:21 UTC


README

ext-glslang

glslang bound 1:1 into PHP 8.4+, at the 15.1.0 C API

CI glslang C API Packagist PHP License Platform OKF PIE

A GPU does not run GLSL. Vulkan takes SPIR-V, a binary intermediate language, and every shader written in GLSL or HLSL has to be compiled to it before vkCreateShaderModule() will accept it. The same SPIR-V is what tools like SPIRV-Cross read to produce Metal's MSL or another GLSL dialect, so it is also the common form shaders are kept in when one shader has to run on several graphics APIs.

glslang is the Khronos reference compiler that does that. It is the front end of glslangValidator, of the Vulkan SDK and of shaderc: give it GLSL or HLSL source, a stage and a target, and it preprocesses, parses, links and generates the SPIR-V words, with an info log that names each error by line. It is written in C++ and exposes a C API.

The only problem? PHP can't reach it. This extension binds glslang's C API one to one under the C names, at the glslang 15.1.0 API: shaders and their input, preprocessing with include callbacks written in PHP, parsing, programs and linking, I/O mapping, the resource limits, and SPIR-V generation, returned as a byte string ready for ext-vulkan's VkShaderModuleCreateInfo::$pCode. Structs are typed classes, enums are the C constants, and the calls that would crash the process are refused before glslang makes them. No defaults, no composites, no opinions: what glslang documents is what you call.

Basic usage

Most ways you will interface with ext-glslang are in two parts -

Describe the shader and compile it. The input names the language, the stage, the client API and the SPIR-V version, and carries the source and the resource limits to check it against; preprocess and parse each answer 1 on success, and the info log says why when they don't:

glslang_initialize_process();

$input = new glslang_input_t();
$input->language = GLSLANG_SOURCE_GLSL;
$input->stage = GLSLANG_STAGE_FRAGMENT;
$input->client = GLSLANG_CLIENT_VULKAN;
$input->client_version = GLSLANG_TARGET_VULKAN_1_2;
$input->target_language = GLSLANG_TARGET_SPV;
$input->target_language_version = GLSLANG_TARGET_SPV_1_5;
$input->code = <<<'GLSL'
    #version 450
    layout(location = 0) in vec2 uv;
    layout(location = 0) out vec4 color;
    void main() { color = vec4(uv, 0.5, 1.0); }
    GLSL;
$input->default_version = 100;
$input->default_profile = GLSLANG_NO_PROFILE;
$input->resource = glslang_default_resource();

$shader = glslang_shader_create($input);
if (! glslang_shader_preprocess($shader, $input) || ! glslang_shader_parse($shader, $input)) {
    exit(glslang_shader_get_info_log($shader));
}

Link it into a program and generate the SPIR-V for its stage. glslang_program_SPIRV_get() writes the words to $spirv as their bytes, which is what a shader module takes. Delete what you made when you're done:

$program = glslang_program_create();
glslang_program_add_shader($program, $shader);
if (! glslang_program_link($program, GLSLANG_MSG_SPV_RULES_BIT | GLSLANG_MSG_VULKAN_RULES_BIT)) {
    exit(glslang_program_get_info_log($program));
}
glslang_program_SPIRV_generate($program, GLSLANG_STAGE_FRAGMENT);
glslang_program_SPIRV_get($program, $spirv);

printf("%d words, magic 0x%08x\n", glslang_program_SPIRV_get_size($program), unpack('V', $spirv)[1]);
file_put_contents('frag.spv', $spirv);

glslang_program_delete($program);
glslang_shader_delete($shader);
glslang_finalize_process();
$ php compile.php
121 words, magic 0x07230203
$ spirv-val --target-env vulkan1.2 frag.spv && echo valid
valid

0x07230203 is SPIR-V's magic number, the first word of every module. With ext-vulkan loaded, the string goes straight into a shader module:

$info = new VkShaderModuleCreateInfo();
$info->codeSize = strlen($spirv);
$info->pCode = $spirv;
vkCreateShaderModule($device, $info, 0, $module);

Requirements

OS Linux or macOS. composer.json lists those two families.
PHP 8.4 or newer, NTS or ZTS.
Extensions None.
Libraries glslang 15.1.0 or newer with its headers, and on Linux the SPIRV-Tools it links with. 15.1.0 is the floor, the version Ubuntu 24.04 and Debian Trixie ship: nothing newer is bound.

Getting glslang

glslang installs no pkg-config file of its own, so the build finds it by its headers (include/glslang/build_info.h) under /opt/homebrew, /usr/local or /usr, in that order; --with-glslang-dir=DIR names another prefix. What each platform ships is different, and the build takes both as they are:

macOS (Homebrew). Homebrew's glslang is a shared library that carries its own SPIRV-Tools; nothing else is needed. Homebrew is at glslang 16, which works: the binding only calls what 15.1.0 already had.

xcode-select --install                 # a C compiler
brew install glslang php@8.4           # glslang pulls in spirv-tools and spirv-headers

Debian Trixie, Ubuntu 24.04+, Raspberry Pi OS Trixie. Debian and Ubuntu ship glslang as static archives in glslang-dev, and glslang's SPIR-V optimizer as libSPIRV-Tools-opt.a in spirv-tools. Both are build-time only: they are linked into glslang.so, which then needs neither package at run time. glslang-dev does not pull in spirv-tools, so install both. On Ubuntu 24.04, glslang 15.1.0 is in noble-updates and noble-security, which a stock install has enabled; the noble release pocket has 14.0.0, older than the floor.

sudo apt install build-essential glslang-dev spirv-tools php8.4-dev

Check what you have before building:

grep -E 'GLSLANG_VERSION_(MAJOR|MINOR|PATCH)' /usr/include/glslang/build_info.h                      # Debian, Ubuntu
grep -E 'GLSLANG_VERSION_(MAJOR|MINOR|PATCH)' "$(brew --prefix glslang)/include/glslang/build_info.h"  # macOS

configure stops with ext-glslang needs glslang 15.1.0 or newer on an older one, and with glslang does not link when the static archives are there without spirv-tools. Linked statically, glslang and SPIRV-Tools stay inside the module: it exports nothing but its module entry, so another copy of either library loaded in the same process never takes its calls.

Installation

With PHP PIE (recommended)

PIE is the PHP Foundation's installer for extensions. It downloads the tagged source from Packagist, builds it against the PHP on your PATH, installs the module and enables it. sudo where your PHP's extension directory is root-owned, as on Debian; not on a Homebrew Mac. Install glslang first, as above:

pie install php-io-extensions/glslang
pie install php-io-extensions/glslang --with-glslang-dir=/opt/glslang   # glslang somewhere else

Manually (install script)

The repository ships two installers, one per platform. Each one builds for each PHP it targets, installs glslang.so into that PHP's extension_dir, writes 30-glslang.ini into its conf.d scan directory, and checks the module loads. The macOS one builds in a temporary copy of the checkout against brew --prefix glslang and ad-hoc signs the module so the system loads it on first use; the Debian one checks for glslang-dev and spirv-tools first, builds in the checkout, writes the ini into the CLI, FPM and Apache conf.d of that PHP, and cleans the build products out afterwards.

git clone https://github.com/php-io-extensions/glslang.git
cd glslang

bash install-debian-trixie.sh          # Debian Trixie, Ubuntu 24.04+, Raspberry Pi OS: the php on PATH
bash install-macos.sh                  # Homebrew php@8.4 and php@8.4-zts
bash install-macos.sh /path/to/bin/php # the macOS script takes specific PHP binaries

Manually (phpize)

git clone https://github.com/php-io-extensions/glslang.git
cd glslang
phpize
./configure --enable-glslang           # add --with-glslang-dir=DIR for another prefix
make -j"$(nproc 2>/dev/null || sysctl -n hw.ncpu)"
sudo make install

Then enable it: add extension=glslang.so to your php.ini, or drop that line into a file in the scan directory that php --ini reports.

Verify

php --ri glslang also names the glslang headers it was built against and the library it runs with:

$ php --ri glslang

glslang

glslang support => enabled
Version => 0.11.0
glslang headers => 16.5.0
glslang library => 16.5.0

API

Function, handle, struct, field and constant names are the C names, so glslang's C API header, glslang_c_interface.h, is the reference: 43 functions, one stub per group in stubs/, each declared with its PHP types. SUPPORT.md lists every function glslang 15.1.0 declares in its C API, bound or not; the API badge is computed from it.

Stub Functions What it covers
glslang 3 glslang_get_version(), glslang_initialize_process(), glslang_finalize_process(); glslang_version_t; the GLSLANG_STAGE_*, GLSLANG_SOURCE_*, GLSLANG_CLIENT_*, GLSLANG_TARGET_*, GLSLANG_MSG_*, GLSLANG_*_PROFILE, GLSLANG_SHADER_*, GLSLANG_RESOURCE_TYPE_*, GLSLANG_REFLECTION_*, GLSLANG_OPT_*, GLSLANG_EX_* and GLSLANG_TEX_SAMP_TRANS_* constants.
glslang_resource 4 glslang_resource(), glslang_default_resource(), glslang_default_resource_string(), glslang_decode_resource_limits(); glslang_resource_t and glslang_limits_t.
glslang_shader 16 Create and delete, the preamble, binding shifts, options, the GLSL version, the default uniform block, resource set bindings, preprocess and parse, the preprocessed code, the info logs; the glslang_shader_t handle, glslang_input_t, glsl_include_callbacks_t and glsl_include_result_t.
glslang_program 20 Create and delete, add a shader, link, source text and file, I/O mapping with or without a resolver and mapper, SPIR-V generation with or without options, the SPIR-V words, their size, address and messages, the info logs; the glslang_program_t, glslang_mapper_t and glslang_resolver_t handles and glslang_spv_options_t.

The GLSLANG_*_COUNT enumerators, which no call takes as a value, are not bound.

Translations

Every C parameter is its own PHP parameter, in C order. The only differences from C:

  • const char * is a string; a length stays its own parameter: glslang_program_add_source_text($program, $stage, $text, strlen($text)).
  • const char *const *bindings is a list of strings; $num_bindings stays its own parameter.
  • The unsigned int * that glslang_program_SPIRV_get() fills is a by-reference string: the glslang_program_SPIRV_get_size() words as their bytes, in host order. glslang_program_SPIRV_get_ptr() answers their address as an int. An out struct is a by-reference parameter: glslang_get_version($version).
  • glslang's enums (glslang_stage_t, glslang_messages_t, …) are int, and the C constants carry their values.
  • A struct is a final class with one typed public property per field: glslang_input_t, glslang_resource_t, glslang_limits_t, glslang_spv_options_t, glslang_version_t, glsl_include_result_t. glslang_input_t's callbacks and glslang_resource_t's limits are embedded structs: new fills them, zeroed. glslang_resource_t's anonymous union is its one field max_dual_source_draw_buffers_ext, which glslang also names maxDualSourceDrawBuffersEXT. A pointer field is a nullable object (?glslang_resource_t $resource), and void *callbacks_ctx is any PHP value.
  • glsl_include_callbacks_t holds closures: include_system and include_local as fn(mixed $ctx, string $header_name, string $includer_name, int $include_depth): ?glsl_include_result_t, free_include_result as fn(mixed $ctx, glsl_include_result_t $result): int. glslang calls them during glslang_shader_preprocess(), on the calling thread; an exception a callback throws fails that include and is thrown from glslang_shader_preprocess(). An answer that is not ?glsl_include_result_t is a TypeError.
  • glslang_shader_t, glslang_program_t, glslang_mapper_t and glslang_resolver_t are final handles with a private constructor, not cloneable or serializable, with pointer(): int and static fromPointer(int $pointer): static. fromPointer() trusts any address. The same native pointer and class in one thread is the same PHP object.

Lifetime

Dropping the last PHP reference does not delete the native object. glslang_shader_delete(), glslang_program_delete(), glslang_glsl_mapper_delete() and glslang_glsl_resolver_delete() release their handle; a second delete passes NULL, which glslang takes.

glslang keeps some of what it is given by pointer: glslang_shader_create() keeps the address of the input's code and reads it at preprocess, and glslang_shader_set_preamble() keeps its string. The extension gives each shader its own copies, owned until glslang_shader_delete(), so the PHP input and strings can go as soon as the call returns. A program keeps the shaders added to it and, with one shader of a stage, links into that shader's own intermediate: delete the shaders after the program is done with them. glslang_default_resource_string() answers a copy of the string glslang allocates, which the extension frees.

glslang_initialize_process() is reference-counted: call glslang_finalize_process() once for each call.

Refusals

glslang reads the objects, stages and lengths it is given without checking them. The extension refuses these calls, each of which crashes the process otherwise (tests/Probes shows each crash on a probe build):

  • A glslang_shader_t or glslang_program_t that holds NULL (deleted, or made from address 0) in any call that reads it: ValueError.
  • A program given a shader that was deleted since, in link, I/O mapping, source text and file, SPIR-V generation and glslang_glsl_resolver_create(): ValueError.
  • A $stage the program has no intermediate for, in glslang_program_add_source_text(), glslang_program_set_source_file(), glslang_program_SPIRV_generate*() and glslang_glsl_resolver_create(): a stage outside glslang_stage_t, a program not linked yet, or a stage none of its shaders has. ValueError.
  • A program whose link failed, in glslang_program_add_source_text(), glslang_program_set_source_file(), glslang_program_SPIRV_generate*() and glslang_glsl_resolver_create(): a failed link can leave a stage's intermediate unset (two ES shaders of one stage, or ES mixed with non-ES), and glslangValidator and shaderc never generate after a failed link either. glslang links a program once, so its first link decides. ValueError.
  • An input without a resource in glslang_shader_preprocess() or glslang_shader_parse(): ValueError.
  • A $num_bindings below 0 or larger than the list in glslang_shader_set_resource_set_binding(): ValueError. A list element that is not a string is a TypeError.
  • A $len below 0 or larger than the string in glslang_program_add_source_text(): ValueError.
  • An include result without a header_name, or whose header_length is below 0 or past its header_data: ValueError, thrown from glslang_shader_preprocess().

Extensions you might use with this one

  • ext-vulkan: takes the SPIR-V this extension generates as VkShaderModuleCreateInfo::$pCode.
  • ext-opengl: compiles GLSL source itself with glShaderSource(); glslang checks that source against a profile and version first.

AI-assisted development

This codebase was built with LLM assistance under human direction. Prompts, reviews, and architectural decisions were owned by a professional software engineer — the model accelerates drafting and exploration; it does not replace judgment on APIs, dependency direction, or what ships.

Agentic Documentation

This repository carries an Open Knowledge Format bundle at .okf/: Markdown with YAML frontmatter, readable by you and loadable by a coding agent. It records what the README doesn't: every function, class and constant by stub, how handles keep one PHP object per native pointer, what the extension owns for each shader and program and why, how inputs, resources and include results are copied, each refusal and what it guards, how glslang is found and linked on each platform, how to build release and probe builds, how to add a binding, and a log of every change that altered behavior.

Start at .okf/index.md. AGENTS.md is the short rule sheet an agent reads before touching the C, the stubs, or packaging; each rule points into the bundle for the reason behind it.

Point your agent at the repo and it has the same context the maintainer has. Point yourself at it when the README's answer is "it does X" and you want "because".

Resources

Running the test suite

The suite is Pest v4: GLSL compiled to SPIR-V, an info log for a shader that does not parse, the code and preamble glslang keeps by pointer, SPIR-V options, include callbacks (resolved, failed, throwing and mistyped), the resource limits and their text form, the version, and handle identity. It runs on Linux and macOS. Install the module first (the probe tests start children that load it from the PHP's extension_dir), then:

composer install
php -d memory_limit=128M vendor/bin/pest
Tests:    17 skipped, 20 passed (42 assertions)
Duration: 0.25s

The seventeen skipped tests are in tests/Probes: they run only against a probe build (./configure --enable-glslang --enable-glslang-probes, or GLSLANG_CONFIGURE=--enable-glslang-probes bash install-macos.sh), which can turn each refusal off to show the crash it prevents. Each probe runs its children with -n and ext-glslang alone, and on macOS under libgmalloc, so a read of freed or short memory faults every time. Never install a probe build for use.

A module only loads in the PHP whose phpize built it: for Homebrew's ZTS build, run phpize and pass --with-php-config from /opt/homebrew/opt/php@8.4-zts/bin. Set XDEBUG_MODE=off for any timing you intend to read.

Contributing

Open an issue first for anything beyond a typo. Pull requests change a stub in stubs/ and the C under src/ together, regenerate the *_arginfo.h files with gen_stub, regenerate SUPPORT.md and the API badge with php scripts/support.php, and add a Pest test for the behavior. A new refusal needs a probe test that shows the crash it prevents. CI must be green on every cell of the matrix, NTS and ZTS alike. Read AGENTS.md for the rules the code follows.

Security

ext-glslang hands shader source, which can come from the network, to glslang. See SECURITY.md for what it guards against and how to report a vulnerability.

License

MIT. See LICENSE. glslang is under the BSD-3-Clause and other permissive licenses listed in its LICENSE.txt; on macOS it is linked as a shared library, and on Linux the build links Debian's static archives of it and of SPIRV-Tools (Apache-2.0) into the module, which ships with neither.