github.com/cybriq/giocore@v0.0.7-0.20210703034601-cfb9cb5f3900/gpu/internal/driver/api.go (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  package driver
     4  
     5  import (
     6  	"fmt"
     7  	"unsafe"
     8  
     9  	"github.com/cybriq/giocore/internal/gl"
    10  )
    11  
    12  // See gpu/api.go for documentation for the API types.
    13  
    14  type API interface {
    15  	implementsAPI()
    16  }
    17  
    18  type OpenGL struct {
    19  	// ES forces the use of ANGLE OpenGL ES libraries on macOS. It is
    20  	// ignored on all other platforms.
    21  	ES bool
    22  	// Context contains the WebGL context for WebAssembly platforms. It is
    23  	// empty for all other platforms; an OpenGL context is assumed current when
    24  	// calling NewDevice.
    25  	Context gl.Context
    26  }
    27  
    28  type Direct3D11 struct {
    29  	// Device contains a *ID3D11Device.
    30  	Device unsafe.Pointer
    31  }
    32  
    33  // API specific device constructors.
    34  var (
    35  	NewOpenGLDevice     func(api OpenGL) (Device, error)
    36  	NewDirect3D11Device func(api Direct3D11) (Device, error)
    37  )
    38  
    39  // NewDevice creates a new Device given the api.
    40  //
    41  // Note that the device does not assume ownership of the resources contained in
    42  // api; the caller must ensure the resources are valid until the device is
    43  // released.
    44  func NewDevice(api API) (Device, error) {
    45  	switch api := api.(type) {
    46  	case OpenGL:
    47  		if NewOpenGLDevice != nil {
    48  			return NewOpenGLDevice(api)
    49  		}
    50  	case Direct3D11:
    51  		if NewDirect3D11Device != nil {
    52  			return NewDirect3D11Device(api)
    53  		}
    54  	}
    55  	return nil, fmt.Errorf("driver: no driver available for the API %T", api)
    56  }
    57  
    58  func (OpenGL) implementsAPI()     {}
    59  func (Direct3D11) implementsAPI() {}