internal/shader: refactoring

This commit is contained in:
Hajime Hoshi 2024-08-25 12:23:13 +09:00
parent 3547d999b1
commit 6db3b11b36

View File

@ -55,9 +55,7 @@ type compileState struct {
ir shaderir.Program ir shaderir.Program
funcs []function funcs []function
vertexOutParams []shaderir.Type
fragmentInParams []shaderir.Type
global block global block
@ -292,18 +290,14 @@ func (cs *compileState) parse(f *ast.File) {
// Parse function names so that any other function call the others. // Parse function names so that any other function call the others.
// The function data is provisional and will be updated soon. // The function data is provisional and will be updated soon.
var vertexOutParams []shaderir.Type
var fragmentInParams []shaderir.Type
for _, d := range f.Decls { for _, d := range f.Decls {
fd, ok := d.(*ast.FuncDecl) fd, ok := d.(*ast.FuncDecl)
if !ok { if !ok {
continue continue
} }
n := fd.Name.Name n := fd.Name.Name
if n == cs.vertexEntry {
continue
}
if n == cs.fragmentEntry {
continue
}
for _, f := range cs.funcs { for _, f := range cs.funcs {
if f.name == n { if f.name == n {
@ -321,6 +315,15 @@ func (cs *compileState) parse(f *ast.File) {
outT = append(outT, v.typ) outT = append(outT, v.typ)
} }
if n == cs.vertexEntry {
vertexOutParams = outT
continue
}
if n == cs.fragmentEntry {
fragmentInParams = inT
continue
}
cs.funcs = append(cs.funcs, function{ cs.funcs = append(cs.funcs, function{
name: n, name: n,
ir: shaderir.Func{ ir: shaderir.Func{
@ -333,6 +336,26 @@ func (cs *compileState) parse(f *ast.File) {
}) })
} }
// Check varying variables.
// In testings, there might not be vertex and fragment entry points.
if len(vertexOutParams) > 0 && len(fragmentInParams) > 0 {
if len(vertexOutParams) != len(fragmentInParams) {
cs.addError(0, "the number of vertex entry point's returning values and the number of fragment entry point's params must be the same")
}
for i, t := range vertexOutParams {
if !t.Equal(&fragmentInParams[i]) {
cs.addError(0, "vertex entry point's returning value types and fragment entry point's param types must match")
}
}
}
// Set varying veraibles.
if len(vertexOutParams) > 0 {
// TODO: Check that these params are not arrays or structs
// The 0th argument is a special variable for position and is not included in varying variables.
cs.ir.Varyings = append(cs.ir.Varyings, vertexOutParams[1:]...)
}
// Parse functions. // Parse functions.
for _, d := range f.Decls { for _, d := range f.Decls {
if f, ok := d.(*ast.FuncDecl); ok { if f, ok := d.(*ast.FuncDecl); ok {
@ -348,29 +371,6 @@ func (cs *compileState) parse(f *ast.File) {
return return
} }
// Parse varying veraibles.
// In testings, there might not be vertex and fragment entry points.
if cs.ir.VertexFunc.Block != nil && cs.ir.FragmentFunc.Block != nil {
if len(cs.fragmentInParams) != len(cs.vertexOutParams) {
cs.addError(0, "the number of vertex entry point's returning values and the number of fragment entry point's params must be the same")
}
for i, t := range cs.vertexOutParams {
if !t.Equal(&cs.fragmentInParams[i]) {
cs.addError(0, "vertex entry point's returning value types and fragment entry point's param types must match")
}
}
}
if cs.ir.VertexFunc.Block != nil {
// TODO: Check that these params are not arrays or structs
// The 0th argument is a special variable for position and is not included in varying variables.
cs.ir.Varyings = append(cs.ir.Varyings, cs.vertexOutParams[1:]...)
}
if len(cs.errs) > 0 {
return
}
for _, f := range cs.funcs { for _, f := range cs.funcs {
cs.ir.Funcs = append(cs.ir.Funcs, f.ir) cs.ir.Funcs = append(cs.ir.Funcs, f.ir)
} }
@ -483,10 +483,8 @@ func (cs *compileState) parseDecl(b *block, fname string, d ast.Decl) ([]shaderi
switch d.Name.Name { switch d.Name.Name {
case cs.vertexEntry: case cs.vertexEntry:
cs.ir.VertexFunc.Block = f.ir.Block cs.ir.VertexFunc.Block = f.ir.Block
cs.vertexOutParams = f.ir.OutParams
case cs.fragmentEntry: case cs.fragmentEntry:
cs.ir.FragmentFunc.Block = f.ir.Block cs.ir.FragmentFunc.Block = f.ir.Block
cs.fragmentInParams = f.ir.InParams
default: default:
// The function is already registered for their names. // The function is already registered for their names.
for i := range cs.funcs { for i := range cs.funcs {