Specifying custom functions would be a useful feature. For example:
func getColor(texCoord vec2) vec4 {
color := texture0At(texCoord)
// Do stuff with color
return color
}
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
color := getColor(texCoord)
// Do more stuff with color
return color
}
I believe you can already do that.
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
color := getColor(texCoord)
This program shadows the argument color and is not valid (even as a regular Go program). I'll fix this to make a more friendly error message.
I think I may have found a bug then. I was trying to implement linear filter and use a custom function at the same time. The shader would not compile, so I assumed that custom functions were not implemented.
Here is my shader code:
package main
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
texelSize := 1.0 / texture0Size()
// Linear filter
p0 := texCoord - texelSize / 2.0
p1 := texCoord + texelSize / 2.0
c0 := getColor(p0)
c1 := getColor(vec2(p1.x, p0.y))
c2 := getColor(vec2(p0.x, p1.y))
c3 := getColor(p1)
rate := fract(p0 * texture0Size())
return mix(mix(c0, c1, rate.x), mix(c2, c3, rate.x), rate.y)
}
func getColor(texCoord vec2) vec4 {
// A simple example
return texture0At(texCoord)
}
It gives the following error:
opengl: vertex shader compile error: opengl: shader compile failed: 0:21(2): error: no function with name 'F6'
Thanks. I'll take a look at this tonight.
Interesting. ~By optimization, a function definition was omitted. Hmm~ This seems a mysterious issue. F6 was actually defined but hidden?
My understanding is that function order matters like C. I'll add prototypes.
That seems to have resolved the issue. Thank you!
Most helpful comment
That seems to have resolved the issue. Thank you!