gpu

package module
v0.0.0-...-1fa7888 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2020 License: AGPL-3.0 Imports: 5 Imported by: 0

README

GPU

A high-level Go module for high-performance GPU operations.

Includes a obj loader and modern renderer capable of rendering up to a hundred thousand individual cubes at 60FPS with a single draw-call (without instancing).

screenshot

Wishlist

Things that we would love to see in the future with this module:
(please contribute)

  • Vulkan driver
  • WebGL/WebGPU driver
  • OpenGLES driver
  • GLTF loader
  • GPU Skeletal Animations
  • CPU/GPU Frustum Culling
  • Shadow rendering
  • Transparency Handling

HelloCube Example

package main

import (
	"log"

	"qlova.tech/gpu"
	"qlova.tech/win"

	"qlova.tech/gpu/models"

	_ "qlova.tech/gpu/driver/opengl"
	_ "qlova.tech/win/driver/glfw"
)

func main() {
	if err := win.Open(); err != nil {
		log.Fatalln("could not open a window: ", err)
	}

	if err := gpu.Open(); err != nil {
		log.Fatalln("could not open the gpu: ", err)
	}

	cube := gpu.NewMesh(models.Cube()).Model()

	if err := gpu.Upload(); err != nil {
		log.Fatalln("gpu upload failed: ", err)
	}

	gpu.Viewport = gpu.Position(3, 3, 3).LookAt(gpu.Position(0, 0, 0))

	var t gpu.Transform
	for win.Update() {
		gpu.DrawModel(&cube, &t)

		if err := gpu.Sync(); err != nil {
			log.Fatalln("there was an error syncing the gpu: ", err)
		}
	}
}

License
This work is subject to the terms of the Qlova Public License, Version 2.0. If a copy of the QPL was not distributed with this work, You can obtain one at https://license.qlova.org/v2

The QPL is compatible with the AGPL which is why both licenses are provided within this repository.

Documentation

Overview

Package gpu provides a modern (Physical Based Rendering), high performance GPU renderer for Go.

Index

Constants

This section is empty.

Variables

View Source
var (
	Triangles = DrawMode{1}
	Lines     = DrawMode{2}
)

DrawModes

View Source
var Darkness color.Color

Darkness is the color of dark objects, things cannot get darker than this. AKA Ambient Light

View Source
var Origin = NewTransform()

Origin is the position 0,0,0

View Source
var Projection = Perspective(60, 1.3, 0.3, 1000)

Projection is the camera projection transform.

View Source
var Viewport = NewTransform()

Viewport is the transform of the camera view.

Functions

func Open

func Open(hints ...string) error

Open opens the GPU ready for uploading data and rendering. You can provide a hint to select the name of the driver to use.

func RegisterDriver

func RegisterDriver(name string, d Driver)

RegisterDriver registers a new GPU driver.

func Render

func Render(model *Model, models []Model, transform *Transform)

Render renders the given model with the given transform.

func Sync

func Sync() error

Sync waits for all queued rendering calls to complete.

func Upload

func Upload() error

Upload waits for all queued data to upload to the GPU including meshes, textures and animations.

Types

type Animation

type Animation struct {

	//Frame is the current frame for this instance of the animation.
	//Increment this to play the animation.
	Frame float32
	// contains filtered or unexported fields
}

Animation is a pointer to an animation on the GPU.

func NewAnimation

func NewAnimation(frames Frames) Animation

NewAnimation creates a new animation on the GPU using the current GPU driver.

type Box

type Box struct {
	Width, Height, Depth float32
}

Box is a bounding box. Used for frustum culling.

type Buffer

type Buffer struct {
	// contains filtered or unexported fields
}

Buffer that can be used to control what data is currently available to the GPU.

var Default Buffer

Default is the default GPU buffer.

func NewBuffer

func NewBuffer() Buffer

NewBuffer creates and returns a new buffer ready for use.

func (Buffer) NewAnimation

func (b Buffer) NewAnimation(frames Frames) Animation

NewAnimation creates a new animation on the GPU using the current GPU driver.

func (Buffer) NewMaterial

func (b Buffer) NewMaterial(texture Texture) Material

NewMaterial creates a new material on the GPU using the current GPU driver.

func (Buffer) NewMesh

func (b Buffer) NewMesh(vertices Vertices) Mesh

NewMesh returns a new mesh on the GPU from the given vertices.

func (Buffer) Render

func (b Buffer) Render(model *Model, models []Model, transform *Transform)

Render renders the given model with the given transform.

func (Buffer) Sync

func (b Buffer) Sync() error

Sync waits for all queued rendering calls to complete.

func (Buffer) Upload

func (b Buffer) Upload() error

Upload waits for all queued data to upload to the GPU including meshes, textures and animations.

type Context

