gioui.org/ui@v0.0.0-20190926171558-ce74bc0cbaea/app/internal/gl/functions.go (about) 1 // SPDX-License-Identifier: Unlicense OR MIT 2 3 // +build darwin linux 4 5 package gl 6 7 import ( 8 "unsafe" 9 ) 10 11 /* 12 #cgo linux LDFLAGS: -lGLESv2 -ldl 13 #cgo darwin,!ios LDFLAGS: -framework OpenGL 14 15 #include <stdlib.h> 16 17 #ifdef __APPLE__ 18 #cgo CFLAGS: -DGL_SILENCE_DEPRECATION 19 #include "TargetConditionals.h" 20 #if TARGET_OS_IPHONE 21 #include <OpenGLES/ES3/gl.h> 22 #else 23 #include <OpenGL/gl3.h> 24 #endif 25 #else 26 #define __USE_GNU 27 #include <dlfcn.h> 28 #include <GLES2/gl2.h> 29 #include <GLES3/gl3.h> 30 #endif 31 32 static void (*_glInvalidateFramebuffer)(GLenum target, GLsizei numAttachments, const GLenum *attachments); 33 34 static void (*_glBeginQuery)(GLenum target, GLuint id); 35 static void (*_glDeleteQueries)(GLsizei n, const GLuint *ids); 36 static void (*_glEndQuery)(GLenum target); 37 static void (*_glGenQueries)(GLsizei n, GLuint *ids); 38 static void (*_glGetQueryObjectuiv)(GLuint id, GLenum pname, GLuint *params); 39 40 // The pointer-free version of glVertexAttribPointer, to avoid the Cgo pointer checks. 41 __attribute__ ((visibility ("hidden"))) void gio_glVertexAttribPointer(GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, uintptr_t offset) { 42 glVertexAttribPointer(index, size, type, normalized, stride, (const GLvoid *)offset); 43 } 44 45 // The pointer-free version of glDrawElements, to avoid the Cgo pointer checks. 46 __attribute__ ((visibility ("hidden"))) void gio_glDrawElements(GLenum mode, GLsizei count, GLenum type, const uintptr_t offset) { 47 glDrawElements(mode, count, type, (const GLvoid *)offset); 48 } 49 50 __attribute__ ((visibility ("hidden"))) void gio_glInvalidateFramebuffer(GLenum target, GLenum attachment) { 51 // Framebuffer invalidation is just a hint and can safely be ignored. 52 if (_glInvalidateFramebuffer != NULL) { 53 _glInvalidateFramebuffer(target, 1, &attachment); 54 } 55 } 56 57 __attribute__ ((visibility ("hidden"))) void gio_glBeginQuery(GLenum target, GLenum attachment) { 58 _glBeginQuery(target, attachment); 59 } 60 61 __attribute__ ((visibility ("hidden"))) void gio_glDeleteQueries(GLsizei n, const GLuint *ids) { 62 _glDeleteQueries(n, ids); 63 } 64 65 __attribute__ ((visibility ("hidden"))) void gio_glEndQuery(GLenum target) { 66 _glEndQuery(target); 67 } 68 69 __attribute__ ((visibility ("hidden"))) void gio_glGenQueries(GLsizei n, GLuint *ids) { 70 _glGenQueries(n, ids); 71 } 72 73 __attribute__ ((visibility ("hidden"))) void gio_glGetQueryObjectuiv(GLuint id, GLenum pname, GLuint *params) { 74 _glGetQueryObjectuiv(id, pname, params); 75 } 76 77 __attribute__((constructor)) static void gio_loadGLFunctions() { 78 #ifdef __APPLE__ 79 #if TARGET_OS_IPHONE 80 _glInvalidateFramebuffer = glInvalidateFramebuffer; 81 _glBeginQuery = glBeginQuery; 82 _glDeleteQueries = glDeleteQueries; 83 _glEndQuery = glEndQuery; 84 _glGenQueries = glGenQueries; 85 _glGetQueryObjectuiv = glGetQueryObjectuiv; 86 #endif 87 #else 88 // Load libGLESv3 if available. 89 dlopen("libGLESv3.so", RTLD_NOW | RTLD_GLOBAL); 90 _glInvalidateFramebuffer = dlsym(RTLD_DEFAULT, "glInvalidateFramebuffer"); 91 // Fall back to EXT_invalidate_framebuffer if available. 92 if (_glInvalidateFramebuffer == NULL) { 93 _glInvalidateFramebuffer = dlsym(RTLD_DEFAULT, "glDiscardFramebufferEXT"); 94 } 95 96 _glBeginQuery = dlsym(RTLD_DEFAULT, "glBeginQuery"); 97 if (_glBeginQuery == NULL) 98 _glBeginQuery = dlsym(RTLD_DEFAULT, "glBeginQueryEXT"); 99 _glDeleteQueries = dlsym(RTLD_DEFAULT, "glDeleteQueries"); 100 if (_glDeleteQueries == NULL) 101 _glDeleteQueries = dlsym(RTLD_DEFAULT, "glDeleteQueriesEXT"); 102 _glEndQuery = dlsym(RTLD_DEFAULT, "glEndQuery"); 103 if (_glEndQuery == NULL) 104 _glEndQuery = dlsym(RTLD_DEFAULT, "glEndQueryEXT"); 105 _glGenQueries = dlsym(RTLD_DEFAULT, "glGenQueries"); 106 if (_glGenQueries == NULL) 107 _glGenQueries = dlsym(RTLD_DEFAULT, "glGenQueriesEXT"); 108 _glGetQueryObjectuiv = dlsym(RTLD_DEFAULT, "glGetQueryObjectuiv"); 109 if (_glGetQueryObjectuiv == NULL) 110 _glGetQueryObjectuiv = dlsym(RTLD_DEFAULT, "glGetQueryObjectuivEXT"); 111 #endif 112 } 113 */ 114 import "C" 115 116 type Functions struct { 117 // Query caches. 118 uints [100]C.GLuint 119 ints [100]C.GLint 120 } 121 122 func (f *Functions) ActiveTexture(texture Enum) { 123 C.glActiveTexture(C.GLenum(texture)) 124 } 125 126 func (f *Functions) AttachShader(p Program, s Shader) { 127 C.glAttachShader(C.GLuint(p.V), C.GLuint(s.V)) 128 } 129 130 func (f *Functions) BeginQuery(target Enum, query Query) { 131 C.gio_glBeginQuery(C.GLenum(target), C.GLenum(query.V)) 132 } 133 134 func (f *Functions) BindAttribLocation(p Program, a Attrib, name string) { 135 cname := C.CString(name) 136 defer C.free(unsafe.Pointer(cname)) 137 C.glBindAttribLocation(C.GLuint(p.V), C.GLuint(a), cname) 138 } 139 140 func (f *Functions) BindBuffer(target Enum, b Buffer) { 141 C.glBindBuffer(C.GLenum(target), C.GLuint(b.V)) 142 } 143 144 func (f *Functions) BindFramebuffer(target Enum, fb Framebuffer) { 145 C.glBindFramebuffer(C.GLenum(target), C.GLuint(fb.V)) 146 } 147 148 func (f *Functions) BindRenderbuffer(target Enum, fb Renderbuffer) { 149 C.glBindRenderbuffer(C.GLenum(target), C.GLuint(fb.V)) 150 } 151 152 func (f *Functions) BindTexture(target Enum, t Texture) { 153 C.glBindTexture(C.GLenum(target), C.GLuint(t.V)) 154 } 155 156 func (f *Functions) BlendEquation(mode Enum) { 157 C.glBlendEquation(C.GLenum(mode)) 158 } 159 160 func (f *Functions) BlendFunc(sfactor, dfactor Enum) { 161 C.glBlendFunc(C.GLenum(sfactor), C.GLenum(dfactor)) 162 } 163 164 func (f *Functions) BufferData(target Enum, src []byte, usage Enum) { 165 var p unsafe.Pointer 166 if len(src) > 0 { 167 p = unsafe.Pointer(&src[0]) 168 } 169 C.glBufferData(C.GLenum(target), C.GLsizeiptr(len(src)), p, C.GLenum(usage)) 170 } 171 172 func (f *Functions) CheckFramebufferStatus(target Enum) Enum { 173 return Enum(C.glCheckFramebufferStatus(C.GLenum(target))) 174 } 175 176 func (f *Functions) Clear(mask Enum) { 177 C.glClear(C.GLbitfield(mask)) 178 } 179 180 func (f *Functions) ClearColor(red float32, green float32, blue float32, alpha float32) { 181 C.glClearColor(C.GLfloat(red), C.GLfloat(green), C.GLfloat(blue), C.GLfloat(alpha)) 182 } 183 184 func (f *Functions) ClearDepthf(d float32) { 185 C.glClearDepthf(C.GLfloat(d)) 186 } 187 188 func (f *Functions) CompileShader(s Shader) { 189 C.glCompileShader(C.GLuint(s.V)) 190 } 191 192 func (f *Functions) CreateBuffer() Buffer { 193 C.glGenBuffers(1, &f.uints[0]) 194 return Buffer{uint(f.uints[0])} 195 } 196 197 func (f *Functions) CreateFramebuffer() Framebuffer { 198 C.glGenFramebuffers(1, &f.uints[0]) 199 return Framebuffer{uint(f.uints[0])} 200 } 201 202 func (f *Functions) CreateProgram() Program { 203 return Program{uint(C.glCreateProgram())} 204 } 205 206 func (f *Functions) CreateQuery() Query { 207 C.gio_glGenQueries(1, &f.uints[0]) 208 return Query{uint(f.uints[0])} 209 } 210 211 func (f *Functions) CreateRenderbuffer() Renderbuffer { 212 C.glGenRenderbuffers(1, &f.uints[0]) 213 return Renderbuffer{uint(f.uints[0])} 214 } 215 216 func (f *Functions) CreateShader(ty Enum) Shader { 217 return Shader{uint(C.glCreateShader(C.GLenum(ty)))} 218 } 219 220 func (f *Functions) CreateTexture() Texture { 221 C.glGenTextures(1, &f.uints[0]) 222 return Texture{uint(f.uints[0])} 223 } 224 225 func (f *Functions) DeleteBuffer(v Buffer) { 226 f.uints[0] = C.GLuint(v.V) 227 C.glDeleteBuffers(1, &f.uints[0]) 228 } 229 230 func (f *Functions) DeleteFramebuffer(v Framebuffer) { 231 f.uints[0] = C.GLuint(v.V) 232 C.glDeleteFramebuffers(1, &f.uints[0]) 233 } 234 235 func (f *Functions) DeleteProgram(p Program) { 236 C.glDeleteProgram(C.GLuint(p.V)) 237 } 238 239 func (f *Functions) DeleteQuery(query Query) { 240 f.uints[0] = C.GLuint(query.V) 241 C.gio_glDeleteQueries(1, &f.uints[0]) 242 } 243 244 func (f *Functions) DeleteRenderbuffer(v Renderbuffer) { 245 f.uints[0] = C.GLuint(v.V) 246 C.glDeleteRenderbuffers(1, &f.uints[0]) 247 } 248 249 func (f *Functions) DeleteShader(s Shader) { 250 C.glDeleteShader(C.GLuint(s.V)) 251 } 252 253 func (f *Functions) DeleteTexture(v Texture) { 254 f.uints[0] = C.GLuint(v.V) 255 C.glDeleteTextures(1, &f.uints[0]) 256 } 257 258 func (f *Functions) DepthFunc(v Enum) { 259 C.glDepthFunc(C.GLenum(v)) 260 } 261 262 func (f *Functions) DepthMask(mask bool) { 263 m := C.GLboolean(C.GL_FALSE) 264 if mask { 265 m = C.GLboolean(C.GL_TRUE) 266 } 267 C.glDepthMask(m) 268 } 269 270 func (f *Functions) DisableVertexAttribArray(a Attrib) { 271 C.glDisableVertexAttribArray(C.GLuint(a)) 272 } 273 274 func (f *Functions) Disable(cap Enum) { 275 C.glDisable(C.GLenum(cap)) 276 } 277 278 func (f *Functions) DrawArrays(mode Enum, first int, count int) { 279 C.glDrawArrays(C.GLenum(mode), C.GLint(first), C.GLsizei(count)) 280 } 281 282 func (f *Functions) DrawElements(mode Enum, count int, ty Enum, offset int) { 283 C.gio_glDrawElements(C.GLenum(mode), C.GLsizei(count), C.GLenum(ty), C.uintptr_t(offset)) 284 } 285 286 func (f *Functions) Enable(cap Enum) { 287 C.glEnable(C.GLenum(cap)) 288 } 289 290 func (f *Functions) EndQuery(target Enum) { 291 C.gio_glEndQuery(C.GLenum(target)) 292 } 293 294 func (f *Functions) EnableVertexAttribArray(a Attrib) { 295 C.glEnableVertexAttribArray(C.GLuint(a)) 296 } 297 298 func (f *Functions) Finish() { 299 C.glFinish() 300 } 301 302 func (f *Functions) FramebufferRenderbuffer(target, attachment, renderbuffertarget Enum, renderbuffer Renderbuffer) { 303 C.glFramebufferRenderbuffer(C.GLenum(target), C.GLenum(attachment), C.GLenum(renderbuffertarget), C.GLuint(renderbuffer.V)) 304 } 305 306 func (f *Functions) FramebufferTexture2D(target, attachment, texTarget Enum, t Texture, level int) { 307 C.glFramebufferTexture2D(C.GLenum(target), C.GLenum(attachment), C.GLenum(texTarget), C.GLuint(t.V), C.GLint(level)) 308 } 309 310 func (c *Functions) GetBinding(pname Enum) Object { 311 return Object{uint(c.GetInteger(pname))} 312 } 313 314 func (f *Functions) GetError() Enum { 315 return Enum(C.glGetError()) 316 } 317 318 func (f *Functions) GetRenderbufferParameteri(target, pname Enum) int { 319 C.glGetRenderbufferParameteriv(C.GLenum(target), C.GLenum(pname), &f.ints[0]) 320 return int(f.ints[0]) 321 } 322 323 func (f *Functions) GetFramebufferAttachmentParameteri(target, attachment, pname Enum) int { 324 C.glGetFramebufferAttachmentParameteriv(C.GLenum(target), C.GLenum(attachment), C.GLenum(pname), &f.ints[0]) 325 return int(f.ints[0]) 326 } 327 328 func (f *Functions) GetInteger(pname Enum) int { 329 C.glGetIntegerv(C.GLenum(pname), &f.ints[0]) 330 return int(f.ints[0]) 331 } 332 333 func (f *Functions) GetProgrami(p Program, pname Enum) int { 334 C.glGetProgramiv(C.GLuint(p.V), C.GLenum(pname), &f.ints[0]) 335 return int(f.ints[0]) 336 } 337 338 func (f *Functions) GetProgramInfoLog(p Program) string { 339 n := f.GetProgrami(p, INFO_LOG_LENGTH) 340 buf := make([]byte, n) 341 C.glGetProgramInfoLog(C.GLuint(p.V), C.GLsizei(len(buf)), nil, (*C.GLchar)(unsafe.Pointer(&buf[0]))) 342 return string(buf) 343 } 344 345 func (f *Functions) GetQueryObjectuiv(query Query, pname Enum) uint { 346 C.gio_glGetQueryObjectuiv(C.GLuint(query.V), C.GLenum(pname), &f.uints[0]) 347 return uint(f.uints[0]) 348 } 349 350 func (f *Functions) GetShaderi(s Shader, pname Enum) int { 351 C.glGetShaderiv(C.GLuint(s.V), C.GLenum(pname), &f.ints[0]) 352 return int(f.ints[0]) 353 } 354 355 func (f *Functions) GetShaderInfoLog(s Shader) string { 356 n := f.GetShaderi(s, INFO_LOG_LENGTH) 357 buf := make([]byte, n) 358 C.glGetShaderInfoLog(C.GLuint(s.V), C.GLsizei(len(buf)), nil, (*C.GLchar)(unsafe.Pointer(&buf[0]))) 359 return string(buf) 360 } 361 362 func (f *Functions) GetString(pname Enum) string { 363 str := C.glGetString(C.GLenum(pname)) 364 return C.GoString((*C.char)(unsafe.Pointer(str))) 365 } 366 367 func (f *Functions) GetUniformLocation(p Program, name string) Uniform { 368 cname := C.CString(name) 369 defer C.free(unsafe.Pointer(cname)) 370 return Uniform{int(C.glGetUniformLocation(C.GLuint(p.V), cname))} 371 } 372 373 func (f *Functions) InvalidateFramebuffer(target, attachment Enum) { 374 C.gio_glInvalidateFramebuffer(C.GLenum(target), C.GLenum(attachment)) 375 } 376 377 func (f *Functions) LinkProgram(p Program) { 378 C.glLinkProgram(C.GLuint(p.V)) 379 } 380 381 func (f *Functions) PixelStorei(pname Enum, param int32) { 382 C.glPixelStorei(C.GLenum(pname), C.GLint(param)) 383 } 384 385 func (f *Functions) Scissor(x, y, width, height int32) { 386 C.glScissor(C.GLint(x), C.GLint(y), C.GLsizei(width), C.GLsizei(height)) 387 } 388 389 func (f *Functions) ReadPixels(x, y, width, height int, format, ty Enum, data []byte) { 390 var p unsafe.Pointer 391 if len(data) > 0 { 392 p = unsafe.Pointer(&data[0]) 393 } 394 C.glReadPixels(C.GLint(x), C.GLint(y), C.GLsizei(width), C.GLsizei(height), C.GLenum(format), C.GLenum(ty), p) 395 } 396 397 func (f *Functions) RenderbufferStorage(target, internalformat Enum, width, height int) { 398 C.glRenderbufferStorage(C.GLenum(target), C.GLenum(internalformat), C.GLsizei(width), C.GLsizei(height)) 399 } 400 401 func (f *Functions) ShaderSource(s Shader, src string) { 402 csrc := C.CString(src) 403 defer C.free(unsafe.Pointer(csrc)) 404 strlen := C.GLint(len(src)) 405 C.glShaderSource(C.GLuint(s.V), 1, &csrc, &strlen) 406 } 407 408 func (f *Functions) TexImage2D(target Enum, level int, internalFormat int, width int, height int, format Enum, ty Enum, data []byte) { 409 var p unsafe.Pointer 410 if len(data) > 0 { 411 p = unsafe.Pointer(&data[0]) 412 } 413 C.glTexImage2D(C.GLenum(target), C.GLint(level), C.GLint(internalFormat), C.GLsizei(width), C.GLsizei(height), 0, C.GLenum(format), C.GLenum(ty), p) 414 } 415 416 func (f *Functions) TexSubImage2D(target Enum, level int, x int, y int, width int, height int, format Enum, ty Enum, data []byte) { 417 var p unsafe.Pointer 418 if len(data) > 0 { 419 p = unsafe.Pointer(&data[0]) 420 } 421 C.glTexSubImage2D(C.GLenum(target), C.GLint(level), C.GLint(x), C.GLint(y), C.GLsizei(width), C.GLsizei(height), C.GLenum(format), C.GLenum(ty), p) 422 } 423 424 func (f *Functions) TexParameteri(target, pname Enum, param int) { 425 C.glTexParameteri(C.GLenum(target), C.GLenum(pname), C.GLint(param)) 426 } 427 428 func (f *Functions) Uniform1f(dst Uniform, v float32) { 429 C.glUniform1f(C.GLint(dst.V), C.GLfloat(v)) 430 } 431 432 func (f *Functions) Uniform1i(dst Uniform, v int) { 433 C.glUniform1i(C.GLint(dst.V), C.GLint(v)) 434 } 435 436 func (f *Functions) Uniform2f(dst Uniform, v0 float32, v1 float32) { 437 C.glUniform2f(C.GLint(dst.V), C.GLfloat(v0), C.GLfloat(v1)) 438 } 439 440 func (f *Functions) Uniform3f(dst Uniform, v0 float32, v1 float32, v2 float32) { 441 C.glUniform3f(C.GLint(dst.V), C.GLfloat(v0), C.GLfloat(v1), C.GLfloat(v2)) 442 } 443 444 func (f *Functions) Uniform4f(dst Uniform, v0 float32, v1 float32, v2 float32, v3 float32) { 445 C.glUniform4f(C.GLint(dst.V), C.GLfloat(v0), C.GLfloat(v1), C.GLfloat(v2), C.GLfloat(v3)) 446 } 447 448 func (f *Functions) UseProgram(p Program) { 449 C.glUseProgram(C.GLuint(p.V)) 450 } 451 452 func (f *Functions) VertexAttribPointer(dst Attrib, size int, ty Enum, normalized bool, stride int, offset int) { 453 var n C.GLboolean = C.GL_FALSE 454 if normalized { 455 n = C.GL_TRUE 456 } 457 C.gio_glVertexAttribPointer(C.GLuint(dst), C.GLint(size), C.GLenum(ty), n, C.GLsizei(stride), C.uintptr_t(offset)) 458 } 459 460 func (f *Functions) Viewport(x int, y int, width int, height int) { 461 C.glViewport(C.GLint(x), C.GLint(y), C.GLsizei(width), C.GLsizei(height)) 462 }