context_mobile.go (13662B)
1 // Copyright 2014 Hajime Hoshi 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 opengl 19 20 import ( 21 "errors" 22 "fmt" 23 24 "github.com/hajimehoshi/ebiten/v2/internal/driver" 25 "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl/gles" 26 "github.com/hajimehoshi/ebiten/v2/internal/shaderir" 27 ) 28 29 type ( 30 textureNative uint32 31 renderbufferNative uint32 32 framebufferNative uint32 33 shader uint32 34 program uint32 35 buffer uint32 36 ) 37 38 func (t textureNative) equal(rhs textureNative) bool { 39 return t == rhs 40 } 41 42 func (r renderbufferNative) equal(rhs renderbufferNative) bool { 43 return r == rhs 44 } 45 46 func (f framebufferNative) equal(rhs framebufferNative) bool { 47 return f == rhs 48 } 49 50 func (s shader) equal(rhs shader) bool { 51 return s == rhs 52 } 53 54 func (b buffer) equal(rhs buffer) bool { 55 return b == rhs 56 } 57 58 func (p program) equal(rhs program) bool { 59 return p == rhs 60 } 61 62 var InvalidTexture textureNative 63 64 type ( 65 uniformLocation int32 66 attribLocation int32 67 ) 68 69 func (u uniformLocation) equal(rhs uniformLocation) bool { 70 return u == rhs 71 } 72 73 type programID uint32 74 75 const ( 76 invalidTexture = 0 77 invalidFramebuffer = (1 << 32) - 1 78 invalidUniform = -1 79 ) 80 81 func getProgramID(p program) programID { 82 return programID(p) 83 } 84 85 const ( 86 zero = operation(gles.ZERO) 87 one = operation(gles.ONE) 88 srcAlpha = operation(gles.SRC_ALPHA) 89 dstAlpha = operation(gles.DST_ALPHA) 90 oneMinusSrcAlpha = operation(gles.ONE_MINUS_SRC_ALPHA) 91 oneMinusDstAlpha = operation(gles.ONE_MINUS_DST_ALPHA) 92 dstColor = operation(gles.DST_COLOR) 93 ) 94 95 type contextImpl struct { 96 ctx gles.Context 97 } 98 99 func (c *context) reset() error { 100 c.locationCache = newLocationCache() 101 c.lastTexture = invalidTexture 102 c.lastFramebuffer = invalidFramebuffer 103 c.lastViewportWidth = 0 104 c.lastViewportHeight = 0 105 c.lastCompositeMode = driver.CompositeModeUnknown 106 c.ctx.Enable(gles.BLEND) 107 c.ctx.Enable(gles.SCISSOR_TEST) 108 c.blendFunc(driver.CompositeModeSourceOver) 109 f := make([]int32, 1) 110 c.ctx.GetIntegerv(f, gles.FRAMEBUFFER_BINDING) 111 c.screenFramebuffer = framebufferNative(f[0]) 112 // TODO: Need to update screenFramebufferWidth/Height? 113 return nil 114 } 115 116 func (c *context) blendFunc(mode driver.CompositeMode) { 117 if c.lastCompositeMode == mode { 118 return 119 } 120 c.lastCompositeMode = mode 121 s, d := mode.Operations() 122 s2, d2 := convertOperation(s), convertOperation(d) 123 c.ctx.BlendFunc(uint32(s2), uint32(d2)) 124 } 125 126 func (c *context) scissor(x, y, width, height int) { 127 c.ctx.Scissor(int32(x), int32(y), int32(width), int32(height)) 128 } 129 130 func (c *context) newTexture(width, height int) (textureNative, error) { 131 t := c.ctx.GenTextures(1)[0] 132 if t <= 0 { 133 return 0, errors.New("opengl: creating texture failed") 134 } 135 c.bindTexture(textureNative(t)) 136 137 c.ctx.TexParameteri(gles.TEXTURE_2D, gles.TEXTURE_MAG_FILTER, gles.NEAREST) 138 c.ctx.TexParameteri(gles.TEXTURE_2D, gles.TEXTURE_MIN_FILTER, gles.NEAREST) 139 c.ctx.TexParameteri(gles.TEXTURE_2D, gles.TEXTURE_WRAP_S, gles.CLAMP_TO_EDGE) 140 c.ctx.TexParameteri(gles.TEXTURE_2D, gles.TEXTURE_WRAP_T, gles.CLAMP_TO_EDGE) 141 c.ctx.PixelStorei(gles.UNPACK_ALIGNMENT, 4) 142 c.ctx.TexImage2D(gles.TEXTURE_2D, 0, gles.RGBA, int32(width), int32(height), gles.RGBA, gles.UNSIGNED_BYTE, nil) 143 144 return textureNative(t), nil 145 } 146 147 func (c *context) bindFramebufferImpl(f framebufferNative) { 148 c.ctx.BindFramebuffer(gles.FRAMEBUFFER, uint32(f)) 149 } 150 151 func (c *context) framebufferPixels(f *framebuffer, width, height int) []byte { 152 c.ctx.Flush() 153 154 c.bindFramebuffer(f.native) 155 156 pixels := make([]byte, 4*width*height) 157 c.ctx.ReadPixels(pixels, 0, 0, int32(width), int32(height), gles.RGBA, gles.UNSIGNED_BYTE) 158 return pixels 159 } 160 161 func (c *context) framebufferPixelsToBuffer(f *framebuffer, buffer buffer, width, height int) { 162 c.ctx.Flush() 163 164 c.bindFramebuffer(f.native) 165 166 c.ctx.BindBuffer(gles.PIXEL_PACK_BUFFER, uint32(buffer)) 167 c.ctx.ReadPixels(nil, 0, 0, int32(width), int32(height), gles.RGBA, gles.UNSIGNED_BYTE) 168 c.ctx.BindBuffer(gles.PIXEL_PACK_BUFFER, 0) 169 } 170 171 func (c *context) activeTexture(idx int) { 172 c.ctx.ActiveTexture(uint32(gles.TEXTURE0 + idx)) 173 } 174 175 func (c *context) bindTextureImpl(t textureNative) { 176 c.ctx.BindTexture(gles.TEXTURE_2D, uint32(t)) 177 } 178 179 func (c *context) deleteTexture(t textureNative) { 180 if !c.ctx.IsTexture(uint32(t)) { 181 return 182 } 183 if c.lastTexture == t { 184 c.lastTexture = invalidTexture 185 } 186 c.ctx.DeleteTextures([]uint32{uint32(t)}) 187 } 188 189 func (c *context) isTexture(t textureNative) bool { 190 return c.ctx.IsTexture(uint32(t)) 191 } 192 193 func (c *context) newRenderbuffer(width, height int) (renderbufferNative, error) { 194 r := c.ctx.GenRenderbuffers(1)[0] 195 if r <= 0 { 196 return 0, errors.New("opengl: creating renderbuffer failed") 197 } 198 199 renderbuffer := renderbufferNative(r) 200 c.bindRenderbuffer(renderbuffer) 201 202 c.ctx.RenderbufferStorage(gles.RENDERBUFFER, gles.STENCIL_INDEX8, int32(width), int32(height)) 203 204 return renderbuffer, nil 205 } 206 207 func (c *context) bindRenderbufferImpl(r renderbufferNative) { 208 c.ctx.BindRenderbuffer(gles.RENDERBUFFER, uint32(r)) 209 } 210 211 func (c *context) deleteRenderbuffer(r renderbufferNative) { 212 if !c.ctx.IsRenderbuffer(uint32(r)) { 213 return 214 } 215 if c.lastRenderbuffer.equal(r) { 216 c.lastRenderbuffer = 0 217 } 218 c.ctx.DeleteRenderbuffers([]uint32{uint32(r)}) 219 } 220 221 func (c *context) newFramebuffer(texture textureNative) (framebufferNative, error) { 222 f := c.ctx.GenFramebuffers(1)[0] 223 if f <= 0 { 224 return 0, fmt.Errorf("opengl: creating framebuffer failed: the returned value is not positive but %d", f) 225 } 226 c.bindFramebuffer(framebufferNative(f)) 227 228 c.ctx.FramebufferTexture2D(gles.FRAMEBUFFER, gles.COLOR_ATTACHMENT0, gles.TEXTURE_2D, uint32(texture), 0) 229 s := c.ctx.CheckFramebufferStatus(gles.FRAMEBUFFER) 230 if s != gles.FRAMEBUFFER_COMPLETE { 231 if s != 0 { 232 return 0, fmt.Errorf("opengl: creating framebuffer failed: %v", s) 233 } 234 if e := c.ctx.GetError(); e != gles.NO_ERROR { 235 return 0, fmt.Errorf("opengl: creating framebuffer failed: (glGetError) %d", e) 236 } 237 return 0, fmt.Errorf("opengl: creating framebuffer failed: unknown error") 238 } 239 return framebufferNative(f), nil 240 } 241 242 func (c *context) bindStencilBuffer(f framebufferNative, r renderbufferNative) error { 243 c.bindFramebuffer(f) 244 245 c.ctx.FramebufferRenderbuffer(gles.FRAMEBUFFER, gles.STENCIL_ATTACHMENT, gles.RENDERBUFFER, uint32(r)) 246 if s := c.ctx.CheckFramebufferStatus(gles.FRAMEBUFFER); s != gles.FRAMEBUFFER_COMPLETE { 247 return errors.New(fmt.Sprintf("opengl: glFramebufferRenderbuffer failed: %d", s)) 248 } 249 return nil 250 } 251 252 func (c *context) setViewportImpl(width, height int) { 253 c.ctx.Viewport(0, 0, int32(width), int32(height)) 254 } 255 256 func (c *context) deleteFramebuffer(f framebufferNative) { 257 if !c.ctx.IsFramebuffer(uint32(f)) { 258 return 259 } 260 // If a framebuffer to be deleted is bound, a newly bound framebuffer 261 // will be a default framebuffer. 262 // https://www.khronos.org/opengles/sdk/docs/man/xhtml/glDeleteFramebuffers.xml 263 if c.lastFramebuffer == f { 264 c.lastFramebuffer = invalidFramebuffer 265 c.lastViewportWidth = 0 266 c.lastViewportHeight = 0 267 } 268 c.ctx.DeleteFramebuffers([]uint32{uint32(f)}) 269 } 270 271 func (c *context) newVertexShader(source string) (shader, error) { 272 return c.newShader(gles.VERTEX_SHADER, source) 273 } 274 275 func (c *context) newFragmentShader(source string) (shader, error) { 276 return c.newShader(gles.FRAGMENT_SHADER, source) 277 } 278 279 func (c *context) newShader(shaderType uint32, source string) (shader, error) { 280 s := c.ctx.CreateShader(shaderType) 281 if s == 0 { 282 return 0, fmt.Errorf("opengl: glCreateShader failed: shader type: %d", shaderType) 283 } 284 c.ctx.ShaderSource(s, source) 285 c.ctx.CompileShader(s) 286 287 v := make([]int32, 1) 288 c.ctx.GetShaderiv(v, s, gles.COMPILE_STATUS) 289 if v[0] == gles.FALSE { 290 log := c.ctx.GetShaderInfoLog(s) 291 return 0, fmt.Errorf("opengl: shader compile failed: %s", log) 292 } 293 return shader(s), nil 294 } 295 296 func (c *context) deleteShader(s shader) { 297 c.ctx.DeleteShader(uint32(s)) 298 } 299 300 func (c *context) newProgram(shaders []shader, attributes []string) (program, error) { 301 p := c.ctx.CreateProgram() 302 if p == 0 { 303 return 0, errors.New("opengl: glCreateProgram failed") 304 } 305 306 for _, shader := range shaders { 307 c.ctx.AttachShader(p, uint32(shader)) 308 } 309 310 for i, name := range attributes { 311 c.ctx.BindAttribLocation(p, uint32(i), name) 312 } 313 314 c.ctx.LinkProgram(p) 315 v := make([]int32, 1) 316 c.ctx.GetProgramiv(v, p, gles.LINK_STATUS) 317 if v[0] == gles.FALSE { 318 info := c.ctx.GetProgramInfoLog(p) 319 return 0, fmt.Errorf("opengl: program error: %s", info) 320 } 321 return program(p), nil 322 } 323 324 func (c *context) useProgram(p program) { 325 c.ctx.UseProgram(uint32(p)) 326 } 327 328 func (c *context) deleteProgram(p program) { 329 c.locationCache.deleteProgram(p) 330 331 if !c.ctx.IsProgram(uint32(p)) { 332 return 333 } 334 c.ctx.DeleteProgram(uint32(p)) 335 } 336 337 func (c *context) getUniformLocationImpl(p program, location string) uniformLocation { 338 u := uniformLocation(c.ctx.GetUniformLocation(uint32(p), location)) 339 return u 340 } 341 342 func (c *context) uniformInt(p program, location string, v int) bool { 343 l := c.locationCache.GetUniformLocation(c, p, location) 344 if l == invalidUniform { 345 return false 346 } 347 c.ctx.Uniform1i(int32(l), int32(v)) 348 return true 349 } 350 351 func (c *context) uniformFloat(p program, location string, v float32) bool { 352 l := c.locationCache.GetUniformLocation(c, p, location) 353 if l == invalidUniform { 354 return false 355 } 356 c.ctx.Uniform1f(int32(l), v) 357 return true 358 } 359 360 func (c *context) uniformFloats(p program, location string, v []float32, typ shaderir.Type) bool { 361 l := c.locationCache.GetUniformLocation(c, p, location) 362 if l == invalidUniform { 363 return false 364 } 365 366 base := typ.Main 367 if base == shaderir.Array { 368 base = typ.Sub[0].Main 369 } 370 371 switch base { 372 case shaderir.Float: 373 c.ctx.Uniform1fv(int32(l), v) 374 case shaderir.Vec2: 375 c.ctx.Uniform2fv(int32(l), v) 376 case shaderir.Vec3: 377 c.ctx.Uniform3fv(int32(l), v) 378 case shaderir.Vec4: 379 c.ctx.Uniform4fv(int32(l), v) 380 case shaderir.Mat2: 381 c.ctx.UniformMatrix2fv(int32(l), false, v) 382 case shaderir.Mat3: 383 c.ctx.UniformMatrix3fv(int32(l), false, v) 384 case shaderir.Mat4: 385 c.ctx.UniformMatrix4fv(int32(l), false, v) 386 default: 387 panic(fmt.Sprintf("opengl: unexpected type: %s", typ.String())) 388 } 389 return true 390 } 391 392 func (c *context) vertexAttribPointer(index int, size int, stride int, offset int) { 393 c.ctx.VertexAttribPointer(uint32(index), int32(size), gles.FLOAT, false, int32(stride), offset) 394 } 395 396 func (c *context) enableVertexAttribArray(index int) { 397 c.ctx.EnableVertexAttribArray(uint32(index)) 398 } 399 400 func (c *context) disableVertexAttribArray(index int) { 401 c.ctx.DisableVertexAttribArray(uint32(index)) 402 } 403 404 func (c *context) newArrayBuffer(size int) buffer { 405 b := c.ctx.GenBuffers(1)[0] 406 c.ctx.BindBuffer(gles.ARRAY_BUFFER, b) 407 c.ctx.BufferData(gles.ARRAY_BUFFER, size, nil, gles.DYNAMIC_DRAW) 408 return buffer(b) 409 } 410 411 func (c *context) newElementArrayBuffer(size int) buffer { 412 b := c.ctx.GenBuffers(1)[0] 413 c.ctx.BindBuffer(gles.ELEMENT_ARRAY_BUFFER, b) 414 c.ctx.BufferData(gles.ELEMENT_ARRAY_BUFFER, size, nil, gles.DYNAMIC_DRAW) 415 return buffer(b) 416 } 417 418 func (c *context) bindArrayBuffer(b buffer) { 419 c.ctx.BindBuffer(gles.ARRAY_BUFFER, uint32(b)) 420 } 421 422 func (c *context) bindElementArrayBuffer(b buffer) { 423 c.ctx.BindBuffer(gles.ELEMENT_ARRAY_BUFFER, uint32(b)) 424 } 425 426 func (c *context) arrayBufferSubData(data []float32) { 427 c.ctx.BufferSubData(gles.ARRAY_BUFFER, 0, float32sToBytes(data)) 428 } 429 430 func (c *context) elementArrayBufferSubData(data []uint16) { 431 c.ctx.BufferSubData(gles.ELEMENT_ARRAY_BUFFER, 0, uint16sToBytes(data)) 432 } 433 434 func (c *context) deleteBuffer(b buffer) { 435 c.ctx.DeleteBuffers([]uint32{uint32(b)}) 436 } 437 438 func (c *context) drawElements(len int, offsetInBytes int) { 439 c.ctx.DrawElements(gles.TRIANGLES, int32(len), gles.UNSIGNED_SHORT, offsetInBytes) 440 } 441 442 func (c *context) maxTextureSizeImpl() int { 443 v := make([]int32, 1) 444 c.ctx.GetIntegerv(v, gles.MAX_TEXTURE_SIZE) 445 return int(v[0]) 446 } 447 448 func (c *context) getShaderPrecisionFormatPrecision() int { 449 _, _, p := c.ctx.GetShaderPrecisionFormat(gles.FRAGMENT_SHADER, gles.HIGH_FLOAT) 450 return p 451 } 452 453 func (c *context) flush() { 454 c.ctx.Flush() 455 } 456 457 func (c *context) needsRestoring() bool { 458 return true 459 } 460 461 func (c *context) canUsePBO() bool { 462 // On Android, using PBO might slow the applications, especially when coming back from the context lost. 463 // Let's not use PBO until we find a good solution. 464 return false 465 } 466 467 func (c *context) texSubImage2D(t textureNative, args []*driver.ReplacePixelsArgs) { 468 c.bindTexture(t) 469 for _, a := range args { 470 c.ctx.TexSubImage2D(gles.TEXTURE_2D, 0, int32(a.X), int32(a.Y), int32(a.Width), int32(a.Height), gles.RGBA, gles.UNSIGNED_BYTE, a.Pixels) 471 } 472 } 473 474 func (c *context) enableStencilTest() { 475 c.ctx.Enable(gles.STENCIL_TEST) 476 } 477 478 func (c *context) disableStencilTest() { 479 c.ctx.Disable(gles.STENCIL_TEST) 480 } 481 482 func (c *context) beginStencilWithEvenOddRule() { 483 c.ctx.Clear(gles.STENCIL_BUFFER_BIT) 484 c.ctx.StencilFunc(gles.ALWAYS, 0x00, 0xff) 485 c.ctx.StencilOp(gles.KEEP, gles.KEEP, gles.INVERT) 486 c.ctx.ColorMask(false, false, false, false) 487 } 488 489 func (c *context) endStencilWithEvenOddRule() { 490 c.ctx.StencilFunc(gles.NOTEQUAL, 0x00, 0xff) 491 c.ctx.StencilOp(gles.KEEP, gles.KEEP, gles.KEEP) 492 c.ctx.ColorMask(true, true, true, true) 493 }