codegen

package
v0.17.16 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Desktop OpenGL versions
	Version330 = Version{Major: 3, Minor: 30, ES: false} // OpenGL 3.3 Core
	Version400 = Version{Major: 4, Minor: 0, ES: false}  // OpenGL 4.0
	Version410 = Version{Major: 4, Minor: 10, ES: false} // OpenGL 4.1
	Version420 = Version{Major: 4, Minor: 20, ES: false} // OpenGL 4.2
	Version430 = Version{Major: 4, Minor: 30, ES: false} // OpenGL 4.3 (compute shaders)
	Version450 = Version{Major: 4, Minor: 50, ES: false} // OpenGL 4.5
	Version460 = Version{Major: 4, Minor: 60, ES: false} // OpenGL 4.6

	// OpenGL ES / WebGL versions
	VersionES300 = Version{Major: 3, Minor: 0, ES: true}  // ES 3.0 / WebGL 2.0
	VersionES310 = Version{Major: 3, Minor: 10, ES: true} // ES 3.1 (compute shaders)
	VersionES320 = Version{Major: 3, Minor: 20, ES: true} // ES 3.2
)

Common GLSL versions.

Functions

This section is empty.

Types

type BindingMapKey

type BindingMapKey struct {
	Group   uint32
	Binding uint32
}

BindingMapKey identifies a resource binding for the BindingMap.

type BoundsCheckPolicies

type BoundsCheckPolicies struct {
	// ImageLoad controls bounds checking for image load operations.
	ImageLoad BoundsCheckPolicy
	// ImageStore controls bounds checking for image store operations.
	ImageStore BoundsCheckPolicy
}

BoundsCheckPolicies holds per-resource-type bounds check policies.

type BoundsCheckPolicy

type BoundsCheckPolicy uint8

BoundsCheckPolicy controls how out-of-bounds resource accesses are handled.

const (
	// BoundsCheckUnchecked performs no bounds checking.
	BoundsCheckUnchecked BoundsCheckPolicy = iota
	// BoundsCheckRestrict clamps indices to valid range.
	BoundsCheckRestrict
	// BoundsCheckReadZeroSkipWrite returns zero for reads, skips writes.
	BoundsCheckReadZeroSkipWrite
)

type Features

type Features uint32

Features represents required GLSL features as bitflags. Matches Rust naga's back::glsl::Features.

const (
	FeatureBufferStorage         Features = 1 << 0
	FeatureArrayOfArrays         Features = 1 << 1
	FeatureDoubleType            Features = 1 << 2
	FeatureFullImageFormats      Features = 1 << 3
	FeatureMultisampledTextures  Features = 1 << 4
	FeatureMultisampledTexArrays Features = 1 << 5
	FeatureCubeTexturesArray     Features = 1 << 6
	FeatureComputeShader         Features = 1 << 7
	FeatureImageLoadStore        Features = 1 << 8
	FeatureConservativeDepth     Features = 1 << 9
	FeatureNoPerspective         Features = 1 << 11
	FeatureSampleQualifier       Features = 1 << 12
	FeatureClipDistance          Features = 1 << 13
	FeatureCullDistance          Features = 1 << 14
	FeatureSampleVariables       Features = 1 << 15
	FeatureDynamicArraySize      Features = 1 << 16
	FeatureMultiView             Features = 1 << 17
	FeatureTextureSamples        Features = 1 << 18
	FeatureTextureLevels         Features = 1 << 19
	FeatureImageSize             Features = 1 << 20
	FeatureDualSourceBlending    Features = 1 << 21
	FeatureInstanceIndex         Features = 1 << 22
	FeatureTextureShadowLod      Features = 1 << 23
	FeatureSubgroupOperations    Features = 1 << 24
	FeatureTextureAtomics        Features = 1 << 25
	FeatureShaderBarycentrics    Features = 1 << 26
)

type Options

type Options struct {
	// LangVersion is the target GLSL version.
	// Defaults to Version330 if zero.
	LangVersion Version

	// EntryPoint specifies which entry point to compile.
	// If empty, the first entry point is compiled.
	EntryPoint string

	// SamplerBindingBase adds offset to sampler binding indices.
	SamplerBindingBase uint32

	// TextureBindingBase adds offset to texture binding indices.
	TextureBindingBase uint32

	// UniformBindingBase adds offset to uniform buffer binding indices.
	UniformBindingBase uint32

	// StorageBindingBase adds offset to storage buffer binding indices.
	StorageBindingBase uint32

	// WriterFlags control output formatting.
	WriterFlags WriterFlags

	// ForceHighPrecision forces highp precision for all float types (ES only).
	// If false, uses default precision qualifiers.
	ForceHighPrecision bool

	// BoundsCheckPolicies controls bounds checking for resource accesses.
	// Matches Rust naga's proc::BoundsCheckPolicies.
	BoundsCheckPolicies BoundsCheckPolicies

	// BindingMap maps resource bindings to flat GL binding indices.
	// Matches Rust naga's back::glsl::BindingMap.
	// When set, layout(binding = N) qualifiers are emitted.
	BindingMap map[BindingMapKey]uint8

	// PipelineConstants provides values for pipeline-overridable constants.
	// Keys are either "@id(N)" numeric IDs as strings or override names.
	// Values are float64 (NaN means "not set, use default").
	// If provided, overrides are resolved before compilation.
	PipelineConstants ir.PipelineConstants
}

Options configures GLSL code generation.

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns sensible default options for GLSL generation.

