ebiten: Add a new shader function imageDstRegionOnTexture

Updates #1428
This commit is contained in:
Hajime Hoshi 2020-12-09 10:09:55 +09:00
parent 8d2a4ac917
commit 718273c2d7
4 changed files with 48 additions and 7 deletions

View File

@ -25,15 +25,19 @@ const (
// Any shaders in Ebiten must have these uniform variables.
PreservedUniformVariablesNum = 1 + // the destination texture size
1 + // the texture sizes array
1 + // the texture destination region's origin
1 + // the texture destination region's size
1 + // the offsets array of the second and the following images
1 + // the texture source region's origin
1 // the texture source region's size
DestinationTextureSizeUniformVariableIndex = 0
TextureSizesUniformVariableIndex = 1
TextureSourceOffsetsUniformVariableIndex = 2
TextureSourceRegionOriginUniformVariableIndex = 3
TextureSourceRegionSizeUniformVariableIndex = 4
DestinationTextureSizeUniformVariableIndex = 0
TextureSizesUniformVariableIndex = 1
TextureDestinationRegionOriginUniformVariableIndex = 2
TextureDestinationRegionSizeUniformVariableIndex = 3
TextureSourceOffsetsUniformVariableIndex = 4
TextureSourceRegionOriginUniformVariableIndex = 5
TextureSourceRegionSizeUniformVariableIndex = 6
)
const (

View File

@ -970,6 +970,14 @@ func (g *Graphics) DrawShader(dstID driver.ImageID, srcIDs [graphics.ShaderImage
}
us[graphics.TextureSizesUniformVariableIndex] = usizes
// Set the destination region's origin.
udorigin := []float32{float32(dstRegion.X) / float32(dw), float32(dstRegion.Y) / float32(dh)}
us[graphics.TextureDestinationRegionOriginUniformVariableIndex] = udorigin
// Set the destination region's size.
udsize := []float32{float32(dstRegion.Width) / float32(dw), float32(dstRegion.Height) / float32(dh)}
us[graphics.TextureDestinationRegionSizeUniformVariableIndex] = udsize
// Set the source offsets.
uoffsets := make([]float32, 2*len(offsets))
for i, offset := range offsets {
@ -979,8 +987,8 @@ func (g *Graphics) DrawShader(dstID driver.ImageID, srcIDs [graphics.ShaderImage
us[graphics.TextureSourceOffsetsUniformVariableIndex] = uoffsets
// Set the source region's origin of texture0.
uorigin := []float32{float32(srcRegion.X), float32(srcRegion.Y)}
us[graphics.TextureSourceRegionOriginUniformVariableIndex] = uorigin
usorigin := []float32{float32(srcRegion.X), float32(srcRegion.Y)}
us[graphics.TextureSourceRegionOriginUniformVariableIndex] = usorigin
// Set the source region's size of texture0.
ussize := []float32{float32(srcRegion.Width), float32(srcRegion.Height)}

View File

@ -341,6 +341,21 @@ func (g *Graphics) DrawShader(dst driver.ImageID, srcs [graphics.ShaderImageNum]
us[idx].value = sizes
us[idx].typ = s.ir.Uniforms[idx]
}
dw, dh := d.framebufferSize()
{
origin := []float32{float32(dstRegion.X) / float32(dw), float32(dstRegion.Y) / float32(dh)}
const idx = graphics.TextureDestinationRegionOriginUniformVariableIndex
us[idx].name = fmt.Sprintf("U%d", idx)
us[idx].value = origin
us[idx].typ = s.ir.Uniforms[idx]
}
{
size := []float32{float32(dstRegion.Width) / float32(dw), float32(dstRegion.Height) / float32(dh)}
const idx = graphics.TextureDestinationRegionSizeUniformVariableIndex
us[idx].name = fmt.Sprintf("U%d", idx)
us[idx].value = size
us[idx].typ = s.ir.Uniforms[idx]
}
{
voffsets := make([]float32, 2*len(offsets))
for i, o := range offsets {

View File

@ -49,6 +49,20 @@ func imageSrcTextureSize() vec2 {
return __textureSizes[0]
}
// The unit is the source texture's texel.
var __textureDestinationRegionOrigin vec2
// The unit is the source texture's texel.
var __textureDestinationRegionSize vec2
// imageDstRegionOnTexture returns the destination image's region (the origin and the size) on its texture.
// The unit is the source texture's texel.
//
// As an image is a part of internal texture, the image can be located at an arbitrary position on the texture.
func imageDstRegionOnTexture() (vec2, vec2) {
return __textureDestinationRegionOrigin, __textureDestinationRegionSize
}
// The unit is the source texture's texel.
var __textureSourceOffsets [%[2]d]vec2