From 718273c2d7d3e3d3f57a027202e1ba8b8d42b5f4 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Wed, 9 Dec 2020 10:09:55 +0900 Subject: [PATCH] ebiten: Add a new shader function imageDstRegionOnTexture Updates #1428 --- internal/graphics/vertex.go | 14 +++++++++----- internal/graphicsdriver/metal/graphics.go | 12 ++++++++++-- internal/graphicsdriver/opengl/graphics.go | 15 +++++++++++++++ shader.go | 14 ++++++++++++++ 4 files changed, 48 insertions(+), 7 deletions(-) diff --git a/internal/graphics/vertex.go b/internal/graphics/vertex.go index 8c94d34f4..cc6c94037 100644 --- a/internal/graphics/vertex.go +++ b/internal/graphics/vertex.go @@ -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 ( diff --git a/internal/graphicsdriver/metal/graphics.go b/internal/graphicsdriver/metal/graphics.go index 506fbbc94..fca8a5d3a 100644 --- a/internal/graphicsdriver/metal/graphics.go +++ b/internal/graphicsdriver/metal/graphics.go @@ -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)} diff --git a/internal/graphicsdriver/opengl/graphics.go b/internal/graphicsdriver/opengl/graphics.go index 232106c6d..074f01d1b 100644 --- a/internal/graphicsdriver/opengl/graphics.go +++ b/internal/graphicsdriver/opengl/graphics.go @@ -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 { diff --git a/shader.go b/shader.go index 3e7af9420..dd470a06b 100644 --- a/shader.go +++ b/shader.go @@ -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