twitchapon-anim

Basic Twitchapon Receiver/Visuals
git clone git://bsandro.tech/twitchapon-anim
Log | Files | Refs | README | LICENSE

vertex.go (3303B)


      1 // Copyright 2019 The Ebiten Authors
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //     http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 package graphics
     16 
     17 import (
     18 	"github.com/hajimehoshi/ebiten/v2/internal/web"
     19 )
     20 
     21 const (
     22 	ShaderImageNum = 4
     23 
     24 	// PreservedUniformVariablesNum represents the number of preserved uniform variables.
     25 	// Any shaders in Ebiten must have these uniform variables.
     26 	PreservedUniformVariablesNum = 1 + // the destination texture size
     27 		1 + // the texture sizes array
     28 		1 + // the offsets array of the second and the following images
     29 		1 + // the texture source region's origin
     30 		1 // the texture source region's size
     31 
     32 	DestinationTextureSizeUniformVariableIndex    = 0
     33 	TextureSizesUniformVariableIndex              = 1
     34 	TextureSourceOffsetsUniformVariableIndex      = 2
     35 	TextureSourceRegionOriginUniformVariableIndex = 3
     36 	TextureSourceRegionSizeUniformVariableIndex   = 4
     37 )
     38 
     39 const (
     40 	IndicesNum     = (1 << 16) / 3 * 3 // Adjust num for triangles.
     41 	VertexFloatNum = 8
     42 )
     43 
     44 var (
     45 	quadIndices = []uint16{0, 1, 2, 1, 2, 3}
     46 )
     47 
     48 func QuadIndices() []uint16 {
     49 	return quadIndices
     50 }
     51 
     52 var (
     53 	theVerticesBackend = &verticesBackend{
     54 		backend: make([]float32, VertexFloatNum*1024),
     55 	}
     56 )
     57 
     58 type verticesBackend struct {
     59 	backend []float32
     60 	head    int
     61 }
     62 
     63 func (v *verticesBackend) slice(n int, last bool) []float32 {
     64 	// As this is called only on browsers, mutex is not required.
     65 
     66 	need := n * VertexFloatNum
     67 	if l := len(v.backend); v.head+need > l {
     68 		for v.head+need > l {
     69 			l *= 2
     70 		}
     71 		v.backend = make([]float32, l)
     72 		v.head = 0
     73 	}
     74 
     75 	s := v.backend[v.head : v.head+need]
     76 	if last {
     77 		// If last is true, the vertices backend is sent to GPU and it is fine to reuse the slice.
     78 		v.head = 0
     79 	} else {
     80 		v.head += need
     81 	}
     82 	return s
     83 }
     84 
     85 func vertexSlice(n int, last bool) []float32 {
     86 	if web.IsBrowser() {
     87 		// In Wasm, allocating memory by make is expensive. Use the backend instead.
     88 		return theVerticesBackend.slice(n, last)
     89 	}
     90 	return make([]float32, n*VertexFloatNum)
     91 }
     92 
     93 func QuadVertices(sx0, sy0, sx1, sy1 float32, a, b, c, d, tx, ty float32, cr, cg, cb, ca float32, last bool) []float32 {
     94 	x := sx1 - sx0
     95 	y := sy1 - sy0
     96 	ax, by, cx, dy := a*x, b*y, c*x, d*y
     97 	u0, v0, u1, v1 := float32(sx0), float32(sy0), float32(sx1), float32(sy1)
     98 
     99 	// This function is very performance-sensitive and implement in a very dumb way.
    100 	vs := vertexSlice(4, last)
    101 	_ = vs[:32]
    102 
    103 	vs[0] = tx
    104 	vs[1] = ty
    105 	vs[2] = u0
    106 	vs[3] = v0
    107 	vs[4] = cr
    108 	vs[5] = cg
    109 	vs[6] = cb
    110 	vs[7] = ca
    111 
    112 	vs[8] = ax + tx
    113 	vs[9] = cx + ty
    114 	vs[10] = u1
    115 	vs[11] = v0
    116 	vs[12] = cr
    117 	vs[13] = cg
    118 	vs[14] = cb
    119 	vs[15] = ca
    120 
    121 	vs[16] = by + tx
    122 	vs[17] = dy + ty
    123 	vs[18] = u0
    124 	vs[19] = v1
    125 	vs[20] = cr
    126 	vs[21] = cg
    127 	vs[22] = cb
    128 	vs[23] = ca
    129 
    130 	vs[24] = ax + by + tx
    131 	vs[25] = cx + dy + ty
    132 	vs[26] = u1
    133 	vs[27] = v1
    134 	vs[28] = cr
    135 	vs[29] = cg
    136 	vs[30] = cb
    137 	vs[31] = ca
    138 
    139 	return vs
    140 }