Help us shape the future of Go! Take the 2026 Go Developer Survey

raytracing

package
v0.34.5 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package raytracing provides core-level resource management for ray tracing acceleration structures (BLAS and TLAS).

This package is internal to wgpu and not intended for direct use by applications. The public API (core/ thin wrappers) will be added separately.

Architecture follows Rust wgpu-core resource.rs (Blas/Tlas structs) with compaction state tracking and build dependency ordering.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompactionSavings

func CompactionSavings(blas *Blas) uint64

CompactionSavings returns the memory saved by compaction in bytes.

Returns 0 if the BLAS has not reached Ready or Compacted state, or if the compacted size is larger than the original (unlikely but possible for pathological geometry).

func CompleteCompaction

func CompleteCompaction(blas *Blas) error

CompleteCompaction marks compaction as done after a copy-compact operation.

Transitions: Ready -> Compacted. Returns an error if the BLAS is not in Ready state. After this call, no further compaction is possible.

The caller is responsible for performing the actual copy-compact via the HAL before calling this function.

func RequestCompaction

func RequestCompaction(blas *Blas) error

RequestCompaction marks a BLAS for compaction query.

Transitions: Idle -> Waiting. Returns an error if the BLAS is not in Idle state or if it was not created with ASFlagAllowCompaction.

After this call, the caller should submit a compacted-size query to the GPU. When the query result is available, call SetCompactedSize.

Matches Rust wgpu-core Blas::prepare_compact (resource.rs:3382-3403).

func SetCompactedSize

func SetCompactedSize(blas *Blas, size uint64) error

SetCompactedSize records the post-compaction size from GPU readback.

Transitions: Waiting -> Ready. Returns an error if the BLAS is not in Waiting state. The size is typically read from a query buffer after GPU execution.

Matches Rust wgpu-core Blas::on_pending_compact_resolve (resource.rs:3414-3451).

func ValidateBlasBuild

func ValidateBlasBuild(ctx DeviceContext, entry *BlasBuildEntry) error

ValidateBlasBuild validates a BLAS build entry against device limits.

Checks:

  • FeatureRayQuery must be enabled.
  • Geometry count must not exceed MaxBlasGeometryCount.
  • AABB geometries: stride >= AABBGeometryMinStride.
  • Triangle geometries: transform buffer alignment.

Reference: Rust wgpu-core command/ray_tracing.rs iter_blas.

func ValidateBlasCreate

func ValidateBlasCreate(
	ctx DeviceContext,
	desc *gputypes.CreateBlasDescriptor,
	sizes *gputypes.BlasGeometrySizeDescriptors,
) error

ValidateBlasCreate validates parameters for BLAS creation.

Checks:

  • FeatureRayQuery must be enabled.
  • Geometry count must not exceed MaxBlasGeometryCount.
  • AABB stride must be >= AABBGeometryMinStride.
  • Update mode PreferUpdate requires ASFlagAllowUpdate.

Reference: Rust wgpu-core device/resource.rs create_blas.

func ValidateTlasBuild

func ValidateTlasBuild(
	ctx DeviceContext,
	entry *TlasBuildEntry,
	blasMap map[uint64]*Blas,
) error

ValidateTlasBuild validates a TLAS build entry.

Checks:

  • FeatureRayQuery must be enabled.
  • Instance count must not exceed MaxTlasInstanceCount.
  • Instance count must not exceed TLAS MaxInstances.
  • All referenced BLASes in blasMap must be built (BuiltIndex > 0).

Reference: Rust wgpu-core command/ray_tracing.rs tlas validation + ValidateAsActionsError (ray_tracing.rs:254-276).

func ValidateTlasCreate

func ValidateTlasCreate(
	ctx DeviceContext,
	desc *gputypes.CreateTlasDescriptor,
) error

ValidateTlasCreate validates parameters for TLAS creation.

Checks:

  • FeatureRayQuery must be enabled.
  • MaxInstances must not exceed MaxTlasInstanceCount.
  • MaxInstances must be > 0.
  • Update mode PreferUpdate requires ASFlagAllowUpdate.

Reference: Rust wgpu-core device/resource.rs create_tlas.

Types

type Blas

