github.com/cybriq/giocore@v0.0.7-0.20210703034601-cfb9cb5f3900/app/internal/wm/gl_js.go (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  package wm
     4  
     5  import (
     6  	"errors"
     7  	"syscall/js"
     8  
     9  	"github.com/cybriq/giocore/gpu"
    10  	"github.com/cybriq/giocore/internal/gl"
    11  )
    12  
    13  type context struct {
    14  	ctx js.Value
    15  	cnv js.Value
    16  }
    17  
    18  func newContext(w *window) (*context, error) {
    19  	args := map[string]interface{}{
    20  		// Enable low latency rendering.
    21  		// See https://developers.google.com/web/updates/2019/05/desynchronized.
    22  		"desynchronized":        true,
    23  		"preserveDrawingBuffer": true,
    24  	}
    25  	ctx := w.cnv.Call("getContext", "webgl2", args)
    26  	if ctx.IsNull() {
    27  		ctx = w.cnv.Call("getContext", "webgl", args)
    28  	}
    29  	if ctx.IsNull() {
    30  		return nil, errors.New("app: webgl is not supported")
    31  	}
    32  	c := &context{
    33  		ctx: ctx,
    34  		cnv: w.cnv,
    35  	}
    36  	return c, nil
    37  }
    38  
    39  func (c *context) API() gpu.API {
    40  	return gpu.OpenGL{Context: gl.Context(c.ctx)}
    41  }
    42  
    43  func (c *context) Release() {
    44  }
    45  
    46  func (c *context) Present() error {
    47  	if c.ctx.Call("isContextLost").Bool() {
    48  		return errors.New("context lost")
    49  	}
    50  	return nil
    51  }
    52  
    53  func (c *context) Lock() {}
    54  
    55  func (c *context) Unlock() {}
    56  
    57  func (c *context) Refresh() error {
    58  	return nil
    59  }
    60  
    61  func (c *context) MakeCurrent() error {
    62  	return nil
    63  }
    64  
    65  func (w *window) NewContext() (Context, error) {
    66  	return newContext(w)
    67  }