mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 02:38:53 +01:00
graphics: Use float values for vertices
This commit is contained in:
parent
b2be6681d4
commit
50b2d8ee94
@ -121,7 +121,7 @@ func (q *commandQueue) Flush(context *opengl.Context) error {
|
||||
n += len(c.vertices)
|
||||
}
|
||||
}
|
||||
vertices := make([]int16, 0, n)
|
||||
vertices := make([]float32, 0, n)
|
||||
for _, c := range g {
|
||||
switch c := c.(type) {
|
||||
case *drawImageCommand:
|
||||
@ -133,7 +133,7 @@ func (q *commandQueue) Flush(context *opengl.Context) error {
|
||||
}
|
||||
// NOTE: WebGL doesn't seem to have Check gl.MAX_ELEMENTS_VERTICES or gl.MAX_ELEMENTS_INDICES so far.
|
||||
// Let's use them to compare to len(quads) in the future.
|
||||
if maxQuads < len(vertices)*2/QuadVertexSizeInBytes() {
|
||||
if maxQuads < len(vertices)*opengl.Float.SizeInBytes()/QuadVertexSizeInBytes() {
|
||||
return fmt.Errorf("len(quads) must be equal to or less than %d", maxQuads)
|
||||
}
|
||||
numc := len(g)
|
||||
@ -143,7 +143,7 @@ func (q *commandQueue) Flush(context *opengl.Context) error {
|
||||
return err
|
||||
}
|
||||
if c, ok := c.(*drawImageCommand); ok {
|
||||
n := len(c.vertices) * 2 / QuadVertexSizeInBytes()
|
||||
n := len(c.vertices) * opengl.Float.SizeInBytes() / QuadVertexSizeInBytes()
|
||||
indexOffsetInBytes += 6 * n * 2
|
||||
}
|
||||
}
|
||||
@ -181,7 +181,7 @@ func (c *fillCommand) Exec(context *opengl.Context, indexOffsetInBytes int) erro
|
||||
type drawImageCommand struct {
|
||||
dst *Image
|
||||
src *Image
|
||||
vertices []int16
|
||||
vertices []float32
|
||||
color affine.ColorM
|
||||
mode opengl.CompositeMode
|
||||
}
|
||||
@ -223,8 +223,9 @@ func (c *drawImageCommand) Exec(context *opengl.Context, indexOffsetInBytes int)
|
||||
func (c *drawImageCommand) split(quadsNum int) [2]*drawImageCommand {
|
||||
c1 := *c
|
||||
c2 := *c
|
||||
c1.vertices = c.vertices[:quadsNum*QuadVertexSizeInBytes()/2]
|
||||
c2.vertices = c.vertices[quadsNum*QuadVertexSizeInBytes()/2:]
|
||||
s := opengl.Float.SizeInBytes()
|
||||
c1.vertices = c.vertices[:quadsNum*QuadVertexSizeInBytes()/s]
|
||||
c2.vertices = c.vertices[quadsNum*QuadVertexSizeInBytes()/s:]
|
||||
return [2]*drawImageCommand{&c1, &c2}
|
||||
}
|
||||
|
||||
@ -246,14 +247,14 @@ func (c *drawImageCommand) isMergeable(other *drawImageCommand) bool {
|
||||
|
||||
func (c *drawImageCommand) merge(other *drawImageCommand) *drawImageCommand {
|
||||
newC := *c
|
||||
newC.vertices = make([]int16, 0, len(c.vertices)+len(other.vertices))
|
||||
newC.vertices = make([]float32, 0, len(c.vertices)+len(other.vertices))
|
||||
newC.vertices = append(newC.vertices, c.vertices...)
|
||||
newC.vertices = append(newC.vertices, other.vertices...)
|
||||
return &newC
|
||||
}
|
||||
|
||||
func (c *drawImageCommand) quadsNum() int {
|
||||
return len(c.vertices) * 2 / QuadVertexSizeInBytes()
|
||||
return len(c.vertices) * opengl.Float.SizeInBytes() / QuadVertexSizeInBytes()
|
||||
}
|
||||
|
||||
type replacePixelsCommand struct {
|
||||
|
@ -95,7 +95,7 @@ func (i *Image) Fill(clr color.RGBA) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *Image) DrawImage(src *Image, vertices []int16, clr affine.ColorM, mode opengl.CompositeMode) error {
|
||||
func (i *Image) DrawImage(src *Image, vertices []float32, clr affine.ColorM, mode opengl.CompositeMode) error {
|
||||
c := &drawImageCommand{
|
||||
dst: i,
|
||||
src: src,
|
||||
|
@ -486,9 +486,9 @@ func (c *Context) BindElementArrayBuffer(b Buffer) {
|
||||
})
|
||||
}
|
||||
|
||||
func (c *Context) BufferSubData(bufferType BufferType, data []int16) {
|
||||
func (c *Context) BufferSubData(bufferType BufferType, data []float32) {
|
||||
_ = c.runOnContextThread(func() error {
|
||||
gl.BufferSubData(uint32(bufferType), 0, len(data)*2, gl.Ptr(data))
|
||||
gl.BufferSubData(uint32(bufferType), 0, len(data)*4, gl.Ptr(data))
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
@ -387,7 +387,7 @@ func (c *Context) BindElementArrayBuffer(b Buffer) {
|
||||
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, b.Object)
|
||||
}
|
||||
|
||||
func (c *Context) BufferSubData(bufferType BufferType, data []int16) {
|
||||
func (c *Context) BufferSubData(bufferType BufferType, data []float32) {
|
||||
gl := c.gl
|
||||
gl.BufferSubData(int(bufferType), 0, data)
|
||||
}
|
||||
|
@ -19,7 +19,9 @@ package opengl
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/internal/endian"
|
||||
mgl "golang.org/x/mobile/gl"
|
||||
)
|
||||
|
||||
@ -386,18 +388,32 @@ func (c *Context) BindElementArrayBuffer(b Buffer) {
|
||||
gl.BindBuffer(mgl.ELEMENT_ARRAY_BUFFER, mgl.Buffer(b))
|
||||
}
|
||||
|
||||
func int16ToBytes(v []int16) []byte {
|
||||
b := make([]byte, len(v)*2)
|
||||
for i, x := range v {
|
||||
b[2*i] = uint8(uint16(x))
|
||||
b[2*i+1] = uint8(uint16(x) >> 8)
|
||||
func float32ToBytes(v []float32) []byte {
|
||||
b := make([]byte, len(v)*4)
|
||||
if endian.IsLittle() {
|
||||
for i, x := range v {
|
||||
bits := math.Float32bits(x)
|
||||
b[4*i] = uint8(bits)
|
||||
b[4*i+1] = uint8(bits >> 8)
|
||||
b[4*i+2] = uint8(bits >> 16)
|
||||
b[4*i+3] = uint8(bits >> 24)
|
||||
}
|
||||
} else {
|
||||
// TODO: Test this
|
||||
for i, x := range v {
|
||||
bits := math.Float32bits(x)
|
||||
b[4*i] = uint8(bits >> 24)
|
||||
b[4*i+1] = uint8(bits >> 16)
|
||||
b[4*i+2] = uint8(bits >> 8)
|
||||
b[4*i+3] = uint8(bits)
|
||||
}
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func (c *Context) BufferSubData(bufferType BufferType, data []int16) {
|
||||
func (c *Context) BufferSubData(bufferType BufferType, data []float32) {
|
||||
gl := c.gl
|
||||
gl.BufferSubData(mgl.Enum(bufferType), 0, int16ToBytes(data))
|
||||
gl.BufferSubData(mgl.Enum(bufferType), 0, float32ToBytes(data))
|
||||
}
|
||||
|
||||
func (c *Context) DeleteBuffer(b Buffer) {
|
||||
|
@ -75,15 +75,15 @@ var (
|
||||
parts: []arrayBufferLayoutPart{
|
||||
{
|
||||
name: "vertex",
|
||||
dataType: opengl.Short,
|
||||
dataType: opengl.Float,
|
||||
num: 2,
|
||||
normalize: false,
|
||||
},
|
||||
{
|
||||
name: "tex_coord",
|
||||
dataType: opengl.Short,
|
||||
dataType: opengl.Float,
|
||||
num: 2,
|
||||
normalize: true,
|
||||
normalize: false,
|
||||
},
|
||||
{
|
||||
name: "geo_matrix_body",
|
||||
|
@ -26,7 +26,7 @@ import (
|
||||
|
||||
type drawImageHistoryItem struct {
|
||||
image *graphics.Image
|
||||
vertices []int16
|
||||
vertices []float32
|
||||
colorm affine.ColorM
|
||||
mode opengl.CompositeMode
|
||||
}
|
||||
@ -145,7 +145,7 @@ func (p *Image) ReplacePixels(pixels []uint8) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Image) DrawImage(img *Image, vertices []int16, colorm affine.ColorM, mode opengl.CompositeMode) error {
|
||||
func (p *Image) DrawImage(img *Image, vertices []float32, colorm affine.ColorM, mode opengl.CompositeMode) error {
|
||||
if img.stale || img.volatile {
|
||||
p.makeStale()
|
||||
} else {
|
||||
@ -157,7 +157,7 @@ func (p *Image) DrawImage(img *Image, vertices []int16, colorm affine.ColorM, mo
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Image) appendDrawImageHistory(image *graphics.Image, vertices []int16, colorm affine.ColorM, mode opengl.CompositeMode) {
|
||||
func (p *Image) appendDrawImageHistory(image *graphics.Image, vertices []float32, colorm affine.ColorM, mode opengl.CompositeMode) {
|
||||
if p.stale {
|
||||
return
|
||||
}
|
||||
|
@ -12,73 +12,51 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// +build !js
|
||||
|
||||
package ebiten
|
||||
|
||||
import (
|
||||
"math"
|
||||
"unsafe"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/internal/endian"
|
||||
"github.com/hajimehoshi/ebiten/internal/graphics"
|
||||
)
|
||||
|
||||
func floatsToInt16s(xs ...float64) []int16 {
|
||||
r := make([]int16, 0, len(xs)*2)
|
||||
for _, x := range xs {
|
||||
x32 := float32(x)
|
||||
n := *(*uint32)(unsafe.Pointer(&x32))
|
||||
if endian.IsLittle() {
|
||||
r = append(r, int16(n), int16(n>>16))
|
||||
} else {
|
||||
r = append(r, int16(n>>16), int16(n))
|
||||
}
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func vertices(parts ImageParts, width, height int, geo *GeoM) []int16 {
|
||||
func vertices(parts ImageParts, width, height int, geo *GeoM) []float32 {
|
||||
// TODO: This function should be in graphics package?
|
||||
totalSize := graphics.QuadVertexSizeInBytes() / 2
|
||||
totalSize := graphics.QuadVertexSizeInBytes() / 4
|
||||
oneSize := totalSize / 4
|
||||
l := parts.Len()
|
||||
vs := make([]int16, l*totalSize)
|
||||
w := uint(0)
|
||||
h := uint(0)
|
||||
for (1 << w) < width {
|
||||
w++
|
||||
}
|
||||
for (1 << h) < height {
|
||||
h++
|
||||
}
|
||||
geo16 := floatsToInt16s(geo.Element(0, 0),
|
||||
geo.Element(0, 1),
|
||||
geo.Element(1, 0),
|
||||
geo.Element(1, 1),
|
||||
geo.Element(0, 2),
|
||||
geo.Element(1, 2))
|
||||
vs := make([]float32, l*totalSize)
|
||||
geos := []float32{
|
||||
float32(geo.Element(0, 0)),
|
||||
float32(geo.Element(0, 1)),
|
||||
float32(geo.Element(1, 0)),
|
||||
float32(geo.Element(1, 1)),
|
||||
float32(geo.Element(0, 2)),
|
||||
float32(geo.Element(1, 2))}
|
||||
n := 0
|
||||
w := float32(1)
|
||||
h := float32(1)
|
||||
for w < float32(width) {
|
||||
w *= 2
|
||||
}
|
||||
for h < float32(height) {
|
||||
h *= 2
|
||||
}
|
||||
for i := 0; i < l; i++ {
|
||||
dx0, dy0, dx1, dy1 := parts.Dst(i)
|
||||
if dx0 == dx1 || dy0 == dy1 {
|
||||
continue
|
||||
}
|
||||
x0, y0, x1, y1 := int16(dx0), int16(dy0), int16(dx1), int16(dy1)
|
||||
x0, y0, x1, y1 := float32(dx0), float32(dy0), float32(dx1), float32(dy1)
|
||||
sx0, sy0, sx1, sy1 := parts.Src(i)
|
||||
if sx0 == sx1 || sy0 == sy1 {
|
||||
continue
|
||||
}
|
||||
u0 := int16((math.MaxInt16 * sx0) >> w)
|
||||
v0 := int16((math.MaxInt16 * sy0) >> h)
|
||||
u1 := int16((math.MaxInt16 * sx1) >> w)
|
||||
v1 := int16((math.MaxInt16 * sy1) >> h)
|
||||
u0, v0, u1, v1 := float32(sx0)/w, float32(sy0)/h, float32(sx1)/w, float32(sy1)/h
|
||||
offset := n * totalSize
|
||||
vs[offset] = x0
|
||||
vs[offset+1] = y0
|
||||
vs[offset+2] = u0
|
||||
vs[offset+3] = v0
|
||||
for j, g := range geo16 {
|
||||
for j, g := range geos {
|
||||
vs[offset+4+j] = g
|
||||
}
|
||||
offset += oneSize
|
||||
@ -86,7 +64,7 @@ func vertices(parts ImageParts, width, height int, geo *GeoM) []int16 {
|
||||
vs[offset+1] = y0
|
||||
vs[offset+2] = u1
|
||||
vs[offset+3] = v0
|
||||
for j, g := range geo16 {
|
||||
for j, g := range geos {
|
||||
vs[offset+4+j] = g
|
||||
}
|
||||
offset += oneSize
|
||||
@ -94,7 +72,7 @@ func vertices(parts ImageParts, width, height int, geo *GeoM) []int16 {
|
||||
vs[offset+1] = y1
|
||||
vs[offset+2] = u0
|
||||
vs[offset+3] = v1
|
||||
for j, g := range geo16 {
|
||||
for j, g := range geos {
|
||||
vs[offset+4+j] = g
|
||||
}
|
||||
offset += oneSize
|
||||
@ -102,7 +80,7 @@ func vertices(parts ImageParts, width, height int, geo *GeoM) []int16 {
|
||||
vs[offset+1] = y1
|
||||
vs[offset+2] = u1
|
||||
vs[offset+3] = v1
|
||||
for j, g := range geo16 {
|
||||
for j, g := range geos {
|
||||
vs[offset+4+j] = g
|
||||
}
|
||||
n++
|
@ -1,98 +0,0 @@
|
||||
// Copyright 2016 The Ebiten 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.
|
||||
|
||||
// +build js
|
||||
|
||||
package ebiten
|
||||
|
||||
import (
|
||||
"math"
|
||||
|
||||
"github.com/gopherjs/gopherjs/js"
|
||||
"github.com/hajimehoshi/ebiten/internal/graphics"
|
||||
)
|
||||
|
||||
func vertices(parts ImageParts, width, height int, geo *GeoM) []int16 {
|
||||
// TODO: This function should be in graphics package?
|
||||
totalSize := graphics.QuadVertexSizeInBytes() / 2
|
||||
oneSize := totalSize / 4
|
||||
l := parts.Len()
|
||||
a := js.Global.Get("ArrayBuffer").New(l * totalSize * 2)
|
||||
af32 := js.Global.Get("Float32Array").New(a)
|
||||
a16 := js.Global.Get("Int16Array").New(a)
|
||||
w := uint(0)
|
||||
h := uint(0)
|
||||
for (1 << w) < width {
|
||||
w++
|
||||
}
|
||||
for (1 << h) < height {
|
||||
h++
|
||||
}
|
||||
gs := []float64{geo.Element(0, 0),
|
||||
geo.Element(0, 1),
|
||||
geo.Element(1, 0),
|
||||
geo.Element(1, 1),
|
||||
geo.Element(0, 2),
|
||||
geo.Element(1, 2)}
|
||||
n := 0
|
||||
for i := 0; i < l; i++ {
|
||||
dx0, dy0, dx1, dy1 := parts.Dst(i)
|
||||
if dx0 == dx1 || dy0 == dy1 {
|
||||
continue
|
||||
}
|
||||
sx0, sy0, sx1, sy1 := parts.Src(i)
|
||||
if sx0 == sx1 || sy0 == sy1 {
|
||||
continue
|
||||
}
|
||||
u0 := (math.MaxInt16 * sx0) >> w
|
||||
v0 := (math.MaxInt16 * sy0) >> h
|
||||
u1 := (math.MaxInt16 * sx1) >> w
|
||||
v1 := (math.MaxInt16 * sy1) >> h
|
||||
offset := n * totalSize
|
||||
a16.SetIndex(offset, dx0)
|
||||
a16.SetIndex(offset+1, dy0)
|
||||
a16.SetIndex(offset+2, u0)
|
||||
a16.SetIndex(offset+3, v0)
|
||||
for j, g := range gs {
|
||||
af32.SetIndex((offset+4)/2+j, g)
|
||||
}
|
||||
offset += oneSize
|
||||
a16.SetIndex(offset, dx1)
|
||||
a16.SetIndex(offset+1, dy0)
|
||||
a16.SetIndex(offset+2, u1)
|
||||
a16.SetIndex(offset+3, v0)
|
||||
for j, g := range gs {
|
||||
af32.SetIndex((offset+4)/2+j, g)
|
||||
}
|
||||
offset += oneSize
|
||||
a16.SetIndex(offset, dx0)
|
||||
a16.SetIndex(offset+1, dy1)
|
||||
a16.SetIndex(offset+2, u0)
|
||||
a16.SetIndex(offset+3, v1)
|
||||
for j, g := range gs {
|
||||
af32.SetIndex((offset+4)/2+j, g)
|
||||
}
|
||||
offset += oneSize
|
||||
a16.SetIndex(offset, dx1)
|
||||
a16.SetIndex(offset+1, dy1)
|
||||
a16.SetIndex(offset+2, u1)
|
||||
a16.SetIndex(offset+3, v1)
|
||||
for j, g := range gs {
|
||||
af32.SetIndex((offset+4)/2+j, g)
|
||||
}
|
||||
n++
|
||||
}
|
||||
// TODO: Need to slice
|
||||
return a16.Interface().([]int16)
|
||||
}
|
Loading…
Reference in New Issue
Block a user