Compare commits

...

3 Commits

Author SHA1 Message Date
Hajime Hoshi
9ad7fe5bed internal/shader: refactoring 2024-08-23 11:05:14 +09:00
Hajime Hoshi
d3d42b3263 internal/shader: refactoring: varying variables 2024-08-23 10:57:59 +09:00
Hajime Hoshi
b71f3f86a8 ebiten: add check that graphics.VertexFloatCount and Vertex match 2024-08-23 09:58:28 +09:00
3 changed files with 38 additions and 40 deletions

View File

@ -18,6 +18,7 @@ import (
"fmt"
"image"
"image/color"
"unsafe"
"github.com/hajimehoshi/ebiten/v2/internal/affine"
"github.com/hajimehoshi/ebiten/v2/internal/atlas"
@ -296,6 +297,8 @@ type Vertex struct {
ColorA float32
}
var _ [0]byte = [unsafe.Sizeof(Vertex{}) - unsafe.Sizeof(float32(0))*graphics.VertexFloatCount]byte{}
// Address represents a sampler address mode.
type Address int

View File

@ -56,11 +56,11 @@ type compileState struct {
ir shaderir.Program
funcs []function
vertexOutParams []shaderir.Type
fragmentInParams []shaderir.Type
global block
varyingParsed bool
errs []string
}
@ -348,6 +348,29 @@ func (cs *compileState) parse(f *ast.File) {
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 {
cs.ir.Funcs = append(cs.ir.Funcs, f.ir)
}
@ -460,8 +483,10 @@ func (cs *compileState) parseDecl(b *block, fname string, d ast.Decl) ([]shaderi
switch d.Name.Name {
case cs.vertexEntry:
cs.ir.VertexFunc.Block = f.ir.Block
cs.vertexOutParams = f.ir.OutParams
case cs.fragmentEntry:
cs.ir.FragmentFunc.Block = f.ir.Block
cs.fragmentInParams = f.ir.InParams
default:
// The function is already registered for their names.
for i := range cs.funcs {
@ -767,18 +792,6 @@ func (cs *compileState) parseFunc(block *block, d *ast.FuncDecl) (function, bool
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 {
switch d.Name.Name {
case cs.vertexEntry:
@ -802,15 +815,6 @@ func (cs *compileState) parseFunc(block *block, d *ast.FuncDecl) (function, bool
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:
if len(inParams) == 0 {
cs.addError(d.Pos(), "fragment entry point must have at least one vec4 parameter for a position")
@ -825,15 +829,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")
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
}
func (t *Type) Equal(rhs *Type) bool {
func (t Type) Equal(rhs *Type) bool {
if t.Main != rhs.Main {
return false
}
@ -43,7 +43,7 @@ func (t *Type) Equal(rhs *Type) bool {
return true
}
func (t *Type) String() string {
func (t Type) String() string {
switch t.Main {
case 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 {
case Int:
return 1
@ -118,7 +118,7 @@ func (t *Type) Uint32Count() int {
}
}
func (t *Type) IsFloatVector() bool {
func (t Type) IsFloatVector() bool {
switch t.Main {
case Vec2, Vec3, Vec4:
return true
@ -126,7 +126,7 @@ func (t *Type) IsFloatVector() bool {
return false
}
func (t *Type) IsIntVector() bool {
func (t Type) IsIntVector() bool {
switch t.Main {
case IVec2, IVec3, IVec4:
return true
@ -134,7 +134,7 @@ func (t *Type) IsIntVector() bool {
return false
}
func (t *Type) VectorElementCount() int {
func (t Type) VectorElementCount() int {
switch t.Main {
case Vec2:
return 2
@ -153,7 +153,7 @@ func (t *Type) VectorElementCount() int {
}
}
func (t *Type) IsMatrix() bool {
func (t Type) IsMatrix() bool {
switch t.Main {
case Mat2, Mat3, Mat4:
return true