PharoVulkan

Description

Vulkan binding for Pharo. Target GPU computing and GUI rendering

Details

Source
GitHub
Dialect
pharo (25% confidence)
Created
Nov. 20, 2026
Updated
Nov. 20, 2026

Categories

UI / Graphics

README excerpt

# PharoVulkan

Vulkan uFFI bindings and SPIR-V DSL for Pharo 13. No third-party Pharo
packages — only the system Vulkan loader and SDL2.

Two goals:
1. **Full Vulkan API** exposed via uFFI with named constants (no magic numbers)
2. **Pharo→SPIR-V compiler** — annotate a Pharo method with `<gpu>` and
   `<param:named:>` pragmas and it compiles to SPIR-V: a compute shader when
   the parameters include storage buffers, a plain function otherwise

---

## Requirements

| Dependency | Minimum version | Notes |
|---|---|---|
| Pharo | 13.1.0 | Tested on 13.1.0 SNAPSHOT |
| libvulkan.so.1 | Vulkan 1.3 | Or 1.2 + VK_KHR_synchronization2 |
| libSDL2.so | 2.0.6+ | Graphics path only |
| GPU | Any with Vulkan 1.3 compute | Developed against an Intel Iris Plus (ICL GT2) and Mesa lavapipe on Linux |
| SPIRV-Tools | v2026.1 | Optional. `PVSpirvValidator` finds `spirv-val` on `PATH`; without it the validation tests report the tool as missing rather than failing |

Linux only at this stage (uFFI library paths are Linux-specific).

---

## Loading

There is no published repository yet, so load from a clone on disk:

```smalltalk
Metacello new
	baseline: 'PharoVulkan';
	repository: 'tonel:///path/to/PharoVulkan/src';
	load.

"or one group only:"
Metacello new
	baseline: 'PharoVulkan';
	repository: 'tonel:///path/to/PharoVulkan/src';
	load: 'compiler'.
```

`load` with no argument loads the `default` group: bindings, core, compute and graphics — enough
to open a device, dispatch a kernel and draw. Named groups load less, or more:

| Group | Loads | Needs a driver |
|---|---|---|
| `bindings` | `PharoVulkan-Bindings` | yes, to call anything |
| `spirv` | `PharoVulkan-SPIRV` | no |
| `compiler` | SPIR-V builder and the Pharo→SPIR-V compiler | no — it answers bytes |
| `core` | the Vulkan lifecycle, on the bindings | yes |
| `compute` | the compute path | yes |
| `graphics` | the graphics path, including SDL2 | yes, and a display server |
| `default` | bindings, core, compute, graphics | yes |
| `examples` | the `<Subject>Examples` classes | depends on the example |
| `generator` | the `vk.xml` and grammar readers | no |
| `tests` | everything, including all seven test packages | yes for most of the suite |

The `compiler` group is the one that loads on a machine with no `libvulkan.so.1`: nothing in it
reaches the bindings, which is a rule `PVArchitectureTest` enforces rather than a claim.

---

## Package layout

Sixteen packages: nine of code and seven of tests. Inside each, class tags group the classes for
the browser and carry no meaning beyond that.

