gioui.org@v0.6.1-0.20240506124620-7a9ce51988ce/internal/gl/gl_unix.go (about) 1 // SPDX-License-Identifier: Unlicense OR MIT 2 3 //go:build darwin || linux || freebsd || openbsd 4 // +build darwin linux freebsd openbsd 5 6 package gl 7 8 import ( 9 "fmt" 10 "runtime" 11 "strings" 12 "unsafe" 13 ) 14 15 /* 16 #cgo CFLAGS: -Werror 17 #cgo linux freebsd LDFLAGS: -ldl 18 19 #include <stdint.h> 20 #include <stdlib.h> 21 #include <sys/types.h> 22 #define __USE_GNU 23 #include <dlfcn.h> 24 25 typedef unsigned int GLenum; 26 typedef unsigned int GLuint; 27 typedef char GLchar; 28 typedef float GLfloat; 29 typedef ssize_t GLsizeiptr; 30 typedef intptr_t GLintptr; 31 typedef unsigned int GLbitfield; 32 typedef int GLint; 33 typedef unsigned char GLboolean; 34 typedef int GLsizei; 35 typedef uint8_t GLubyte; 36 37 typedef void (*_glActiveTexture)(GLenum texture); 38 typedef void (*_glAttachShader)(GLuint program, GLuint shader); 39 typedef void (*_glBindAttribLocation)(GLuint program, GLuint index, const GLchar *name); 40 typedef void (*_glBindBuffer)(GLenum target, GLuint buffer); 41 typedef void (*_glBindFramebuffer)(GLenum target, GLuint framebuffer); 42 typedef void (*_glBindRenderbuffer)(GLenum target, GLuint renderbuffer); 43 typedef void (*_glBindTexture)(GLenum target, GLuint texture); 44 typedef void (*_glBlendEquation)(GLenum mode); 45 typedef void (*_glBlendFuncSeparate)(GLenum srcRGB, GLenum dstRGB, GLenum srcA, GLenum dstA); 46 typedef void (*_glBufferData)(GLenum target, GLsizeiptr size, const void *data, GLenum usage); 47 typedef void (*_glBufferSubData)(GLenum target, GLintptr offset, GLsizeiptr size, const void *data); 48 typedef GLenum (*_glCheckFramebufferStatus)(GLenum target); 49 typedef void (*_glClear)(GLbitfield mask); 50 typedef void (*_glClearColor)(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha); 51 typedef void (*_glClearDepthf)(GLfloat d); 52 typedef void (*_glCompileShader)(GLuint shader); 53 typedef void (*_glCopyTexSubImage2D)(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height); 54 typedef GLuint (*_glCreateProgram)(void); 55 typedef GLuint (*_glCreateShader)(GLenum type); 56 typedef void (*_glDeleteBuffers)(GLsizei n, const GLuint *buffers); 57 typedef void (*_glDeleteFramebuffers)(GLsizei n, const GLuint *framebuffers); 58 typedef void (*_glDeleteProgram)(GLuint program); 59 typedef void (*_glDeleteRenderbuffers)(GLsizei n, const GLuint *renderbuffers); 60 typedef void (*_glDeleteShader)(GLuint shader); 61 typedef void (*_glDeleteTextures)(GLsizei n, const GLuint *textures); 62 typedef void (*_glDepthFunc)(GLenum func); 63 typedef void (*_glDepthMask)(GLboolean flag); 64 typedef void (*_glDisable)(GLenum cap); 65 typedef void (*_glDisableVertexAttribArray)(GLuint index); 66 typedef void (*_glDrawArrays)(GLenum mode, GLint first, GLsizei count); 67 typedef void (*_glDrawElements)(GLenum mode, GLsizei count, GLenum type, const void *indices); 68 typedef void (*_glEnable)(GLenum cap); 69 typedef void (*_glEnableVertexAttribArray)(GLuint index); 70 typedef void (*_glFinish)(void); 71 typedef void (*_glFlush)(void); 72 typedef void (*_glFramebufferRenderbuffer)(GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer); 73 typedef void (*_glFramebufferTexture2D)(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level); 74 typedef void (*_glGenBuffers)(GLsizei n, GLuint *buffers); 75 typedef void (*_glGenerateMipmap)(GLenum target); 76 typedef void (*_glGenFramebuffers)(GLsizei n, GLuint *framebuffers); 77 typedef void (*_glGenRenderbuffers)(GLsizei n, GLuint *renderbuffers); 78 typedef void (*_glGenTextures)(GLsizei n, GLuint *textures); 79 typedef GLenum (*_glGetError)(void); 80 typedef void (*_glGetFramebufferAttachmentParameteriv)(GLenum target, GLenum attachment, GLenum pname, GLint *params); 81 typedef void (*_glGetFloatv)(GLenum pname, GLfloat *data); 82 typedef void (*_glGetIntegerv)(GLenum pname, GLint *data); 83 typedef void (*_glGetIntegeri_v)(GLenum pname, GLuint idx, GLint *data); 84 typedef void (*_glGetProgramiv)(GLuint program, GLenum pname, GLint *params); 85 typedef void (*_glGetProgramInfoLog)(GLuint program, GLsizei bufSize, GLsizei *length, GLchar *infoLog); 86 typedef void (*_glGetRenderbufferParameteriv)(GLenum target, GLenum pname, GLint *params); 87 typedef void (*_glGetShaderiv)(GLuint shader, GLenum pname, GLint *params); 88 typedef void (*_glGetShaderInfoLog)(GLuint shader, GLsizei bufSize, GLsizei *length, GLchar *infoLog); 89 typedef const GLubyte *(*_glGetString)(GLenum name); 90 typedef GLint (*_glGetUniformLocation)(GLuint program, const GLchar *name); 91 typedef void (*_glGetVertexAttribiv)(GLuint index, GLenum pname, GLint *params); 92 typedef void (*_glGetVertexAttribPointerv)(GLuint index, GLenum pname, void **params); 93 typedef GLboolean (*_glIsEnabled)(GLenum cap); 94 typedef void (*_glLinkProgram)(GLuint program); 95 typedef void (*_glPixelStorei)(GLenum pname, GLint param); 96 typedef void (*_glReadPixels)(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void *pixels); 97 typedef void (*_glRenderbufferStorage)(GLenum target, GLenum internalformat, GLsizei width, GLsizei height); 98 typedef void (*_glScissor)(GLint x, GLint y, GLsizei width, GLsizei height); 99 typedef void (*_glShaderSource)(GLuint shader, GLsizei count, const GLchar *const*string, const GLint *length); 100 typedef void (*_glTexImage2D)(GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const void *pixels); 101 typedef void (*_glTexParameteri)(GLenum target, GLenum pname, GLint param); 102 typedef void (*_glTexSubImage2D)(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels); 103 typedef void (*_glUniform1f)(GLint location, GLfloat v0); 104 typedef void (*_glUniform1i)(GLint location, GLint v0); 105 typedef void (*_glUniform2f)(GLint location, GLfloat v0, GLfloat v1); 106 typedef void (*_glUniform3f)(GLint location, GLfloat v0, GLfloat v1, GLfloat v2); 107 typedef void (*_glUniform4f)(GLint location, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3); 108 typedef void (*_glUseProgram)(GLuint program); 109 typedef void (*_glVertexAttribPointer)(GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void *pointer); 110 typedef void (*_glViewport)(GLint x, GLint y, GLsizei width, GLsizei height); 111 typedef void (*_glBindVertexArray)(GLuint array); 112 typedef void (*_glBindBufferBase)(GLenum target, GLuint index, GLuint buffer); 113 typedef GLuint (*_glGetUniformBlockIndex)(GLuint program, const GLchar *uniformBlockName); 114 typedef void (*_glUniformBlockBinding)(GLuint program, GLuint uniformBlockIndex, GLuint uniformBlockBinding); 115 typedef void (*_glInvalidateFramebuffer)(GLenum target, GLsizei numAttachments, const GLenum *attachments); 116 typedef void (*_glBeginQuery)(GLenum target, GLuint id); 117 typedef void (*_glDeleteQueries)(GLsizei n, const GLuint *ids); 118 typedef void (*_glDeleteVertexArrays)(GLsizei n, const GLuint *ids); 119 typedef void (*_glEndQuery)(GLenum target); 120 typedef void (*_glGenQueries)(GLsizei n, GLuint *ids); 121 typedef void (*_glGenVertexArrays)(GLsizei n, GLuint *ids); 122 typedef void (*_glGetProgramBinary)(GLuint program, GLsizei bufsize, GLsizei *length, GLenum *binaryFormat, void *binary); 123 typedef void (*_glGetQueryObjectuiv)(GLuint id, GLenum pname, GLuint *params); 124 typedef const GLubyte* (*_glGetStringi)(GLenum name, GLuint index); 125 typedef void (*_glDispatchCompute)(GLuint x, GLuint y, GLuint z); 126 typedef void (*_glMemoryBarrier)(GLbitfield barriers); 127 typedef void* (*_glMapBufferRange)(GLenum target, GLintptr offset, GLsizeiptr length, GLbitfield access); 128 typedef GLboolean (*_glUnmapBuffer)(GLenum target); 129 typedef void (*_glBindImageTexture)(GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format); 130 typedef void (*_glTexStorage2D)(GLenum target, GLsizei levels, GLenum internalformat, GLsizei width, GLsizei height); 131 typedef void (*_glBlitFramebuffer)(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, GLbitfield mask, GLenum filter); 132 133 static void glActiveTexture(_glActiveTexture f, GLenum texture) { 134 f(texture); 135 } 136 137 static void glAttachShader(_glAttachShader f, GLuint program, GLuint shader) { 138 f(program, shader); 139 } 140 141 static void glBindAttribLocation(_glBindAttribLocation f, GLuint program, GLuint index, const GLchar *name) { 142 f(program, index, name); 143 } 144 145 static void glBindBuffer(_glBindBuffer f, GLenum target, GLuint buffer) { 146 f(target, buffer); 147 } 148 149 static void glBindFramebuffer(_glBindFramebuffer f, GLenum target, GLuint framebuffer) { 150 f(target, framebuffer); 151 } 152 153 static void glBindRenderbuffer(_glBindRenderbuffer f, GLenum target, GLuint renderbuffer) { 154 f(target, renderbuffer); 155 } 156 157 static void glBindTexture(_glBindTexture f, GLenum target, GLuint texture) { 158 f(target, texture); 159 } 160 161 static void glBindVertexArray(_glBindVertexArray f, GLuint array) { 162 f(array); 163 } 164 165 static void glBlendEquation(_glBlendEquation f, GLenum mode) { 166 f(mode); 167 } 168 169 static void glBlendFuncSeparate(_glBlendFuncSeparate f, GLenum srcRGB, GLenum dstRGB, GLenum srcA, GLenum dstA) { 170 f(srcRGB, dstRGB, srcA, dstA); 171 } 172 173 static void glBufferData(_glBufferData f, GLenum target, GLsizeiptr size, const void *data, GLenum usage) { 174 f(target, size, data, usage); 175 } 176 177 static void glBufferSubData(_glBufferSubData f, GLenum target, GLintptr offset, GLsizeiptr size, const void *data) { 178 f(target, offset, size, data); 179 } 180 181 static GLenum glCheckFramebufferStatus(_glCheckFramebufferStatus f, GLenum target) { 182 return f(target); 183 } 184 185 static void glClear(_glClear f, GLbitfield mask) { 186 f(mask); 187 } 188 189 static void glClearColor(_glClearColor f, GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha) { 190 f(red, green, blue, alpha); 191 } 192 193 static void glClearDepthf(_glClearDepthf f, GLfloat d) { 194 f(d); 195 } 196 197 static void glCompileShader(_glCompileShader f, GLuint shader) { 198 f(shader); 199 } 200 201 static void glCopyTexSubImage2D(_glCopyTexSubImage2D f, GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height) { 202 f(target, level, xoffset, yoffset, x, y, width, height); 203 } 204 205 static GLuint glCreateProgram(_glCreateProgram f) { 206 return f(); 207 } 208 209 static GLuint glCreateShader(_glCreateShader f, GLenum type) { 210 return f(type); 211 } 212 213 static void glDeleteBuffers(_glDeleteBuffers f, GLsizei n, const GLuint *buffers) { 214 f(n, buffers); 215 } 216 217 static void glDeleteFramebuffers(_glDeleteFramebuffers f, GLsizei n, const GLuint *framebuffers) { 218 f(n, framebuffers); 219 } 220 221 static void glDeleteProgram(_glDeleteProgram f, GLuint program) { 222 f(program); 223 } 224 225 static void glDeleteRenderbuffers(_glDeleteRenderbuffers f, GLsizei n, const GLuint *renderbuffers) { 226 f(n, renderbuffers); 227 } 228 229 static void glDeleteShader(_glDeleteShader f, GLuint shader) { 230 f(shader); 231 } 232 233 static void glDeleteTextures(_glDeleteTextures f, GLsizei n, const GLuint *textures) { 234 f(n, textures); 235 } 236 237 static void glDepthFunc(_glDepthFunc f, GLenum func) { 238 f(func); 239 } 240 241 static void glDepthMask(_glDepthMask f, GLboolean flag) { 242 f(flag); 243 } 244 245 static void glDisable(_glDisable f, GLenum cap) { 246 f(cap); 247 } 248 249 static void glDisableVertexAttribArray(_glDisableVertexAttribArray f, GLuint index) { 250 f(index); 251 } 252 253 static void glDrawArrays(_glDrawArrays f, GLenum mode, GLint first, GLsizei count) { 254 f(mode, first, count); 255 } 256 257 // offset is defined as an uintptr_t to omit Cgo pointer checks. 258 static void glDrawElements(_glDrawElements f, GLenum mode, GLsizei count, GLenum type, const uintptr_t offset) { 259 f(mode, count, type, (const void *)offset); 260 } 261 262 static void glEnable(_glEnable f, GLenum cap) { 263 f(cap); 264 } 265 266 static void glEnableVertexAttribArray(_glEnableVertexAttribArray f, GLuint index) { 267 f(index); 268 } 269 270 static void glFinish(_glFinish f) { 271 f(); 272 } 273 274 static void glFlush(_glFlush f) { 275 f(); 276 } 277 278 static void glFramebufferRenderbuffer(_glFramebufferRenderbuffer f, GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer) { 279 f(target, attachment, renderbuffertarget, renderbuffer); 280 } 281 282 static void glFramebufferTexture2D(_glFramebufferTexture2D f, GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level) { 283 f(target, attachment, textarget, texture, level); 284 } 285 286 static void glGenBuffers(_glGenBuffers f, GLsizei n, GLuint *buffers) { 287 f(n, buffers); 288 } 289 290 static void glGenerateMipmap(_glGenerateMipmap f, GLenum target) { 291 f(target); 292 } 293 294 static void glGenFramebuffers(_glGenFramebuffers f, GLsizei n, GLuint *framebuffers) { 295 f(n, framebuffers); 296 } 297 298 static void glGenRenderbuffers(_glGenRenderbuffers f, GLsizei n, GLuint *renderbuffers) { 299 f(n, renderbuffers); 300 } 301 302 static void glGenTextures(_glGenTextures f, GLsizei n, GLuint *textures) { 303 f(n, textures); 304 } 305 306 static GLenum glGetError(_glGetError f) { 307 return f(); 308 } 309 310 static void glGetFramebufferAttachmentParameteriv(_glGetFramebufferAttachmentParameteriv f, GLenum target, GLenum attachment, GLenum pname, GLint *params) { 311 f(target, attachment, pname, params); 312 } 313 314 static void glGetIntegerv(_glGetIntegerv f, GLenum pname, GLint *data) { 315 f(pname, data); 316 } 317 318 static void glGetFloatv(_glGetFloatv f, GLenum pname, GLfloat *data) { 319 f(pname, data); 320 } 321 322 static void glGetIntegeri_v(_glGetIntegeri_v f, GLenum pname, GLuint idx, GLint *data) { 323 f(pname, idx, data); 324 } 325 326 static void glGetProgramiv(_glGetProgramiv f, GLuint program, GLenum pname, GLint *params) { 327 f(program, pname, params); 328 } 329 330 static void glGetProgramInfoLog(_glGetProgramInfoLog f, GLuint program, GLsizei bufSize, GLsizei *length, GLchar *infoLog) { 331 f(program, bufSize, length, infoLog); 332 } 333 334 static void glGetRenderbufferParameteriv(_glGetRenderbufferParameteriv f, GLenum target, GLenum pname, GLint *params) { 335 f(target, pname, params); 336 } 337 338 static void glGetShaderiv(_glGetShaderiv f, GLuint shader, GLenum pname, GLint *params) { 339 f(shader, pname, params); 340 } 341 342 static void glGetShaderInfoLog(_glGetShaderInfoLog f, GLuint shader, GLsizei bufSize, GLsizei *length, GLchar *infoLog) { 343 f(shader, bufSize, length, infoLog); 344 } 345 346 static const GLubyte *glGetString(_glGetString f, GLenum name) { 347 return f(name); 348 } 349 350 static GLint glGetUniformLocation(_glGetUniformLocation f, GLuint program, const GLchar *name) { 351 return f(program, name); 352 } 353 354 static void glGetVertexAttribiv(_glGetVertexAttribiv f, GLuint index, GLenum pname, GLint *data) { 355 f(index, pname, data); 356 } 357 358 // Return uintptr_t to avoid Cgo pointer check. 359 static uintptr_t glGetVertexAttribPointerv(_glGetVertexAttribPointerv f, GLuint index, GLenum pname) { 360 void *ptrs; 361 f(index, pname, &ptrs); 362 return (uintptr_t)ptrs; 363 } 364 365 static GLboolean glIsEnabled(_glIsEnabled f, GLenum cap) { 366 return f(cap); 367 } 368 369 static void glLinkProgram(_glLinkProgram f, GLuint program) { 370 f(program); 371 } 372 373 static void glPixelStorei(_glPixelStorei f, GLenum pname, GLint param) { 374 f(pname, param); 375 } 376 377 static void glReadPixels(_glReadPixels f, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void *pixels) { 378 f(x, y, width, height, format, type, pixels); 379 } 380 381 static void glRenderbufferStorage(_glRenderbufferStorage f, GLenum target, GLenum internalformat, GLsizei width, GLsizei height) { 382 f(target, internalformat, width, height); 383 } 384 385 static void glScissor(_glScissor f, GLint x, GLint y, GLsizei width, GLsizei height) { 386 f(x, y, width, height); 387 } 388 389 static void glShaderSource(_glShaderSource f, GLuint shader, GLsizei count, const GLchar *const*string, const GLint *length) { 390 f(shader, count, string, length); 391 } 392 393 static void glTexImage2D(_glTexImage2D f, GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const void *pixels) { 394 f(target, level, internalformat, width, height, border, format, type, pixels); 395 } 396 397 static void glTexParameteri(_glTexParameteri f, GLenum target, GLenum pname, GLint param) { 398 f(target, pname, param); 399 } 400 401 static void glTexSubImage2D(_glTexSubImage2D f, GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels) { 402 f(target, level, xoffset, yoffset, width, height, format, type, pixels); 403 } 404 405 static void glUniform1f(_glUniform1f f, GLint location, GLfloat v0) { 406 f(location, v0); 407 } 408 409 static void glUniform1i(_glUniform1i f, GLint location, GLint v0) { 410 f(location, v0); 411 } 412 413 static void glUniform2f(_glUniform2f f, GLint location, GLfloat v0, GLfloat v1) { 414 f(location, v0, v1); 415 } 416 417 static void glUniform3f(_glUniform3f f, GLint location, GLfloat v0, GLfloat v1, GLfloat v2) { 418 f(location, v0, v1, v2); 419 } 420 421 static void glUniform4f(_glUniform4f f, GLint location, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3) { 422 f(location, v0, v1, v2, v3); 423 } 424 425 static void glUseProgram(_glUseProgram f, GLuint program) { 426 f(program); 427 } 428 429 // offset is defined as an uintptr_t to omit Cgo pointer checks. 430 static void glVertexAttribPointer(_glVertexAttribPointer f, GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, uintptr_t offset) { 431 f(index, size, type, normalized, stride, (const void *)offset); 432 } 433 434 static void glViewport(_glViewport f, GLint x, GLint y, GLsizei width, GLsizei height) { 435 f(x, y, width, height); 436 } 437 438 static void glBindBufferBase(_glBindBufferBase f, GLenum target, GLuint index, GLuint buffer) { 439 f(target, index, buffer); 440 } 441 442 static void glUniformBlockBinding(_glUniformBlockBinding f, GLuint program, GLuint uniformBlockIndex, GLuint uniformBlockBinding) { 443 f(program, uniformBlockIndex, uniformBlockBinding); 444 } 445 446 static GLuint glGetUniformBlockIndex(_glGetUniformBlockIndex f, GLuint program, const GLchar *uniformBlockName) { 447 return f(program, uniformBlockName); 448 } 449 450 static void glInvalidateFramebuffer(_glInvalidateFramebuffer f, GLenum target, GLenum attachment) { 451 // Framebuffer invalidation is just a hint and can safely be ignored. 452 if (f != NULL) { 453 f(target, 1, &attachment); 454 } 455 } 456 457 static void glBeginQuery(_glBeginQuery f, GLenum target, GLenum attachment) { 458 f(target, attachment); 459 } 460 461 static void glDeleteQueries(_glDeleteQueries f, GLsizei n, const GLuint *ids) { 462 f(n, ids); 463 } 464 465 static void glDeleteVertexArrays(_glDeleteVertexArrays f, GLsizei n, const GLuint *ids) { 466 f(n, ids); 467 } 468 469 static void glEndQuery(_glEndQuery f, GLenum target) { 470 f(target); 471 } 472 473 static const GLubyte* glGetStringi(_glGetStringi f, GLenum name, GLuint index) { 474 return f(name, index); 475 } 476 477 static void glGenQueries(_glGenQueries f, GLsizei n, GLuint *ids) { 478 f(n, ids); 479 } 480 481 static void glGenVertexArrays(_glGenVertexArrays f, GLsizei n, GLuint *ids) { 482 f(n, ids); 483 } 484 485 static void glGetProgramBinary(_glGetProgramBinary f, GLuint program, GLsizei bufsize, GLsizei *length, GLenum *binaryFormat, void *binary) { 486 f(program, bufsize, length, binaryFormat, binary); 487 } 488 489 static void glGetQueryObjectuiv(_glGetQueryObjectuiv f, GLuint id, GLenum pname, GLuint *params) { 490 f(id, pname, params); 491 } 492 493 static void glMemoryBarrier(_glMemoryBarrier f, GLbitfield barriers) { 494 f(barriers); 495 } 496 497 static void glDispatchCompute(_glDispatchCompute f, GLuint x, GLuint y, GLuint z) { 498 f(x, y, z); 499 } 500 501 static void *glMapBufferRange(_glMapBufferRange f, GLenum target, GLintptr offset, GLsizeiptr length, GLbitfield access) { 502 return f(target, offset, length, access); 503 } 504 505 static GLboolean glUnmapBuffer(_glUnmapBuffer f, GLenum target) { 506 return f(target); 507 } 508 509 static void glBindImageTexture(_glBindImageTexture f, GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format) { 510 f(unit, texture, level, layered, layer, access, format); 511 } 512 513 static void glTexStorage2D(_glTexStorage2D f, GLenum target, GLsizei levels, GLenum internalFormat, GLsizei width, GLsizei height) { 514 f(target, levels, internalFormat, width, height); 515 } 516 517 static void glBlitFramebuffer(_glBlitFramebuffer f, GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, GLbitfield mask, GLenum filter) { 518 f(srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1, mask, filter); 519 } 520 */ 521 import "C" 522 523 type Context interface{} 524 525 type Functions struct { 526 // Query caches. 527 uints [100]C.GLuint 528 ints [100]C.GLint 529 floats [100]C.GLfloat 530 531 glActiveTexture C._glActiveTexture 532 glAttachShader C._glAttachShader 533 glBindAttribLocation C._glBindAttribLocation 534 glBindBuffer C._glBindBuffer 535 glBindFramebuffer C._glBindFramebuffer 536 glBindRenderbuffer C._glBindRenderbuffer 537 glBindTexture C._glBindTexture 538 glBlendEquation C._glBlendEquation 539 glBlendFuncSeparate C._glBlendFuncSeparate 540 glBufferData C._glBufferData 541 glBufferSubData C._glBufferSubData 542 glCheckFramebufferStatus C._glCheckFramebufferStatus 543 glClear C._glClear 544 glClearColor C._glClearColor 545 glClearDepthf C._glClearDepthf 546 glCompileShader C._glCompileShader 547 glCopyTexSubImage2D C._glCopyTexSubImage2D 548 glCreateProgram C._glCreateProgram 549 glCreateShader C._glCreateShader 550 glDeleteBuffers C._glDeleteBuffers 551 glDeleteFramebuffers C._glDeleteFramebuffers 552 glDeleteProgram C._glDeleteProgram 553 glDeleteRenderbuffers C._glDeleteRenderbuffers 554 glDeleteShader C._glDeleteShader 555 glDeleteTextures C._glDeleteTextures 556 glDepthFunc C._glDepthFunc 557 glDepthMask C._glDepthMask 558 glDisable C._glDisable 559 glDisableVertexAttribArray C._glDisableVertexAttribArray 560 glDrawArrays C._glDrawArrays 561 glDrawElements C._glDrawElements 562 glEnable C._glEnable 563 glEnableVertexAttribArray C._glEnableVertexAttribArray 564 glFinish C._glFinish 565 glFlush C._glFlush 566 glFramebufferRenderbuffer C._glFramebufferRenderbuffer 567 glFramebufferTexture2D C._glFramebufferTexture2D 568 glGenBuffers C._glGenBuffers 569 glGenerateMipmap C._glGenerateMipmap 570 glGenFramebuffers C._glGenFramebuffers 571 glGenRenderbuffers C._glGenRenderbuffers 572 glGenTextures C._glGenTextures 573 glGetError C._glGetError 574 glGetFramebufferAttachmentParameteriv C._glGetFramebufferAttachmentParameteriv 575 glGetFloatv C._glGetFloatv 576 glGetIntegerv C._glGetIntegerv 577 glGetIntegeri_v C._glGetIntegeri_v 578 glGetProgramiv C._glGetProgramiv 579 glGetProgramInfoLog C._glGetProgramInfoLog 580 glGetRenderbufferParameteriv C._glGetRenderbufferParameteriv 581 glGetShaderiv C._glGetShaderiv 582 glGetShaderInfoLog C._glGetShaderInfoLog 583 glGetString C._glGetString 584 glGetUniformLocation C._glGetUniformLocation 585 glGetVertexAttribiv C._glGetVertexAttribiv 586 glGetVertexAttribPointerv C._glGetVertexAttribPointerv 587 glIsEnabled C._glIsEnabled 588 glLinkProgram C._glLinkProgram 589 glPixelStorei C._glPixelStorei 590 glReadPixels C._glReadPixels 591 glRenderbufferStorage C._glRenderbufferStorage 592 glScissor C._glScissor 593 glShaderSource C._glShaderSource 594 glTexImage2D C._glTexImage2D 595 glTexParameteri C._glTexParameteri 596 glTexSubImage2D C._glTexSubImage2D 597 glUniform1f C._glUniform1f 598 glUniform1i C._glUniform1i 599 glUniform2f C._glUniform2f 600 glUniform3f C._glUniform3f 601 glUniform4f C._glUniform4f 602 glUseProgram C._glUseProgram 603 glVertexAttribPointer C._glVertexAttribPointer 604 glViewport C._glViewport 605 glBindVertexArray C._glBindVertexArray 606 glBindBufferBase C._glBindBufferBase 607 glGetUniformBlockIndex C._glGetUniformBlockIndex 608 glUniformBlockBinding C._glUniformBlockBinding 609 glInvalidateFramebuffer C._glInvalidateFramebuffer 610 glBeginQuery C._glBeginQuery 611 glDeleteQueries C._glDeleteQueries 612 glDeleteVertexArrays C._glDeleteVertexArrays 613 glEndQuery C._glEndQuery 614 glGenQueries C._glGenQueries 615 glGenVertexArrays C._glGenVertexArrays 616 glGetProgramBinary C._glGetProgramBinary 617 glGetQueryObjectuiv C._glGetQueryObjectuiv 618 glGetStringi C._glGetStringi 619 glDispatchCompute C._glDispatchCompute 620 glMemoryBarrier C._glMemoryBarrier 621 glMapBufferRange C._glMapBufferRange 622 glUnmapBuffer C._glUnmapBuffer 623 glBindImageTexture C._glBindImageTexture 624 glTexStorage2D C._glTexStorage2D 625 glBlitFramebuffer C._glBlitFramebuffer 626 } 627 628 func NewFunctions(ctx Context, forceES bool) (*Functions, error) { 629 if ctx != nil { 630 panic("non-nil context") 631 } 632 f := new(Functions) 633 err := f.load(forceES) 634 if err != nil { 635 return nil, err 636 } 637 return f, nil 638 } 639 640 func dlsym(handle unsafe.Pointer, s string) unsafe.Pointer { 641 cs := C.CString(s) 642 defer C.free(unsafe.Pointer(cs)) 643 return C.dlsym(handle, cs) 644 } 645 646 func dlopen(lib string) unsafe.Pointer { 647 clib := C.CString(lib) 648 defer C.free(unsafe.Pointer(clib)) 649 return C.dlopen(clib, C.RTLD_NOW|C.RTLD_LOCAL) 650 } 651 652 func (f *Functions) load(forceES bool) error { 653 var ( 654 loadErr error 655 libNames []string 656 handles []unsafe.Pointer 657 ) 658 switch { 659 case runtime.GOOS == "darwin" && !forceES: 660 libNames = []string{"/System/Library/Frameworks/OpenGL.framework/OpenGL"} 661 case runtime.GOOS == "darwin" && forceES: 662 libNames = []string{"libGLESv2.dylib"} 663 case runtime.GOOS == "ios": 664 libNames = []string{"/System/Library/Frameworks/OpenGLES.framework/OpenGLES"} 665 case runtime.GOOS == "android": 666 libNames = []string{"libGLESv2.so", "libGLESv3.so"} 667 default: 668 libNames = []string{"libGLESv2.so.2", "libGLESv2.so.3.0"} 669 } 670 for _, lib := range libNames { 671 if h := dlopen(lib); h != nil { 672 handles = append(handles, h) 673 } 674 } 675 if len(handles) == 0 { 676 return fmt.Errorf("gl: no OpenGL implementation could be loaded (tried %q)", libNames) 677 } 678 load := func(s string) *[0]byte { 679 for _, h := range handles { 680 if f := dlsym(h, s); f != nil { 681 return (*[0]byte)(f) 682 } 683 } 684 return nil 685 } 686 must := func(s string) *[0]byte { 687 ptr := load(s) 688 if ptr == nil { 689 loadErr = fmt.Errorf("gl: failed to load symbol %q", s) 690 } 691 return ptr 692 } 693 // GL ES 2.0 functions. 694 f.glActiveTexture = must("glActiveTexture") 695 f.glAttachShader = must("glAttachShader") 696 f.glBindAttribLocation = must("glBindAttribLocation") 697 f.glBindBuffer = must("glBindBuffer") 698 f.glBindFramebuffer = must("glBindFramebuffer") 699 f.glBindRenderbuffer = must("glBindRenderbuffer") 700 f.glBindTexture = must("glBindTexture") 701 f.glBlendEquation = must("glBlendEquation") 702 f.glBlendFuncSeparate = must("glBlendFuncSeparate") 703 f.glBufferData = must("glBufferData") 704 f.glBufferSubData = must("glBufferSubData") 705 f.glCheckFramebufferStatus = must("glCheckFramebufferStatus") 706 f.glClear = must("glClear") 707 f.glClearColor = must("glClearColor") 708 f.glClearDepthf = must("glClearDepthf") 709 f.glCompileShader = must("glCompileShader") 710 f.glCopyTexSubImage2D = must("glCopyTexSubImage2D") 711 f.glCreateProgram = must("glCreateProgram") 712 f.glCreateShader = must("glCreateShader") 713 f.glDeleteBuffers = must("glDeleteBuffers") 714 f.glDeleteFramebuffers = must("glDeleteFramebuffers") 715 f.glDeleteProgram = must("glDeleteProgram") 716 f.glDeleteRenderbuffers = must("glDeleteRenderbuffers") 717 f.glDeleteShader = must("glDeleteShader") 718 f.glDeleteTextures = must("glDeleteTextures") 719 f.glDepthFunc = must("glDepthFunc") 720 f.glDepthMask = must("glDepthMask") 721 f.glDisable = must("glDisable") 722 f.glDisableVertexAttribArray = must("glDisableVertexAttribArray") 723 f.glDrawArrays = must("glDrawArrays") 724 f.glDrawElements = must("glDrawElements") 725 f.glEnable = must("glEnable") 726 f.glEnableVertexAttribArray = must("glEnableVertexAttribArray") 727 f.glFinish = must("glFinish") 728 f.glFlush = must("glFlush") 729 f.glFramebufferRenderbuffer = must("glFramebufferRenderbuffer") 730 f.glFramebufferTexture2D = must("glFramebufferTexture2D") 731 f.glGenBuffers = must("glGenBuffers") 732 f.glGenerateMipmap = must("glGenerateMipmap") 733 f.glGenFramebuffers = must("glGenFramebuffers") 734 f.glGenRenderbuffers = must("glGenRenderbuffers") 735 f.glGenTextures = must("glGenTextures") 736 f.glGetError = must("glGetError") 737 f.glGetFramebufferAttachmentParameteriv = must("glGetFramebufferAttachmentParameteriv") 738 f.glGetIntegerv = must("glGetIntegerv") 739 f.glGetFloatv = must("glGetFloatv") 740 f.glGetProgramiv = must("glGetProgramiv") 741 f.glGetProgramInfoLog = must("glGetProgramInfoLog") 742 f.glGetRenderbufferParameteriv = must("glGetRenderbufferParameteriv") 743 f.glGetShaderiv = must("glGetShaderiv") 744 f.glGetShaderInfoLog = must("glGetShaderInfoLog") 745 f.glGetString = must("glGetString") 746 f.glGetUniformLocation = must("glGetUniformLocation") 747 f.glGetVertexAttribiv = must("glGetVertexAttribiv") 748 f.glGetVertexAttribPointerv = must("glGetVertexAttribPointerv") 749 f.glIsEnabled = must("glIsEnabled") 750 f.glLinkProgram = must("glLinkProgram") 751 f.glPixelStorei = must("glPixelStorei") 752 f.glReadPixels = must("glReadPixels") 753 f.glRenderbufferStorage = must("glRenderbufferStorage") 754 f.glScissor = must("glScissor") 755 f.glShaderSource = must("glShaderSource") 756 f.glTexImage2D = must("glTexImage2D") 757 f.glTexParameteri = must("glTexParameteri") 758 f.glTexSubImage2D = must("glTexSubImage2D") 759 f.glUniform1f = must("glUniform1f") 760 f.glUniform1i = must("glUniform1i") 761 f.glUniform2f = must("glUniform2f") 762 f.glUniform3f = must("glUniform3f") 763 f.glUniform4f = must("glUniform4f") 764 f.glUseProgram = must("glUseProgram") 765 f.glVertexAttribPointer = must("glVertexAttribPointer") 766 f.glViewport = must("glViewport") 767 768 // Extensions and GL ES 3 functions. 769 f.glBindBufferBase = load("glBindBufferBase") 770 f.glBindVertexArray = load("glBindVertexArray") 771 f.glGetIntegeri_v = load("glGetIntegeri_v") 772 f.glGetUniformBlockIndex = load("glGetUniformBlockIndex") 773 f.glUniformBlockBinding = load("glUniformBlockBinding") 774 f.glInvalidateFramebuffer = load("glInvalidateFramebuffer") 775 f.glGetStringi = load("glGetStringi") 776 // Fall back to EXT_invalidate_framebuffer if available. 777 if f.glInvalidateFramebuffer == nil { 778 f.glInvalidateFramebuffer = load("glDiscardFramebufferEXT") 779 } 780 781 f.glBeginQuery = load("glBeginQuery") 782 if f.glBeginQuery == nil { 783 f.glBeginQuery = load("glBeginQueryEXT") 784 } 785 f.glDeleteQueries = load("glDeleteQueries") 786 if f.glDeleteQueries == nil { 787 f.glDeleteQueries = load("glDeleteQueriesEXT") 788 } 789 f.glEndQuery = load("glEndQuery") 790 if f.glEndQuery == nil { 791 f.glEndQuery = load("glEndQueryEXT") 792 } 793 f.glGenQueries = load("glGenQueries") 794 if f.glGenQueries == nil { 795 f.glGenQueries = load("glGenQueriesEXT") 796 } 797 f.glGetQueryObjectuiv = load("glGetQueryObjectuiv") 798 if f.glGetQueryObjectuiv == nil { 799 f.glGetQueryObjectuiv = load("glGetQueryObjectuivEXT") 800 } 801 802 f.glDeleteVertexArrays = load("glDeleteVertexArrays") 803 f.glGenVertexArrays = load("glGenVertexArrays") 804 f.glMemoryBarrier = load("glMemoryBarrier") 805 f.glDispatchCompute = load("glDispatchCompute") 806 f.glMapBufferRange = load("glMapBufferRange") 807 f.glUnmapBuffer = load("glUnmapBuffer") 808 f.glBindImageTexture = load("glBindImageTexture") 809 f.glTexStorage2D = load("glTexStorage2D") 810 f.glBlitFramebuffer = load("glBlitFramebuffer") 811 f.glGetProgramBinary = load("glGetProgramBinary") 812 813 return loadErr 814 } 815 816 func (f *Functions) ActiveTexture(texture Enum) { 817 C.glActiveTexture(f.glActiveTexture, C.GLenum(texture)) 818 } 819 820 func (f *Functions) AttachShader(p Program, s Shader) { 821 C.glAttachShader(f.glAttachShader, C.GLuint(p.V), C.GLuint(s.V)) 822 } 823 824 func (f *Functions) BeginQuery(target Enum, query Query) { 825 C.glBeginQuery(f.glBeginQuery, C.GLenum(target), C.GLenum(query.V)) 826 } 827 828 func (f *Functions) BindAttribLocation(p Program, a Attrib, name string) { 829 cname := C.CString(name) 830 defer C.free(unsafe.Pointer(cname)) 831 C.glBindAttribLocation(f.glBindAttribLocation, C.GLuint(p.V), C.GLuint(a), cname) 832 } 833 834 func (f *Functions) BindBufferBase(target Enum, index int, b Buffer) { 835 C.glBindBufferBase(f.glBindBufferBase, C.GLenum(target), C.GLuint(index), C.GLuint(b.V)) 836 } 837 838 func (f *Functions) BindBuffer(target Enum, b Buffer) { 839 C.glBindBuffer(f.glBindBuffer, C.GLenum(target), C.GLuint(b.V)) 840 } 841 842 func (f *Functions) BindFramebuffer(target Enum, fb Framebuffer) { 843 C.glBindFramebuffer(f.glBindFramebuffer, C.GLenum(target), C.GLuint(fb.V)) 844 } 845 846 func (f *Functions) BindRenderbuffer(target Enum, fb Renderbuffer) { 847 C.glBindRenderbuffer(f.glBindRenderbuffer, C.GLenum(target), C.GLuint(fb.V)) 848 } 849 850 func (f *Functions) BindImageTexture(unit int, t Texture, level int, layered bool, layer int, access, format Enum) { 851 l := C.GLboolean(FALSE) 852 if layered { 853 l = TRUE 854 } 855 C.glBindImageTexture(f.glBindImageTexture, C.GLuint(unit), C.GLuint(t.V), C.GLint(level), l, C.GLint(layer), C.GLenum(access), C.GLenum(format)) 856 } 857 858 func (f *Functions) BindTexture(target Enum, t Texture) { 859 C.glBindTexture(f.glBindTexture, C.GLenum(target), C.GLuint(t.V)) 860 } 861 862 func (f *Functions) BindVertexArray(a VertexArray) { 863 C.glBindVertexArray(f.glBindVertexArray, C.GLuint(a.V)) 864 } 865 866 func (f *Functions) BlendEquation(mode Enum) { 867 C.glBlendEquation(f.glBlendEquation, C.GLenum(mode)) 868 } 869 870 func (f *Functions) BlendFuncSeparate(srcRGB, dstRGB, srcA, dstA Enum) { 871 C.glBlendFuncSeparate(f.glBlendFuncSeparate, C.GLenum(srcRGB), C.GLenum(dstRGB), C.GLenum(srcA), C.GLenum(dstA)) 872 } 873 874 func (f *Functions) BlitFramebuffer(sx0, sy0, sx1, sy1, dx0, dy0, dx1, dy1 int, mask Enum, filter Enum) { 875 C.glBlitFramebuffer(f.glBlitFramebuffer, 876 C.GLint(sx0), C.GLint(sy0), C.GLint(sx1), C.GLint(sy1), 877 C.GLint(dx0), C.GLint(dy0), C.GLint(dx1), C.GLint(dy1), 878 C.GLenum(mask), C.GLenum(filter), 879 ) 880 } 881 882 func (f *Functions) BufferData(target Enum, size int, usage Enum, data []byte) { 883 var p unsafe.Pointer 884 if len(data) > 0 { 885 p = unsafe.Pointer(&data[0]) 886 } 887 C.glBufferData(f.glBufferData, C.GLenum(target), C.GLsizeiptr(size), p, C.GLenum(usage)) 888 } 889 890 func (f *Functions) BufferSubData(target Enum, offset int, src []byte) { 891 var p unsafe.Pointer 892 if len(src) > 0 { 893 p = unsafe.Pointer(&src[0]) 894 } 895 C.glBufferSubData(f.glBufferSubData, C.GLenum(target), C.GLintptr(offset), C.GLsizeiptr(len(src)), p) 896 } 897 898 func (f *Functions) CheckFramebufferStatus(target Enum) Enum { 899 return Enum(C.glCheckFramebufferStatus(f.glCheckFramebufferStatus, C.GLenum(target))) 900 } 901 902 func (f *Functions) Clear(mask Enum) { 903 C.glClear(f.glClear, C.GLbitfield(mask)) 904 } 905 906 func (f *Functions) ClearColor(red float32, green float32, blue float32, alpha float32) { 907 C.glClearColor(f.glClearColor, C.GLfloat(red), C.GLfloat(green), C.GLfloat(blue), C.GLfloat(alpha)) 908 } 909 910 func (f *Functions) ClearDepthf(d float32) { 911 C.glClearDepthf(f.glClearDepthf, C.GLfloat(d)) 912 } 913 914 func (f *Functions) CompileShader(s Shader) { 915 C.glCompileShader(f.glCompileShader, C.GLuint(s.V)) 916 } 917 918 func (f *Functions) CopyTexSubImage2D(target Enum, level, xoffset, yoffset, x, y, width, height int) { 919 C.glCopyTexSubImage2D(f.glCopyTexSubImage2D, C.GLenum(target), C.GLint(level), C.GLint(xoffset), C.GLint(yoffset), C.GLint(x), C.GLint(y), C.GLsizei(width), C.GLsizei(height)) 920 } 921 922 func (f *Functions) CreateBuffer() Buffer { 923 C.glGenBuffers(f.glGenBuffers, 1, &f.uints[0]) 924 return Buffer{uint(f.uints[0])} 925 } 926 927 func (f *Functions) CreateFramebuffer() Framebuffer { 928 C.glGenFramebuffers(f.glGenFramebuffers, 1, &f.uints[0]) 929 return Framebuffer{uint(f.uints[0])} 930 } 931 932 func (f *Functions) CreateProgram() Program { 933 return Program{uint(C.glCreateProgram(f.glCreateProgram))} 934 } 935 936 func (f *Functions) CreateQuery() Query { 937 C.glGenQueries(f.glGenQueries, 1, &f.uints[0]) 938 return Query{uint(f.uints[0])} 939 } 940 941 func (f *Functions) CreateRenderbuffer() Renderbuffer { 942 C.glGenRenderbuffers(f.glGenRenderbuffers, 1, &f.uints[0]) 943 return Renderbuffer{uint(f.uints[0])} 944 } 945 946 func (f *Functions) CreateShader(ty Enum) Shader { 947 return Shader{uint(C.glCreateShader(f.glCreateShader, C.GLenum(ty)))} 948 } 949 950 func (f *Functions) CreateTexture() Texture { 951 C.glGenTextures(f.glGenTextures, 1, &f.uints[0]) 952 return Texture{uint(f.uints[0])} 953 } 954 955 func (f *Functions) CreateVertexArray() VertexArray { 956 C.glGenVertexArrays(f.glGenVertexArrays, 1, &f.uints[0]) 957 return VertexArray{uint(f.uints[0])} 958 } 959 960 func (f *Functions) DeleteBuffer(v Buffer) { 961 f.uints[0] = C.GLuint(v.V) 962 C.glDeleteBuffers(f.glDeleteBuffers, 1, &f.uints[0]) 963 } 964 965 func (f *Functions) DeleteFramebuffer(v Framebuffer) { 966 f.uints[0] = C.GLuint(v.V) 967 C.glDeleteFramebuffers(f.glDeleteFramebuffers, 1, &f.uints[0]) 968 } 969 970 func (f *Functions) DeleteProgram(p Program) { 971 C.glDeleteProgram(f.glDeleteProgram, C.GLuint(p.V)) 972 } 973 974 func (f *Functions) DeleteQuery(query Query) { 975 f.uints[0] = C.GLuint(query.V) 976 C.glDeleteQueries(f.glDeleteQueries, 1, &f.uints[0]) 977 } 978 979 func (f *Functions) DeleteVertexArray(array VertexArray) { 980 f.uints[0] = C.GLuint(array.V) 981 C.glDeleteVertexArrays(f.glDeleteVertexArrays, 1, &f.uints[0]) 982 } 983 984 func (f *Functions) DeleteRenderbuffer(v Renderbuffer) { 985 f.uints[0] = C.GLuint(v.V) 986 C.glDeleteRenderbuffers(f.glDeleteRenderbuffers, 1, &f.uints[0]) 987 } 988 989 func (f *Functions) DeleteShader(s Shader) { 990 C.glDeleteShader(f.glDeleteShader, C.GLuint(s.V)) 991 } 992 993 func (f *Functions) DeleteTexture(v Texture) { 994 f.uints[0] = C.GLuint(v.V) 995 C.glDeleteTextures(f.glDeleteTextures, 1, &f.uints[0]) 996 } 997 998 func (f *Functions) DepthFunc(v Enum) { 999 C.glDepthFunc(f.glDepthFunc, C.GLenum(v)) 1000 } 1001 1002 func (f *Functions) DepthMask(mask bool) { 1003 m := C.GLboolean(FALSE) 1004 if mask { 1005 m = C.GLboolean(TRUE) 1006 } 1007 C.glDepthMask(f.glDepthMask, m) 1008 } 1009 1010 func (f *Functions) DisableVertexAttribArray(a Attrib) { 1011 C.glDisableVertexAttribArray(f.glDisableVertexAttribArray, C.GLuint(a)) 1012 } 1013 1014 func (f *Functions) Disable(cap Enum) { 1015 C.glDisable(f.glDisable, C.GLenum(cap)) 1016 } 1017 1018 func (f *Functions) DrawArrays(mode Enum, first int, count int) { 1019 C.glDrawArrays(f.glDrawArrays, C.GLenum(mode), C.GLint(first), C.GLsizei(count)) 1020 } 1021 1022 func (f *Functions) DrawElements(mode Enum, count int, ty Enum, offset int) { 1023 C.glDrawElements(f.glDrawElements, C.GLenum(mode), C.GLsizei(count), C.GLenum(ty), C.uintptr_t(offset)) 1024 } 1025 1026 func (f *Functions) DispatchCompute(x, y, z int) { 1027 C.glDispatchCompute(f.glDispatchCompute, C.GLuint(x), C.GLuint(y), C.GLuint(z)) 1028 } 1029 1030 func (f *Functions) Enable(cap Enum) { 1031 C.glEnable(f.glEnable, C.GLenum(cap)) 1032 } 1033 1034 func (f *Functions) EndQuery(target Enum) { 1035 C.glEndQuery(f.glEndQuery, C.GLenum(target)) 1036 } 1037 1038 func (f *Functions) EnableVertexAttribArray(a Attrib) { 1039 C.glEnableVertexAttribArray(f.glEnableVertexAttribArray, C.GLuint(a)) 1040 } 1041 1042 func (f *Functions) Finish() { 1043 C.glFinish(f.glFinish) 1044 } 1045 1046 func (f *Functions) Flush() { 1047 C.glFlush(f.glFinish) 1048 } 1049 1050 func (f *Functions) FramebufferRenderbuffer(target, attachment, renderbuffertarget Enum, renderbuffer Renderbuffer) { 1051 C.glFramebufferRenderbuffer(f.glFramebufferRenderbuffer, C.GLenum(target), C.GLenum(attachment), C.GLenum(renderbuffertarget), C.GLuint(renderbuffer.V)) 1052 } 1053 1054 func (f *Functions) FramebufferTexture2D(target, attachment, texTarget Enum, t Texture, level int) { 1055 C.glFramebufferTexture2D(f.glFramebufferTexture2D, C.GLenum(target), C.GLenum(attachment), C.GLenum(texTarget), C.GLuint(t.V), C.GLint(level)) 1056 } 1057 1058 func (f *Functions) GenerateMipmap(target Enum) { 1059 C.glGenerateMipmap(f.glGenerateMipmap, C.GLenum(target)) 1060 } 1061 1062 func (c *Functions) GetBinding(pname Enum) Object { 1063 return Object{uint(c.GetInteger(pname))} 1064 } 1065 1066 func (c *Functions) GetBindingi(pname Enum, idx int) Object { 1067 return Object{uint(c.GetIntegeri(pname, idx))} 1068 } 1069 1070 func (f *Functions) GetError() Enum { 1071 return Enum(C.glGetError(f.glGetError)) 1072 } 1073 1074 func (f *Functions) GetRenderbufferParameteri(target, pname Enum) int { 1075 C.glGetRenderbufferParameteriv(f.glGetRenderbufferParameteriv, C.GLenum(target), C.GLenum(pname), &f.ints[0]) 1076 return int(f.ints[0]) 1077 } 1078 1079 func (f *Functions) GetFramebufferAttachmentParameteri(target, attachment, pname Enum) int { 1080 C.glGetFramebufferAttachmentParameteriv(f.glGetFramebufferAttachmentParameteriv, C.GLenum(target), C.GLenum(attachment), C.GLenum(pname), &f.ints[0]) 1081 return int(f.ints[0]) 1082 } 1083 1084 func (f *Functions) GetFloat4(pname Enum) [4]float32 { 1085 C.glGetFloatv(f.glGetFloatv, C.GLenum(pname), &f.floats[0]) 1086 var r [4]float32 1087 for i := range r { 1088 r[i] = float32(f.floats[i]) 1089 } 1090 return r 1091 } 1092 1093 func (f *Functions) GetFloat(pname Enum) float32 { 1094 C.glGetFloatv(f.glGetFloatv, C.GLenum(pname), &f.floats[0]) 1095 return float32(f.floats[0]) 1096 } 1097 1098 func (f *Functions) GetInteger4(pname Enum) [4]int { 1099 C.glGetIntegerv(f.glGetIntegerv, C.GLenum(pname), &f.ints[0]) 1100 var r [4]int 1101 for i := range r { 1102 r[i] = int(f.ints[i]) 1103 } 1104 return r 1105 } 1106 1107 func (f *Functions) GetInteger(pname Enum) int { 1108 C.glGetIntegerv(f.glGetIntegerv, C.GLenum(pname), &f.ints[0]) 1109 return int(f.ints[0]) 1110 } 1111 1112 func (f *Functions) GetIntegeri(pname Enum, idx int) int { 1113 C.glGetIntegeri_v(f.glGetIntegeri_v, C.GLenum(pname), C.GLuint(idx), &f.ints[0]) 1114 return int(f.ints[0]) 1115 } 1116 1117 func (f *Functions) GetProgrami(p Program, pname Enum) int { 1118 C.glGetProgramiv(f.glGetProgramiv, C.GLuint(p.V), C.GLenum(pname), &f.ints[0]) 1119 return int(f.ints[0]) 1120 } 1121 1122 func (f *Functions) GetProgramBinary(p Program) []byte { 1123 sz := f.GetProgrami(p, PROGRAM_BINARY_LENGTH) 1124 if sz == 0 { 1125 return nil 1126 } 1127 buf := make([]byte, sz) 1128 var format C.GLenum 1129 C.glGetProgramBinary(f.glGetProgramBinary, C.GLuint(p.V), C.GLsizei(sz), nil, &format, unsafe.Pointer(&buf[0])) 1130 return buf 1131 } 1132 1133 func (f *Functions) GetProgramInfoLog(p Program) string { 1134 n := f.GetProgrami(p, INFO_LOG_LENGTH) 1135 buf := make([]byte, n) 1136 C.glGetProgramInfoLog(f.glGetProgramInfoLog, C.GLuint(p.V), C.GLsizei(len(buf)), nil, (*C.GLchar)(unsafe.Pointer(&buf[0]))) 1137 return string(buf) 1138 } 1139 1140 func (f *Functions) GetQueryObjectuiv(query Query, pname Enum) uint { 1141 C.glGetQueryObjectuiv(f.glGetQueryObjectuiv, C.GLuint(query.V), C.GLenum(pname), &f.uints[0]) 1142 return uint(f.uints[0]) 1143 } 1144 1145 func (f *Functions) GetShaderi(s Shader, pname Enum) int { 1146 C.glGetShaderiv(f.glGetShaderiv, C.GLuint(s.V), C.GLenum(pname), &f.ints[0]) 1147 return int(f.ints[0]) 1148 } 1149 1150 func (f *Functions) GetShaderInfoLog(s Shader) string { 1151 n := f.GetShaderi(s, INFO_LOG_LENGTH) 1152 buf := make([]byte, n) 1153 C.glGetShaderInfoLog(f.glGetShaderInfoLog, C.GLuint(s.V), C.GLsizei(len(buf)), nil, (*C.GLchar)(unsafe.Pointer(&buf[0]))) 1154 return string(buf) 1155 } 1156 1157 func (f *Functions) getStringi(pname Enum, index int) string { 1158 str := C.glGetStringi(f.glGetStringi, C.GLenum(pname), C.GLuint(index)) 1159 if str == nil { 1160 return "" 1161 } 1162 return C.GoString((*C.char)(unsafe.Pointer(str))) 1163 } 1164 1165 func (f *Functions) GetString(pname Enum) string { 1166 switch { 1167 case runtime.GOOS == "darwin" && pname == EXTENSIONS: 1168 // macOS OpenGL 3 core profile doesn't support glGetString(GL_EXTENSIONS). 1169 // Use glGetStringi(GL_EXTENSIONS, <index>). 1170 var exts []string 1171 nexts := f.GetInteger(NUM_EXTENSIONS) 1172 for i := 0; i < nexts; i++ { 1173 ext := f.getStringi(EXTENSIONS, i) 1174 exts = append(exts, ext) 1175 } 1176 return strings.Join(exts, " ") 1177 default: 1178 str := C.glGetString(f.glGetString, C.GLenum(pname)) 1179 return C.GoString((*C.char)(unsafe.Pointer(str))) 1180 } 1181 } 1182 1183 func (f *Functions) GetUniformBlockIndex(p Program, name string) uint { 1184 cname := C.CString(name) 1185 defer C.free(unsafe.Pointer(cname)) 1186 return uint(C.glGetUniformBlockIndex(f.glGetUniformBlockIndex, C.GLuint(p.V), cname)) 1187 } 1188 1189 func (f *Functions) GetUniformLocation(p Program, name string) Uniform { 1190 cname := C.CString(name) 1191 defer C.free(unsafe.Pointer(cname)) 1192 return Uniform{int(C.glGetUniformLocation(f.glGetUniformLocation, C.GLuint(p.V), cname))} 1193 } 1194 1195 func (f *Functions) GetVertexAttrib(index int, pname Enum) int { 1196 C.glGetVertexAttribiv(f.glGetVertexAttribiv, C.GLuint(index), C.GLenum(pname), &f.ints[0]) 1197 return int(f.ints[0]) 1198 } 1199 1200 func (f *Functions) GetVertexAttribBinding(index int, pname Enum) Object { 1201 return Object{uint(f.GetVertexAttrib(index, pname))} 1202 } 1203 1204 func (f *Functions) GetVertexAttribPointer(index int, pname Enum) uintptr { 1205 ptr := C.glGetVertexAttribPointerv(f.glGetVertexAttribPointerv, C.GLuint(index), C.GLenum(pname)) 1206 return uintptr(ptr) 1207 } 1208 1209 func (f *Functions) InvalidateFramebuffer(target, attachment Enum) { 1210 C.glInvalidateFramebuffer(f.glInvalidateFramebuffer, C.GLenum(target), C.GLenum(attachment)) 1211 } 1212 1213 func (f *Functions) IsEnabled(cap Enum) bool { 1214 return C.glIsEnabled(f.glIsEnabled, C.GLenum(cap)) == TRUE 1215 } 1216 1217 func (f *Functions) LinkProgram(p Program) { 1218 C.glLinkProgram(f.glLinkProgram, C.GLuint(p.V)) 1219 } 1220 1221 func (f *Functions) PixelStorei(pname Enum, param int) { 1222 C.glPixelStorei(f.glPixelStorei, C.GLenum(pname), C.GLint(param)) 1223 } 1224 1225 func (f *Functions) MemoryBarrier(barriers Enum) { 1226 C.glMemoryBarrier(f.glMemoryBarrier, C.GLbitfield(barriers)) 1227 } 1228 1229 func (f *Functions) MapBufferRange(target Enum, offset, length int, access Enum) []byte { 1230 p := C.glMapBufferRange(f.glMapBufferRange, C.GLenum(target), C.GLintptr(offset), C.GLsizeiptr(length), C.GLbitfield(access)) 1231 if p == nil { 1232 return nil 1233 } 1234 return (*[1 << 30]byte)(p)[:length:length] 1235 } 1236 1237 func (f *Functions) Scissor(x, y, width, height int32) { 1238 C.glScissor(f.glScissor, C.GLint(x), C.GLint(y), C.GLsizei(width), C.GLsizei(height)) 1239 } 1240 1241 func (f *Functions) ReadPixels(x, y, width, height int, format, ty Enum, data []byte) { 1242 var p unsafe.Pointer 1243 if len(data) > 0 { 1244 p = unsafe.Pointer(&data[0]) 1245 } 1246 C.glReadPixels(f.glReadPixels, C.GLint(x), C.GLint(y), C.GLsizei(width), C.GLsizei(height), C.GLenum(format), C.GLenum(ty), p) 1247 } 1248 1249 func (f *Functions) RenderbufferStorage(target, internalformat Enum, width, height int) { 1250 C.glRenderbufferStorage(f.glRenderbufferStorage, C.GLenum(target), C.GLenum(internalformat), C.GLsizei(width), C.GLsizei(height)) 1251 } 1252 1253 func (f *Functions) ShaderSource(s Shader, src string) { 1254 csrc := C.CString(src) 1255 defer C.free(unsafe.Pointer(csrc)) 1256 strlen := C.GLint(len(src)) 1257 C.glShaderSource(f.glShaderSource, C.GLuint(s.V), 1, &csrc, &strlen) 1258 } 1259 1260 func (f *Functions) TexImage2D(target Enum, level int, internalFormat Enum, width int, height int, format Enum, ty Enum) { 1261 C.glTexImage2D(f.glTexImage2D, C.GLenum(target), C.GLint(level), C.GLint(internalFormat), C.GLsizei(width), C.GLsizei(height), 0, C.GLenum(format), C.GLenum(ty), nil) 1262 } 1263 1264 func (f *Functions) TexStorage2D(target Enum, levels int, internalFormat Enum, width, height int) { 1265 C.glTexStorage2D(f.glTexStorage2D, C.GLenum(target), C.GLsizei(levels), C.GLenum(internalFormat), C.GLsizei(width), C.GLsizei(height)) 1266 } 1267 1268 func (f *Functions) TexSubImage2D(target Enum, level int, x int, y int, width int, height int, format Enum, ty Enum, data []byte) { 1269 var p unsafe.Pointer 1270 if len(data) > 0 { 1271 p = unsafe.Pointer(&data[0]) 1272 } 1273 C.glTexSubImage2D(f.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) 1274 } 1275 1276 func (f *Functions) TexParameteri(target, pname Enum, param int) { 1277 C.glTexParameteri(f.glTexParameteri, C.GLenum(target), C.GLenum(pname), C.GLint(param)) 1278 } 1279 1280 func (f *Functions) UniformBlockBinding(p Program, uniformBlockIndex uint, uniformBlockBinding uint) { 1281 C.glUniformBlockBinding(f.glUniformBlockBinding, C.GLuint(p.V), C.GLuint(uniformBlockIndex), C.GLuint(uniformBlockBinding)) 1282 } 1283 1284 func (f *Functions) Uniform1f(dst Uniform, v float32) { 1285 C.glUniform1f(f.glUniform1f, C.GLint(dst.V), C.GLfloat(v)) 1286 } 1287 1288 func (f *Functions) Uniform1i(dst Uniform, v int) { 1289 C.glUniform1i(f.glUniform1i, C.GLint(dst.V), C.GLint(v)) 1290 } 1291 1292 func (f *Functions) Uniform2f(dst Uniform, v0 float32, v1 float32) { 1293 C.glUniform2f(f.glUniform2f, C.GLint(dst.V), C.GLfloat(v0), C.GLfloat(v1)) 1294 } 1295 1296 func (f *Functions) Uniform3f(dst Uniform, v0 float32, v1 float32, v2 float32) { 1297 C.glUniform3f(f.glUniform3f, C.GLint(dst.V), C.GLfloat(v0), C.GLfloat(v1), C.GLfloat(v2)) 1298 } 1299 1300 func (f *Functions) Uniform4f(dst Uniform, v0 float32, v1 float32, v2 float32, v3 float32) { 1301 C.glUniform4f(f.glUniform4f, C.GLint(dst.V), C.GLfloat(v0), C.GLfloat(v1), C.GLfloat(v2), C.GLfloat(v3)) 1302 } 1303 1304 func (f *Functions) UseProgram(p Program) { 1305 C.glUseProgram(f.glUseProgram, C.GLuint(p.V)) 1306 } 1307 1308 func (f *Functions) UnmapBuffer(target Enum) bool { 1309 r := C.glUnmapBuffer(f.glUnmapBuffer, C.GLenum(target)) 1310 return r == TRUE 1311 } 1312 1313 func (f *Functions) VertexAttribPointer(dst Attrib, size int, ty Enum, normalized bool, stride int, offset int) { 1314 var n C.GLboolean = FALSE 1315 if normalized { 1316 n = TRUE 1317 } 1318 C.glVertexAttribPointer(f.glVertexAttribPointer, C.GLuint(dst), C.GLint(size), C.GLenum(ty), n, C.GLsizei(stride), C.uintptr_t(offset)) 1319 } 1320 1321 func (f *Functions) Viewport(x int, y int, width int, height int) { 1322 C.glViewport(f.glViewport, C.GLint(x), C.GLint(y), C.GLsizei(width), C.GLsizei(height)) 1323 }