type Blas struct {
	// Label is the debug label from the creation descriptor.
	Label string

	// Raw is the underlying HAL acceleration structure handle.
	// Nil if the BLAS is in an invalid state (e.g., creation failed).
	Raw hal.AccelerationStructure

	// SizeInfo contains the build/update/structure sizes returned by
	// GetAccelerationStructureBuildSizes at creation time.
	SizeInfo hal.AccelerationStructureBuildSizes

	// Flags are the acceleration structure creation flags.
	Flags gputypes.AccelerationStructureFlags

	// UpdateMode determines whether this BLAS supports incremental updates.
	UpdateMode gputypes.AccelerationStructureUpdateMode

	// Compaction tracks the compaction lifecycle state.
	// Matches Rust wgpu-core Blas.compacted_state (Mutex<BlasCompactState>).
	Compaction CompactionState

	// CompactedSize holds the compacted size in bytes, valid only when
	// Compaction == CompactionReady.
	CompactedSize uint64

	// BuiltIndex is a monotonically increasing index assigned when this BLAS
	// is built. Used for dependency ordering: a TLAS can only reference BLASes
	// that were built before or in the same build command.
	// Zero means not yet built.
	// Matches Rust wgpu-core Blas.built_index (RwLock<Option<NonZeroU64>>).
	BuiltIndex uint64

	// GeometryCount is the number of geometries in this BLAS, stored at
	// creation time for validation during build commands.
	GeometryCount uint32

	// Handle is the device address of this BLAS, used by TLAS instances
	// to reference this structure. Set after the first successful build.
	// Matches Rust wgpu-core Blas.handle (u64).
	Handle uint64
}

Blas represents a bottom-level acceleration structure.

A BLAS contains geometry data (triangles or AABBs) and is referenced by TLAS instances. It owns a HAL AccelerationStructure handle and tracks compaction state for memory optimization.

Matches Rust wgpu-core resource.rs Blas struct (lines 3238-3252).

func (*Blas) AllowsCompaction

func (b *Blas) AllowsCompaction() bool

AllowsCompaction returns true if this BLAS was created with the ASFlagAllowCompaction flag.

func (*Blas) AllowsUpdate

func (b *Blas) AllowsUpdate() bool

AllowsUpdate returns true if this BLAS was created with the ASFlagAllowUpdate flag.

func (*Blas) IsBuilt

func (b *Blas) IsBuilt() bool

IsBuilt returns true if this BLAS has been built at least once.

type BlasAction

type BlasAction uint8

BlasAction describes what needs to happen to a BLAS during a build command.

const (
	// BlasActionNone means no build action is required.
	BlasActionNone BlasAction = iota

	// BlasActionBuild means a full acceleration structure build is required.
	BlasActionBuild

	// BlasActionUpdate means an incremental update is sufficient (the BLAS
	// must have been previously built with ASFlagAllowUpdate).
	BlasActionUpdate
)

func (BlasAction) String

func (a BlasAction) String() string

String returns a human-readable name for the BLAS action.

type BlasBuildEntry

type BlasBuildEntry struct {
	// Blas is the acceleration structure to build or update.
	Blas *Blas

	// Geometries provides the geometry input for the build.
	// Must match the geometry type (triangles or AABBs) used at creation.
	Geometries *hal.AccelerationStructureEntries
}

BlasBuildEntry pairs a BLAS with the geometry data for building.

type BuildContext

type BuildContext struct {
	// contains filtered or unexported fields
}

BuildContext holds state for a single BuildAccelerationStructures call.

It tracks a monotonically increasing build index used for dependency ordering: a TLAS can only reference BLASes that were built in the same or an earlier build command (BuiltIndex > 0).

Matches Rust wgpu-core's build_acceleration_structures scratch size accumulation and built_index assignment.

func NewBuildContext

func NewBuildContext(device DeviceContext, currentBuildIndex uint64) *BuildContext

NewBuildContext creates a build context with the given device and current build index. The build index should be incremented by the caller for each build command.

func (*BuildContext) PrepareBlasBuild

func (bc *BuildContext) PrepareBlasBuild(entries []*BlasBuildEntry) (uint64, error)

PrepareBlasBuild validates and prepares BLAS entries for building.

It calculates the total scratch buffer size needed (aligned per adapter requirements) and assigns a BuiltIndex to each BLAS. The returned scratch size can be used to allocate a shared scratch buffer.

Reference: Rust wgpu-core command/ray_tracing.rs iter_blas().

func (*BuildContext) PrepareTlasBuild

func (bc *BuildContext) PrepareTlasBuild(entries []*TlasBuildEntry) (uint64, error)

PrepareTlasBuild validates and prepares TLAS entries for building.

It verifies that all referenced BLASes have been built (BuiltIndex > 0) and calculates the total scratch buffer size. Each TLAS is assigned the current build index after successful preparation.

Reference: Rust wgpu-core command/ray_tracing.rs tlas loop (line 243-315).

type CompactionState

type CompactionState uint8

CompactionState tracks the compaction lifecycle of a BLAS.

Transitions: Idle -> Waiting -> Ready -> Compacted.

Matches Rust wgpu-core BlasCompactState (resource.rs:3216-3225).

const (
	// CompactionIdle means the BLAS has not been compacted and no compaction
	// has been requested. This is the initial state.
	CompactionIdle CompactionState = iota

	// CompactionWaiting means a compacted-size query has been submitted to
	// the GPU but the result is not yet available.
	CompactionWaiting

	// CompactionReady means the compacted size is available and the BLAS is
	// ready to be compacted via a copy operation.
	CompactionReady

	// CompactionCompacted means the BLAS has been compacted. No further
	// compaction is allowed (double-compaction is an error).
	CompactionCompacted
)