| Package | Classes | What it contains |
|---|---|---|
| `PharoVulkan-Bindings` | 144 | uFFI Vulkan commands, structures, handle wrappers, every named constant class, `VkResult` and `VkError` |
| `PharoVulkan-SPIRV` | 39 | SPIR-V builder: `SpvModule`, `SpvBuilder`, `SpvFunction`, `SpvBlock`, the type hierarchy, constant pool, serialiser, validator |
| `PharoVulkan-Compiler` | 24 | Pharo bytecode → SPIR-V: decode, register IR, type inference, blocks, recogniser, emit, the DSL selector table, and the `spirv-val` wrapper |
| `PharoVulkan-Core` | 17 | `VkContext`, `VkDevice`, `VkPhysicalDevice`, `VkQueue`, `VkBuffer`, command pool and buffer, `VkShaderModule`, `VkPipelineLayout`, the device capability survey |
| `PharoVulkan-Compute` | 18 | Descriptor set layout, descriptor pool and set, compute pipeline, `PVComputeKernel`, the cooperative matrix path, the benchmarks |
| `PharoVulkan-Graphics` | 17 | `VkSurface`, `VkSwapchain`, `VkRenderPass`, `VkFramebuffer`, `VkGraphicsPipeline`, offscreen targets, the sync wrappers, the scenes, and the SDL2 integration |
| `PharoVulkan-Generator` | 12 | Reads `vk.xml` and `spirv.core.grammar.json` and emits binding classes |
| `PharoVulkan-Examples` | 20 | One `<Subject>Examples` class per subject, holding every `<sampleInstance>` example |
| `PharoVulkan-Inspector` | 1 | `PVFactRow`, the one line of the two-column fact table every inspector view of this project shows |

| Test package | Test classes (tests) |
|---|---|
| `PharoVulkan-Bindings-Tests` | `VkBindingsTest` (286) |
| `PharoVulkan-SPIRV-Tests` | `SpvBuilderTest` (24) |
| `PharoVulkan-Compiler-Tests` | `PVCompilerTest` (86), `PVSpirvValidatorTest` (33) |
| `PharoVulkan-Core-Tests` | `VkCoreTest` (68) |
| `PharoVulkan-Compute-Tests` | `PVComputeTest` (63) |
| `PharoVulkan-Graphics-Tests` | `PVGraphicsTest` (53), `VkImageTest` (8) |
| `PharoVulkan-Tests` | `PVArchitectureTest` (22), `PVDocumentationTest` (7), `PVExamplesTest` (7), `PVFactRowTest` (5) |

662 tests in all. Each test package also carries a `Fixtures` tag for the stubs its tests build, so
the `Tests` tag holds test classes and nothing else.

Which package may depend on which is stated as a test, not as a paragraph:
`PVArchitectureTest class >> allowedDependencies` declares the allowed outgoing edges of all
sixteen packages, and its twenty-two tests fail both on an edge the code makes and the table does
not grant, and on an edge the table grants that the baseline does not declare.

See `doc/architecture.md` for the full layer diagram and class descriptions.

---

## Running the compute example

The test suite includes a GPU vector-add: 64 floats `a[i] + b[i]` dispatched
with 64 threads, verified against a CPU reference.

```smalltalk
"In a Pharo Playground:"
PVComputeTest new testVectorAdd
```

Or run the full compute test suite:

```smalltalk
TestRunner runClass: PVComputeTest
```

### What the test does

1. Builds a SPIR-V compute shader directly with `SpvBuilder` (accesses
   `gl_GlobalInvocationID` and three storage buffers)
2. Creates a headless `VkContext` (no window)
3. Allocates three 256-byte buffers (64 × f32)
4. Fills buffer A with `[0.0, 1.0, … 63.0]`, buffer B with the same
5. Runs `PVComputeKernel dispatch: 1 y: 1 z: 1` (64 threads via LocalSize)
6. Reads buffer C back and asserts `c[i] == i * 2.0`

### Minimal headless compute in a Playground

This runs `PVKernelExamples >> #vectorAdd:a:b:out:` — a four-parameter compute
kernel, one `WorkItem` plus three storage buffers — through the Pharo→SPIR-V
compiler. It is the same sequence as `PVComputeTest >> #testVectorAddFromCompiledMethod`,
so it is covered by the suite.

