# gfx/shaders

`import "github.com/matjam/bunyip/gfx/shaders"`

Package shaders holds the gfx package's WGSL sources and committed SPIR-V, and the preludes that game shaders are compiled against. Compose builds WGSL without compiling; Compiler and bunyip-shader use the native Go compiler. Mesh bundles contain regular and order-independent fragment programs and optionally static/skinned vertex programs for lit and shadow passes. Embedded byte slices are shared and must not be modified.

## Variables

<a id="SpriteVert"></a>

<a id="SpriteFrag"></a>

<a id="MatrixFrag"></a>

<a id="LitFrag"></a>

<a id="SDFFrag"></a>

<a id="TextOutlineFrag"></a>

```go
var (
	//go:embed sprite.vert.spv
	SpriteVert []byte
	//go:embed sprite.frag.spv
	SpriteFrag []byte
	//go:embed colormatrix.frag.spv
	MatrixFrag []byte
	//go:embed lit.frag.spv
	LitFrag []byte
	//go:embed sdf.frag.spv
	SDFFrag []byte
	//go:embed text_outline.frag.spv
	TextOutlineFrag []byte
)
```

<a id="SkyFrag"></a>

<a id="SkyParamFrag"></a>

<a id="LineVert"></a>

<a id="LineFrag"></a>

<a id="ParticleVert"></a>

<a id="ParticleFrag"></a>

<a id="Particle3DVert"></a>

<a id="Particle3DFrag"></a>

<a id="OutlineVert"></a>

<a id="SolidFrag"></a>

<a id="DecalVert"></a>

<a id="DecalFrag"></a>

<a id="PBRFrag"></a>

<a id="PBROITFrag"></a>

<a id="TerrainFrag"></a>

<a id="PBRVert"></a>

<a id="PBRSkinVert"></a>

<a id="ShadowVert"></a>

<a id="ShadowSkinVert"></a>

<a id="ShadowFrag"></a>

<a id="PostVert"></a>

<a id="PostFrag"></a>

<a id="BrightFrag"></a>

<a id="BlurFrag"></a>

<a id="FXAAFrag"></a>

<a id="SSAOFrag"></a>

<a id="SSRFrag"></a>

<a id="SSRApplyFrag"></a>

<a id="SSRBlendFrag"></a>

<a id="DepthHalfFrag"></a>

<a id="AOBlurFrag"></a>

<a id="VelocityVert"></a>

<a id="VelocitySkinVert"></a>

<a id="VelocityFrag"></a>

<a id="TAAFrag"></a>

<a id="DOFFrag"></a>

<a id="DOFCombineFrag"></a>

<a id="MotionBlurFrag"></a>

<a id="GodRaysFrag"></a>

<a id="OITFrag"></a>

```go
var (
	//go:embed sky.frag.spv
	SkyFrag []byte
	//go:embed skyparam.frag.spv
	SkyParamFrag []byte
	//go:embed line.vert.spv
	LineVert []byte
	//go:embed line.frag.spv
	LineFrag []byte
	//go:embed particle.vert.spv
	ParticleVert []byte
	//go:embed particle.frag.spv
	ParticleFrag []byte
	//go:embed particle3d.vert.spv
	Particle3DVert []byte
	//go:embed particle3d.frag.spv
	Particle3DFrag []byte
	//go:embed outline.vert.spv
	OutlineVert []byte
	//go:embed solid.frag.spv
	SolidFrag []byte
	//go:embed decal.vert.spv
	DecalVert []byte
	//go:embed decal.frag.spv
	DecalFrag []byte
	//go:embed pbr.frag.spv
	PBRFrag []byte
	//go:embed pbr_oit.frag.spv
	PBROITFrag []byte
	// TerrainFrag blends four tiling layers by a splat map, for gfx.Terrain.
	//go:embed terrain.frag.spv
	TerrainFrag []byte
	//go:embed pbr.vert.spv
	PBRVert []byte
	//go:embed pbr_skin.vert.spv
	PBRSkinVert []byte
	//go:embed shadow.vert.spv
	ShadowVert []byte
	//go:embed shadow_skin.vert.spv
	ShadowSkinVert []byte
	//go:embed shadow.frag.spv
	ShadowFrag []byte
	//go:embed post.vert.spv
	PostVert []byte
	//go:embed post.frag.spv
	PostFrag []byte
	//go:embed bright.frag.spv
	BrightFrag []byte
	//go:embed blur.frag.spv
	BlurFrag []byte
	//go:embed fxaa.frag.spv
	FXAAFrag []byte
	//go:embed ssao.frag.spv
	SSAOFrag []byte
	//go:embed ssr.frag.spv
	SSRFrag []byte
	//go:embed ssrapply.frag.spv
	SSRApplyFrag []byte
	//go:embed ssrblend.frag.spv
	SSRBlendFrag []byte
	//go:embed depthhalf.frag.spv
	DepthHalfFrag []byte
	//go:embed aoblur.frag.spv
	AOBlurFrag []byte
	//go:embed velocity.vert.spv
	VelocityVert []byte
	//go:embed velocity_skin.vert.spv
	VelocitySkinVert []byte
	//go:embed velocity.frag.spv
	VelocityFrag []byte
	//go:embed taa.frag.spv
	TAAFrag []byte
	//go:embed dof.frag.spv
	DOFFrag []byte
	//go:embed dofcombine.frag.spv
	DOFCombineFrag []byte
	//go:embed motionblur.frag.spv
	MotionBlurFrag []byte
	//go:embed godrays.frag.spv
	GodRaysFrag []byte
	//go:embed oit.frag.spv
	OITFrag []byte
)
```

