shaders

package
v1.17.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2026 License: BSD-3-Clause Imports: 16 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package shaders implements the `glutter-gen shaders` subcommand: it compiles the WGSL shaders of the explicit renderer to SPIR-V and to HLSL (shader model 5.1) with naga, and writes the Go file that holds them (D20, D25).

Flutter: no counterpart.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckApp

func CheckApp(dir string, naga Compiler, opts AppOptions) error

CheckApp generates into a temporary directory and compares with dir: the files of a new generation must be in dir with the same bytes, and dir must hold no .spv without a .wgsl. It fails with GPUShadersOutOfDate, naming the files that differ.

Flutter: no counterpart

func Generate

func Generate(pkg, srcDir, outDir string, naga Compiler, gpu bool) error

Generate reads srcDir/shaders/*.wgsl and writes outDir/shaders/*.spv and outDir/shaders_gen.go, which holds the HLSL of each shader (naga's, rewritten by rewriteHLSL and checked against the WGSL by checkHLSL). The files are the same for the same shaders and the same naga, so a test can generate into a temporary directory and compare.

gpu allows what the shaders of an app may have and the 2D renderer's may not (Parse).

The generated file is in the Go package pkg. A package other than explicit gets the types of explicit by their qualified names (a package of test shaders, which no program that is not a test links).

func GenerateApp

func GenerateApp(srcDir, outDir string, naga Compiler, opts AppOptions) error

GenerateApp reads srcDir/*.wgsl and writes outDir/<name>.spv and outDir/shaders_gen.go. The files are the same for the same shaders and the same naga, so CheckApp can generate into a temporary directory and compare. It fails with ShadersNoShaders for a directory without a .wgsl file.

Flutter: no counterpart

func PackageName

func PackageName(dir string) string

PackageName is the name of the Go package of a directory: the package clause of a Go file in it other than shaders_gen.go, else the name of the directory made into an identifier ("shaders" when it has none).

func Run

func Run(args []string) error

Run executes `glutter-gen shaders [-dir dir] [-naga command]` with the arguments that follow the subcommand name. In dir (internal/gpu/explicit) it reads shaders/*.wgsl, writes shaders/*.spv and shaders_gen.go (which holds the HLSL), and removes the .spv of a shader that is gone.

Flutter: no counterpart

func RunGPU

func RunGPU(args []string) error

RunGPU executes `glutter-gen gpushaders [-check] [-naga command] [-wine auto|on|off] dir` with the arguments that follow the subcommand name. It reads dir/*.wgsl and writes dir/<name>.spv and dir/shaders_gen.go, and removes the .spv of a shader that is gone. With -check it writes nothing and fails with GPUShadersOutOfDate when the files in dir differ from a new generation.

Flutter: no counterpart

Types

type AppOptions

type AppOptions struct {
	// Package is the name of the Go package of shaders_gen.go; the name of the package of the directory when
	// empty (PackageName).
	Package string
	// ValidateSPIRV checks the module written to a file; nil checks none. RunGPU gives it spirv-val.
	ValidateSPIRV func(spv string) error
	// CheckHLSL compiles the HLSL of each shader; nil checks none. RunGPU gives it D3DCompile under Wine.
	CheckHLSL func(shaders []HLSLShader) error
}

AppOptions are what GenerateApp does besides compiling.

type Binding

type Binding struct {
	Name           string
	Group, Binding int
	Kind           Kind
	// Dim is the dimension of a texture, as the generated code spells it ("" is a 2D texture).
	Dim string
	// Sample is the sample type of a texture, as the generated code spells it ("" is SampleTypeFloat).
	Sample string
	// StorageFormat is the texel format of a storage texture, as the generated code spells it
	// ("TextureFormatRGBA8Unorm").
	StorageFormat string
	// Size is the size in bytes of the type of a uniform or storage buffer (gpushaders only; 0 otherwise):
	// by the layout rules of WGSL, with one element for an array that has no length.
	Size int
	// MetalIndex is the index of the binding in the Metal argument table of its class (MetalClass): the
	// place of the binding among the bindings of the module of its class, in the order of group and binding
	// (SPEC §29.5).
	MetalIndex int
}

Binding is one `@group(g) @binding(b) var` declaration of a module.

func (Binding) MetalClass

func (b Binding) MetalClass() string

MetalClass is the argument table that the binding is in: buffer for the uniform and storage buffers, texture for the sampled and storage textures, sampler for the samplers.

func (Binding) Register

func (b Binding) Register() (class string, space, index int)

Register returns where HLSL binds the binding: constant buffers in b registers, textures and read-only storage buffers in t, samplers in s, and writable storage buffers and storage textures in u, the @binding as the register and the @group as the register space.

type Compiler

type Compiler interface {
	// Version is naga's version, such as "30.0.1".
	Version() (string, error)
	// Compile validates the WGSL file and writes its SPIR-V to the second file.
	Compile(wgsl, spirv string) error
	// CompileHLSL writes the HLSL for shader model 5.1 of the WGSL file to the second file,
	// as naga writes it (hlsl.go rewrites it afterwards).
	CompileHLSL(wgsl, hlsl string) error
	// CompileMSL writes the Metal Shading Language 2.4 of the WGSL file to the second file, as naga
	// writes it (msl.go rewrites it afterwards).
	CompileMSL(wgsl, msl string) error
}

Compiler is naga, or a stand-in for it in tests.

type Entry

type Entry struct {
	Name     string
	Stage    string // "Vertex", "Fragment" or "Compute"
	Bindings []Binding
	// WorkgroupSize is the @workgroup_size of a compute entry point, the missing dimensions 1.
	WorkgroupSize [3]int
	// ReadsInstanceIndex and ReadsVertexIndex say that a vertex entry point takes
	// @builtin(instance_index) or @builtin(vertex_index).
	ReadsInstanceIndex, ReadsVertexIndex bool
	// Inputs are the @location inputs of a vertex entry point (gpushaders only).
	Inputs []Input
}

Entry is an entry point of a module and the bindings it reads.

type HLSLEntry

type HLSLEntry struct {
	Name, Target string
}

HLSLEntry is an entry point of the HLSL, with the shader model it is compiled for.

type HLSLShader

type HLSLShader struct {
	Name    string
	HLSL    string
	Entries []HLSLEntry
}

HLSLShader is the HLSL of a shader with its entry points, for CheckHLSL.

type Input

type Input struct {
	Name     string
	Location int
	// Type is the shader-side type as the generated code spells it ("Float32x4", "Uint32").
	Type string
}

Input is an `@location(n)` input of a vertex entry point.

type Kind

type Kind string

Kind is what a binding is, as the generated code spells it.

const (
	KindUniform Kind = "BindingUniformBuffer"
	KindTexture Kind = "BindingSampledTexture"
	KindSampler Kind = "BindingSampler"
	// The kinds of step 30.2 (SPEC §27.1), which only a module generated with -gpu may have.
	KindStorageRO      Kind = "BindingStorageBufferReadOnly"
	KindStorage        Kind = "BindingStorageBuffer"
	KindStorageTexture Kind = "BindingStorageTexture"
)

The kinds of binding the design limits allow (SPEC §9.3).

type Module

type Module struct {
	Bindings []Binding
	Entries  []Entry
}

Module is what the generator reads from a WGSL file.

func Parse

func Parse(src string, gpu bool) (Module, error)

Parse reads the bindings and the entry points of a WGSL module: the declarations `@group(g) @binding(b) var<uniform> x: T;`, `var t: texture_2d<f32>;` and `var s: sampler;`, and the functions marked @vertex or @fragment. An entry point reads a binding when its name is in the body of the entry point or of a function it calls. A binding of another kind (a texture of another type), a compute entry point, or two declarations of one group and binding, is an error.

With gpu (glutter-gen shaders -gpu, the shaders of step 30.2 and later) the module may also have `var<storage, read>` and `var<storage, read_write>` buffers, `texture_storage_2d`, `_2d_array` and `_3d` textures of rgba8unorm, rgba16float, r32float and r32uint that are written, and @compute entry points with a literal @workgroup_size; the 2D renderer's own shaders have none of these.

type Naga

type Naga struct {
	// Path is the command; "naga" when empty.
	Path string
	// App makes the errors those of glutter-gen gpushaders (GPUShadersNagaMissing, GPUShadersCompileFailed).
	App bool
}

Naga runs the naga command.

func (Naga) Compile

func (n Naga) Compile(wgsl, spirv string) error

Compile runs `naga in.wgsl out.spv`, which validates the shader and writes the SPIR-V. It fails with ShadersNagaFailed, carrying naga's message, when the shader is not valid.

func (Naga) CompileHLSL

func (n Naga) CompileHLSL(wgsl, hlsl string) error

CompileHLSL runs `naga in.wgsl out.hlsl --shader-model 51`. It fails with ShadersNagaFailed, carrying naga's message, when naga cannot write the HLSL.

func (Naga) CompileMSL

func (n Naga) CompileMSL(wgsl, msl string) error

CompileMSL runs `naga in.wgsl out.metal --metal-version 2.4`. It fails with ShadersNagaFailed, carrying naga's message, when naga cannot write the MSL.

func (Naga) Version

func (n Naga) Version() (string, error)

Version runs `naga --version`. It fails with ShadersNagaMissing when naga is not installed.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL