github.com/gop9/olt@v0.0.0-20200202132135-d956aad50b08/gio/app/internal/gl/functions.go (about)

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