Compare commits

...

21 Commits

Author SHA1 Message Date
Bertrand Jung
74b6d77efc
Merge 1dd96726c4 into dc68152f17 2024-08-25 21:35:24 +02:00
Hajime Hoshi
dc68152f17 internal/shader: better error message for the fragment entry point 2024-08-25 19:38:57 +09:00
Hajime Hoshi
9693ce8382 internal/shader: allow less arguments at Fragment
Closes #3073
2024-08-25 19:17:41 +09:00
Hajime Hoshi
e2662a8af7 internal/shader: bug fix: wrong indexing 2024-08-25 18:58:05 +09:00
Hajime Hoshi
41b762ba2c ebiten: update comments
Updates #2640
2024-08-25 18:45:26 +09:00
Hajime Hoshi
2cc809516f ebiten: add Vertex.Custom0 to Custom3
Closes #2640
2024-08-25 18:11:39 +09:00
Hajime Hoshi
bff760af01 internal/shader: bug fix: test failures
Updates #2640
2024-08-25 17:43:44 +09:00
Hajime Hoshi
fef487e09d internal/shaderir/hlsl: refactoring: more flexible generation
Updates #2640
2024-08-25 17:27:00 +09:00
Hajime Hoshi
ed45843c13 internal/graphicsdriver/opengl: assume custom attributes are vec4
Due to HLSL restrictions, all the attributes must have a semantics.
Always assuming custom attributes are vec4 makes things simpler.

Updates #2640
2024-08-25 17:11:28 +09:00
Hajime Hoshi
107189a00d internal/shader: use strings.TrimPrefix 2024-08-25 15:43:44 +09:00
Hajime Hoshi
7142a3bcd9 internal/graphicsdriver/opengl: bug fix: total must be reset 2024-08-25 15:29:22 +09:00
Hajime Hoshi
d570406735 all: update ebitengine/gomobile 2024-08-25 13:39:36 +09:00
Hajime Hoshi
65aec12d88 all: update PureGo to v0.8.0-alpha.5 2024-08-25 13:03:30 +09:00
Hajime Hoshi
a42a8548b1 internal/shader: refactoring 2024-08-25 12:57:11 +09:00
Zyko
1dd96726c4 Add a benchmark + fix sub image allocations 2024-08-15 19:48:36 +02:00
Zyko
30157b5dea Add license header 2024-08-05 20:41:04 +02:00
Zyko
b20692f523 Fixed colorscale mode 2024-08-05 20:33:53 +02:00
Zyko
2eebe55b90 Restore go1.19 2024-08-05 20:27:36 +02:00
Zyko
ec06c68fa3 Re-use internal/packing logic and remove external dep 2024-08-05 20:25:54 +02:00
Zyko
4601cffaba Cleanup 2024-07-27 18:01:06 +02:00
Zyko
5e8d969034 PoC text/v2 glyph atlas 2024-07-27 17:41:53 +02:00
24 changed files with 822 additions and 189 deletions

4
go.mod
View File

