zorldo

Goofing around with Ebiten
git clone git://bsandro.tech/zorldo
Log | Files | Refs | README

gomobile.go (11478B)


      1 // Copyright 2020 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 //go:build android || ios
     16 // +build android ios
     17 
     18 package gles
     19 
     20 import (
     21 	"golang.org/x/mobile/gl"
     22 )
     23 
     24 type GomobileContext struct {
     25 	ctx gl.Context
     26 }
     27 
     28 func gmProgram(program uint32) gl.Program {
     29 	return gl.Program{
     30 		Init:  true,
     31 		Value: program,
     32 	}
     33 }
     34 
     35 func NewGomobileContext(ctx gl.Context) *GomobileContext {
     36 	return &GomobileContext{ctx}
     37 }
     38 
     39 func (g *GomobileContext) ActiveTexture(texture uint32) {
     40 	g.ctx.ActiveTexture(gl.Enum(texture))
     41 }
     42 
     43 func (g *GomobileContext) AttachShader(program uint32, shader uint32) {
     44 	g.ctx.AttachShader(gmProgram(program), gl.Shader{Value: shader})
     45 }
     46 
     47 func (g *GomobileContext) BindAttribLocation(program uint32, index uint32, name string) {
     48 	g.ctx.BindAttribLocation(gmProgram(program), gl.Attrib{Value: uint(index)}, name)
     49 }
     50 
     51 func (g *GomobileContext) BindBuffer(target uint32, buffer uint32) {
     52 	g.ctx.BindBuffer(gl.Enum(target), gl.Buffer{Value: buffer})
     53 }
     54 
     55 func (g *GomobileContext) BindFramebuffer(target uint32, framebuffer uint32) {
     56 	g.ctx.BindFramebuffer(gl.Enum(target), gl.Framebuffer{Value: framebuffer})
     57 }
     58 
     59 func (g *GomobileContext) BindRenderbuffer(target uint32, renderbuffer uint32) {
     60 	g.ctx.BindRenderbuffer(gl.Enum(target), gl.Renderbuffer{Value: renderbuffer})
     61 }
     62 
     63 func (g *GomobileContext) BindTexture(target uint32, texture uint32) {
     64 	g.ctx.BindTexture(gl.Enum(target), gl.Texture{Value: texture})
     65 }
     66 
     67 func (g *GomobileContext) BlendFunc(sfactor uint32, dfactor uint32) {
     68 	g.ctx.BlendFunc(gl.Enum(sfactor), gl.Enum(dfactor))
     69 }
     70 
     71 func (g *GomobileContext) BufferData(target uint32, size int, data []byte, usage uint32) {
     72 	if data == nil {
     73 		g.ctx.BufferInit(gl.Enum(target), size, gl.Enum(usage))
     74 	} else {
     75 		if size != len(data) {
     76 			panic("gles: size and len(data) must be same at BufferData")
     77 		}
     78 		g.ctx.BufferData(gl.Enum(target), data, gl.Enum(usage))
     79 	}
     80 }
     81 
     82 func (g *GomobileContext) BufferSubData(target uint32, offset int, data []byte) {
     83 	g.ctx.BufferSubData(gl.Enum(target), offset, data)
     84 }
     85 
     86 func (g *GomobileContext) CheckFramebufferStatus(target uint32) uint32 {
     87 	return uint32(g.ctx.CheckFramebufferStatus(gl.Enum(target)))
     88 }
     89 
     90 func (g *GomobileContext) Clear(mask uint32) {
     91 	g.ctx.Clear(gl.Enum(mask))
     92 }
     93 
     94 func (g *GomobileContext) ColorMask(red, green, blue, alpha bool) {
     95 	g.ctx.ColorMask(red, green, blue, alpha)
     96 }
     97 
     98 func (g *GomobileContext) CompileShader(shader uint32) {
     99 	g.ctx.CompileShader(gl.Shader{Value: shader})
    100 }
    101 
    102 func (g *GomobileContext) CreateProgram() uint32 {
    103 	return g.ctx.CreateProgram().Value
    104 }
    105 
    106 func (g *GomobileContext) CreateShader(xtype uint32) uint32 {
    107 	return g.ctx.CreateShader(gl.Enum(xtype)).Value
    108 }
    109 
    110 func (g *GomobileContext) DeleteBuffers(buffers []uint32) {
    111 	for _, b := range buffers {
    112 		g.ctx.DeleteBuffer(gl.Buffer{Value: b})
    113 	}
    114 }
    115 
    116 func (g *GomobileContext) DeleteFramebuffers(framebuffers []uint32) {
    117 	for _, b := range framebuffers {
    118 		g.ctx.DeleteFramebuffer(gl.Framebuffer{Value: b})
    119 	}
    120 }
    121 
    122 func (g *GomobileContext) DeleteProgram(program uint32) {
    123 	g.ctx.DeleteProgram(gmProgram(program))
    124 }
    125 
    126 func (g *GomobileContext) DeleteRenderbuffers(renderbuffers []uint32) {
    127 	for _, r := range renderbuffers {
    128 		g.ctx.DeleteRenderbuffer(gl.Renderbuffer{Value: r})
    129 	}
    130 }
    131 
    132 func (g *GomobileContext) DeleteShader(shader uint32) {
    133 	g.ctx.DeleteShader(gl.Shader{Value: shader})
    134 }
    135 
    136 func (g *GomobileContext) DeleteTextures(textures []uint32) {
    137 	for _, t := range textures {
    138 		g.ctx.DeleteTexture(gl.Texture{Value: t})
    139 	}
    140 }
    141 
    142 func (g *GomobileContext) Disable(cap uint32) {
    143 	g.ctx.Disable(gl.Enum(cap))
    144 }
    145 
    146 func (g *GomobileContext) DisableVertexAttribArray(index uint32) {
    147 	g.ctx.DisableVertexAttribArray(gl.Attrib{Value: uint(index)})
    148 }
    149 
    150 func (g *GomobileContext) DrawElements(mode uint32, count int32, xtype uint32, offset int) {
    151 	g.ctx.DrawElements(gl.Enum(mode), int(count), gl.Enum(xtype), offset)
    152 }
    153 
    154 func (g *GomobileContext) Enable(cap uint32) {
    155 	g.ctx.Enable(gl.Enum(cap))
    156 }
    157 
    158 func (g *GomobileContext) EnableVertexAttribArray(index uint32) {
    159 	g.ctx.EnableVertexAttribArray(gl.Attrib{Value: uint(index)})
    160 }
    161 
    162 func (g *GomobileContext) Flush() {
    163 	g.ctx.Flush()
    164 }
    165 
    166 func (g *GomobileContext) FramebufferRenderbuffer(target uint32, attachment uint32, renderbuffertarget uint32, renderbuffer uint32) {
    167 	g.ctx.FramebufferRenderbuffer(gl.Enum(target), gl.Enum(attachment), gl.Enum(renderbuffertarget), gl.Renderbuffer{Value: renderbuffer})
    168 }
    169 
    170 func (g *GomobileContext) FramebufferTexture2D(target uint32, attachment uint32, textarget uint32, texture uint32, level int32) {
    171 	g.ctx.FramebufferTexture2D(gl.Enum(target), gl.Enum(attachment), gl.Enum(textarget), gl.Texture{Value: texture}, int(level))
    172 }
    173 
    174 func (g *GomobileContext) GenBuffers(n int32) []uint32 {
    175 	buffers := make([]uint32, n)
    176 	for i := range buffers {
    177 		buffers[i] = g.ctx.CreateBuffer().Value
    178 	}
    179 	return buffers
    180 }
    181 
    182 func (g *GomobileContext) GenFramebuffers(n int32) []uint32 {
    183 	framebuffers := make([]uint32, n)
    184 	for i := range framebuffers {
    185 		framebuffers[i] = g.ctx.CreateFramebuffer().Value
    186 	}
    187 	return framebuffers
    188 }
    189 
    190 func (g *GomobileContext) GenRenderbuffers(n int32) []uint32 {
    191 	renderbuffers := make([]uint32, n)
    192 	for i := range renderbuffers {
    193 		renderbuffers[i] = g.ctx.CreateRenderbuffer().Value
    194 	}
    195 	return renderbuffers
    196 }
    197 
    198 func (g *GomobileContext) GenTextures(n int32) []uint32 {
    199 	textures := make([]uint32, n)
    200 	for i := range textures {
    201 		textures[i] = g.ctx.CreateTexture().Value
    202 	}
    203 	return textures
    204 }
    205 
    206 func (g *GomobileContext) GetError() uint32 {
    207 	return uint32(g.ctx.GetError())
    208 }
    209 
    210 func (g *GomobileContext) GetIntegerv(dst []int32, pname uint32) {
    211 	g.ctx.GetIntegerv(dst, gl.Enum(pname))
    212 }
    213 
    214 func (g *GomobileContext) GetProgramiv(dst []int32, program uint32, pname uint32) {
    215 	dst[0] = int32(g.ctx.GetProgrami(gmProgram(program), gl.Enum(pname)))
    216 }
    217 
    218 func (g *GomobileContext) GetProgramInfoLog(program uint32) string {
    219 	return g.ctx.GetProgramInfoLog(gmProgram(program))
    220 }
    221 
    222 func (g *GomobileContext) GetShaderiv(dst []int32, shader uint32, pname uint32) {
    223 	dst[0] = int32(g.ctx.GetShaderi(gl.Shader{Value: shader}, gl.Enum(pname)))
    224 }
    225 
    226 func (g *GomobileContext) GetShaderInfoLog(shader uint32) string {
    227 	return g.ctx.GetShaderInfoLog(gl.Shader{Value: shader})
    228 }
    229 
    230 func (g *GomobileContext) GetShaderPrecisionFormat(shadertype uint32, precisiontype uint32) (rangeLow, rangeHigh, precision int) {
    231 	return g.ctx.GetShaderPrecisionFormat(gl.Enum(shadertype), gl.Enum(precisiontype))
    232 }
    233 
    234 func (g *GomobileContext) GetUniformLocation(program uint32, name string) int32 {
    235 	return g.ctx.GetUniformLocation(gmProgram(program), name).Value
    236 }
    237 
    238 func (g *GomobileContext) IsFramebuffer(framebuffer uint32) bool {
    239 	return g.ctx.IsFramebuffer(gl.Framebuffer{Value: framebuffer})
    240 }
    241 
    242 func (g *GomobileContext) IsProgram(program uint32) bool {
    243 	return g.ctx.IsProgram(gmProgram(program))
    244 }
    245 
    246 func (g *GomobileContext) IsRenderbuffer(renderbuffer uint32) bool {
    247 	return g.ctx.IsRenderbuffer(gl.Renderbuffer{Value: renderbuffer})
    248 }
    249 
    250 func (g *GomobileContext) IsTexture(texture uint32) bool {
    251 	return g.ctx.IsTexture(gl.Texture{Value: texture})
    252 }
    253 
    254 func (g *GomobileContext) LinkProgram(program uint32) {
    255 	g.ctx.LinkProgram(gmProgram(program))
    256 }
    257 
    258 func (g *GomobileContext) PixelStorei(pname uint32, param int32) {
    259 	g.ctx.PixelStorei(gl.Enum(pname), param)
    260 }
    261 
    262 func (g *GomobileContext) ReadPixels(dst []byte, x int32, y int32, width int32, height int32, format uint32, xtype uint32) {
    263 	g.ctx.ReadPixels(dst, int(x), int(y), int(width), int(height), gl.Enum(format), gl.Enum(xtype))
    264 }
    265 
    266 func (g *GomobileContext) RenderbufferStorage(target uint32, internalFormat uint32, width int32, height int32) {
    267 	g.ctx.RenderbufferStorage(gl.Enum(target), gl.Enum(internalFormat), int(width), int(height))
    268 }
    269 
    270 func (g *GomobileContext) Scissor(x, y, width, height int32) {
    271 	g.ctx.Scissor(x, y, width, height)
    272 }
    273 
    274 func (g *GomobileContext) ShaderSource(shader uint32, xstring string) {
    275 	g.ctx.ShaderSource(gl.Shader{Value: shader}, xstring)
    276 }
    277 
    278 func (g *GomobileContext) StencilFunc(func_ uint32, ref int32, mask uint32) {
    279 	g.ctx.StencilFunc(gl.Enum(func_), int(ref), mask)
    280 }
    281 
    282 func (g *GomobileContext) StencilOp(sfail, dpfail, dppass uint32) {
    283 	g.ctx.StencilOp(gl.Enum(sfail), gl.Enum(dpfail), gl.Enum(dppass))
    284 }
    285 
    286 func (g *GomobileContext) TexImage2D(target uint32, level int32, internalformat int32, width int32, height int32, format uint32, xtype uint32, pixels []byte) {
    287 	g.ctx.TexImage2D(gl.Enum(target), int(level), int(internalformat), int(width), int(height), gl.Enum(format), gl.Enum(xtype), pixels)
    288 }
    289 
    290 func (g *GomobileContext) TexParameteri(target uint32, pname uint32, param int32) {
    291 	g.ctx.TexParameteri(gl.Enum(target), gl.Enum(pname), int(param))
    292 }
    293 
    294 func (g *GomobileContext) TexSubImage2D(target uint32, level int32, xoffset int32, yoffset int32, width int32, height int32, format uint32, xtype uint32, pixels []byte) {
    295 	g.ctx.TexSubImage2D(gl.Enum(target), int(level), int(xoffset), int(yoffset), int(width), int(height), gl.Enum(format), gl.Enum(xtype), pixels)
    296 }
    297 
    298 func (g *GomobileContext) Uniform1f(location int32, v0 float32) {
    299 	g.ctx.Uniform1f(gl.Uniform{Value: location}, v0)
    300 }
    301 
    302 func (g *GomobileContext) Uniform1fv(location int32, value []float32) {
    303 	g.ctx.Uniform1fv(gl.Uniform{Value: location}, value)
    304 }
    305 
    306 func (g *GomobileContext) Uniform1i(location int32, v0 int32) {
    307 	g.ctx.Uniform1i(gl.Uniform{Value: location}, int(v0))
    308 }
    309 
    310 func (g *GomobileContext) Uniform2fv(location int32, value []float32) {
    311 	g.ctx.Uniform2fv(gl.Uniform{Value: location}, value)
    312 }
    313 
    314 func (g *GomobileContext) Uniform3fv(location int32, value []float32) {
    315 	g.ctx.Uniform3fv(gl.Uniform{Value: location}, value)
    316 }
    317 
    318 func (g *GomobileContext) Uniform4fv(location int32, value []float32) {
    319 	g.ctx.Uniform4fv(gl.Uniform{Value: location}, value)
    320 }
    321 
    322 func (g *GomobileContext) UniformMatrix2fv(location int32, transpose bool, value []float32) {
    323 	if transpose {
    324 		panic("gles: UniformMatrix2fv with transpose is not implemented")
    325 	}
    326 	g.ctx.UniformMatrix2fv(gl.Uniform{Value: location}, value)
    327 }
    328 
    329 func (g *GomobileContext) UniformMatrix3fv(location int32, transpose bool, value []float32) {
    330 	if transpose {
    331 		panic("gles: UniformMatrix3fv with transpose is not implemented")
    332 	}
    333 	g.ctx.UniformMatrix3fv(gl.Uniform{Value: location}, value)
    334 }
    335 
    336 func (g *GomobileContext) UniformMatrix4fv(location int32, transpose bool, value []float32) {
    337 	if transpose {
    338 		panic("gles: UniformMatrix4fv with transpose is not implemented")
    339 	}
    340 	g.ctx.UniformMatrix4fv(gl.Uniform{Value: location}, value)
    341 }
    342 
    343 func (g *GomobileContext) UseProgram(program uint32) {
    344 	g.ctx.UseProgram(gmProgram(program))
    345 }
    346 
    347 func (g *GomobileContext) VertexAttribPointer(index uint32, size int32, xtype uint32, normalized bool, stride int32, offset int) {
    348 	g.ctx.VertexAttribPointer(gl.Attrib{Value: uint(index)}, int(size), gl.Enum(xtype), normalized, int(stride), int(offset))
    349 }
    350 
    351 func (g *GomobileContext) Viewport(x int32, y int32, width int32, height int32) {
    352 	g.ctx.Viewport(int(x), int(y), int(width), int(height))
    353 }