func (CompactionState) String

func (s CompactionState) String() string

String returns a human-readable name for the compaction state.

type DeviceContext

type DeviceContext interface {
	// CreateBuffer creates a GPU buffer for scratch or instance data.
	CreateBuffer(desc *hal.BufferDescriptor) (hal.Buffer, error)

	// DestroyBuffer releases a GPU buffer.
	DestroyBuffer(buffer hal.Buffer)

	// HALDevice returns the underlying HAL device for acceleration
	// structure operations (create, destroy, build size queries).
	HALDevice() hal.Device

	// DeviceFeatures returns the features enabled on this device.
	// Used to validate that RT features are present before operations.
	DeviceFeatures() gputypes.Features

	// DeviceLimits returns the resource limits of this device.
	// Used to validate geometry counts, instance counts, etc.
	DeviceLimits() gputypes.Limits

	// DeviceAlignments returns the alignment requirements of this device.
	// Used for scratch buffer alignment during AS builds.
	DeviceAlignments() hal.Alignments
}

DeviceContext provides access to device-level operations needed by the ray tracing package. This interface breaks the import cycle between internal/raytracing/ and core/ — core.Device implements this interface and passes itself when calling RT functions.

Pattern: same as ssa.Frontend in Go compiler (internal package defines callback interface, parent implements it).

type Tlas

type Tlas struct {
	// Label is the debug label from the creation descriptor.
	Label string

	// Raw is the underlying HAL acceleration structure handle.
	// Nil if the TLAS is in an invalid state.
	Raw hal.AccelerationStructure

	// SizeInfo contains the build/update/structure sizes returned by
	// GetAccelerationStructureBuildSizes at creation time.
	SizeInfo hal.AccelerationStructureBuildSizes

	// Flags are the acceleration structure creation flags.
	Flags gputypes.AccelerationStructureFlags

	// UpdateMode determines whether this TLAS supports incremental updates.
	UpdateMode gputypes.AccelerationStructureUpdateMode

	// MaxInstances is the maximum number of BLAS instances this TLAS can
	// hold, specified at creation time. Determines the instance buffer size.
	// Matches Rust wgpu-core Tlas.max_instance_count.
	MaxInstances uint32

	// BuiltIndex is a monotonically increasing index assigned when this TLAS
	// is built. Used for ordering builds within a single command.
	// Zero means not yet built.
	// Matches Rust wgpu-core Tlas.built_index (RwLock<Option<NonZeroU64>>).
	BuiltIndex uint64

	// InstanceBuffer is the persistent GPU buffer holding packed TlasInstance
	// data (64 bytes per instance). Allocated at creation time and reused
	// across rebuilds.
	// Matches Rust wgpu-core TlasState.instance_buffer.
	InstanceBuffer hal.Buffer

	// Dependencies tracks the BLAS device addresses that this TLAS currently
	// references. Updated on each build to reflect the active instance set.
	// Used for validation: all referenced BLASes must be built before or in
	// the same build command as this TLAS.
	// Matches Rust wgpu-core Tlas.dependencies (RwLock<Vec<Arc<Blas>>>).
	Dependencies []uint64
}

Tlas represents a top-level acceleration structure.

A TLAS contains instances that reference BLASes, forming the top of the two-level acceleration structure hierarchy used for ray tracing. It owns a HAL AccelerationStructure handle and a persistent GPU instance buffer.

Matches Rust wgpu-core resource.rs Tlas struct (lines 3474-3486).

func (*Tlas) AllowsUpdate

func (t *Tlas) AllowsUpdate() bool

AllowsUpdate returns true if this TLAS was created with the ASFlagAllowUpdate flag.

func (*Tlas) IsBuilt

func (t *Tlas) IsBuilt() bool

IsBuilt returns true if this TLAS has been built at least once.

type TlasBuildEntry

type TlasBuildEntry struct {
	// Tlas is the acceleration structure to build or update.
	Tlas *Tlas

	// InstanceBuffer is the GPU buffer containing packed instance data.
	InstanceBuffer hal.Buffer

	// InstanceCount is the number of active instances in the buffer.
	InstanceCount uint32
}

TlasBuildEntry pairs a TLAS with the instance buffer for building.

type ValidationError

type ValidationError struct {
	Op      string
	Message string
}

ValidationError represents a ray tracing validation failure.

Op identifies the operation that failed (e.g., "CreateBlas", "BuildTlas"). Message describes the specific validation rule that was violated.

func (*ValidationError) Error

func (e *ValidationError) Error() string

Error implements the error interface.

Jump to

Keyboard shortcuts

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