## Functions

<a id="Bundle"></a>

### Bundle

```go
func Bundle(stages map[Stage][]byte) []byte
```

Bundle copies stage programs into a new blob in stage-number order. Supply only defined Stage values and include StageFrag for Unbundle. It does not validate SPIR-V program bytes.

<a id="Compose"></a>

### Compose

```go
func Compose(kind Kind, stage Stage, source string) (string, int, error)
```

Compose adds Bunyip's WGSL declarations and entry point around game source. Kind defaults to Sprite. The second result is the number of prefix lines before the source body, useful when reading compiler diagnostics.

<a id="HasVertexHook"></a>

### HasVertexHook

```go
func HasVertexHook(source string) bool
```

HasVertexHook reports whether the source declares the optional vertex hook. Comments are ignored, including nested block comments.

<a id="Unbundle"></a>

### Unbundle

```go
func Unbundle(data []byte) (map[Stage][]byte, error)
```

Unbundle splits a bundle into its stages; plain SPIR-V comes back as the fragment stage alone. Returned slices alias data. It validates bundle record bounds and stage numbers, but not the SPIR-V bytes themselves; NewMeshShader validates those when loading the programs.

## Types

<a id="Compiler"></a>

### Compiler

```go
type Compiler struct{}
```

Compiler translates WGSL to SPIR-V in Go, without external tools or cgo. Its zero value is ready to use and may be used concurrently. Compilation is synchronous. Context cancellation is checked between compiler phases; it cannot interrupt a phase already executing.

<a id="Compiler.Compile"></a>

#### Compiler.Compile

```go
func (c Compiler) Compile(ctx context.Context, kind Kind, source string) ([]byte, error)
```

Compile composes game source with Bunyip's bindings and entry points. Kind defaults to Sprite. Sprite returns fragment SPIR-V; Mesh returns a bundle containing both fragment variants and, when source defines vertex, all four vertex variants. Returned bytes can be stored and loaded later.

<a id="Compiler.CompileRaw"></a>

#### Compiler.CompileRaw

```go
func (Compiler) CompileRaw(ctx context.Context, source string) ([]byte, error)
```

CompileRaw compiles a complete WGSL module with explicit entry points and bindings. It does not add Bunyip helpers. Output uses SPIR-V 1.3 and Vulkan coordinates without an automatic Y flip. The caller must match the renderer's pipeline and descriptor interface. This method creates no GPU resources. Module-level private initializers are currently rejected because Naga can drop them; use constants or assign in the entry point or hook instead.

<a id="Compiler.CompileStage"></a>

#### Compiler.CompileStage

```go
func (c Compiler) CompileStage(ctx context.Context, kind Kind, stage Stage, source string) ([]byte, error)
```

CompileStage composes and compiles one Bunyip shader stage. Use Compile for complete game shaders. Diagnostics refer to the composed source; errors include the prefix line count for locating the original body.

<a id="Kind"></a>

### Kind

```go
type Kind string
```

Kind is which pipeline a game shader is written for.

<a id="Sprite"></a>

<a id="Mesh"></a>

```go
const (
	// Sprite shaders colour 2D fragments: sprites, text, shapes.
	Sprite Kind = "sprite"
	// Mesh shaders adjust a Surface before the engine lights it, and may
	// move vertices first.
	Mesh Kind = "mesh"
)
```

<a id="Stage"></a>

### Stage

```go
type Stage uint32
```

Stage is one program of a mesh shader.

<a id="StageFrag"></a>

<a id="StageVert"></a>

<a id="StageSkinVert"></a>

<a id="StageShadowVert"></a>

<a id="StageShadowSkinVert"></a>

<a id="StageOITFrag"></a>

```go
const (
	StageFrag           Stage = iota // the lit fragment program
	StageVert                        // static meshes, lit pass
	StageSkinVert                    // skinned meshes, lit pass
	StageShadowVert                  // static meshes, shadow pass
	StageShadowSkinVert              // skinned meshes, shadow pass
	StageOITFrag                     // the fragment program of the order-independent transparency pass

)
```

<a id="ParseStage"></a>

#### ParseStage

```go
func ParseStage(s string) (Stage, bool)
```

ParseStage reads a -stage flag value.

<a id="Stage.String"></a>

#### Stage.String

```go
func (s Stage) String() string
```

String names a stage as bunyip-shader's -stage flag spells it.