type Context interface {

	//NewMesh schedules a mesh to be uploaded to the GPU and returns two internal values as a descriptor IE the index and vertex-count.
	NewMesh(Vertices) (index, count uint32)

	//NewMaterial schedules/uploads a material on the GPU and returns references to the four texture maps.
	NewMaterial(Texture) (albedo, physical, normal, emission uint64)

	//NewAnimation schedules an animation to be uploaded to the GPU and returns the index and frame count of the animation.
	NewAnimation(Frames) (index, count uint32)

	//Upload waits for any scheduled meshes, materials and/or animations to upload to the GPU.
	Upload() error

	//Render schedules rendering of the Model or Models with the given transform applied.
	Render(*Model, []Model, *Transform)

	//Sync waits for all scheduled render operations to complete.
	Sync() error
}

Context provides an interface to a context on the GPU.

type DrawMode

type DrawMode struct {
	// contains filtered or unexported fields
}

DrawMode is a primitive draw mode.

type Driver

type Driver func() (Context, error)

Driver can open a new Context. If nil is returned once, it must continue to return nil.

type Frame

type Frame []Joint

Frame specifies the position and rotation of each joint.

type Frames

type Frames []Frame

Frames is a list of animation frames.

type Joint

type Joint struct {
	Position, Rotation [3]float32
}

Joint defines a current joint transform.

type Light

type Light struct {
	Transform
	Angle float32
	Color [3]float32
}

Light is either a spot light or point light depending on the cuttoff Angle of the light.

var Sun Light

Sun is the base directional light of the scene.

type Material

type Material struct {
	// contains filtered or unexported fields
}

Material is a Material on the GPU.

func NewMaterial

func NewMaterial(texture Texture) Material

NewMaterial creates a new material on the GPU using the current GPU driver.

func (Material) AlbedoMap

func (m Material) AlbedoMap() uint64

AlbedoMap returns the internal gpu representation for the albedo map of this material.

type Mesh

type Mesh struct {
	Mode
	// contains filtered or unexported fields
}

Mesh is a group of vertices on the GPU.

func NewMesh

func NewMesh(vertices Vertices) Mesh

NewMesh returns a new mesh on the GPU from the given vertices.

func (Mesh) Count

func (m Mesh) Count() uint32

Count returns the number of vertices or indicies of this mesh.

func (Mesh) Index

func (m Mesh) Index() uint32

Index returns the internal gpu index for this mesh.

func (Mesh) Model

func (m Mesh) Model() *Model

Model returns a new model with this Mesh as its Mesh.

type Mode

type Mode struct {
	Draw DrawMode
	// contains filtered or unexported fields
}

Mode describes a mode for rendering a mesh.

func (Mode) Indexed

func (mode Mode) Indexed() bool

Indexed returns true if the mesh is using indexed vertices.

type Model

type Model struct {
	Box
	Transform
	Mesh
	Material
	Animation
}

Model combines all core GPU elements together ready for rendering.

type Texture

type Texture struct {
	image.Image

	PhysicalMap, NormalMap, EmissionMap image.Image
}

Texture is a texture that can be used to create a material on the GPU.

type Transform

type Transform [16]float32

Transform describes the position, orientation and scale to apply to a target.

func NewTransform

func NewTransform() Transform

NewTransform returns a new indentity transform that does not transform the target in any way.

func Perspective

func Perspective(fov, aspect, near, far float32) Transform

Perspective returns a new perspective transform.

func Position

func Position(X, Y, Z float32) Transform

Position returns a new transform that translates the target to the given x,y,z coordinates.

func (Transform) LookAt

func (t Transform) LookAt(target Transform) Transform

LookAt returns a transform that looks from t to the target positions.

func (Transform) Position

func (t Transform) Position() (X, Y, Z float32)

Position returns the xyz translation component of the transform.

func (*Transform) SetPosition

func (t *Transform) SetPosition(X, Y, Z float32)

SetPosition sets the transform to translate the target to the given x,y,z coordinates.

func (*Transform) Translate

func (t *Transform) Translate(X, Y, Z float32)

Translate translates the transfrom by the given x,y,z coordinates.

type Vertices

type Vertices struct {
	//DrawMode for the vertices.
	Draw DrawMode

	//Vertices data.
	Vertices, Normals []float32

	//Texture mapping data.
	UVs []float32

	//Vertex color data.
	Colors []uint8

	//Animation data.
	Weights []float32
	Joints  []uint8

	//Indices of the vertices, when vertex data is shared.
	Indices []uint32
}

Vertices describes the components of a mesh.

func (Vertices) Mode

func (v Vertices) Mode() Mode

Mode returns the Mode of the vertices.

Directories

Path Synopsis
driver
obj
Package obj is used to parse the Wavefront OBJ file format (*.obj), including associated materials (*.mtl).
Package obj is used to parse the Wavefront OBJ file format (*.obj), including associated materials (*.mtl).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL