Documentation
¶
Index ¶
- Variables
- func CopyFile(src, dst string) ([][]byte, error)
- func Fprint(output io.Writer, pkg *packages.Package, node any) error
- func IsGoFile(f fs.DirEntry) bool
- func IsWGSLFile(f fs.DirEntry) bool
- func MathReplaceAll(mat, ln []byte) []byte
- func MoveLines(lines *[][]byte, to, st, ed int)
- func ReadFileLines(fn string) ([][]byte, error)
- func RemoveGenFiles(dir string)
- func Run(cfg *Config) error
- func SlBoolReplace(lines [][]byte)
- func SlEdits(src []byte) (lines [][]byte, hasSlrand bool, hasSltype bool)
- func SlEditsReplace(lines [][]byte) (bool, bool)
- func SlRemoveComments(lines [][]byte) [][]byte
- func WriteFileLines(fn string, lines [][]byte) error
- type CommentedNode
- type Config
- type File
- type Function
- type GetGlobalVar
- type Group
- type Kernel
- type Mode
- type PrintConfig
- type Replace
- type State
- func (st *State) AllFuncs(name string) map[string]*Function
- func (st *State) AppendGoHeader(lines [][]byte) [][]byte
- func (st *State) AtomicVars(funcs map[string]*Function) map[string]*Var
- func (st *State) CompileFile(fn string) error
- func (st *State) CopyPackageFile(fnm, packagePath string) error
- func (st *State) ExtractFiles()
- func (st *State) ExtractGosl(lines [][]byte) (outLines [][]byte, hasVars bool)
- func (st *State) ExtractImports()
- func (st *State) ExtractWGSL(lines [][]byte) [][]byte
- func (st *State) GenGPU()
- func (st *State) GenGPUSystemInit(sy *System) string
- func (st *State) GenGPUSystemOps(sy *System) string
- func (st *State) GenKernelHeader(sy *System, kn *Kernel, avars map[string]*Var) string
- func (st *State) GenTensorFuncs(sy *System) string
- func (st *State) GetTempVar(vrnm string) *GetGlobalVar
- func (st *State) GlobalVar(vrnm string) *Var
- func (st *State) HasGoslTag(lines [][]byte) bool
- func (st *State) ImportFiles(lines [][]byte)
- func (st *State) Init(cfg *Config)
- func (st *State) PrintFuncGraph()
- func (st *State) ProjectFiles()
- func (st *State) RecycleFunc(name string) *Function
- func (st *State) Run() error
- func (st *State) System(sysname string) *System
- func (st *State) TranslateDir(pf string) error
- func (st *State) VarIsReadWrite(vrnm string) bool
- func (st *State) VarsAdded()
- type System
- type Var
Constants ¶
This section is empty.
Variables ¶
var Replaces = []Replace{ {[]byte("sltype.Uint32Vec2"), []byte("vec2<u32>")}, {[]byte("sltype.Float32Vec2"), []byte("vec2<f32>")}, {[]byte("float32"), []byte("f32")}, {[]byte("float64"), []byte("f64")}, {[]byte("uint32"), []byte("u32")}, {[]byte("uint64"), []byte("su64")}, {[]byte("int32"), []byte("i32")}, {[]byte("math32.FastExp("), []byte("FastExp(")}, {[]byte("math.Float32frombits("), []byte("bitcast<f32>(")}, {[]byte("math.Float32bits("), []byte("bitcast<u32>(")}, {[]byte("shaders."), []byte("")}, {[]byte("slrand."), []byte("Rand")}, {[]byte("RandUi32"), []byte("RandUint32")}, {[]byte(".SetFromVector2("), []byte("=(")}, {[]byte(".SetFrom2("), []byte("=(")}, {[]byte(".IsTrue()"), []byte("==1")}, {[]byte(".IsFalse()"), []byte("==0")}, {[]byte(".SetBool(true)"), []byte("=1")}, {[]byte(".SetBool(false)"), []byte("=0")}, {[]byte(".SetBool("), []byte("=i32(")}, {[]byte("slbool.Bool"), []byte("i32")}, {[]byte("slbool.True"), []byte("1")}, {[]byte("slbool.False"), []byte("0")}, {[]byte("slbool.IsTrue("), []byte("(1 == ")}, {[]byte("slbool.IsFalse("), []byte("(0 == ")}, {[]byte("slbool.FromBool("), []byte("i32(")}, {[]byte("bools.ToFloat32("), []byte("f32(")}, {[]byte("bools.FromFloat32("), []byte("bool(")}, {[]byte("num.FromBool[f32]("), []byte("f32(")}, {[]byte("num.ToBool("), []byte("bool(")}, {[]byte("sltype."), []byte("")}, }
var SLBools = []Replace{ {[]byte(".IsTrue()"), []byte("==1")}, {[]byte(".IsFalse()"), []byte("==0")}, {[]byte(".SetBool(true)"), []byte("=1")}, {[]byte(".SetBool(false)"), []byte("=0")}, {[]byte(".SetBool("), []byte("=int32(")}, {[]byte("slbool.Bool"), []byte("int32")}, {[]byte("slbool.True"), []byte("1")}, {[]byte("slbool.False"), []byte("0")}, {[]byte("slbool.IsTrue("), []byte("(1 == ")}, {[]byte("slbool.IsFalse("), []byte("(0 == ")}, {[]byte("slbool.FromBool("), []byte("int32(")}, {[]byte("bools.ToFloat32("), []byte("float32(")}, {[]byte("bools.FromFloat32("), []byte("bool(")}, {[]byte("num.FromBool[f32]("), []byte("float32(")}, {[]byte("num.ToBool("), []byte("bool(")}, }
Functions ¶
func Fprint ¶
Fprint "pretty-prints" an AST node to output. It calls PrintConfig.Fprint with default settings. Note that gofmt uses tabs for indentation but spaces for alignment; use format.Node (package go/format) for output that matches gofmt.
func IsWGSLFile ¶
func MathReplaceAll ¶
func ReadFileLines ¶
func RemoveGenFiles ¶
func RemoveGenFiles(dir string)
RemoveGenFiles removes .go, .wgsl, .spv files in shader generated dir
func SlBoolReplace ¶
func SlBoolReplace(lines [][]byte)
SlBoolReplace replaces all the slbool methods with literal int32 expressions.
func SlEdits ¶
SlEdits performs post-generation edits for wgsl, replacing type names, slbool, function calls, etc. returns true if a slrand. or sltype. prefix was found, driveing copying of those files.
func SlEditsReplace ¶
SlEditsReplace replaces Go with equivalent WGSL code returns true if has slrand. or sltype. to auto include that header file if so.
func SlRemoveComments ¶
func WriteFileLines ¶
Types ¶
type CommentedNode ¶
type CommentedNode struct { Node any // *ast.File, or ast.Expr, ast.Decl, ast.Spec, or ast.Stmt Comments []*ast.CommentGroup }
A CommentedNode bundles an AST node and corresponding comments. It may be provided as argument to any of the Fprint functions.
type Config ¶
type Config struct { // Output is the output directory for shader code, // relative to where gosl is invoked; must not be an empty string. Output string `flag:"out" default:"shaders"` // Exclude is a comma-separated list of names of functions to exclude from exporting to WGSL. Exclude string `default:"Update,Defaults"` // Keep keeps temporary converted versions of the source files, for debugging. Keep bool // Debug enables debugging messages while running. Debug bool }
Config has the configuration info for the gosl system.
type Function ¶
type Function struct { Name string Funcs map[string]*Function Atomics map[string]*Var // variables that have atomic operations in this function }
Function represents the call graph of functions
func NewFunction ¶
type GetGlobalVar ¶
type GetGlobalVar struct { // global variable Var *Var // name of temporary variable TmpVar string // index passed to the Get function IdxExpr ast.Expr // rw override ReadWrite bool }
GetGlobalVar holds GetVar expression, to Set variable back when done.
type Group ¶
type Group struct { Name string // comment docs about this group Doc string // Uniform indicates a uniform group; else default is Storage. Uniform bool Vars []*Var }
Group represents one variable group.
type Kernel ¶
type Kernel struct { Name string Args string // Filename is the name of the kernel shader file, e.g., shaders/Compute.wgsl Filename string // function code FuncCode string // Lines is full shader code Lines [][]byte // ReadWriteVars are variables marked as read-write for current kernel. ReadWriteVars map[string]bool }
Kernel represents a kernel function, which is the basis for each wgsl generated code file.
type PrintConfig ¶
type PrintConfig struct { Mode Mode // default: 0 Tabwidth int // default: 8 Indent int // default: 0 (all code is indented at least by this much) GoToSL *State // gosl: ExcludeFunctions map[string]bool }
A PrintConfig node controls the output of Fprint.
func (*PrintConfig) Fprint ¶
Fprint "pretty-prints" an AST node to output for a given configuration cfg. Position information is interpreted relative to the file set fset. The node type must be *ast.File, *CommentedNode, []ast.Decl, []ast.Stmt, or assignment-compatible to ast.Expr, ast.Decl, ast.Spec, or ast.Stmt.
type State ¶
type State struct { // Config options. Config *Config // path to shaders/imports directory. ImportsDir string // name of the package Package string // GoFiles are all the files with gosl content in current directory. GoFiles map[string]*File // GoVarsFiles are all the files with gosl:vars content in current directory. // These must be processed first! they are moved from GoFiles to here. GoVarsFiles map[string]*File // GoImports has all the imported files. GoImports map[string]map[string]*File // ImportPackages has short package names, to remove from go code // so everything lives in same main package. ImportPackages map[string]bool // Systems has the kernels and variables for each system. // There is an initial "Default" system when system is not specified. Systems map[string]*System // GetFuncs is a map of GetVar, SetVar function names for global vars. GetFuncs map[string]*Var // SLImportFiles are all the extracted and translated WGSL files in shaders/imports, // which are copied into the generated shader kernel files. SLImportFiles []*File // generated Go GPU gosl.go file contents GPUFile File // ExcludeMap is the compiled map of functions to exclude in Go -> WGSL translation. ExcludeMap map[string]bool // GetVarStack is a stack per function definition of GetVar variables // that need to be set at the end. GetVarStack stack.Stack[map[string]*GetGlobalVar] // GetFuncGraph is true if getting the function graph (first pass). GetFuncGraph bool // CurKernel is the current Kernel for second pass processing. CurKernel *Kernel // KernelFuncs are the list of functions to include for current kernel. KernelFuncs map[string]*Function // FuncGraph is the call graph of functions, for dead code elimination FuncGraph map[string]*Function }
State holds the current Go -> WGSL processing state.
func (*State) AppendGoHeader ¶
AppendGoHeader appends Go header
func (*State) AtomicVars ¶
AtomicVars returns all the variables marked as atomic within the list of functions.
func (*State) CompileFile ¶
func (*State) CopyPackageFile ¶
CopyPackageFile copies given file name from given package path into the current imports directory. e.g., "slrand.wgsl", "cogentcore.org/lab/gosl/slrand"
func (*State) ExtractFiles ¶
func (st *State) ExtractFiles()
ExtractFiles processes all the package files and saves the corresponding .go files with simple go header.
func (*State) ExtractGosl ¶
ExtractGosl gosl comment-directive tagged regions from given file.
func (*State) ExtractImports ¶
func (st *State) ExtractImports()
ExtractImports processes all the imported files and saves the corresponding .go files with simple go header.
func (*State) ExtractWGSL ¶
ExtractWGSL extracts the WGSL code embedded within .Go files, which is commented out in the Go code -- remove comments.
func (*State) GenGPUSystemInit ¶
GenGPUSystemInit generates GPU Init code for given system.
func (*State) GenGPUSystemOps ¶
GenGPUSystemOps generates GPU helper functions for given system.
func (*State) GenKernelHeader ¶
GenKernelHeader returns the novel generated WGSL kernel code for given kernel, which goes at the top of the resulting file.
func (*State) GenTensorFuncs ¶
GenTensorFuncs returns the generated WGSL code for indexing the tensors in given system.
func (*State) GetTempVar ¶
func (st *State) GetTempVar(vrnm string) *GetGlobalVar
GetTempVar returns temp var for global variable of given name, if found.
func (*State) HasGoslTag ¶
HasGoslTag returns true if given file has a //gosl: tag
func (*State) ImportFiles ¶
ImportFiles checks the given content for //gosl:import tags and imports the package if so.
func (*State) PrintFuncGraph ¶
func (st *State) PrintFuncGraph()
func (*State) ProjectFiles ¶
func (st *State) ProjectFiles()
ProjectFiles gets the files in the current directory.
func (*State) RecycleFunc ¶
get or add a function of given name
func (*State) System ¶
System returns the given system by name, making if not made. if name is empty, "Default" is used.
func (*State) TranslateDir ¶
TranslateDir translate all .Go files in given directory to WGSL.
func (*State) VarIsReadWrite ¶
VarIsReadWrite returns true if var of name is set as read-write for current kernel.
type System ¶
type System struct { Name string // Kernels are the kernels using this compute system. Kernels map[string]*Kernel // Groups are the variables for this compute system. Groups []*Group // NTensors is the number of tensor vars. NTensors int }
System represents a ComputeSystem, and its kernels and variables.
type Var ¶
type Var struct { Name string // comment docs about this var. Doc string // Type of variable: either []Type or F32, U32 for tensors Type string // ReadOnly indicates that this variable is never read back from GPU, // specified by the gosl:read-only property in the variable comments. // It is important to optimize GPU memory usage to indicate this. ReadOnly bool // ReadOrWrite indicates that this variable defaults to ReadOnly // but is also flagged as read-write in some cases. It is registered // as read_write in the gpu ComputeSystem, but processed as ReadOnly // by default except for kernels that declare it as read-write. ReadOrWrite bool // True if a tensor type Tensor bool // Number of dimensions TensorDims int // data kind of the tensor TensorKind reflect.Kind // index of tensor in list of tensor variables, for indexing. TensorIndex int }
Var represents one global system buffer variable.
func (*Var) IndexStride ¶
IndexStride returns the tensor stride variable reference
func (*Var) SetTensorKind ¶
func (vr *Var) SetTensorKind()