2019-06-21 17:00:37 +02:00
|
|
|
// Copyright 2019 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.
|
|
|
|
|
2020-07-15 18:53:46 +02:00
|
|
|
package graphics
|
2019-06-21 17:00:37 +02:00
|
|
|
|
|
|
|
import (
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/web"
|
2019-06-21 17:00:37 +02:00
|
|
|
)
|
|
|
|
|
2020-07-18 12:56:22 +02:00
|
|
|
const (
|
|
|
|
ShaderImageNum = 4
|
|
|
|
|
|
|
|
// PreservedUniformVariablesNum represents the number of preserved uniform variables.
|
|
|
|
// Any shaders in Ebiten must have these uniform variables.
|
2020-07-25 17:54:00 +02:00
|
|
|
PreservedUniformVariablesNum = 1 + // the destination texture size
|
2020-08-01 09:20:49 +02:00
|
|
|
1 + // the texture sizes array
|
2020-12-09 02:09:55 +01:00
|
|
|
1 + // the texture destination region's origin
|
|
|
|
1 + // the texture destination region's size
|
2020-08-10 18:11:19 +02:00
|
|
|
1 + // the offsets array of the second and the following images
|
2020-08-10 20:42:21 +02:00
|
|
|
1 + // the texture source region's origin
|
|
|
|
1 // the texture source region's size
|
2020-07-17 18:09:58 +02:00
|
|
|
|
2020-12-09 02:09:55 +01:00
|
|
|
DestinationTextureSizeUniformVariableIndex = 0
|
|
|
|
TextureSizesUniformVariableIndex = 1
|
|
|
|
TextureDestinationRegionOriginUniformVariableIndex = 2
|
|
|
|
TextureDestinationRegionSizeUniformVariableIndex = 3
|
|
|
|
TextureSourceOffsetsUniformVariableIndex = 4
|
|
|
|
TextureSourceRegionOriginUniformVariableIndex = 5
|
|
|
|
TextureSourceRegionSizeUniformVariableIndex = 6
|
2020-08-01 09:20:49 +02:00
|
|
|
)
|
2020-07-26 04:25:21 +02:00
|
|
|
|
2020-07-15 18:53:46 +02:00
|
|
|
const (
|
|
|
|
IndicesNum = (1 << 16) / 3 * 3 // Adjust num for triangles.
|
|
|
|
VertexFloatNum = 8
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
quadIndices = []uint16{0, 1, 2, 1, 2, 3}
|
|
|
|
)
|
|
|
|
|
|
|
|
func QuadIndices() []uint16 {
|
|
|
|
return quadIndices
|
|
|
|
}
|
|
|
|
|
2019-06-21 17:00:37 +02:00
|
|
|
var (
|
2019-09-28 18:31:01 +02:00
|
|
|
theVerticesBackend = &verticesBackend{
|
2020-07-15 18:53:46 +02:00
|
|
|
backend: make([]float32, VertexFloatNum*1024),
|
2019-09-28 18:31:01 +02:00
|
|
|
}
|
2019-06-21 17:00:37 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type verticesBackend struct {
|
|
|
|
backend []float32
|
|
|
|
head int
|
|
|
|
}
|
|
|
|
|
2019-09-28 18:31:01 +02:00
|
|
|
func (v *verticesBackend) slice(n int, last bool) []float32 {
|
2020-10-06 18:03:19 +02:00
|
|
|
// As this is called only on browsers, mutex is not required.
|
2019-06-21 17:00:37 +02:00
|
|
|
|
2020-07-15 18:53:46 +02:00
|
|
|
need := n * VertexFloatNum
|
2019-09-28 18:31:01 +02:00
|
|
|
if l := len(v.backend); v.head+need > l {
|
|
|
|
for v.head+need > l {
|
|
|
|
l *= 2
|
2019-09-28 18:20:55 +02:00
|
|
|
}
|
2019-09-28 18:31:01 +02:00
|
|
|
v.backend = make([]float32, l)
|
|
|
|
v.head = 0
|
2019-09-28 15:53:13 +02:00
|
|
|
}
|
2019-06-21 17:00:37 +02:00
|
|
|
|
2019-09-28 18:20:55 +02:00
|
|
|
s := v.backend[v.head : v.head+need]
|
2019-09-28 18:31:01 +02:00
|
|
|
if last {
|
|
|
|
// If last is true, the vertices backend is sent to GPU and it is fine to reuse the slice.
|
|
|
|
v.head = 0
|
|
|
|
} else {
|
|
|
|
v.head += need
|
|
|
|
}
|
2019-06-21 17:00:37 +02:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2019-09-28 18:31:01 +02:00
|
|
|
func vertexSlice(n int, last bool) []float32 {
|
2020-09-20 21:19:26 +02:00
|
|
|
if web.IsBrowser() {
|
2020-10-06 18:03:19 +02:00
|
|
|
// In Wasm, allocating memory by make is expensive. Use the backend instead.
|
2020-07-14 05:14:56 +02:00
|
|
|
return theVerticesBackend.slice(n, last)
|
|
|
|
}
|
2020-07-15 18:53:46 +02:00
|
|
|
return make([]float32, n*VertexFloatNum)
|
2019-06-21 17:00:37 +02:00
|
|
|
}
|
2019-09-20 19:35:18 +02:00
|
|
|
|
2020-07-15 18:53:46 +02:00
|
|
|
func QuadVertices(sx0, sy0, sx1, sy1 float32, a, b, c, d, tx, ty float32, cr, cg, cb, ca float32, last bool) []float32 {
|
2020-07-14 17:22:25 +02:00
|
|
|
x := sx1 - sx0
|
|
|
|
y := sy1 - sy0
|
2019-09-20 19:35:18 +02:00
|
|
|
ax, by, cx, dy := a*x, b*y, c*x, d*y
|
|
|
|
u0, v0, u1, v1 := float32(sx0), float32(sy0), float32(sx1), float32(sy1)
|
|
|
|
|
|
|
|
// This function is very performance-sensitive and implement in a very dumb way.
|
2019-09-28 18:31:01 +02:00
|
|
|
vs := vertexSlice(4, last)
|
2020-07-02 16:06:58 +02:00
|
|
|
_ = vs[:32]
|
2019-09-20 19:35:18 +02:00
|
|
|
|
|
|
|
vs[0] = tx
|
|
|
|
vs[1] = ty
|
|
|
|
vs[2] = u0
|
|
|
|
vs[3] = v0
|
2020-07-02 16:06:58 +02:00
|
|
|
vs[4] = cr
|
|
|
|
vs[5] = cg
|
|
|
|
vs[6] = cb
|
|
|
|
vs[7] = ca
|
|
|
|
|
|
|
|
vs[8] = ax + tx
|
|
|
|
vs[9] = cx + ty
|
|
|
|
vs[10] = u1
|
|
|
|
vs[11] = v0
|
|
|
|
vs[12] = cr
|
|
|
|
vs[13] = cg
|
|
|
|
vs[14] = cb
|
|
|
|
vs[15] = ca
|
|
|
|
|
|
|
|
vs[16] = by + tx
|
|
|
|
vs[17] = dy + ty
|
|
|
|
vs[18] = u0
|
2019-09-20 19:35:18 +02:00
|
|
|
vs[19] = v1
|
|
|
|
vs[20] = cr
|
|
|
|
vs[21] = cg
|
|
|
|
vs[22] = cb
|
|
|
|
vs[23] = ca
|
|
|
|
|
2020-07-02 16:06:58 +02:00
|
|
|
vs[24] = ax + by + tx
|
|
|
|
vs[25] = cx + dy + ty
|
|
|
|
vs[26] = u1
|
2019-09-20 19:35:18 +02:00
|
|
|
vs[27] = v1
|
2020-07-02 16:06:58 +02:00
|
|
|
vs[28] = cr
|
|
|
|
vs[29] = cg
|
|
|
|
vs[30] = cb
|
|
|
|
vs[31] = ca
|
2019-09-20 19:35:18 +02:00
|
|
|
|
|
|
|
return vs
|
|
|
|
}
|