Skip to content

Commit 9ebddf3

Browse files
committed
support GLES3 from X-Lab
1 parent 9141d09 commit 9ebddf3

9 files changed

+176
-155
lines changed

Diff for: gl_backend.go

+104-92
Large diffs are not rendered by default.

Diff for: gl_struct.go

+13-17
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package nanovgo
22

3-
import (
4-
"github.com/goxjs/gl"
5-
)
6-
73
const (
84
nsvgShaderFILLGRAD = iota
95
nsvgShaderFILLIMG
@@ -24,19 +20,19 @@ const (
2420

2521
type glCall struct {
2622
callType glnvgCallType
27-
image int
28-
pathOffset int
29-
pathCount int
30-
triangleOffset int
31-
triangleCount int
32-
uniformOffset int
23+
image int32
24+
pathOffset int32
25+
pathCount int32
26+
triangleOffset int32
27+
triangleCount int32
28+
uniformOffset int32
3329
}
3430

3531
type glPath struct {
36-
fillOffset int
37-
fillCount int
38-
strokeOffset int
39-
strokeCount int
32+
fillOffset int32
33+
fillCount int32
34+
strokeOffset int32
35+
strokeCount int32
4036
}
4137

4238
type glFragUniforms [44]float32
@@ -108,9 +104,9 @@ func (u *glFragUniforms) setType(typeCode float32) {
108104
}
109105

110106
type glTexture struct {
111-
id int
112-
tex gl.Texture
113-
width, height int
107+
id int32
108+
tex uint32 //gl.Texture
109+
width, height int32
114110
texType nvgTextureType
115111
flags ImageFlags
116112
}

Diff for: nanovgo.go

+33-32
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package nanovgo
22

33
import (
44
"bytes"
5-
"github.com/shibukawa/nanovgo/fontstashmini"
65
"image"
76
_ "image/jpeg" // to read jpeg
87
_ "image/png" // to read png
98
"log"
109
"os"
10+
11+
"github.com/shibukawa/nanovgo/fontstashmini"
1112
)
1213

1314
// Context is an entry point object to use NanoVGo API and created by NewContext() function.
@@ -116,12 +117,12 @@ type Context struct {
116117
fringeWidth float32
117118
devicePxRatio float32
118119
fs *fontstashmini.FontStash
119-
fontImages []int
120-
fontImageIdx int
121-
drawCallCount int
122-
fillTriCount int
123-
strokeTriCount int
124-
textTriCount int
120+
fontImages []int32
121+
fontImageIdx int32
122+
drawCallCount int32
123+
fillTriCount int32
124+
strokeTriCount int32
125+
textTriCount int32
125126
}
126127

127128
// Delete is called when tearing down NanoVGo context
@@ -144,7 +145,7 @@ func (c *Context) Delete() {
144145
// For example, GLFW returns two dimension for an opened window: window size and
145146
// frame buffer size. In that case you would set windowWidth/Height to the window size
146147
// devicePixelRatio to: frameBufferWidth / windowWidth.
147-
func (c *Context) BeginFrame(windowWidth, windowHeight int, devicePixelRatio float32) {
148+
func (c *Context) BeginFrame(windowWidth, windowHeight int32, devicePixelRatio float32) {
148149
c.states = c.states[:0]
149150
c.Save()
150151
c.Reset()
@@ -173,7 +174,7 @@ func (c *Context) EndFrame() {
173174
}
174175
iw, ih, _ := c.ImageSize(fontImage)
175176
j := 0
176-
for i := 0; i < c.fontImageIdx; i++ {
177+
for i := 0; int32(i) < c.fontImageIdx; i++ {
177178
nw, nh, _ := c.ImageSize(c.fontImages[i])
178179
if nw < iw || nh < ih {
179180
c.DeleteImage(c.fontImages[i])
@@ -369,7 +370,7 @@ func (c *Context) SetFillPaint(paint Paint) {
369370

370371
// CreateImage creates image by loading it from the disk from specified file name.
371372
// Returns handle to the image.
372-
func (c *Context) CreateImage(filePath string, flags ImageFlags) int {
373+
func (c *Context) CreateImage(filePath string, flags ImageFlags) int32 {
373374
file, err := os.Open(filePath)
374375
defer file.Close()
375376
if err != nil {
@@ -384,7 +385,7 @@ func (c *Context) CreateImage(filePath string, flags ImageFlags) int {
384385

385386
// CreateImageFromMemory creates image by loading it from the specified chunk of memory.
386387
// Returns handle to the image.
387-
func (c *Context) CreateImageFromMemory(flags ImageFlags, data []byte) int {
388+
func (c *Context) CreateImageFromMemory(flags ImageFlags, data []byte) int32 {
388389
reader := bytes.NewReader(data)
389390
img, _, err := image.Decode(reader)
390391
if err != nil {
@@ -395,30 +396,30 @@ func (c *Context) CreateImageFromMemory(flags ImageFlags, data []byte) int {
395396

396397
// CreateImageFromGoImage creates image by loading it from the specified image.Image object.
397398
// Returns handle to the image.
398-
func (c *Context) CreateImageFromGoImage(imageFlag ImageFlags, img image.Image) int {
399+
func (c *Context) CreateImageFromGoImage(imageFlag ImageFlags, img image.Image) int32 {
399400
bounds := img.Bounds()
400401
size := bounds.Size()
401402
rgba, ok := img.(*image.RGBA)
402403
if ok {
403-
return c.CreateImageRGBA(size.X, size.Y, imageFlag, rgba.Pix)
404+
return c.CreateImageRGBA(int32(size.X), int32(size.Y), imageFlag, rgba.Pix)
404405
}
405406
rgba = image.NewRGBA(bounds)
406407
for x := 0; x < size.X; x++ {
407408
for y := 0; y < size.Y; y++ {
408409
rgba.Set(x, y, img.At(x, y))
409410
}
410411
}
411-
return c.CreateImageRGBA(size.X, size.Y, imageFlag, rgba.Pix)
412+
return c.CreateImageRGBA(int32(size.X), int32(size.Y), imageFlag, rgba.Pix)
412413
}
413414

414415
// CreateImageRGBA creates image from specified image data.
415416
// Returns handle to the image.
416-
func (c *Context) CreateImageRGBA(w, h int, imageFlags ImageFlags, data []byte) int {
417+
func (c *Context) CreateImageRGBA(w, h int32, imageFlags ImageFlags, data []byte) int32 {
417418
return c.params.renderCreateTexture(nvgTextureRGBA, w, h, imageFlags, data)
418419
}
419420

420421
// UpdateImage updates image data specified by image handle.
421-
func (c *Context) UpdateImage(img int, data []byte) error {
422+
func (c *Context) UpdateImage(img int32, data []byte) error {
422423
w, h, err := c.params.renderGetTextureSize(img)
423424
if err != nil {
424425
return err
@@ -427,12 +428,12 @@ func (c *Context) UpdateImage(img int, data []byte) error {
427428
}
428429

429430
// ImageSize returns the dimensions of a created image.
430-
func (c *Context) ImageSize(img int) (int, int, error) {
431+
func (c *Context) ImageSize(img int32) (int32, int32, error) {
431432
return c.params.renderGetTextureSize(img)
432433
}
433434

434435
// DeleteImage deletes created image.
435-
func (c *Context) DeleteImage(img int) {
436+
func (c *Context) DeleteImage(img int32) {
436437
c.params.renderDeleteTexture(img)
437438
}
438439

@@ -725,8 +726,8 @@ func (c *Context) Fill() {
725726
// Count triangles
726727
for i := 0; i < len(c.cache.paths); i++ {
727728
path := &c.cache.paths[i]
728-
c.fillTriCount += len(path.fills) - 2
729-
c.strokeTriCount += len(path.strokes) - 2
729+
c.fillTriCount += int32(len(path.fills) - 2)
730+
c.strokeTriCount += int32(len(path.strokes) - 2)
730731
c.drawCallCount += 2
731732
}
732733
}
@@ -767,7 +768,7 @@ func (c *Context) Stroke() {
767768
// Count triangles
768769
for i := 0; i < len(c.cache.paths); i++ {
769770
path := &c.cache.paths[i]
770-
c.strokeTriCount += len(path.strokes) - 2
771+
c.strokeTriCount += int32(len(path.strokes) - 2)
771772
c.drawCallCount += 2
772773
}
773774
}
@@ -1346,7 +1347,7 @@ func createInternal(params nvgParams) (*Context, error) {
13461347
context := &Context{
13471348
params: params,
13481349
states: make([]nvgState, 0, nvgMaxStates),
1349-
fontImages: make([]int, nvgMaxFontImages),
1350+
fontImages: make([]int32, nvgMaxFontImages),
13501351
commands: make([]float32, 0, nvgInitCommandsSize),
13511352
cache: nvgPathCache{
13521353
points: make([]nvgPoint, 0, nvgInitPointsSize),
@@ -1499,10 +1500,10 @@ func (c *Context) flushTextTexture() {
14991500
// Update texture
15001501
if fontImage != 0 {
15011502
data, _, _ := c.fs.GetTextureData()
1502-
x := dirty[0]
1503-
y := dirty[1]
1504-
w := dirty[2] - x
1505-
h := dirty[3] - y
1503+
x := int32(dirty[0])
1504+
y := int32(dirty[1])
1505+
w := int32(dirty[2]) - x
1506+
h := int32(dirty[3]) - y
15061507
c.params.renderUpdateTexture(fontImage, x, y, w, h, data)
15071508
}
15081509
}
@@ -1513,7 +1514,7 @@ func (c *Context) allocTextAtlas() bool {
15131514
if c.fontImageIdx >= nvgMaxFontImages-1 {
15141515
return false
15151516
}
1516-
var iw, ih int
1517+
var iw, ih int32
15171518
// if next fontImage already have a texture
15181519
if c.fontImages[c.fontImageIdx+1] != 0 {
15191520
iw, ih, _ = c.ImageSize(c.fontImages[c.fontImageIdx+1])
@@ -1524,14 +1525,14 @@ func (c *Context) allocTextAtlas() bool {
15241525
} else {
15251526
iw *= 2
15261527
}
1527-
if iw > nvgMaxFontImageSize || ih > nvgMaxFontImageSize {
1528-
iw = nvgMaxFontImageSize
1529-
ih = nvgMaxFontImageSize
1528+
if iw > int32(nvgMaxFontImageSize) || ih > int32(nvgMaxFontImageSize) {
1529+
iw = int32(nvgMaxFontImageSize)
1530+
ih = int32(nvgMaxFontImageSize)
15301531
}
15311532
c.fontImages[c.fontImageIdx+1] = c.params.renderCreateTexture(nvgTextureALPHA, iw, ih, 0, nil)
15321533
}
15331534
c.fontImageIdx++
1534-
c.fs.ResetAtlas(iw, ih)
1535+
c.fs.ResetAtlas(int(iw), int(ih))
15351536
return true
15361537
}
15371538

@@ -1549,5 +1550,5 @@ func (c *Context) renderText(vertexes []nvgVertex) {
15491550
c.params.renderTriangleStrip(&paint, &state.scissor, vertexes)
15501551

15511552
c.drawCallCount++
1552-
c.textTriCount += len(vertexes) / 3
1553+
c.textTriCount += int32(len(vertexes) / 3)
15531554
}

Diff for: paint.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type Paint struct {
1313
feather float32
1414
innerColor Color
1515
outerColor Color
16-
image int
16+
image int32
1717
}
1818

1919
func (p *Paint) setPaintColor(color Color) {
@@ -89,7 +89,7 @@ func BoxGradient(x, y, w, h, r, f float32, iColor, oColor Color) Paint {
8989
// ImagePattern creates and returns an image patter. Parameters (ox,oy) specify the left-top location of the image pattern,
9090
// (ex,ey) the size of one image, angle rotation around the top-left corner, image is handle to the image to render.
9191
// The gradient is transformed by the current transform when it is passed to Context.FillPaint() or Context.StrokePaint().
92-
func ImagePattern(cx, cy, w, h, angle float32, img int, alpha float32) Paint {
92+
func ImagePattern(cx, cy, w, h, angle float32, img int32, alpha float32) Paint {
9393
xform := RotateMatrix(angle)
9494
xform[4] = cx
9595
xform[5] = cy

Diff for: platform_mobile.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// +build darwin linux
2-
// +build arm arm64
2+
// +build arm arm64 armv7
3+
// +build android
34

45
package nanovgo
56

@@ -16,7 +17,7 @@ var shaderHeader string = `
1617
#define UNIFORMARRAY_SIZE 11
1718
`
1819

19-
func prepareTextureBuffer(data []byte, w, h, bpp int) []byte {
20+
func prepareTextureBuffer(data []byte, w, h, bpp int32) []byte {
2021
return data
2122
}
2223

Diff for: platform_other.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
// +build !arm !arm64
22
// +build !js
3+
// +build !android
34

45
package nanovgo
56

67
import (
7-
"log"
8-
"unsafe"
98
"encoding/binary"
9+
"log"
1010
"math"
11+
"unsafe"
1112
)
1213

1314
type Float float32

Diff for: platform_webgl.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
// +build js
2+
// +build !android
23

34
package nanovgo
45

56
import (
67
"encoding/binary"
7-
"honnef.co/go/js/console"
88
"math"
9+
10+
"honnef.co/go/js/console"
911
)
1012

1113
var shaderHeader string = `

Diff for: structs.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import (
77
type nvgParams interface {
88
edgeAntiAlias() bool
99
renderCreate() error
10-
renderCreateTexture(texType nvgTextureType, w, h int, flags ImageFlags, data []byte) int
11-
renderDeleteTexture(image int) error
12-
renderUpdateTexture(image, x, y, w, h int, data []byte) error
13-
renderGetTextureSize(image int) (int, int, error)
14-
renderViewport(width, height int)
10+
renderCreateTexture(texType nvgTextureType, w, h int32, flags ImageFlags, data []byte) int32
11+
renderDeleteTexture(image int32) error
12+
renderUpdateTexture(image, x, y, w, h int32, data []byte) error
13+
renderGetTextureSize(image int32) (int32, int32, error)
14+
renderViewport(width, height int32)
1515
renderCancel()
1616
renderFlush()
1717
renderFill(paint *Paint, scissor *nvgScissor, fringe float32, bounds [4]float32, paths []nvgPath)

Diff for: util.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package nanovgo
22

33
import (
4+
"bytes"
5+
"encoding/binary"
46
"math"
57
)
68

@@ -424,7 +426,7 @@ func roundCapEnd(dst []nvgVertex, index int, p *nvgPoint, dx, dy, w float32, nCa
424426
return index
425427
}
426428

427-
func nearestPow2(num int) int {
429+
func nearestPow2(num int32) int32 {
428430
var n uint
429431
uNum := uint(num)
430432
if uNum > 0 {
@@ -438,9 +440,15 @@ func nearestPow2(num int) int {
438440
n |= n >> 8
439441
n |= n >> 16
440442
n++
441-
return int(num)
443+
return int32(num)
442444
}
443445

444446
func quantize(a, d float32) float32 {
445447
return float32(int(a/d+0.5)) * d
446448
}
449+
450+
func readUint32(data []byte) (ret *uint32) {
451+
buf := bytes.NewBuffer(data)
452+
binary.Read(buf, binary.LittleEndian, ret)
453+
return
454+
}

0 commit comments

Comments
 (0)