resource

package
v0.0.0-...-6aa7f09 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2023 License: Apache-2.0 Imports: 29 Imported by: 5

README

resource package revamp

Things to care about

  • Resource tracking
  • Avoid dup asset memory
  • Avoid double loading
  • Resource release
Trigger message directly to gpu
	func MySystem(g *gorge.Gorge, r *resource.Context) {
		texData := gorge.TextureData{}
		// Load right here
		r.Load(&texData, "file.jpg")
		tex :=gorge.NewTexture(texData)

		g.Handle(func(gorge.EventUpdate) {
			// Bad will block the renderer and possibly lock stuff in wasm
			r.Load(...,"somefile")
		})
	}
  • If we trigger the resource to load, we might not want it on GPU yet, so we need something to choose if we load from
  • Resource loading delegation

Remove reference counter from renderer, resource should be the one that loads, unloads, cache, tracks

This way we will do the ref counter of the resource in the resource system it will be responsible for:

  • Loading stuff
  • Tracking resources
  • Unloading

Current:

  • (good) each system tracks it's own hardware resources
  • (good) avoid reloading hardware resources
  • (bad) depends on bundle loading and unloading
  • (bad) It has a complex back and forth system
  • (bad) async resource loading which might get trickier on things that we need to load right away
Process:
  • [app] get bundle loader from resources package that implement gorge.Bundler
  • [app] get resource from bundle (bundle registers it)
  • [resource] bundle as a message
  • [any...] receives the bundle message
  • [any...] sends back load intent with required resources (each system signs the intention, so if the resource is already in the system it might be avoided)
  • [resource] loads the necessary resources
  • [resource] send data via message for each individual resource
  • [any...] receives data and track the loader to the gpu data
  • [...][]byte data is no longer needed
  • [app] unload bundle
  • [any...] discard any hardware resource related to bundled loaders

Planned

Try to achieve garbage collected resources

  • (good) resource tracks all resources triggering load and release messages
  • (good) systems don't need to track resources
  • (good) resource can implement a []byte cache system for specific resource
  • (good) we don't depend on scenes, for tracking resources, and resources will be cross scenes
  • (bad) bundle will be harder (not impossible)
  • (bad) resource might reload if dynamically loaded
Process:
  • [app] resource provider is declared in function params
  • [app] Load resource on init
  • [resource] send message with resource data and increases the resource reference
  • [any...] receive the asset data, and registers data locally
  • [app] unreference the resource somehow
  • [gc,resource] uppon GC we decrease reference, when reference is lost we trigger the message to release the resource
  • [any...] unload the hardware resource

Documentation

Overview

Package resource handles resources

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReadTexture

func ReadTexture(rd io.Reader) (*gorge.TextureData, error)

ReadTexture reads an image from the reader and returns a textureData.

func Register

func Register(v any, ext string, fn LoaderFunc)

Register registers a loader for a type and extension.

func System

func System(g *gorge.Context)

func TextureDataFromImage

func TextureDataFromImage(im image.Image) (*gorge.TextureData, error)

TextureDataFromImage converts a go image.Image to gorge.TextureData.

func TextureFromImage

func TextureFromImage(im image.Image) (*gorge.Texture, error)

TextureFromImage converts a go image.Image to gorge.Texture.

Types

type Context

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

Context to be used in gorge systems

func FromContext

func FromContext(g *gorge.Context) *Context

FromContext returns a Context from a gorge Context

func (*Context) Material

func (r *Context) Material(name string, opts ...any) *gorge.Material

Material loads everytime right away

func (*Context) Mesh

func (r *Context) Mesh(name string, opts ...any) *gorge.Mesh

Mesh helper that returns a mesh with a resourcer ref

func (*Context) Texture

func (r *Context) Texture(name string, opts ...any) *gorge.Texture

Texture helper that returns a texture with a resourcer ref

type EventLoadComplete

type EventLoadComplete struct {
	Name     string
	Resource any
	Err      error
}

EventLoadComplete is triggered when a resource finished loading.

type EventLoadStart

type EventLoadStart struct {
	Name     string
	Resource any
}

EventLoadStart is trigger in gorge when a resource starts loading.

type EventOpen

type EventOpen struct {
	Name string
}

EventOpen is triggered when a resource is opened which differs from LoadStart it was meant to create some kind of progress bar while loading resources.

type FileFS

type FileFS struct {
	BasePath string
}

FileFS file system implementation opened files will be prefixed with BasePath

func (FileFS) Open

func (l FileFS) Open(p string) (fs.File, error)

Open File from filesystem prefixed by BasePath.

type HTTPFS

type HTTPFS struct {
	BaseURL string
}

HTTPFS asset that loads based on a starting url

func (HTTPFS) Open

func (l HTTPFS) Open(p string) (fs.File, error)

Open the asset

type LoaderFunc

type LoaderFunc func(res *Context, v any, names string, opts ...any) error

LoaderFunc is the type of a loader func

type Resource

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

Resource the resource manager.

func (*Resource) AddFS

func (r *Resource) AddFS(prefix string, fs fs.FS)

AddFS adds a new file system with the prefix if a path exists it will overlay the existing file system.

func (*Resource) Error

func (r *Resource) Error(err error)

func (*Resource) Gorge

func (r *Resource) Gorge() *gorge.Context

Gorge convinient way to return a gorge context to loaders.

func (*Resource) Load

func (r *Resource) Load(v any, name string, opts ...any) error

Load stuff

func (*Resource) LoadBytes

func (r *Resource) LoadBytes(name string) ([]byte, error)

LoadBytes returns the asset as string

func (*Resource) LoadString

func (r *Resource) LoadString(name string) (string, error)

LoadString returns asset as a string

func (*Resource) MustLoad

func (r *Resource) MustLoad(v any, path string, opts ...any)

MustLoad loads the resource if an error occurs it will be sent to gorge as an event.

func (*Resource) Open

func (r *Resource) Open(name string) (io.ReadCloser, error)

Open opens a reousrce based on the configured sourcer.

Jump to

Keyboard shortcuts

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