```smalltalk
| n ctx bufA bufB bufC kernel result |
n := 64.

"1. Create context (no window needed)"
ctx := VkContext computeContext.

"2. Allocate three storage buffers of n floats each.
    storageBuffer:on: takes an element count and applies the usage flag for you."
bufA := VkBuffer storageBuffer: n on: ctx device.
bufB := VkBuffer storageBuffer: n on: ctx device.
bufC := VkBuffer storageBuffer: n on: ctx device.

"3. Fill the inputs; putF32s: does the IEEE-754 packing"
bufA putF32s: ((0 to: n - 1) asArray collect: [:i | i asFloat]).
bufB putF32s: ((0 to: n - 1) asArray collect: [:i | i asFloat]).
bufC putF32s: (Array new: n withAll: 0.0).

"4. Compile the kernel and build the pipeline.
    Buffers bind in parameter order: a, b, output."
kernel := PVComputeKernel fromMethod: (PVKernelExamples >> #vectorAdd:a:b:out:) on: ctx.
kernel bindBuffer: bufA; bindBuffer: bufB; bindBuffer: bufC.
kernel prepare.

"5. One workgroup, 64 invocations"
kernel dispatch: 1 y: 1 z: 1.

"6. Read back: [0.0, 2.0, 4.0, … 126.0]"
result := bufC getF32s.

"7. Clean up in reverse creation order"
kernel destroy.
bufA destroy. bufB destroy. bufC destroy.
ctx destroy.
```

### `<gpu>` alone does not produce a shader module

A `<gpu>` method whose parameters are all scalars — `PVKernelExamples >> #relu:`,
for example — compiles to a SPIR-V *function*: no `OpEntryPoint`, no execution
mode, no descriptor bindings. `PVComputeKernel` cannot load it, and `spirv-val`
rejects it on its own with `No OpEntryPoint instruction was found`. An entry
point is emitted only for

- a compute kernel: at least one storage buffer parameter, usually with a
  `#workItem` parameter beside it, as in `vectorAdd:a:b:out:` above, or
- a `<gpuVertex>` or `<gpuFragment>` shader.

`PVKernelCompiler class >> computeParamTypes` is the list that decides, and it
derives from the emitter's own tables rather than being written out by hand:
`#workItem`, the buffer types `#bufferOf_f32`, `#bufferOf_i32`, `#bufferOf_f16`,
`#matrixABufferOf_f16`, `#matrixBBufferOf_f16`, `#accumulatorBufferOf_f32`, and
the shared-memory types `#sharedOf_f32`, `#sharedOf_i32`, `#sharedOf_f16`.

Scalar `<gpu>` methods are lowering targets for those, not standalone shaders.
Giving them an entry point would mean inventing a descriptor-binding convention
for scalar parameters, which the `WorkItem`-plus-storage-buffer kernel ABI
deliberately does not have. Of the 32 methods in `PVKernelExamples`, 21 get an
entry point and 11 are scalar functions with no entry point for the validator to
start from. `PVSpirvValidatorTest` runs spirv-val over 10 of the 21, plus
`relu:` as the negative case that must be rejected with
`No OpEntryPoint instruction was found`.

For a full walkthrough see `doc/compute-tutorial.md`.

---

## Compute features beyond vector add

Everything in this section is covered by `PVComputeTest` on the device, not only
by compiler tests.

### Control flow

A compute kernel may contain one `ifTrue:ifFalse:`, one `whileTrue:`, or an
outer `whileTrue:` whose body is an inner `whileTrue:` and nothing else. See
"Known limitations" below for what is refused.

### Work item

`#workItem` parameters answer `x`, `y`, `z` for the global invocation id,
`localX`, `localY`, `localZ` for the position inside the workgroup, `gridWidth`
for the dispatch width in invocations, and `barrier` for a workgroup barrier.
`PVWorkItem` is the host-side class, so a kernel body also runs in the image.

### Workgroup size

`<localSize: #( 16 16 1 )>` on the kernel. The default is `#( 64 1 1 )`. The
grid must cover the problem exactly: a benchmark whose order is not a multiple
of the local size raises `PVTypeError`.

### Half precision

`#f16` and `#bufferOf_f16`, with `asF16` and `asF32` for conversion, and 
← Back to results