Skip to content

Commit

Permalink
internal/graphics: add uniform variables for source image sizes
Browse files Browse the repository at this point in the history
Updates #1870
  • Loading branch information
hajimehoshi committed Aug 24, 2023
1 parent 7fe89b1 commit fd5e2d3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 35 deletions.
6 changes: 3 additions & 3 deletions internal/graphics/shader.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ var __imageSrcOffsets [%[2]d]vec2
var __imageSrcRegionOrigin vec2
// The unit is the source texture's pixel or texel.
var __imageSrcRegionSize vec2
var __imageSrcRegionSizes [%[1]d]vec2
// imageSrcRegionOnTexture returns the source image's region (the origin and the size) on its texture.
// The unit is the source texture's pixel or texel.
//
// As an image is a part of internal texture, the image can be located at an arbitrary position on the texture.
func imageSrcRegionOnTexture() (vec2, vec2) {
return __imageSrcRegionOrigin, __imageSrcRegionSize
return __imageSrcRegionOrigin, __imageSrcRegionSizes[0]
}
`, ShaderImageCount, ShaderImageCount-1)

Expand All @@ -97,7 +97,7 @@ func imageSrc%[1]dUnsafeAt(pos vec2) vec4 {
func imageSrc%[1]dAt(pos vec2) vec4 {
// 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.
in := step(__imageSrcRegionOrigin, pos) - step(__imageSrcRegionOrigin + __imageSrcRegionSize, pos)
in := step(__imageSrcRegionOrigin, pos) - step(__imageSrcRegionOrigin + __imageSrcRegionSizes[%[1]d], pos)
return __texelAt(__t%[1]d, %[2]s) * in.x * in.y
}
`, i, pos)
Expand Down
25 changes: 9 additions & 16 deletions internal/graphics/vertex.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,22 @@ const (
// Any shaders in Ebitengine must have these uniform variables.
PreservedUniformVariablesCount = 1 + // the destination texture size
1 + // the source texture sizes array
1 + // the destination image region's origin
1 + // the destination image region's size
1 + // the destination image region origin
1 + // the destination image region size
1 + // the offsets array of the second and the following source images
1 + // the source image region's origin
1 + // the source image region's size
1 + // the source image region origin
1 + // the source image region sizes array
1 // the projection matrix

TextureDestinationSizeUniformVariableIndex = 0
TextureSourceSizesUniformVariableIndex = 1
ImageDestinationRegionOriginUniformVariableIndex = 2
ImageDestinationRegionSizeUniformVariableIndex = 3
ImageSourceOffsetsUniformVariableIndex = 4
ImageSourceRegionOriginUniformVariableIndex = 5
ImageSourceRegionSizeUniformVariableIndex = 6
ProjectionMatrixUniformVariableIndex = 7
ProjectionMatrixUniformVariableIndex = 7

PreservedUniformUint32Count = 2 + // the destination texture size
2*ShaderImageCount + // the source texture sizes array
2 + // the destination image region's origin
2 + // the destination image region's size
2 + // the destination image region origin
2 + // the destination image region size
2*(ShaderImageCount-1) + // the offsets array of the second and the following source images
2 + // the source image region's origin
2 + // the source image region's size
2 + // the source image region origin
2*ShaderImageCount + // the source image region sizes array
16 // the projection matrix
)

Expand Down
44 changes: 28 additions & 16 deletions internal/graphicscommand/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,9 +709,11 @@ func (q *commandQueue) prependPreservedUniforms(uniforms []uint32, shader *Shade
dstRegion.Height /= float32(dh)
}

// Set the destination region.
// Set the destination region origin.
uniforms[10] = math.Float32bits(dstRegion.X)
uniforms[11] = math.Float32bits(dstRegion.Y)

// Set the destination region size.
uniforms[12] = math.Float32bits(dstRegion.Width)
uniforms[13] = math.Float32bits(dstRegion.Height)

Expand All @@ -723,36 +725,46 @@ func (q *commandQueue) prependPreservedUniforms(uniforms []uint32, shader *Shade
srcRegion.Height /= float32(h)
}

// Set the source offsets.
// Set the source region offsets.
uniforms[14] = math.Float32bits(offsets[0][0])
uniforms[15] = math.Float32bits(offsets[0][1])
uniforms[16] = math.Float32bits(offsets[1][0])
uniforms[17] = math.Float32bits(offsets[1][1])
uniforms[18] = math.Float32bits(offsets[2][0])
uniforms[19] = math.Float32bits(offsets[2][1])

// Set the source region of texture0.
// Set the source region origin.
uniforms[20] = math.Float32bits(srcRegion.X)
uniforms[21] = math.Float32bits(srcRegion.Y)

// Set the source region sizes.
// TODO: Set a different sizes for a different source (#1870).
uniforms[22] = math.Float32bits(srcRegion.Width)
uniforms[23] = math.Float32bits(srcRegion.Height)

uniforms[24] = math.Float32bits(2 / float32(dw))
uniforms[25] = 0
uniforms[26] = 0
uniforms[27] = 0
uniforms[28] = 0
uniforms[29] = math.Float32bits(2 / float32(dh))
uniforms[30] = 0
uniforms[24] = math.Float32bits(srcRegion.Width)
uniforms[25] = math.Float32bits(srcRegion.Height)
uniforms[26] = math.Float32bits(srcRegion.Width)
uniforms[27] = math.Float32bits(srcRegion.Height)
uniforms[28] = math.Float32bits(srcRegion.Width)
uniforms[29] = math.Float32bits(srcRegion.Height)

// Set the projection matrix.
uniforms[30] = math.Float32bits(2 / float32(dw))
uniforms[31] = 0
uniforms[32] = 0
uniforms[33] = 0
uniforms[34] = math.Float32bits(1)
uniforms[35] = 0
uniforms[36] = math.Float32bits(-1)
uniforms[37] = math.Float32bits(-1)
uniforms[34] = 0
uniforms[35] = math.Float32bits(2 / float32(dh))
uniforms[36] = 0
uniforms[37] = 0
uniforms[38] = 0
uniforms[39] = math.Float32bits(1)
uniforms[39] = 0
uniforms[40] = math.Float32bits(1)
uniforms[41] = 0
uniforms[42] = math.Float32bits(-1)
uniforms[43] = math.Float32bits(-1)
uniforms[44] = 0
uniforms[45] = math.Float32bits(1)

return uniforms
}
Expand Down

0 comments on commit fd5e2d3

Please sign in to comment.