internal/shaderir: rename the built-in func to get a texel to __texelAt

This commit is contained in:
Hajime Hoshi 2023-04-23 22:57:30 +09:00
parent 49582519c1
commit 9d2178cf71
8 changed files with 13 additions and 13 deletions

View File

@ -91,14 +91,14 @@ func imageSrcRegionOnTexture() (vec2, vec2) {
shaderSuffix += fmt.Sprintf(` shaderSuffix += fmt.Sprintf(`
func imageSrc%[1]dUnsafeAt(pos vec2) vec4 { func imageSrc%[1]dUnsafeAt(pos vec2) vec4 {
// pos is the position in positions of the source texture (= 0th image's texture). // pos is the position in positions of the source texture (= 0th image's texture).
return texture2D(__t%[1]d, %[2]s) return __texelAt(__t%[1]d, %[2]s)
} }
func imageSrc%[1]dAt(pos vec2) vec4 { func imageSrc%[1]dAt(pos vec2) vec4 {
// pos is the position of the source texture (= 0th image's texture). // pos is the position of the source texture (= 0th image's texture).
// If pos is in the region, the result is (1, 1). Otherwise, either element is 0. // If pos is in the region, the result is (1, 1). Otherwise, either element is 0.
in := step(__textureSourceRegionOrigin, pos) - step(__textureSourceRegionOrigin + __textureSourceRegionSize, pos) in := step(__textureSourceRegionOrigin, pos) - step(__textureSourceRegionOrigin + __textureSourceRegionSize, pos)
return texture2D(__t%[1]d, %[2]s) * in.x * in.y return __texelAt(__t%[1]d, %[2]s) * in.x * in.y
} }
`, i, pos) `, i, pos)
} }

View File

@ -602,7 +602,7 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar
return nil, nil, nil, false return nil, nil, nil, false
} }
t = shaderir.Type{Main: shaderir.Mat4} t = shaderir.Type{Main: shaderir.Mat4}
case shaderir.Texture2DF: case shaderir.TexelAt:
if len(args) != 2 { if len(args) != 2 {
cs.addError(e.Pos(), fmt.Sprintf("number of %s's arguments must be 2 but %d", callee.BuiltinFunc, len(args))) cs.addError(e.Pos(), fmt.Sprintf("number of %s's arguments must be 2 but %d", callee.BuiltinFunc, len(args)))
return nil, nil, nil, false return nil, nil, nil, false

View File

@ -121,7 +121,7 @@ func (c *compileContext) builtinFuncString(f shaderir.BuiltinFunc) string {
return "dFdx" return "dFdx"
case shaderir.Dfdy: case shaderir.Dfdy:
return "dFdy" return "dFdy"
case shaderir.Texture2DF: case shaderir.TexelAt:
if c.unit == shaderir.Pixel { if c.unit == shaderir.Pixel {
return "texelFetch" return "texelFetch"
} }

View File

@ -476,7 +476,7 @@ func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Bl
if len(args) == 1 { if len(args) == 1 {
return fmt.Sprintf("float4x4FromScalar(%s)", args[0]) return fmt.Sprintf("float4x4FromScalar(%s)", args[0])
} }
case shaderir.Texture2DF: case shaderir.TexelAt:
switch c.unit { switch c.unit {
case shaderir.Pixel: case shaderir.Pixel:
return fmt.Sprintf("%s.Load(int3(%s, 0))", args[0], strings.Join(args[1:], ", ")) return fmt.Sprintf("%s.Load(int3(%s, 0))", args[0], strings.Join(args[1:], ", "))

View File

@ -143,8 +143,8 @@ func (c *compileContext) builtinFuncString(f shaderir.BuiltinFunc) string {
return "ddx" return "ddx"
case shaderir.Dfdy: case shaderir.Dfdy:
return "ddy" return "ddy"
case shaderir.Texture2DF: case shaderir.TexelAt:
return "?(texture2D)" return "?(__texelAt)"
default: default:
return string(f) return string(f)
} }

View File

@ -401,7 +401,7 @@ func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Bl
for _, exp := range e.Exprs[1:] { for _, exp := range e.Exprs[1:] {
args = append(args, expr(&exp)) args = append(args, expr(&exp))
} }
if callee.Type == shaderir.BuiltinFuncExpr && callee.BuiltinFunc == shaderir.Texture2DF { if callee.Type == shaderir.BuiltinFuncExpr && callee.BuiltinFunc == shaderir.TexelAt {
switch p.Unit { switch p.Unit {
case shaderir.Texel: case shaderir.Texel:
return fmt.Sprintf("%s.sample(texture_sampler, %s)", args[0], strings.Join(args[1:], ", ")) return fmt.Sprintf("%s.sample(texture_sampler, %s)", args[0], strings.Join(args[1:], ", "))

View File

@ -149,8 +149,8 @@ func builtinFuncString(f shaderir.BuiltinFunc) string {
return "float4x4" return "float4x4"
case shaderir.Inversesqrt: case shaderir.Inversesqrt:
return "rsqrt" return "rsqrt"
case shaderir.Texture2DF: case shaderir.TexelAt:
return "?(texture2D)" return "?(__texelAt)"
} }
return string(f) return string(f)
} }

View File

@ -276,11 +276,11 @@ const (
Reflect BuiltinFunc = "reflect" Reflect BuiltinFunc = "reflect"
Refract BuiltinFunc = "refract" Refract BuiltinFunc = "refract"
Transpose BuiltinFunc = "transpose" Transpose BuiltinFunc = "transpose"
Texture2DF BuiltinFunc = "texture2D"
Dfdx BuiltinFunc = "dfdx" Dfdx BuiltinFunc = "dfdx"
Dfdy BuiltinFunc = "dfdy" Dfdy BuiltinFunc = "dfdy"
Fwidth BuiltinFunc = "fwidth" Fwidth BuiltinFunc = "fwidth"
DiscardF BuiltinFunc = "discard" DiscardF BuiltinFunc = "discard"
TexelAt BuiltinFunc = "__texelAt"
) )
func ParseBuiltinFunc(str string) (BuiltinFunc, bool) { func ParseBuiltinFunc(str string) (BuiltinFunc, bool) {
@ -334,11 +334,11 @@ func ParseBuiltinFunc(str string) (BuiltinFunc, bool) {
Reflect, Reflect,
Refract, Refract,
Transpose, Transpose,
Texture2DF,
Dfdx, Dfdx,
Dfdy, Dfdy,
Fwidth, Fwidth,
DiscardF: DiscardF,
TexelAt:
return BuiltinFunc(str), true return BuiltinFunc(str), true
} }
return "", false return "", false