ebiten: Add texture[ID]Size functions for shaders

Fixes #1239
This commit is contained in:
Hajime Hoshi 2020-07-26 11:25:21 +09:00
parent d08a04a635
commit ca3fd4eaa4
5 changed files with 43 additions and 8 deletions

View File

@ -26,9 +26,17 @@ const (
// //
// All the preversed uniform variables are vec2 so far. // All the preversed uniform variables are vec2 so far.
PreservedUniformVariablesNum = 1 + // the destination texture size PreservedUniformVariablesNum = 1 + // the destination texture size
ShaderImageNum + // the texture sizes
(ShaderImageNum - 1) // the offsets of the second and the following images (ShaderImageNum - 1) // the offsets of the second and the following images
) )
func TextureOffsetUniformVariableIndex(i int) int {
if i == 0 {
panic("graphics: the texture 0 doesn't have its offset")
}
return 1 + ShaderImageNum + i - 1
}
const ( const (
IndicesNum = (1 << 16) / 3 * 3 // Adjust num for triangles. IndicesNum = (1 << 16) / 3 * 3 // Adjust num for triangles.
VertexFloatNum = 8 VertexFloatNum = 8

View File

@ -304,8 +304,19 @@ func (g *Graphics) DrawShader(dst driver.ImageID, srcs [graphics.ShaderImageNum]
us[0].name = "U0" us[0].name = "U0"
us[0].value = []float32{float32(vw), float32(vh)} us[0].value = []float32{float32(vw), float32(vh)}
for i, o := range offsets { for i, src := range srcs {
img := g.images[src]
var w, h int
if img != nil {
w, h = img.framebufferSize()
}
const offset = 1 const offset = 1
us[i+offset].name = fmt.Sprintf("U%d", i+offset)
us[i+offset].value = []float32{float32(w), float32(h)}
}
for i, o := range offsets {
const offset = 1 + graphics.ShaderImageNum
o := o o := o
us[i+offset].name = fmt.Sprintf("U%d", i+offset) us[i+offset].name = fmt.Sprintf("U%d", i+offset)
us[i+offset].value = o[:] us[i+offset].value = o[:]

View File

@ -71,20 +71,26 @@ func (i *Image) Pixels() ([]byte, error) {
return p, nil return p, nil
} }
func (i *Image) framebufferSize() (int, int) {
if i.screen {
// The (default) framebuffer size can't be converted to a power of 2.
// On browsers, i.width and i.height are used as viewport size and
// Edge can't treat a bigger viewport than the drawing area (#71).
return i.width, i.height
}
return graphics.InternalImageSize(i.width), graphics.InternalImageSize(i.height)
}
func (i *Image) ensureFramebuffer() error { func (i *Image) ensureFramebuffer() error {
if i.framebuffer != nil { if i.framebuffer != nil {
return nil return nil
} }
w, h := i.framebufferSize()
if i.screen { if i.screen {
// The (default) framebuffer size can't be converted to a power of 2. i.framebuffer = newScreenFramebuffer(&i.graphics.context, w, h)
// On browsers, c.width and c.height are used as viewport size and
// Edge can't treat a bigger viewport than the drawing area (#71).
i.framebuffer = newScreenFramebuffer(&i.graphics.context, i.width, i.height)
return nil return nil
} }
w, h := graphics.InternalImageSize(i.width), graphics.InternalImageSize(i.height)
f, err := newFramebufferFromTexture(&i.graphics.context, i.textureNative, w, h) f, err := newFramebufferFromTexture(&i.graphics.context, i.textureNative, w, h)
if err != nil { if err != nil {
return err return err

View File

@ -351,7 +351,7 @@ func ShaderProgramImages(imageNum int) shaderir.Program {
texPos, texPos,
{ {
Type: shaderir.UniformVariable, Type: shaderir.UniformVariable,
Index: 1 + i - 1, Index: graphics.TextureOffsetUniformVariableIndex(i),
}, },
}, },
} }

View File

@ -36,6 +36,16 @@ func textureDstSize() vec2 {
} }
` `
for i := 0; i < graphics.ShaderImageNum; i++ {
shaderSuffix += fmt.Sprintf(`
var __textureSize%[1]d vec2
func texture%[1]dSize() vec2 {
return __textureSize%[1]d
}
`, i)
}
for i := 0; i < graphics.ShaderImageNum; i++ { for i := 0; i < graphics.ShaderImageNum; i++ {
var offset string var offset string
if i >= 1 { if i >= 1 {