internal/shader: refactoring: varying variables

This commit is contained in:
Hajime Hoshi 2024-08-23 10:48:53 +09:00
parent b71f3f86a8
commit d3d42b3263
2 changed files with 36 additions and 40 deletions

View File

@ -55,12 +55,12 @@ type compileState struct {
ir shaderir.Program ir shaderir.Program
funcs []function funcs []function
vertexOutParams []shaderir.Type
fragmentInParams []shaderir.Type
global block global block
varyingParsed bool
errs []string errs []string
} }
@ -348,6 +348,30 @@ 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
cs.ir.Varyings = append(cs.ir.Varyings, cs.vertexOutParams[1:]...)
} else if cs.ir.FragmentFunc.Block != nil {
cs.ir.Varyings = append(cs.ir.Varyings, cs.fragmentInParams[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)
} }
@ -460,8 +484,10 @@ 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 {
@ -767,18 +793,6 @@ func (cs *compileState) parseFunc(block *block, d *ast.FuncDecl) (function, bool
inParams, outParams, returnType := cs.parseFuncParams(block, d.Name.Name, d) inParams, outParams, returnType := cs.parseFuncParams(block, d.Name.Name, d)
checkVaryings := func(vs []variable) {
if len(cs.ir.Varyings) != len(vs) {
cs.addError(d.Pos(), "the number of vertex entry point's returning values and the number of fragment entry point's params must be the same")
return
}
for i, t := range cs.ir.Varyings {
if t.Main != vs[i].typ.Main {
cs.addError(d.Pos(), "vertex entry point's returning value types and fragment entry point's param types must match")
}
}
}
if block == &cs.global { if block == &cs.global {
switch d.Name.Name { switch d.Name.Name {
case cs.vertexEntry: case cs.vertexEntry:
@ -802,15 +816,6 @@ func (cs *compileState) parseFunc(block *block, d *ast.FuncDecl) (function, bool
return function{}, false return function{}, false
} }
if cs.varyingParsed {
checkVaryings(outParams[1:])
} else {
for _, v := range outParams[1:] {
// TODO: Check that these params are not arrays or structs
cs.ir.Varyings = append(cs.ir.Varyings, v.typ)
}
}
cs.varyingParsed = true
case cs.fragmentEntry: case cs.fragmentEntry:
if len(inParams) == 0 { if len(inParams) == 0 {
cs.addError(d.Pos(), "fragment entry point must have at least one vec4 parameter for a position") cs.addError(d.Pos(), "fragment entry point must have at least one vec4 parameter for a position")
@ -825,15 +830,6 @@ func (cs *compileState) parseFunc(block *block, d *ast.FuncDecl) (function, bool
cs.addError(d.Pos(), "fragment entry point must have one returning vec4 value for a color") cs.addError(d.Pos(), "fragment entry point must have one returning vec4 value for a color")
return function{}, false return function{}, false
} }
if cs.varyingParsed {
checkVaryings(inParams[1:])
} else {
for _, v := range inParams[1:] {
cs.ir.Varyings = append(cs.ir.Varyings, v.typ)
}
}
cs.varyingParsed = true
} }
} }

View File

@ -25,7 +25,7 @@ type Type struct {
Length int Length int
} }
func (t *Type) Equal(rhs *Type) bool { func (t Type) Equal(rhs *Type) bool {
if t.Main != rhs.Main { if t.Main != rhs.Main {
return false return false
} }
@ -43,7 +43,7 @@ func (t *Type) Equal(rhs *Type) bool {
return true return true
} }
func (t *Type) String() string { func (t Type) String() string {
switch t.Main { switch t.Main {
case None: case None:
return "none" return "none"
@ -87,7 +87,7 @@ func (t *Type) String() string {
} }
} }
func (t *Type) Uint32Count() int { func (t Type) Uint32Count() int {
switch t.Main { switch t.Main {
case Int: case Int:
return 1 return 1
@ -118,7 +118,7 @@ func (t *Type) Uint32Count() int {
} }
} }
func (t *Type) IsFloatVector() bool { func (t Type) IsFloatVector() bool {
switch t.Main { switch t.Main {
case Vec2, Vec3, Vec4: case Vec2, Vec3, Vec4:
return true return true
@ -126,7 +126,7 @@ func (t *Type) IsFloatVector() bool {
return false return false
} }
func (t *Type) IsIntVector() bool { func (t Type) IsIntVector() bool {
switch t.Main { switch t.Main {
case IVec2, IVec3, IVec4: case IVec2, IVec3, IVec4:
return true return true
@ -134,7 +134,7 @@ func (t *Type) IsIntVector() bool {
return false return false
} }
func (t *Type) VectorElementCount() int { func (t Type) VectorElementCount() int {
switch t.Main { switch t.Main {
case Vec2: case Vec2:
return 2 return 2
@ -153,7 +153,7 @@ func (t *Type) VectorElementCount() int {
} }
} }
func (t *Type) IsMatrix() bool { func (t Type) IsMatrix() bool {
switch t.Main { switch t.Main {
case Mat2, Mat3, Mat4: case Mat2, Mat3, Mat4:
return true return true