type TextureMapping

type TextureMapping struct {
	// TextureBinding is the (group, binding) of the texture global variable.
	TextureBinding ir.ResourceBinding

	// SamplerBinding is the (group, binding) of the associated sampler, if any.
	// Nil for storage images which have no sampler.
	SamplerBinding *ir.ResourceBinding
}

TextureMapping describes a combined texture-sampler pair generated by the GLSL backend. Matches Rust naga's TextureMapping (glsl/mod.rs:401-406).

In GLSL, separate texture2D + sampler become a single sampler2D uniform. The combined uniform uses the texture's binding. The GLES HAL needs to know which sampler is associated with which texture to bind the GL sampler object to the correct texture unit (not the sampler's own WGSL binding).

type TranslationInfo

type TranslationInfo struct {
	// EntryPointNames maps original entry point names to generated GLSL names.
	EntryPointNames map[string]string

	// UsedExtensions lists GLSL extensions required by the shader.
	UsedExtensions []string

	// RequiredVersion is the minimum GLSL version needed for this shader.
	// May be higher than the requested version if features require it.
	RequiredVersion Version

	// TextureSamplerPairs lists the combined texture-sampler pairs generated.
	// Each entry is "textureName_samplerName".
	TextureSamplerPairs []string

	// TextureMappings maps combined sampler2D uniform names to their texture
	// and sampler source bindings. Used by GLES HAL to build SamplerBindMap
	// (bind GL sampler to texture's unit, not sampler's own binding).
	// Matches Rust naga ReflectionInfo.texture_mapping.
	TextureMappings map[string]TextureMapping

	// Uniforms maps global variable block names to their binding info.
	// Used by GLES HAL for runtime binding fallback on GL < 4.2 where
	// layout(binding=N) is unavailable. After glLinkProgram, the HAL
	// queries block indices by name and assigns bindings via GL calls.
	// Matches Rust naga ReflectionInfo.uniforms.
	Uniforms []UniformInfo
}

TranslationInfo contains metadata about the translation.

func Compile

func Compile(module *ir.Module, options Options) (string, TranslationInfo, error)

Compile generates GLSL source code from an IR module. Returns the GLSL source as a string, translation info, or an error.

type UniformInfo added in v0.17.14

type UniformInfo struct {
	// BlockName is the GLSL block name (e.g., "Uniforms_block_0Vertex").
	// Used with glGetUniformBlockIndex (uniform buffers) or
	// glGetShaderStorageBlockIndex (storage buffers).
	BlockName string

	// Binding is the source (group, binding) from the IR.
	Binding ir.ResourceBinding

	// IsStorage is true for storage buffers (SSBO), false for uniform buffers (UBO).
	IsStorage bool
}

UniformInfo describes a GLSL uniform or storage buffer block for reflection. Used by the HAL runtime binding fallback on GL < 4.2 where layout(binding=N) is unavailable and bindings must be assigned after linking via GL calls. Matches Rust naga ReflectionInfo.uniforms.

type Version

type Version struct {
	Major uint8
	Minor uint8
	ES    bool // true for GLSL ES (OpenGL ES / WebGL)
}

Version represents a GLSL version.

func (Version) String

func (v Version) String() string

String returns the version as a GLSL version directive value.

func (Version) SupportsCompute

func (v Version) SupportsCompute() bool

SupportsCompute returns true if this version supports compute shaders.

func (Version) SupportsStorageBuffers

func (v Version) SupportsStorageBuffers() bool

SupportsStorageBuffers returns true if this version supports storage buffers.

func (Version) VersionNumber

func (v Version) VersionNumber() string

VersionNumber returns just the numeric version (e.g., "330", "300").

type Writer

type Writer struct {
	textutil.IndentWriter // provides Out, Indent, WriteLine, WriteIndent, PushIndent, PopIndent
	// contains filtered or unexported fields
}

Writer generates GLSL source code from IR.

func (*Writer) String

func (w *Writer) String() string

String returns the generated GLSL source code.

type WriterFlags

type WriterFlags uint32

WriterFlags control output formatting.

const (
	// WriterFlagNone uses default settings.
	WriterFlagNone WriterFlags = 0

	// WriterFlagExplicitTypes forces explicit type annotations.
	WriterFlagExplicitTypes WriterFlags = 1 << iota

	// WriterFlagDebugInfo adds source comments for debugging.
	WriterFlagDebugInfo

	// WriterFlagMinify removes unnecessary whitespace.
	WriterFlagMinify

	// WriterFlagAdjustCoordinateSpace adds gl_Position coordinate adjustment
	// at the end of vertex shaders to convert from Vulkan/WebGPU conventions
	// (Y-down, Z in [0,1]) to OpenGL conventions (Y-up, Z in [-1,1]).
	// Matches Rust naga's WriterFlags::ADJUST_COORDINATE_SPACE.
	// Emits: gl_Position.yz = vec2(-gl_Position.y, gl_Position.z * 2.0 - gl_Position.w);
	WriterFlagAdjustCoordinateSpace

	// WriterFlagForcePointSize emits gl_PointSize = 1.0 in vertex shaders.
	// Required for WebGL compatibility.
	WriterFlagForcePointSize

	// WriterFlagTextureShadowLod enables GL_EXT_texture_shadow_lod extension
	// for sampling cube/array shadow textures with explicit LOD.
	WriterFlagTextureShadowLod
)

Jump to

Keyboard shortcuts

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