@ -3,10 +3,10 @@ module github.com/hajimehoshi/ebiten/v2
go 1.19
require (
github.com/ebitengine/gomobile v0.0.0-20240802043200-192f051f4fcc
github.com/ebitengine/gomobile v0.0.0-20240825043811-96c531f5bd83
github.com/ebitengine/hideconsole v1.0.0
github.com/ebitengine/oto/v3 v3.3.0-alpha.4
github.com/ebitengine/purego v0.8.0-alpha.4
github.com/ebitengine/purego v0.8.0-alpha.5
github.com/gen2brain/mpeg v0.3.2-0.20240412154320-a2ac4fc8a46f
github.com/go-text/typesetting v0.1.1
github.com/hajimehoshi/bitmapfont/v3 v3.2.0-alpha.4

8
go.sum
View File

@ -1,11 +1,11 @@
github.com/ebitengine/gomobile v0.0.0-20240802043200-192f051f4fcc h1:76TYsaP1F48tiQRlrr71NsbfxBcFM9/8bEHS9/JbsQg=
github.com/ebitengine/gomobile v0.0.0-20240802043200-192f051f4fcc/go.mod h1:RM/c3pvru6dRqgGEW7RCTb6czFXYAa3MxbXu3u8/dcI=
github.com/ebitengine/gomobile v0.0.0-20240825043811-96c531f5bd83 h1:yA0CtFKYZI/db1snCOInRS0Z18QGZU6aBYkqUT0H6RI=
github.com/ebitengine/gomobile v0.0.0-20240825043811-96c531f5bd83/go.mod h1:n2NbB/F4d9wOXFzC7FT1ipERidmYWC5I4YNOYRs5N7I=
github.com/ebitengine/hideconsole v1.0.0 h1:5J4U0kXF+pv/DhiXt5/lTz0eO5ogJ1iXb8Yj1yReDqE=
github.com/ebitengine/hideconsole v1.0.0/go.mod h1:hTTBTvVYWKBuxPr7peweneWdkUwEuHuB3C1R/ielR1A=
github.com/ebitengine/oto/v3 v3.3.0-alpha.4 h1:w9SD7kK4GgJULkh5pWVTToMA5Ia1bP7VxD4rIjQqb8M=
github.com/ebitengine/oto/v3 v3.3.0-alpha.4/go.mod h1:B+Sz3hzZXcx251YqSPIj+cVMicvlx7Xiq29AEUIbc7E=
github.com/ebitengine/purego v0.8.0-alpha.4 h1:Dg9xRGC3giyQedfISyHH94eQM0md4a84+HHr7KBBH/Q=
github.com/ebitengine/purego v0.8.0-alpha.4/go.mod h1:SQ56/omnSL8DdaBSKswoBvsMjgaWQyxyeMtb48sOskI=
github.com/ebitengine/purego v0.8.0-alpha.5 h1:M0+PSgsdVNczTB8ijX89HmYqCfb2HUuBEx4A+wNuHto=
github.com/ebitengine/purego v0.8.0-alpha.5/go.mod h1:SQ56/omnSL8DdaBSKswoBvsMjgaWQyxyeMtb48sOskI=
github.com/gen2brain/mpeg v0.3.2-0.20240412154320-a2ac4fc8a46f h1:ysqRe+lvUiL0dH5XzkH0Bz68bFMPJ4f5Si4L/HD9SGk=
github.com/gen2brain/mpeg v0.3.2-0.20240412154320-a2ac4fc8a46f/go.mod h1:i/ebyRRv/IoHixuZ9bElZnXbmfoUVPGQpdsJ4sVuX38=
github.com/go-text/typesetting v0.1.1 h1:bGAesCuo85nXnEN5LmFMVGAGpGkCPtHrZLi//qD7EJo=

View File

@ -295,6 +295,16 @@ type Vertex struct {
ColorG float32
ColorB float32
ColorA float32
// Custom0/Custom1/Custom2/Custom3 represents general-purpose values passed to the shader.
// In order to use them, Fragment must have an additional vec4 argument.
//
// These values are valid only when DrawTrianglesShader is used.
// In other cases, these values are ignored.
Custom0 float32
Custom1 float32
Custom2 float32
Custom3 float32
}
var _ [0]byte = [unsafe.Sizeof(Vertex{}) - unsafe.Sizeof(float32(0))*graphics.VertexFloatCount]byte{}
@ -668,6 +678,10 @@ func (i *Image) DrawTrianglesShader(vertices []Vertex, indices []uint16, shader
vs[i*graphics.VertexFloatCount+5] = v.ColorG
vs[i*graphics.VertexFloatCount+6] = v.ColorB
vs[i*graphics.VertexFloatCount+7] = v.ColorA
vs[i*graphics.VertexFloatCount+8] = v.Custom0
vs[i*graphics.VertexFloatCount+9] = v.Custom1
vs[i*graphics.VertexFloatCount+10] = v.Custom2
vs[i*graphics.VertexFloatCount+11] = v.Custom3
}
is := i.ensureTmpIndices(len(indices))

View File

@ -170,7 +170,7 @@ var ScreenShaderSource = []byte(`//kage:unit pixels
package main
func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
func Fragment(dstPos vec4, srcPos vec2) vec4 {
// Blend source colors in a square region, which size is 1/scale.
scale := imageDstSize()/imageSrc0Size()
pos := srcPos
@ -193,7 +193,7 @@ var ClearShaderSource = []byte(`//kage:unit pixels
package main
func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
func Fragment() vec4 {
return vec4(0)
}
`)

View File

@ -154,8 +154,8 @@ func imageSrc%[1]dAt(pos vec2) vec4 {
shaderSuffix += `
var __projectionMatrix mat4
func __vertex(dstPos vec2, srcPos vec2, color vec4) (vec4, vec2, vec4) {
return __projectionMatrix * vec4(dstPos, 0, 1), srcPos, color
func __vertex(dstPos vec2, srcPos vec2, color vec4, custom vec4) (vec4, vec2, vec4, vec4) {
return __projectionMatrix * vec4(dstPos, 0, 1), srcPos, color, custom
}
`
return shaderSuffix, nil

View File

@ -39,7 +39,7 @@ const (
)
const (
VertexFloatCount = 8
VertexFloatCount = 12
)
var (

View File

@ -27,34 +27,56 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/shaderir/hlsl"
)
var inputElementDescsForDX11 = []_D3D11_INPUT_ELEMENT_DESC{
{
SemanticName: &([]byte("POSITION\000"))[0],
SemanticIndex: 0,
Format: _DXGI_FORMAT_R32G32_FLOAT,
InputSlot: 0,
AlignedByteOffset: _D3D11_APPEND_ALIGNED_ELEMENT,
InputSlotClass: _D3D11_INPUT_PER_VERTEX_DATA,
InstanceDataStepRate: 0,
},
{
SemanticName: &([]byte("TEXCOORD\000"))[0],
SemanticIndex: 0,
Format: _DXGI_FORMAT_R32G32_FLOAT,
InputSlot: 0,
AlignedByteOffset: _D3D11_APPEND_ALIGNED_ELEMENT,
InputSlotClass: _D3D11_INPUT_PER_VERTEX_DATA,
InstanceDataStepRate: 0,
},
{
SemanticName: &([]byte("COLOR\000"))[0],
SemanticIndex: 0,
Format: _DXGI_FORMAT_R32G32B32A32_FLOAT,
InputSlot: 0,
AlignedByteOffset: _D3D11_APPEND_ALIGNED_ELEMENT,
InputSlotClass: _D3D11_INPUT_PER_VERTEX_DATA,
InstanceDataStepRate: 0,
},
var inputElementDescsForDX11 []_D3D11_INPUT_ELEMENT_DESC
func init() {
inputElementDescsForDX11 = []_D3D11_INPUT_ELEMENT_DESC{
{
SemanticName: &([]byte("POSITION\000"))[0],
SemanticIndex: 0,
Format: _DXGI_FORMAT_R32G32_FLOAT,
InputSlot: 0,
AlignedByteOffset: _D3D11_APPEND_ALIGNED_ELEMENT,
InputSlotClass: _D3D11_INPUT_PER_VERTEX_DATA,
InstanceDataStepRate: 0,
},
{
SemanticName: &([]byte("TEXCOORD\000"))[0],
SemanticIndex: 0,
Format: _DXGI_FORMAT_R32G32_FLOAT,
InputSlot: 0,
AlignedByteOffset: _D3D11_APPEND_ALIGNED_ELEMENT,
InputSlotClass: _D3D11_INPUT_PER_VERTEX_DATA,
InstanceDataStepRate: 0,
},
{
SemanticName: &([]byte("COLOR\000"))[0],
SemanticIndex: 0,
Format: _DXGI_FORMAT_R32G32B32A32_FLOAT,
InputSlot: 0,
AlignedByteOffset: _D3D11_APPEND_ALIGNED_ELEMENT,
InputSlotClass: _D3D11_INPUT_PER_VERTEX_DATA,
InstanceDataStepRate: 0,
},
}
diff := graphics.VertexFloatCount - 8
if diff == 0 {
return
}
if diff%4 != 0 {
panic("directx: unexpected attribute layout")
}
for i := 0; i < diff/4; i++ {
inputElementDescsForDX11 = append(inputElementDescsForDX11, _D3D11_INPUT_ELEMENT_DESC{
SemanticName: &([]byte("COLOR\000"))[0],
SemanticIndex: uint32(i) + 1,
Format: _DXGI_FORMAT_R32G32B32A32_FLOAT,
InputSlot: 0,
AlignedByteOffset: _D3D11_APPEND_ALIGNED_ELEMENT,
InputSlotClass: _D3D11_INPUT_PER_VERTEX_DATA,
InstanceDataStepRate: 0,
})
}
}
func blendFactorToBlend11(f graphicsdriver.BlendFactor, alpha bool) _D3D11_BLEND {

View File

@ -23,34 +23,56 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
)
var inputElementDescsForDX12 = []_D3D12_INPUT_ELEMENT_DESC{
{
SemanticName: &([]byte("POSITION\000"))[0],
SemanticIndex: 0,
Format: _DXGI_FORMAT_R32G32_FLOAT,
InputSlot: 0,
AlignedByteOffset: _D3D12_APPEND_ALIGNED_ELEMENT,
InputSlotClass: _D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA,
InstanceDataStepRate: 0,
},
{
SemanticName: &([]byte("TEXCOORD\000"))[0],
SemanticIndex: 0,
Format: _DXGI_FORMAT_R32G32_FLOAT,
InputSlot: 0,
AlignedByteOffset: _D3D12_APPEND_ALIGNED_ELEMENT,
InputSlotClass: _D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA,
InstanceDataStepRate: 0,
},
{
SemanticName: &([]byte("COLOR\000"))[0],
SemanticIndex: 0,
Format: _DXGI_FORMAT_R32G32B32A32_FLOAT,
InputSlot: 0,
AlignedByteOffset: _D3D12_APPEND_ALIGNED_ELEMENT,
InputSlotClass: _D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA,
InstanceDataStepRate: 0,
},
var inputElementDescsForDX12 []_D3D12_INPUT_ELEMENT_DESC
func init() {
inputElementDescsForDX12 = []_D3D12_INPUT_ELEMENT_DESC{
{
SemanticName: &([]byte("POSITION\000"))[0],
SemanticIndex: 0,
Format: _DXGI_FORMAT_R32G32_FLOAT,
InputSlot: 0,
AlignedByteOffset: _D3D12_APPEND_ALIGNED_ELEMENT,
InputSlotClass: _D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA,
InstanceDataStepRate: 0,
},
{
SemanticName: &([]byte("TEXCOORD\000"))[0],
SemanticIndex: 0,
Format: _DXGI_FORMAT_R32G32_FLOAT,
InputSlot: 0,
AlignedByteOffset: _D3D12_APPEND_ALIGNED_ELEMENT,
InputSlotClass: _D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA,
InstanceDataStepRate: 0,
},
{
SemanticName: &([]byte("COLOR\000"))[0],
SemanticIndex: 0,
Format: _DXGI_FORMAT_R32G32B32A32_FLOAT,
InputSlot: 0,
AlignedByteOffset: _D3D12_APPEND_ALIGNED_ELEMENT,
InputSlotClass: _D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA,
InstanceDataStepRate: 0,
},
}
diff := graphics.VertexFloatCount - 8
if diff == 0 {
return
}
if diff%4 != 0 {
panic("directx: unexpected attribute layout")
}
for i := 0; i < diff/4; i++ {
inputElementDescsForDX12 = append(inputElementDescsForDX12, _D3D12_INPUT_ELEMENT_DESC{
SemanticName: &([]byte("COLOR\000"))[0],
SemanticIndex: uint32(i) + 1,
Format: _DXGI_FORMAT_R32G32B32A32_FLOAT,
InputSlot: 0,
AlignedByteOffset: _D3D12_APPEND_ALIGNED_ELEMENT,
InputSlotClass: _D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA,
InstanceDataStepRate: 0,
})
}
}
const numDescriptorsPerFrame = 32

View File

@ -102,7 +102,7 @@ func compileShader(program *shaderir.Program) (vsh, psh *_ID3DBlob, ferr error)
return vsh, psh, nil
}
vs, ps := hlsl.Compile(program)
vs, ps, _ := hlsl.Compile(program)
var flag uint32 = uint32(_D3DCOMPILE_OPTIMIZATION_LEVEL3)
var wg errgroup.Group

View File

@ -66,6 +66,11 @@ func (a *arrayBufferLayout) float32Count() int {
return a.total
}
func (a *arrayBufferLayout) addPart(part arrayBufferLayoutPart) {
a.parts = append(a.parts, part)
a.total = 0
}
// enable starts using the array buffer.
func (a *arrayBufferLayout) enable(context *context) {
for i := range a.parts {
@ -109,17 +114,17 @@ func init() {
},
}
n := theArrayBufferLayout.float32Count()
if n > graphics.VertexFloatCount {
panic("opengl: the array buffer layout is too large")
diff := graphics.VertexFloatCount - n
if diff == 0 {
return
}
if n < graphics.VertexFloatCount {
d := graphics.VertexFloatCount - n
if d > 4 {
panic("opengl: the array buffer layout is too small")
}
theArrayBufferLayout.parts = append(theArrayBufferLayout.parts, arrayBufferLayoutPart{
name: "A3",
num: d,
if diff%4 != 0 {
panic("opengl: unexpected attribute layout")
}
for i := 0; i < diff/4; i++ {
theArrayBufferLayout.addPart(arrayBufferLayoutPart{
name: fmt.Sprintf("A%d", i+3),
num: 4,
})
}
}

View File

@ -290,8 +290,11 @@ func (cs *compileState) parse(f *ast.File) {
// Parse function names so that any other function call the others.
// The function data is provisional and will be updated soon.
var vertexOutParams []shaderir.Type
var fragmentInParams []shaderir.Type
var vertexInParams []variable
var vertexOutParams []variable
var fragmentInParams []variable
var fragmentOutParams []variable
var fragmentReturnType shaderir.Type
for _, d := range f.Decls {
fd, ok := d.(*ast.FuncDecl)
if !ok {
@ -307,6 +310,19 @@ func (cs *compileState) parse(f *ast.File) {
}
inParams, outParams, ret := cs.parseFuncParams(&cs.global, n, fd)
if n == cs.vertexEntry {
vertexInParams = inParams
vertexOutParams = outParams
continue
}
if n == cs.fragmentEntry {
fragmentInParams = inParams
fragmentOutParams = outParams
fragmentReturnType = ret
continue
}
var inT, outT []shaderir.Type
for _, v := range inParams {
inT = append(inT, v.typ)
@ -314,16 +330,6 @@ func (cs *compileState) parse(f *ast.File) {
for _, v := range outParams {
outT = append(outT, v.typ)
}
if n == cs.vertexEntry {
vertexOutParams = outT
continue
}
if n == cs.fragmentEntry {
fragmentInParams = inT
continue
}
cs.funcs = append(cs.funcs, function{
name: n,
ir: shaderir.Func{
@ -339,21 +345,40 @@ func (cs *compileState) parse(f *ast.File) {
// Check varying variables.
// In testings, there might not be vertex and fragment entry points.
if len(vertexOutParams) > 0 && len(fragmentInParams) > 0 {
if len(vertexOutParams) != len(fragmentInParams) {
cs.addError(0, "the number of vertex entry point's returning values and the number of fragment entry point's params must be the same")
}
for i, t := range vertexOutParams {
if !t.Equal(&fragmentInParams[i]) {
cs.addError(0, "vertex entry point's returning value types and fragment entry point's param types must match")
for i, p := range vertexOutParams {
if len(fragmentInParams) <= i {
break
}
t := fragmentInParams[i].typ
if !p.typ.Equal(&t) {
name := fragmentInParams[i].name
cs.addError(0, fmt.Sprintf("fragment argument %s must be %s but was %s", name, p.typ.String(), t.String()))
}
}
// The first out-param is treated as gl_Position in GLSL.
if vertexOutParams[0].typ.Main != shaderir.Vec4 {
cs.addError(0, "vertex entry point must have at least one returning vec4 value for a position")
}
if len(fragmentOutParams) != 0 || fragmentReturnType.Main != shaderir.Vec4 {
cs.addError(0, "fragment entry point must have one returning vec4 value for a color")
}
}
// Set varying veraibles.
if len(cs.errs) > 0 {
return
}
// Set attribute and varying veraibles.
for _, p := range vertexInParams {
cs.ir.Attributes = append(cs.ir.Attributes, p.typ)
}
if len(vertexOutParams) > 0 {
// TODO: Check that these params are not arrays or structs
// The 0th argument is a special variable for position and is not included in varying variables.
cs.ir.Varyings = append(cs.ir.Varyings, vertexOutParams[1:]...)
for _, p := range vertexOutParams[1:] {
cs.ir.Varyings = append(cs.ir.Varyings, p.typ)
}
}
// Parse functions.
@ -766,7 +791,13 @@ func (cs *compileState) parseFuncParams(block *block, fname string, d *ast.FuncD
// If there is only one returning value, it is treated as a returning value.
// An array cannot be a returning value, especially for HLSL (#2923).
if len(out) == 1 && out[0].name == "" && out[0].typ.Main != shaderir.Array {
//
// For the vertex entry, a parameter (variable) is used as a returning value.
// For example, GLSL doesn't treat gl_Position as a returning value.
// Thus, the returning value is not set for the vertex entry.
// TODO: This can be resolved by having an indirect function like what the fragment entry already does.
// See internal/shaderir/glsl.adjustProgram.
if len(out) == 1 && out[0].name == "" && out[0].typ.Main != shaderir.Array && fname != cs.vertexEntry {
ret = out[0].typ
out = nil
}
@ -789,47 +820,25 @@ func (cs *compileState) parseFunc(block *block, d *ast.FuncDecl) (function, bool
}
inParams, outParams, returnType := cs.parseFuncParams(block, d.Name.Name, d)
if block == &cs.global {
switch d.Name.Name {
case cs.vertexEntry:
for _, v := range inParams {
cs.ir.Attributes = append(cs.ir.Attributes, v.typ)
}
// For the vertex entry, a parameter (variable) is used as a returning value.
// For example, GLSL doesn't treat gl_Position as a returning value.
// TODO: This can be resolved by having an indirect function like what the fragment entry already does.
// See internal/shaderir/glsl.adjustProgram.
if len(outParams) == 0 {
outParams = append(outParams, variable{
typ: shaderir.Type{Main: shaderir.Vec4},
if d.Name.Name == cs.fragmentEntry {
if len(inParams) == 0 {
inParams = append(inParams, variable{
name: "_",
typ: shaderir.Type{Main: shaderir.Vec4},
})
}
// The 0th inParams is a special variable for position and is not included in varying variables.
if diff := len(cs.ir.Varyings) - (len(inParams) - 1); diff > 0 {
// inParams is not enough when the vertex shader has more returning values than the fragment shader's arguments.
orig := len(inParams) - 1
for i := 0; i < diff; i++ {
inParams = append(inParams, variable{
name: "_",
typ: cs.ir.Varyings[orig+i],
})
}
// The first out-param is treated as gl_Position in GLSL.
if outParams[0].typ.Main != shaderir.Vec4 {
cs.addError(d.Pos(), "vertex entry point must have at least one returning vec4 value for a position")
return function{}, false
}
case cs.fragmentEntry:
if len(inParams) == 0 {
cs.addError(d.Pos(), "fragment entry point must have at least one vec4 parameter for a position")
return function{}, false
}
if inParams[0].typ.Main != shaderir.Vec4 {
cs.addError(d.Pos(), "fragment entry point must have at least one vec4 parameter for a position")
return function{}, false
}
if len(outParams) != 0 || returnType.Main != shaderir.Vec4 {
cs.addError(d.Pos(), "fragment entry point must have one returning vec4 value for a color")
return function{}, false
}
}
}
b, ok := cs.parseBlock(block, d.Name.Name, d.Body.List, inParams, outParams, returnType, true)
if !ok {
return function{}, false

View File

@ -31,32 +31,24 @@ import (
func glslVertexNormalize(str string) string {
p := glsl.VertexPrelude(glsl.GLSLVersionDefault)
if strings.HasPrefix(str, p) {
str = str[len(p):]
}
str = strings.TrimPrefix(str, p)
return strings.TrimSpace(str)
}
func glslFragmentNormalize(str string) string {
p := glsl.FragmentPrelude(glsl.GLSLVersionDefault)
if strings.HasPrefix(str, p) {
str = str[len(p):]
}
str = strings.TrimPrefix(str, p)
return strings.TrimSpace(str)
}
func hlslNormalize(str string) string {
if strings.HasPrefix(str, hlsl.Prelude) {
str = str[len(hlsl.Prelude):]
}
func hlslNormalize(str string, prelude string) string {
str = strings.TrimPrefix(str, prelude)
return strings.TrimSpace(str)
}
func metalNormalize(str string) string {
prelude := msl.Prelude(shaderir.Texels)
if strings.HasPrefix(str, prelude) {
str = str[len(prelude):]
}
str = strings.TrimPrefix(str, prelude)
return strings.TrimSpace(str)
}
@ -188,8 +180,8 @@ func TestCompile(t *testing.T) {
}
if tc.HLSL != nil {
vs, _ := hlsl.Compile(s)
if got, want := hlslNormalize(vs), hlslNormalize(string(tc.HLSL)); got != want {
vs, _, prelude := hlsl.Compile(s)
if got, want := hlslNormalize(vs, prelude), hlslNormalize(string(tc.HLSL), prelude); got != want {
compare(t, "HLSL", got, want)
}
}

View File

@ -2,7 +2,7 @@ cbuffer Uniforms : register(b0) {
float2 U0 : packoffset(c0);
}
Varyings VSMain(float2 A0 : POSITION, float2 A1 : TEXCOORD, float4 A2 : COLOR) {
Varyings VSMain(float2 A0 : POSITION, float2 A1 : TEXCOORD, float4 A2 : COLOR0) {
Varyings varyings;
float4x4 l0 = 0.0;
varyings.Position = 0.0;

View File

@ -52,13 +52,7 @@ func (c *compileContext) structName(p *shaderir.Program, t *shaderir.Type) strin
return n
}
const Prelude = `struct Varyings {
float4 Position : SV_POSITION;
float2 M0 : TEXCOORD0;
float4 M1 : COLOR;
};
float mod(float x, float y) {
const utilFuncs = `float mod(float x, float y) {
return x - y * floor(x/y);
}
@ -86,7 +80,7 @@ float4x4 float4x4FromScalar(float x) {
return float4x4(x, 0, 0, 0, 0, x, 0, 0, 0, 0, x, 0, 0, 0, 0, x);
}`
func Compile(p *shaderir.Program) (vertexShader, pixelShader string) {
func Compile(p *shaderir.Program) (vertexShader, pixelShader, prelude string) {
offsets := CalcUniformMemoryOffsets(p)
c := &compileContext{
@ -94,7 +88,30 @@ func Compile(p *shaderir.Program) (vertexShader, pixelShader string) {
}
var lines []string
lines = append(lines, strings.Split(Prelude, "\n")...)
lines = append(lines, strings.Split(utilFuncs, "\n")...)
lines = append(lines, "", "struct Varyings {")
lines = append(lines, "\tfloat4 Position : SV_POSITION;")
if len(p.Varyings) > 0 {
for i, v := range p.Varyings {
switch i {
case 0:
lines = append(lines, fmt.Sprintf("\tfloat2 M%d : TEXCOORD;", i))
case 1:
lines = append(lines, fmt.Sprintf("\tfloat4 M%d : COLOR0;", i))
default:
// Use COLOR[n] as a general purpose varying.
if v.Main != shaderir.Vec4 {
lines = append(lines, fmt.Sprintf("\t?(unexpected type: %s) M%d : COLOR%d;", v, i, i-1))
} else {
lines = append(lines, fmt.Sprintf("\tfloat4 M%d : COLOR%d;", i, i-1))
}
}
}
}
lines = append(lines, "};")
prelude = strings.Join(lines, "\n")
lines = append(lines, "", "{{.Structs}}")
if len(p.Uniforms) > 0 {
@ -156,7 +173,25 @@ func Compile(p *shaderir.Program) (vertexShader, pixelShader string) {
}
if p.VertexFunc.Block != nil && len(p.VertexFunc.Block.Stmts) > 0 {
vslines = append(vslines, "")
vslines = append(vslines, "Varyings VSMain(float2 A0 : POSITION, float2 A1 : TEXCOORD, float4 A2 : COLOR) {")
var args []string
for i, a := range p.Attributes {
switch i {
case 0:
args = append(args, fmt.Sprintf("float2 A%d : POSITION", i))
case 1:
args = append(args, fmt.Sprintf("float2 A%d : TEXCOORD", i))
case 2:
args = append(args, fmt.Sprintf("float4 A%d : COLOR0", i))
default:
// Use COLOR[n] as a general purpose varying.
if a.Main != shaderir.Vec4 {
args = append(args, fmt.Sprintf("?(unexpected type: %s) A%d : COLOR%d", a, i, i-2))
} else {
args = append(args, fmt.Sprintf("float4 A%d : COLOR%d", i, i-2))
}
}
}
vslines = append(vslines, "Varyings VSMain("+strings.Join(args, ", ")+") {")
vslines = append(vslines, fmt.Sprintf("\tVaryings %s;", vsOut))
vslines = append(vslines, c.block(p, p.VertexFunc.Block, p.VertexFunc.Block, 0)...)
if last := fmt.Sprintf("\treturn %s;", vsOut); vslines[len(vslines)-1] != last {

View File

@ -2595,3 +2595,193 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
}
}
}
func TestShaderCustomValues(t *testing.T) {
const w, h = 16, 16
dst := ebiten.NewImage(w, h)
s, err := ebiten.NewShader([]byte(`//kage:unit pixels
package main
func Fragment(dstPos vec4, srcPos vec2, color vec4, custom vec4) vec4 {
return custom
}
`))
if err != nil {
t.Fatal(err)
}
clr := color.RGBA{R: 0x10, G: 0x20, B: 0x30, A: 0x40}
dst.DrawTrianglesShader([]ebiten.Vertex{
{
DstX: 0,
DstY: 0,
SrcX: 0,
SrcY: 0,
ColorR: 1,
ColorG: 1,
ColorB: 1,
ColorA: 1,
Custom0: float32(clr.R) / 0xff,
Custom1: float32(clr.G) / 0xff,
Custom2: float32(clr.B) / 0xff,
Custom3: float32(clr.A) / 0xff,
},
{
DstX: w,
DstY: 0,
SrcX: w,
SrcY: 0,
ColorR: 1,
ColorG: 1,
ColorB: 1,
ColorA: 1,
Custom0: float32(clr.R) / 0xff,
Custom1: float32(clr.G) / 0xff,
Custom2: float32(clr.B) / 0xff,
Custom3: float32(clr.A) / 0xff,
},
{
DstX: 0,
DstY: h,
SrcX: 0,
SrcY: h,
ColorR: 1,
ColorG: 1,
ColorB: 1,
ColorA: 1,
Custom0: float32(clr.R) / 0xff,
Custom1: float32(clr.G) / 0xff,
Custom2: float32(clr.B) / 0xff,
Custom3: float32(clr.A) / 0xff,
},
{
DstX: w,
DstY: h,
SrcX: w,
SrcY: h,
ColorR: 1,
ColorG: 1,
ColorB: 1,
ColorA: 1,
Custom0: float32(clr.R) / 0xff,
Custom1: float32(clr.G) / 0xff,
Custom2: float32(clr.B) / 0xff,
Custom3: float32(clr.A) / 0xff,
},
}, []uint16{0, 1, 2, 1, 2, 3}, s, nil)
for j := 0; j < h; j++ {
for i := 0; i < w; i++ {
got := dst.At(i, j).(color.RGBA)
want := clr
if !sameColors(got, want, 2) {
t.Errorf("dst.At(%d, %d): got: %v, want: %v", i, j, got, want)
}
}
}
}
func TestShaderFragmentLessArguments(t *testing.T) {
const w, h = 16, 16
s0, err := ebiten.NewShader([]byte(`//kage:unit pixels
package main
func Fragment() vec4 {
return vec4(1, 0, 0, 1)
}
`))
if err != nil {
t.Fatal(err)
}
s1, err := ebiten.NewShader([]byte(`//kage:unit pixels
package main
func Fragment(dstPos vec4) vec4 {
return vec4(0, 1, 0, 1)
}
`))
if err != nil {
t.Fatal(err)
}
s2, err := ebiten.NewShader([]byte(`//kage:unit pixels
package main
func Fragment(dstPos vec4, srcPos vec2) vec4 {
return vec4(0, 0, 1, 1)
}
`))
if err != nil {
t.Fatal(err)
}
dst := ebiten.NewImage(w, h)
for idx, s := range []*ebiten.Shader{s0, s1, s2} {
dst.Clear()
dst.DrawTrianglesShader([]ebiten.Vertex{
{
DstX: 0,
DstY: 0,
SrcX: 0,
SrcY: 0,
ColorR: 1,
ColorG: 1,
ColorB: 1,
ColorA: 1,
},
{
DstX: w,
DstY: 0,
SrcX: w,
SrcY: 0,
ColorR: 1,
ColorG: 1,
ColorB: 1,
ColorA: 1,
},
{
DstX: 0,
DstY: h,
SrcX: 0,
SrcY: h,
ColorR: 1,
ColorG: 1,
ColorB: 1,
ColorA: 1,
},
{
DstX: w,
DstY: h,
SrcX: w,
SrcY: h,
ColorR: 1,
ColorG: 1,
ColorB: 1,
ColorA: 1,
},
}, []uint16{0, 1, 2, 1, 2, 3}, s, nil)
for j := 0; j < h; j++ {
for i := 0; i < w; i++ {
got := dst.At(i, j).(color.RGBA)
var want color.RGBA
switch idx {
case 0:
want = color.RGBA{R: 0xff, A: 0xff}
case 1:
want = color.RGBA{G: 0xff, A: 0xff}
case 2:
want = color.RGBA{B: 0xff, A: 0xff}
}
if !sameColors(got, want, 2) {
t.Errorf("dst.At(%d, %d): got: %v, want: %v", i, j, got, want)
}
}
}
}
}

297
text/v2/atlas.go Normal file
View File

@ -0,0 +1,297 @@
// Copyright 2024 The Ebitengine Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package text
import (
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/internal/packing"
)
type glyphAtlas struct {
page *packing.Page
image *ebiten.Image
}
type glyphImage struct {
atlas *glyphAtlas
node *packing.Node
img *ebiten.Image
}
func (i *glyphImage) Image() *ebiten.Image {
return i.img
}
func newGlyphAtlas() *glyphAtlas {
return &glyphAtlas{
// Note: 128x128 is arbitrary, maybe a better value can be inferred
// from the font size or something
page: packing.NewPage(128, 128, 1024), // TODO: not 1024
image: ebiten.NewImage(128, 128),
}
}
func (g *glyphAtlas) NewImage(w, h int) *glyphImage {
n := g.page.Alloc(w, h)
pw, ph := g.page.Size()
if pw > g.image.Bounds().Dx() || ph > g.image.Bounds().Dy() {
newImage := ebiten.NewImage(pw, ph)
newImage.DrawImage(g.image, nil)
g.image = newImage
}
return &glyphImage{
atlas: g,
node: n,
img: g.image.SubImage(n.Region()).(*ebiten.Image),
}
}
func (g *glyphAtlas) Free(img *glyphImage) {
g.page.Free(img.node)
}
type drawRange struct {
atlas *glyphAtlas
end int
}
// drawList stores triangle versions of DrawImage calls when
// all images are sub-images of an atlas.
// Temporary vertices and indices can be re-used after calling
// Flush, so it is more efficient to keep a reference to a drawList
// instead of creating a new one every frame.
type drawList struct {
ranges []drawRange
vx []ebiten.Vertex
ix []uint16
}
// drawCommand is the equivalent of the regular DrawImageOptions
// but only including options that will not break batching.
// Filter, Address, Blend and AntiAlias are determined at Flush()
type drawCommand struct {
Image *glyphImage
ColorScale ebiten.ColorScale
GeoM ebiten.GeoM
}
var rectIndices = [6]uint16{0, 1, 2, 1, 2, 3}
type point struct {
X, Y float32
}
func pt(x, y float64) point {
return point{
X: float32(x),
Y: float32(y),
}
}
type rectOpts struct {
Dsts [4]point
SrcX0, SrcY0 float32
SrcX1, SrcY1 float32
R, G, B, A float32
}
// adjustDestinationPixel is the original ebitengine implementation found here:
// https://github.com/hajimehoshi/ebiten/blob/v2.8.0-alpha.1/internal/graphics/vertex.go#L102-L126
func adjustDestinationPixel(x float32) float32 {
// Avoid the center of the pixel, which is problematic (#929, #1171).
// Instead, align the vertices with about 1/3 pixels.
//
// The intention here is roughly this code:
//
// float32(math.Floor((float64(x)+1.0/6.0)*3) / 3)
//
// The actual implementation is more optimized than the above implementation.
ix := float32(int(x))
if x < 0 && x != ix {
ix -= 1
}
frac := x - ix
switch {
case frac < 3.0/16.0:
return ix
case frac < 8.0/16.0:
return ix + 5.0/16.0
case frac < 13.0/16.0:
return ix + 11.0/16.0
default:
return ix + 16.0/16.0
}
}
func appendRectVerticesIndices(vertices []ebiten.Vertex, indices []uint16, index int, opts *rectOpts) ([]ebiten.Vertex, []uint16) {
sx0, sy0, sx1, sy1 := opts.SrcX0, opts.SrcY0, opts.SrcX1, opts.SrcY1
r, g, b, a := opts.R, opts.G, opts.B, opts.A
vertices = append(vertices,
ebiten.Vertex{
DstX: adjustDestinationPixel(opts.Dsts[0].X),
DstY: adjustDestinationPixel(opts.Dsts[0].Y),
SrcX: sx0,
SrcY: sy0,
ColorR: r,
ColorG: g,
ColorB: b,
ColorA: a,
},
ebiten.Vertex{
DstX: adjustDestinationPixel(opts.Dsts[1].X),
DstY: adjustDestinationPixel(opts.Dsts[1].Y),
SrcX: sx1,
SrcY: sy0,
ColorR: r,
ColorG: g,
ColorB: b,
ColorA: a,
},
ebiten.Vertex{
DstX: adjustDestinationPixel(opts.Dsts[2].X),
DstY: adjustDestinationPixel(opts.Dsts[2].Y),
SrcX: sx0,
SrcY: sy1,
ColorR: r,
ColorG: g,
ColorB: b,
ColorA: a,
},
ebiten.Vertex{
DstX: adjustDestinationPixel(opts.Dsts[3].X),
DstY: adjustDestinationPixel(opts.Dsts[3].Y),
SrcX: sx1,
SrcY: sy1,
ColorR: r,
ColorG: g,
ColorB: b,
ColorA: a,
},
)
indiceCursor := uint16(index * 4)
indices = append(indices,
rectIndices[0]+indiceCursor,
rectIndices[1]+indiceCursor,
rectIndices[2]+indiceCursor,
rectIndices[3]+indiceCursor,
rectIndices[4]+indiceCursor,
rectIndices[5]+indiceCursor,
)
return vertices, indices
}
// Add adds DrawImage commands to the DrawList, images from multiple
// atlases can be added but they will break the previous batch bound to
// a different atlas, requiring an additional draw call internally.
// So, it is better to have the maximum of consecutive DrawCommand images
// sharing the same atlas.
func (dl *drawList) Add(commands ...*drawCommand) {
if len(commands) == 0 {
return
}
var batch *drawRange
if len(dl.ranges) > 0 {
batch = &dl.ranges[len(dl.ranges)-1]
} else {
dl.ranges = append(dl.ranges, drawRange{
atlas: commands[0].Image.atlas,
})
batch = &dl.ranges[0]
}
// Add vertices and indices
opts := &rectOpts{}
for _, cmd := range commands {
if cmd.Image.atlas != batch.atlas {
dl.ranges = append(dl.ranges, drawRange{
atlas: cmd.Image.atlas,
})
batch = &dl.ranges[len(dl.ranges)-1]
}
// Dst attributes
bounds := cmd.Image.node.Region()
opts.Dsts[0] = pt(cmd.GeoM.Apply(0, 0))
opts.Dsts[1] = pt(cmd.GeoM.Apply(
float64(bounds.Dx()), 0,
))
opts.Dsts[2] = pt(cmd.GeoM.Apply(
0, float64(bounds.Dy()),
))
opts.Dsts[3] = pt(cmd.GeoM.Apply(
float64(bounds.Dx()), float64(bounds.Dy()),
))
// Color and source attributes
opts.R = cmd.ColorScale.R()
opts.G = cmd.ColorScale.G()
opts.B = cmd.ColorScale.B()
opts.A = cmd.ColorScale.A()
opts.SrcX0 = float32(bounds.Min.X)
opts.SrcY0 = float32(bounds.Min.Y)
opts.SrcX1 = float32(bounds.Max.X)
opts.SrcY1 = float32(bounds.Max.Y)
dl.vx, dl.ix = appendRectVerticesIndices(
dl.vx, dl.ix, batch.end, opts,
)
batch.end++
}
}
// DrawOptions are additional options that will be applied to
// all draw commands from the draw list when calling Flush().
type drawOptions struct {
ColorScaleMode ebiten.ColorScaleMode
Blend ebiten.Blend
Filter ebiten.Filter
Address ebiten.Address
AntiAlias bool
}
// Flush executes all the draw commands as the smallest possible
// amount of draw calls, and then clears the list for next uses.
func (dl *drawList) Flush(dst *ebiten.Image, opts *drawOptions) {
var topts *ebiten.DrawTrianglesOptions
if opts != nil {
topts = &ebiten.DrawTrianglesOptions{
ColorScaleMode: opts.ColorScaleMode,
Blend: opts.Blend,
Filter: opts.Filter,
Address: opts.Address,
AntiAlias: opts.AntiAlias,
}
}
index := 0
for _, r := range dl.ranges {
dst.DrawTriangles(
dl.vx[index*4:(index+r.end)*4],
dl.ix[index*6:(index+r.end)*6],
r.atlas.image,
topts,
)
index += r.end
}
// Clear buffers
dl.ranges = dl.ranges[:0]
dl.vx = dl.vx[:0]
dl.ix = dl.ix[:0]
}

View File

@ -18,7 +18,6 @@ import (
"math"
"sync"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/internal/hook"
)
@ -38,17 +37,18 @@ func init() {
}
type glyphImageCacheEntry struct {
image *ebiten.Image
image *glyphImage
atime int64
}
type glyphImageCache[Key comparable] struct {
atlas *glyphAtlas
cache map[Key]*glyphImageCacheEntry
atime int64
m sync.Mutex
}
func (g *glyphImageCache[Key]) getOrCreate(face Face, key Key, create func() *ebiten.Image) *ebiten.Image {
func (g *glyphImageCache[Key]) getOrCreate(face Face, key Key, create func(a *glyphAtlas) *glyphImage) *glyphImage {
g.m.Lock()
defer g.m.Unlock()
@ -61,10 +61,11 @@ func (g *glyphImageCache[Key]) getOrCreate(face Face, key Key, create func() *eb
}
if g.cache == nil {
g.atlas = newGlyphAtlas()
g.cache = map[Key]*glyphImageCacheEntry{}
}
img := create()
img := create(g.atlas)
e = &glyphImageCacheEntry{
image: img,
}
@ -91,6 +92,7 @@ func (g *glyphImageCache[Key]) getOrCreate(face Face, key Key, create func() *eb
continue
}
delete(g.cache, key)
g.atlas.Free(e.image)
}
}
}

View File

@ -311,11 +311,16 @@ func (g *GoTextFace) appendGlyphsForLine(glyphs []Glyph, line string, indexOffse
img, imgX, imgY := g.glyphImage(glyph, o)
// Append a glyph even if img is nil.
// This is necessary to return index information for control characters.
var ebitenImage *ebiten.Image
if img != nil {
ebitenImage = img.Image()
}
glyphs = append(glyphs, Glyph{
img: img,
StartIndexInBytes: indexOffset + glyph.startIndex,
EndIndexInBytes: indexOffset + glyph.endIndex,
GID: uint32(glyph.shapingGlyph.GlyphID),
Image: img,
Image: ebitenImage,
X: float64(imgX),
Y: float64(imgY),
OriginX: fixed26_6ToFloat64(origin.X),
@ -332,7 +337,7 @@ func (g *GoTextFace) appendGlyphsForLine(glyphs []Glyph, line string, indexOffse
return glyphs
}
func (g *GoTextFace) glyphImage(glyph glyph, origin fixed.Point26_6) (*ebiten.Image, int, int) {
func (g *GoTextFace) glyphImage(glyph glyph, origin fixed.Point26_6) (*glyphImage, int, int) {
if g.direction().isHorizontal() {
origin.X = adjustGranularity(origin.X, g)
origin.Y &^= ((1 << 6) - 1)
@ -352,8 +357,8 @@ func (g *GoTextFace) glyphImage(glyph glyph, origin fixed.Point26_6) (*ebiten.Im
yoffset: subpixelOffset.Y,
variations: g.ensureVariationsString(),
}
img := g.Source.getOrCreateGlyphImage(g, key, func() *ebiten.Image {
return segmentsToImage(glyph.scaledSegments, subpixelOffset, b)
img := g.Source.getOrCreateGlyphImage(g, key, func(a *glyphAtlas) *glyphImage {
return segmentsToImage(a, glyph.scaledSegments, subpixelOffset, b)
})
imgX := (origin.X + b.Min.X).Floor()

View File

@ -26,8 +26,6 @@ import (
"github.com/go-text/typesetting/opentype/loader"
"github.com/go-text/typesetting/shaping"
"golang.org/x/image/math/fixed"
"github.com/hajimehoshi/ebiten/v2"
)
type goTextOutputCacheKey struct {
@ -282,7 +280,7 @@ func (g *GoTextFaceSource) scale(size float64) float64 {
return size / float64(g.f.Upem())
}
func (g *GoTextFaceSource) getOrCreateGlyphImage(goTextFace *GoTextFace, key goTextGlyphImageCacheKey, create func() *ebiten.Image) *ebiten.Image {
func (g *GoTextFaceSource) getOrCreateGlyphImage(goTextFace *GoTextFace, key goTextGlyphImageCacheKey, create func(a *glyphAtlas) *glyphImage) *glyphImage {
if g.glyphImageCache == nil {
g.glyphImageCache = map[float64]*glyphImageCache[goTextGlyphImageCacheKey]{}
}

View File

@ -19,11 +19,11 @@ import (
"image/draw"
"math"
"github.com/go-text/typesetting/opentype/api"
"golang.org/x/image/math/fixed"
gvector "golang.org/x/image/vector"
"github.com/hajimehoshi/ebiten/v2"
"github.com/go-text/typesetting/opentype/api"
"golang.org/x/image/math/fixed"
"github.com/hajimehoshi/ebiten/v2/vector"
)
@ -75,7 +75,7 @@ func segmentsToBounds(segs []api.Segment) fixed.Rectangle26_6 {
}
}
func segmentsToImage(segs []api.Segment, subpixelOffset fixed.Point26_6, glyphBounds fixed.Rectangle26_6) *ebiten.Image {
func segmentsToImage(a *glyphAtlas, segs []api.Segment, subpixelOffset fixed.Point26_6, glyphBounds fixed.Rectangle26_6) *glyphImage {
if len(segs) == 0 {
return nil
}
@ -122,7 +122,10 @@ func segmentsToImage(segs []api.Segment, subpixelOffset fixed.Point26_6, glyphBo
dst := image.NewRGBA(image.Rect(0, 0, w, h))
rast.Draw(dst, dst.Bounds(), image.Opaque, image.Point{})
return ebiten.NewImageFromImage(dst)
img := a.NewImage(w, h)
img.Image().WritePixels(dst.Pix)
return img
}
func appendVectorPathFromSegments(path *vector.Path, segs []api.Segment, x, y float32) {

View File

@ -21,7 +21,6 @@ import (
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/vector"
)
@ -119,9 +118,10 @@ func (s *GoXFace) appendGlyphsForLine(glyphs []Glyph, line string, indexOffset i
// Append a glyph even if img is nil.
// This is necessary to return index information for control characters.
glyphs = append(glyphs, Glyph{
img: img,
StartIndexInBytes: indexOffset + i,
EndIndexInBytes: indexOffset + i + size,
Image: img,
Image: img.Image(),
X: float64(imgX),
Y: float64(imgY),
OriginX: fixed26_6ToFloat64(origin.X),
@ -136,7 +136,7 @@ func (s *GoXFace) appendGlyphsForLine(glyphs []Glyph, line string, indexOffset i
return glyphs
}
func (s *GoXFace) glyphImage(r rune, origin fixed.Point26_6) (*ebiten.Image, int, int, fixed.Int26_6) {
func (s *GoXFace) glyphImage(r rune, origin fixed.Point26_6) (*glyphImage, int, int, fixed.Int26_6) {
// Assume that GoXFace's direction is always horizontal.
origin.X = adjustGranularity(origin.X, s)
origin.Y &^= ((1 << 6) - 1)
@ -150,15 +150,15 @@ func (s *GoXFace) glyphImage(r rune, origin fixed.Point26_6) (*ebiten.Image, int
rune: r,
xoffset: subpixelOffset.X,
}
img := s.glyphImageCache.getOrCreate(s, key, func() *ebiten.Image {
return s.glyphImageImpl(r, subpixelOffset, b)
img := s.glyphImageCache.getOrCreate(s, key, func(a *glyphAtlas) *glyphImage {
return s.glyphImageImpl(a, r, subpixelOffset, b)
})
imgX := (origin.X + b.Min.X).Floor()
imgY := (origin.Y + b.Min.Y).Floor()
return img, imgX, imgY, a
}
func (s *GoXFace) glyphImageImpl(r rune, subpixelOffset fixed.Point26_6, glyphBounds fixed.Rectangle26_6) *ebiten.Image {
func (s *GoXFace) glyphImageImpl(a *glyphAtlas, r rune, subpixelOffset fixed.Point26_6, glyphBounds fixed.Rectangle26_6) *glyphImage {
w, h := (glyphBounds.Max.X - glyphBounds.Min.X).Ceil(), (glyphBounds.Max.Y - glyphBounds.Min.Y).Ceil()
if w == 0 || h == 0 {
return nil
@ -182,7 +182,10 @@ func (s *GoXFace) glyphImageImpl(r rune, subpixelOffset fixed.Point26_6, glyphBo
}
d.DrawString(string(r))
return ebiten.NewImageFromImage(rgba)
img := a.NewImage(w, h)
img.Image().WritePixels(rgba.Pix)
return img
}
// direction implements Face.

View File

@ -111,15 +111,24 @@ func Draw(dst *ebiten.Image, text string, face Face, options *DrawOptions) {
geoM := drawOp.GeoM
dl := &drawList{}
dc := &drawCommand{}
for _, g := range AppendGlyphs(nil, text, face, &layoutOp) {
if g.Image == nil {
continue
}
drawOp.GeoM.Reset()
drawOp.GeoM.Translate(g.X, g.Y)
drawOp.GeoM.Concat(geoM)
dst.DrawImage(g.Image, &drawOp)
dc.GeoM.Reset()
dc.GeoM.Translate(g.X, g.Y)
dc.GeoM.Concat(geoM)
dc.ColorScale = drawOp.ColorScale
dc.Image = g.img
dl.Add(dc)
}
dl.Flush(dst, &drawOptions{
Blend: drawOp.Blend,
Filter: drawOp.Filter,
ColorScaleMode: ebiten.ColorScaleModePremultipliedAlpha,
})
}
// AppendGlyphs appends glyphs to the given slice and returns a slice.

View File

@ -115,6 +115,11 @@ func adjustGranularity(x fixed.Int26_6, face Face) fixed.Int26_6 {
// Glyph represents one glyph to render.
type Glyph struct {
// Image is a rasterized glyph image.
// Image is a grayscale image i.e. RGBA values are the same.
// Image should be used as a render source and should not be modified.
img *glyphImage
// StartIndexInBytes is the start index in bytes for the given string at AppendGlyphs.
StartIndexInBytes int

View File

@ -15,6 +15,7 @@
package text_test
import (
"bytes"
"image"
"image/color"
"regexp"
@ -23,6 +24,7 @@ import (
"github.com/hajimehoshi/bitmapfont/v3"
"golang.org/x/image/font"
"golang.org/x/image/font/gofont/goregular"
"golang.org/x/image/math/fixed"
"github.com/hajimehoshi/ebiten/v2"
@ -371,3 +373,23 @@ func TestDrawOptionsNotModified(t *testing.T) {
t.Errorf("got: %v, want: %v", got, want)
}
}
func BenchmarkDrawText(b *testing.B) {
var txt string
for i := 0; i < 32; i++ {
txt += "The quick brown fox jumps over the lazy dog.\n"
}
screen := ebiten.NewImage(16, 16)
source, err := text.NewGoTextFaceSource(bytes.NewReader(goregular.TTF))
if err != nil {
b.Fatal(err)
}
f := &text.GoTextFace{
Source: source,
Size: 10,
}
op := &text.DrawOptions{}
for i := 0; i < b.N; i++ {
text.Draw(screen, txt, f, op)
}
}