github.com/Seikaijyu/gio@v0.0.1/internal/gl/gl_js.go (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  package gl
     4  
     5  import (
     6  	"errors"
     7  	"strings"
     8  	"syscall/js"
     9  )
    10  
    11  type Functions struct {
    12  	Ctx                             js.Value
    13  	EXT_disjoint_timer_query        js.Value
    14  	EXT_disjoint_timer_query_webgl2 js.Value
    15  
    16  	// Cached reference to the Uint8Array JS type.
    17  	uint8Array js.Value
    18  
    19  	// Cached JS arrays.
    20  	arrayBuf js.Value
    21  	int32Buf js.Value
    22  
    23  	isWebGL2 bool
    24  
    25  	_getExtension                      js.Value
    26  	_activeTexture                     js.Value
    27  	_attachShader                      js.Value
    28  	_beginQuery                        js.Value
    29  	_beginQueryEXT                     js.Value
    30  	_bindAttribLocation                js.Value
    31  	_bindBuffer                        js.Value
    32  	_bindBufferBase                    js.Value
    33  	_bindFramebuffer                   js.Value
    34  	_bindRenderbuffer                  js.Value
    35  	_bindTexture                       js.Value
    36  	_blendEquation                     js.Value
    37  	_blendFunc                         js.Value
    38  	_bufferData                        js.Value
    39  	_bufferSubData                     js.Value
    40  	_checkFramebufferStatus            js.Value
    41  	_clear                             js.Value
    42  	_clearColor                        js.Value
    43  	_clearDepth                        js.Value
    44  	_compileShader                     js.Value
    45  	_copyTexSubImage2D                 js.Value
    46  	_createBuffer                      js.Value
    47  	_createFramebuffer                 js.Value
    48  	_createProgram                     js.Value
    49  	_createQuery                       js.Value
    50  	_createRenderbuffer                js.Value
    51  	_createShader                      js.Value
    52  	_createTexture                     js.Value
    53  	_deleteBuffer                      js.Value
    54  	_deleteFramebuffer                 js.Value
    55  	_deleteProgram                     js.Value
    56  	_deleteQuery                       js.Value
    57  	_deleteQueryEXT                    js.Value
    58  	_deleteShader                      js.Value
    59  	_deleteRenderbuffer                js.Value
    60  	_deleteTexture                     js.Value
    61  	_depthFunc                         js.Value
    62  	_depthMask                         js.Value
    63  	_disableVertexAttribArray          js.Value
    64  	_disable                           js.Value
    65  	_drawArrays                        js.Value
    66  	_drawElements                      js.Value
    67  	_enable                            js.Value
    68  	_enableVertexAttribArray           js.Value
    69  	_endQuery                          js.Value
    70  	_endQueryEXT                       js.Value
    71  	_finish                            js.Value
    72  	_flush                             js.Value
    73  	_framebufferRenderbuffer           js.Value
    74  	_framebufferTexture2D              js.Value
    75  	_generateMipmap                    js.Value
    76  	_getRenderbufferParameteri         js.Value
    77  	_getFramebufferAttachmentParameter js.Value
    78  	_getParameter                      js.Value
    79  	_getIndexedParameter               js.Value
    80  	_getProgramParameter               js.Value
    81  	_getProgramInfoLog                 js.Value
    82  	_getQueryParameter                 js.Value
    83  	_getQueryObjectEXT                 js.Value
    84  	_getShaderParameter                js.Value
    85  	_getShaderInfoLog                  js.Value
    86  	_getSupportedExtensions            js.Value
    87  	_getUniformBlockIndex              js.Value
    88  	_getUniformLocation                js.Value
    89  	_getVertexAttrib                   js.Value
    90  	_getVertexAttribOffset             js.Value
    91  	_invalidateFramebuffer             js.Value
    92  	_isEnabled                         js.Value
    93  	_linkProgram                       js.Value
    94  	_pixelStorei                       js.Value
    95  	_renderbufferStorage               js.Value
    96  	_readPixels                        js.Value
    97  	_scissor                           js.Value
    98  	_shaderSource                      js.Value
    99  	_texImage2D                        js.Value
   100  	_texStorage2D                      js.Value
   101  	_texSubImage2D                     js.Value
   102  	_texParameteri                     js.Value
   103  	_uniformBlockBinding               js.Value
   104  	_uniform1f                         js.Value
   105  	_uniform1i                         js.Value
   106  	_uniform2f                         js.Value
   107  	_uniform3f                         js.Value
   108  	_uniform4f                         js.Value
   109  	_useProgram                        js.Value
   110  	_vertexAttribPointer               js.Value
   111  	_viewport                          js.Value
   112  }
   113  
   114  type Context js.Value
   115  
   116  func NewFunctions(ctx Context, forceES bool) (*Functions, error) {
   117  	webgl := js.Value(ctx)
   118  	f := &Functions{
   119  		Ctx:                                webgl,
   120  		uint8Array:                         js.Global().Get("Uint8Array"),
   121  		_getExtension:                      _bind(webgl, `getExtension`),
   122  		_activeTexture:                     _bind(webgl, `activeTexture`),
   123  		_attachShader:                      _bind(webgl, `attachShader`),
   124  		_beginQuery:                        _bind(webgl, `beginQuery`),
   125  		_beginQueryEXT:                     _bind(webgl, `beginQueryEXT`),
   126  		_bindAttribLocation:                _bind(webgl, `bindAttribLocation`),
   127  		_bindBuffer:                        _bind(webgl, `bindBuffer`),
   128  		_bindBufferBase:                    _bind(webgl, `bindBufferBase`),
   129  		_bindFramebuffer:                   _bind(webgl, `bindFramebuffer`),
   130  		_bindRenderbuffer:                  _bind(webgl, `bindRenderbuffer`),
   131  		_bindTexture:                       _bind(webgl, `bindTexture`),
   132  		_blendEquation:                     _bind(webgl, `blendEquation`),
   133  		_blendFunc:                         _bind(webgl, `blendFunc`),
   134  		_bufferData:                        _bind(webgl, `bufferData`),
   135  		_bufferSubData:                     _bind(webgl, `bufferSubData`),
   136  		_checkFramebufferStatus:            _bind(webgl, `checkFramebufferStatus`),
   137  		_clear:                             _bind(webgl, `clear`),
   138  		_clearColor:                        _bind(webgl, `clearColor`),
   139  		_clearDepth:                        _bind(webgl, `clearDepth`),
   140  		_compileShader:                     _bind(webgl, `compileShader`),
   141  		_copyTexSubImage2D:                 _bind(webgl, `copyTexSubImage2D`),
   142  		_createBuffer:                      _bind(webgl, `createBuffer`),
   143  		_createFramebuffer:                 _bind(webgl, `createFramebuffer`),
   144  		_createProgram:                     _bind(webgl, `createProgram`),
   145  		_createQuery:                       _bind(webgl, `createQuery`),
   146  		_createRenderbuffer:                _bind(webgl, `createRenderbuffer`),
   147  		_createShader:                      _bind(webgl, `createShader`),
   148  		_createTexture:                     _bind(webgl, `createTexture`),
   149  		_deleteBuffer:                      _bind(webgl, `deleteBuffer`),
   150  		_deleteFramebuffer:                 _bind(webgl, `deleteFramebuffer`),
   151  		_deleteProgram:                     _bind(webgl, `deleteProgram`),
   152  		_deleteQuery:                       _bind(webgl, `deleteQuery`),
   153  		_deleteQueryEXT:                    _bind(webgl, `deleteQueryEXT`),
   154  		_deleteShader:                      _bind(webgl, `deleteShader`),
   155  		_deleteRenderbuffer:                _bind(webgl, `deleteRenderbuffer`),
   156  		_deleteTexture:                     _bind(webgl, `deleteTexture`),
   157  		_depthFunc:                         _bind(webgl, `depthFunc`),
   158  		_depthMask:                         _bind(webgl, `depthMask`),
   159  		_disableVertexAttribArray:          _bind(webgl, `disableVertexAttribArray`),
   160  		_disable:                           _bind(webgl, `disable`),
   161  		_drawArrays:                        _bind(webgl, `drawArrays`),
   162  		_drawElements:                      _bind(webgl, `drawElements`),
   163  		_enable:                            _bind(webgl, `enable`),
   164  		_enableVertexAttribArray:           _bind(webgl, `enableVertexAttribArray`),
   165  		_endQuery:                          _bind(webgl, `endQuery`),
   166  		_endQueryEXT:                       _bind(webgl, `endQueryEXT`),
   167  		_finish:                            _bind(webgl, `finish`),
   168  		_flush:                             _bind(webgl, `flush`),
   169  		_framebufferRenderbuffer:           _bind(webgl, `framebufferRenderbuffer`),
   170  		_framebufferTexture2D:              _bind(webgl, `framebufferTexture2D`),
   171  		_generateMipmap:                    _bind(webgl, `generateMipmap`),
   172  		_getRenderbufferParameteri:         _bind(webgl, `getRenderbufferParameteri`),
   173  		_getFramebufferAttachmentParameter: _bind(webgl, `getFramebufferAttachmentParameter`),
   174  		_getParameter:                      _bind(webgl, `getParameter`),
   175  		_getIndexedParameter:               _bind(webgl, `getIndexedParameter`),
   176  		_getProgramParameter:               _bind(webgl, `getProgramParameter`),
   177  		_getProgramInfoLog:                 _bind(webgl, `getProgramInfoLog`),
   178  		_getQueryParameter:                 _bind(webgl, `getQueryParameter`),
   179  		_getQueryObjectEXT:                 _bind(webgl, `getQueryObjectEXT`),
   180  		_getShaderParameter:                _bind(webgl, `getShaderParameter`),
   181  		_getShaderInfoLog:                  _bind(webgl, `getShaderInfoLog`),
   182  		_getSupportedExtensions:            _bind(webgl, `getSupportedExtensions`),
   183  		_getUniformBlockIndex:              _bind(webgl, `getUniformBlockIndex`),
   184  		_getUniformLocation:                _bind(webgl, `getUniformLocation`),
   185  		_getVertexAttrib:                   _bind(webgl, `getVertexAttrib`),
   186  		_getVertexAttribOffset:             _bind(webgl, `getVertexAttribOffset`),
   187  		_invalidateFramebuffer:             _bind(webgl, `invalidateFramebuffer`),
   188  		_isEnabled:                         _bind(webgl, `isEnabled`),
   189  		_linkProgram:                       _bind(webgl, `linkProgram`),
   190  		_pixelStorei:                       _bind(webgl, `pixelStorei`),
   191  		_renderbufferStorage:               _bind(webgl, `renderbufferStorage`),
   192  		_readPixels:                        _bind(webgl, `readPixels`),
   193  		_scissor:                           _bind(webgl, `scissor`),
   194  		_shaderSource:                      _bind(webgl, `shaderSource`),
   195  		_texImage2D:                        _bind(webgl, `texImage2D`),
   196  		_texStorage2D:                      _bind(webgl, `texStorage2D`),
   197  		_texSubImage2D:                     _bind(webgl, `texSubImage2D`),
   198  		_texParameteri:                     _bind(webgl, `texParameteri`),
   199  		_uniformBlockBinding:               _bind(webgl, `uniformBlockBinding`),
   200  		_uniform1f:                         _bind(webgl, `uniform1f`),
   201  		_uniform1i:                         _bind(webgl, `uniform1i`),
   202  		_uniform2f:                         _bind(webgl, `uniform2f`),
   203  		_uniform3f:                         _bind(webgl, `uniform3f`),
   204  		_uniform4f:                         _bind(webgl, `uniform4f`),
   205  		_useProgram:                        _bind(webgl, `useProgram`),
   206  		_vertexAttribPointer:               _bind(webgl, `vertexAttribPointer`),
   207  		_viewport:                          _bind(webgl, `viewport`),
   208  	}
   209  	if err := f.Init(); err != nil {
   210  		return nil, err
   211  	}
   212  	return f, nil
   213  }
   214  
   215  func _bind(ctx js.Value, p string) js.Value {
   216  	if o := ctx.Get(p); o.Truthy() {
   217  		return o.Call("bind", ctx)
   218  	}
   219  	return js.Undefined()
   220  }
   221  
   222  func (f *Functions) Init() error {
   223  	webgl2Class := js.Global().Get("WebGL2RenderingContext")
   224  	f.isWebGL2 = !webgl2Class.IsUndefined() && f.Ctx.InstanceOf(webgl2Class)
   225  	if !f.isWebGL2 {
   226  		f.EXT_disjoint_timer_query = f.getExtension("EXT_disjoint_timer_query")
   227  		if f.getExtension("OES_texture_half_float").IsNull() && f.getExtension("OES_texture_float").IsNull() {
   228  			return errors.New("gl: no support for neither OES_texture_half_float nor OES_texture_float")
   229  		}
   230  		if f.getExtension("EXT_sRGB").IsNull() {
   231  			return errors.New("gl: EXT_sRGB not supported")
   232  		}
   233  	} else {
   234  		// WebGL2 extensions.
   235  		f.EXT_disjoint_timer_query_webgl2 = f.getExtension("EXT_disjoint_timer_query_webgl2")
   236  		if f.getExtension("EXT_color_buffer_half_float").IsNull() && f.getExtension("EXT_color_buffer_float").IsNull() {
   237  			return errors.New("gl: no support for neither EXT_color_buffer_half_float nor EXT_color_buffer_float")
   238  		}
   239  	}
   240  	return nil
   241  }
   242  
   243  func (f *Functions) getExtension(name string) js.Value {
   244  	return f._getExtension.Invoke(name)
   245  }
   246  
   247  func (f *Functions) ActiveTexture(t Enum) {
   248  	f._activeTexture.Invoke(int(t))
   249  }
   250  func (f *Functions) AttachShader(p Program, s Shader) {
   251  	f._attachShader.Invoke(js.Value(p), js.Value(s))
   252  }
   253  func (f *Functions) BeginQuery(target Enum, query Query) {
   254  	if !f.EXT_disjoint_timer_query_webgl2.IsNull() {
   255  		f._beginQuery.Invoke(int(target), js.Value(query))
   256  	} else {
   257  		f.EXT_disjoint_timer_query.Call("beginQueryEXT", int(target), js.Value(query))
   258  	}
   259  }
   260  func (f *Functions) BindAttribLocation(p Program, a Attrib, name string) {
   261  	f._bindAttribLocation.Invoke(js.Value(p), int(a), name)
   262  }
   263  func (f *Functions) BindBuffer(target Enum, b Buffer) {
   264  	f._bindBuffer.Invoke(int(target), js.Value(b))
   265  }
   266  func (f *Functions) BindBufferBase(target Enum, index int, b Buffer) {
   267  	f._bindBufferBase.Invoke(int(target), index, js.Value(b))
   268  }
   269  func (f *Functions) BindFramebuffer(target Enum, fb Framebuffer) {
   270  	f._bindFramebuffer.Invoke(int(target), js.Value(fb))
   271  }
   272  func (f *Functions) BindRenderbuffer(target Enum, rb Renderbuffer) {
   273  	f._bindRenderbuffer.Invoke(int(target), js.Value(rb))
   274  }
   275  func (f *Functions) BindTexture(target Enum, t Texture) {
   276  	f._bindTexture.Invoke(int(target), js.Value(t))
   277  }
   278  func (f *Functions) BindImageTexture(unit int, t Texture, level int, layered bool, layer int, access, format Enum) {
   279  	panic("not implemented")
   280  }
   281  func (f *Functions) BindVertexArray(a VertexArray) {
   282  	panic("not supported")
   283  }
   284  func (f *Functions) BlendEquation(mode Enum) {
   285  	f._blendEquation.Invoke(int(mode))
   286  }
   287  func (f *Functions) BlendFuncSeparate(srcRGB, dstRGB, srcA, dstA Enum) {
   288  	f._blendFunc.Invoke(int(srcRGB), int(dstRGB), int(srcA), int(dstA))
   289  }
   290  func (f *Functions) BufferData(target Enum, size int, usage Enum, data []byte) {
   291  	if data == nil {
   292  		f._bufferData.Invoke(int(target), size, int(usage))
   293  	} else {
   294  		if len(data) != size {
   295  			panic("size mismatch")
   296  		}
   297  		f._bufferData.Invoke(int(target), f.byteArrayOf(data), int(usage))
   298  	}
   299  }
   300  func (f *Functions) BufferSubData(target Enum, offset int, src []byte) {
   301  	f._bufferSubData.Invoke(int(target), offset, f.byteArrayOf(src))
   302  }
   303  func (f *Functions) CheckFramebufferStatus(target Enum) Enum {
   304  	status := Enum(f._checkFramebufferStatus.Invoke(int(target)).Int())
   305  	if status != FRAMEBUFFER_COMPLETE && f.Ctx.Call("isContextLost").Bool() {
   306  		// If the context is lost, we say that everything is fine. That saves internal/opengl/opengl.go from panic.
   307  		return FRAMEBUFFER_COMPLETE
   308  	}
   309  	return status
   310  }
   311  func (f *Functions) Clear(mask Enum) {
   312  	f._clear.Invoke(int(mask))
   313  }
   314  func (f *Functions) ClearColor(red, green, blue, alpha float32) {
   315  	f._clearColor.Invoke(red, green, blue, alpha)
   316  }
   317  func (f *Functions) ClearDepthf(d float32) {
   318  	f._clearDepth.Invoke(d)
   319  }
   320  func (f *Functions) CompileShader(s Shader) {
   321  	f._compileShader.Invoke(js.Value(s))
   322  }
   323  func (f *Functions) CopyTexSubImage2D(target Enum, level, xoffset, yoffset, x, y, width, height int) {
   324  	f._copyTexSubImage2D.Invoke(int(target), level, xoffset, yoffset, x, y, width, height)
   325  }
   326  func (f *Functions) CreateBuffer() Buffer {
   327  	return Buffer(f._createBuffer.Invoke())
   328  }
   329  func (f *Functions) CreateFramebuffer() Framebuffer {
   330  	return Framebuffer(f._createFramebuffer.Invoke())
   331  }
   332  func (f *Functions) CreateProgram() Program {
   333  	return Program(f._createProgram.Invoke())
   334  }
   335  func (f *Functions) CreateQuery() Query {
   336  	return Query(f._createQuery.Invoke())
   337  }
   338  func (f *Functions) CreateRenderbuffer() Renderbuffer {
   339  	return Renderbuffer(f._createRenderbuffer.Invoke())
   340  }
   341  func (f *Functions) CreateShader(ty Enum) Shader {
   342  	return Shader(f._createShader.Invoke(int(ty)))
   343  }
   344  func (f *Functions) CreateTexture() Texture {
   345  	return Texture(f._createTexture.Invoke())
   346  }
   347  func (f *Functions) CreateVertexArray() VertexArray {
   348  	panic("not supported")
   349  }
   350  func (f *Functions) DeleteBuffer(v Buffer) {
   351  	f._deleteBuffer.Invoke(js.Value(v))
   352  }
   353  func (f *Functions) DeleteFramebuffer(v Framebuffer) {
   354  	f._deleteFramebuffer.Invoke(js.Value(v))
   355  }
   356  func (f *Functions) DeleteProgram(p Program) {
   357  	f._deleteProgram.Invoke(js.Value(p))
   358  }
   359  func (f *Functions) DeleteQuery(query Query) {
   360  	if !f.EXT_disjoint_timer_query_webgl2.IsNull() {
   361  		f._deleteQuery.Invoke(js.Value(query))
   362  	} else {
   363  		f.EXT_disjoint_timer_query.Call("deleteQueryEXT", js.Value(query))
   364  	}
   365  }
   366  func (f *Functions) DeleteShader(s Shader) {
   367  	f._deleteShader.Invoke(js.Value(s))
   368  }
   369  func (f *Functions) DeleteRenderbuffer(v Renderbuffer) {
   370  	f._deleteRenderbuffer.Invoke(js.Value(v))
   371  }
   372  func (f *Functions) DeleteTexture(v Texture) {
   373  	f._deleteTexture.Invoke(js.Value(v))
   374  }
   375  func (f *Functions) DeleteVertexArray(a VertexArray) {
   376  	panic("not implemented")
   377  }
   378  func (f *Functions) DepthFunc(fn Enum) {
   379  	f._depthFunc.Invoke(int(fn))
   380  }
   381  func (f *Functions) DepthMask(mask bool) {
   382  	f._depthMask.Invoke(mask)
   383  }
   384  func (f *Functions) DisableVertexAttribArray(a Attrib) {
   385  	f._disableVertexAttribArray.Invoke(int(a))
   386  }
   387  func (f *Functions) Disable(cap Enum) {
   388  	f._disable.Invoke(int(cap))
   389  }
   390  func (f *Functions) DrawArrays(mode Enum, first, count int) {
   391  	f._drawArrays.Invoke(int(mode), first, count)
   392  }
   393  func (f *Functions) DrawElements(mode Enum, count int, ty Enum, offset int) {
   394  	f._drawElements.Invoke(int(mode), count, int(ty), offset)
   395  }
   396  func (f *Functions) DispatchCompute(x, y, z int) {
   397  	panic("not implemented")
   398  }
   399  func (f *Functions) Enable(cap Enum) {
   400  	f._enable.Invoke(int(cap))
   401  }
   402  func (f *Functions) EnableVertexAttribArray(a Attrib) {
   403  	f._enableVertexAttribArray.Invoke(int(a))
   404  }
   405  func (f *Functions) EndQuery(target Enum) {
   406  	if !f.EXT_disjoint_timer_query_webgl2.IsNull() {
   407  		f._endQuery.Invoke(int(target))
   408  	} else {
   409  		f.EXT_disjoint_timer_query.Call("endQueryEXT", int(target))
   410  	}
   411  }
   412  func (f *Functions) Finish() {
   413  	f._finish.Invoke()
   414  }
   415  func (f *Functions) Flush() {
   416  	f._flush.Invoke()
   417  }
   418  func (f *Functions) FramebufferRenderbuffer(target, attachment, renderbuffertarget Enum, renderbuffer Renderbuffer) {
   419  	f._framebufferRenderbuffer.Invoke(int(target), int(attachment), int(renderbuffertarget), js.Value(renderbuffer))
   420  }
   421  func (f *Functions) FramebufferTexture2D(target, attachment, texTarget Enum, t Texture, level int) {
   422  	f._framebufferTexture2D.Invoke(int(target), int(attachment), int(texTarget), js.Value(t), level)
   423  }
   424  func (f *Functions) GenerateMipmap(target Enum) {
   425  	f._generateMipmap.Invoke(int(target))
   426  }
   427  func (f *Functions) GetError() Enum {
   428  	// Avoid slow getError calls. See gio#179.
   429  	return 0
   430  }
   431  func (f *Functions) GetRenderbufferParameteri(target, pname Enum) int {
   432  	return paramVal(f._getRenderbufferParameteri.Invoke(int(pname)))
   433  }
   434  func (f *Functions) GetFramebufferAttachmentParameteri(target, attachment, pname Enum) int {
   435  	if !f.isWebGL2 && pname == FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING {
   436  		// FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING is only available on WebGL 2
   437  		return LINEAR
   438  	}
   439  	return paramVal(f._getFramebufferAttachmentParameter.Invoke(int(target), int(attachment), int(pname)))
   440  }
   441  func (f *Functions) GetBinding(pname Enum) Object {
   442  	obj := f._getParameter.Invoke(int(pname))
   443  	if !obj.Truthy() {
   444  		return Object{}
   445  	}
   446  	return Object(obj)
   447  }
   448  func (f *Functions) GetBindingi(pname Enum, idx int) Object {
   449  	obj := f._getIndexedParameter.Invoke(int(pname), idx)
   450  	if !obj.Truthy() {
   451  		return Object{}
   452  	}
   453  	return Object(obj)
   454  }
   455  func (f *Functions) GetInteger(pname Enum) int {
   456  	if !f.isWebGL2 {
   457  		switch pname {
   458  		case PACK_ROW_LENGTH, UNPACK_ROW_LENGTH:
   459  			return 0 // PACK_ROW_LENGTH and UNPACK_ROW_LENGTH is only available on WebGL 2
   460  		}
   461  	}
   462  	return paramVal(f._getParameter.Invoke(int(pname)))
   463  }
   464  func (f *Functions) GetFloat(pname Enum) float32 {
   465  	return float32(f._getParameter.Invoke(int(pname)).Float())
   466  }
   467  func (f *Functions) GetInteger4(pname Enum) [4]int {
   468  	arr := f._getParameter.Invoke(int(pname))
   469  	var res [4]int
   470  	for i := range res {
   471  		res[i] = arr.Index(i).Int()
   472  	}
   473  	return res
   474  }
   475  func (f *Functions) GetFloat4(pname Enum) [4]float32 {
   476  	arr := f._getParameter.Invoke(int(pname))
   477  	var res [4]float32
   478  	for i := range res {
   479  		res[i] = float32(arr.Index(i).Float())
   480  	}
   481  	return res
   482  }
   483  func (f *Functions) GetProgrami(p Program, pname Enum) int {
   484  	return paramVal(f._getProgramParameter.Invoke(js.Value(p), int(pname)))
   485  }
   486  func (f *Functions) GetProgramInfoLog(p Program) string {
   487  	return f._getProgramInfoLog.Invoke(js.Value(p)).String()
   488  }
   489  func (f *Functions) GetQueryObjectuiv(query Query, pname Enum) uint {
   490  	if !f.EXT_disjoint_timer_query_webgl2.IsNull() {
   491  		return uint(paramVal(f._getQueryParameter.Invoke(js.Value(query), int(pname))))
   492  	} else {
   493  		return uint(paramVal(f.EXT_disjoint_timer_query.Call("getQueryObjectEXT", js.Value(query), int(pname))))
   494  	}
   495  }
   496  func (f *Functions) GetShaderi(s Shader, pname Enum) int {
   497  	return paramVal(f._getShaderParameter.Invoke(js.Value(s), int(pname)))
   498  }
   499  func (f *Functions) GetShaderInfoLog(s Shader) string {
   500  	return f._getShaderInfoLog.Invoke(js.Value(s)).String()
   501  }
   502  func (f *Functions) GetString(pname Enum) string {
   503  	switch pname {
   504  	case EXTENSIONS:
   505  		extsjs := f._getSupportedExtensions.Invoke()
   506  		var exts []string
   507  		for i := 0; i < extsjs.Length(); i++ {
   508  			exts = append(exts, "GL_"+extsjs.Index(i).String())
   509  		}
   510  		return strings.Join(exts, " ")
   511  	default:
   512  		return f._getParameter.Invoke(int(pname)).String()
   513  	}
   514  }
   515  func (f *Functions) GetUniformBlockIndex(p Program, name string) uint {
   516  	return uint(paramVal(f._getUniformBlockIndex.Invoke(js.Value(p), name)))
   517  }
   518  func (f *Functions) GetUniformLocation(p Program, name string) Uniform {
   519  	return Uniform(f._getUniformLocation.Invoke(js.Value(p), name))
   520  }
   521  func (f *Functions) GetVertexAttrib(index int, pname Enum) int {
   522  	return paramVal(f._getVertexAttrib.Invoke(index, int(pname)))
   523  }
   524  func (f *Functions) GetVertexAttribBinding(index int, pname Enum) Object {
   525  	obj := f._getVertexAttrib.Invoke(index, int(pname))
   526  	if !obj.Truthy() {
   527  		return Object{}
   528  	}
   529  	return Object(obj)
   530  }
   531  func (f *Functions) GetVertexAttribPointer(index int, pname Enum) uintptr {
   532  	return uintptr(f._getVertexAttribOffset.Invoke(index, int(pname)).Int())
   533  }
   534  func (f *Functions) InvalidateFramebuffer(target, attachment Enum) {
   535  	fn := f.Ctx.Get("invalidateFramebuffer")
   536  	if !fn.IsUndefined() {
   537  		if f.int32Buf.IsUndefined() {
   538  			f.int32Buf = js.Global().Get("Int32Array").New(1)
   539  		}
   540  		f.int32Buf.SetIndex(0, int32(attachment))
   541  		f._invalidateFramebuffer.Invoke(int(target), f.int32Buf)
   542  	}
   543  }
   544  func (f *Functions) IsEnabled(cap Enum) bool {
   545  	return f._isEnabled.Invoke(int(cap)).Truthy()
   546  }
   547  func (f *Functions) LinkProgram(p Program) {
   548  	f._linkProgram.Invoke(js.Value(p))
   549  }
   550  func (f *Functions) PixelStorei(pname Enum, param int) {
   551  	f._pixelStorei.Invoke(int(pname), param)
   552  }
   553  func (f *Functions) MemoryBarrier(barriers Enum) {
   554  	panic("not implemented")
   555  }
   556  func (f *Functions) MapBufferRange(target Enum, offset, length int, access Enum) []byte {
   557  	panic("not implemented")
   558  }
   559  func (f *Functions) RenderbufferStorage(target, internalformat Enum, width, height int) {
   560  	f._renderbufferStorage.Invoke(int(target), int(internalformat), width, height)
   561  }
   562  func (f *Functions) ReadPixels(x, y, width, height int, format, ty Enum, data []byte) {
   563  	ba := f.byteArrayOf(data)
   564  	f._readPixels.Invoke(x, y, width, height, int(format), int(ty), ba)
   565  	js.CopyBytesToGo(data, ba)
   566  }
   567  func (f *Functions) Scissor(x, y, width, height int32) {
   568  	f._scissor.Invoke(x, y, width, height)
   569  }
   570  func (f *Functions) ShaderSource(s Shader, src string) {
   571  	f._shaderSource.Invoke(js.Value(s), src)
   572  }
   573  func (f *Functions) TexImage2D(target Enum, level int, internalFormat Enum, width, height int, format, ty Enum) {
   574  	f._texImage2D.Invoke(int(target), int(level), int(internalFormat), int(width), int(height), 0, int(format), int(ty), nil)
   575  }
   576  func (f *Functions) TexStorage2D(target Enum, levels int, internalFormat Enum, width, height int) {
   577  	f._texStorage2D.Invoke(int(target), levels, int(internalFormat), width, height)
   578  }
   579  func (f *Functions) TexSubImage2D(target Enum, level int, x, y, width, height int, format, ty Enum, data []byte) {
   580  	f._texSubImage2D.Invoke(int(target), level, x, y, width, height, int(format), int(ty), f.byteArrayOf(data))
   581  }
   582  func (f *Functions) TexParameteri(target, pname Enum, param int) {
   583  	f._texParameteri.Invoke(int(target), int(pname), int(param))
   584  }
   585  func (f *Functions) UniformBlockBinding(p Program, uniformBlockIndex uint, uniformBlockBinding uint) {
   586  	f._uniformBlockBinding.Invoke(js.Value(p), int(uniformBlockIndex), int(uniformBlockBinding))
   587  }
   588  func (f *Functions) Uniform1f(dst Uniform, v float32) {
   589  	f._uniform1f.Invoke(js.Value(dst), v)
   590  }
   591  func (f *Functions) Uniform1i(dst Uniform, v int) {
   592  	f._uniform1i.Invoke(js.Value(dst), v)
   593  }
   594  func (f *Functions) Uniform2f(dst Uniform, v0, v1 float32) {
   595  	f._uniform2f.Invoke(js.Value(dst), v0, v1)
   596  }
   597  func (f *Functions) Uniform3f(dst Uniform, v0, v1, v2 float32) {
   598  	f._uniform3f.Invoke(js.Value(dst), v0, v1, v2)
   599  }
   600  func (f *Functions) Uniform4f(dst Uniform, v0, v1, v2, v3 float32) {
   601  	f._uniform4f.Invoke(js.Value(dst), v0, v1, v2, v3)
   602  }
   603  func (f *Functions) UseProgram(p Program) {
   604  	f._useProgram.Invoke(js.Value(p))
   605  }
   606  func (f *Functions) UnmapBuffer(target Enum) bool {
   607  	panic("not implemented")
   608  }
   609  func (f *Functions) VertexAttribPointer(dst Attrib, size int, ty Enum, normalized bool, stride, offset int) {
   610  	f._vertexAttribPointer.Invoke(int(dst), size, int(ty), normalized, stride, offset)
   611  }
   612  func (f *Functions) Viewport(x, y, width, height int) {
   613  	f._viewport.Invoke(x, y, width, height)
   614  }
   615  
   616  func (f *Functions) byteArrayOf(data []byte) js.Value {
   617  	if len(data) == 0 {
   618  		return js.Null()
   619  	}
   620  	f.resizeByteBuffer(len(data))
   621  	ba := f.uint8Array.New(f.arrayBuf, int(0), int(len(data)))
   622  	js.CopyBytesToJS(ba, data)
   623  	return ba
   624  }
   625  
   626  func (f *Functions) resizeByteBuffer(n int) {
   627  	if n == 0 {
   628  		return
   629  	}
   630  	if !f.arrayBuf.IsUndefined() && f.arrayBuf.Length() >= n {
   631  		return
   632  	}
   633  	f.arrayBuf = js.Global().Get("ArrayBuffer").New(n)
   634  }
   635  
   636  func paramVal(v js.Value) int {
   637  	switch v.Type() {
   638  	case js.TypeBoolean:
   639  		if b := v.Bool(); b {
   640  			return 1
   641  		} else {
   642  			return 0
   643  		}
   644  	case js.TypeNumber:
   645  		return v.Int()
   646  	case js.TypeUndefined:
   647  		return 0
   648  	case js.TypeNull:
   649  		return 0
   650  	default:
   651  		panic("unknown parameter type")
   652  	}
   653  }