vdraw

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2024 License: BSD-3-Clause Imports: 11 Imported by: 0

README

vDraw: vGPU version of Go image/draw compositing functionality

GoDocs for ReComp

This package uses Alpha Compositing to render rectangular regions onto a render target, using vGPU, consistent with the image/draw package in Go. Although "draw" is not a great name for this functionality, it is hard to come up with a better one that isn't crazy long, so we're adopting it -- at least it is familiar.

The Cogent Core GUI, and probably other 2D-based GUI frameworks, uses a strategy of rendering to various rectangular sub-regions (in Cogent Core these are core.Viewport objects) that are updated separately, and then the final result can be composited together into a single overall image that can be pasted onto the final window surface that the user sees. Furthermore, in Gi3D, the 3D Scene is rendered to a framebuffer, which is likewise composited into the final surface window.

This package supports these rectangular image region composition operations, via a simple render pipeline that just renders a rectangular shape with a texture. There is also a simple fill pipeline that renders a single color into a rectangle.

The max number of images that can be pre-loaded and used per set, per render pass is only 16 -- see MaxImages. Therefore, we use 4 sets of 16. The last set is configured for 3D images with 128 layers to be used as sprites.

The fill color is uploaded as a push constant and thus is not subject to any limitations.

Implementation: Var map

Set: -2
    Role: Vertex
        Var: 0:	Pos	Float32Vector2[4]	(size: 8)	Values: 1
    Role: Index
        Var: 1:	Index	Uint16[6]	(size: 2)	Values: 1
Set: -1
    Role: Push
        Var: 0:	Mtxs	Struct	(size: 128)	Values: 0
Set: 0
    Role: TextureRole
        Var: 0:	Tex	ImageRGBA32	(size: 4)	Values: 1

Documentation

Index

Constants

View Source
const (
	Over = draw.Over
	Src  = draw.Src
)

These draw.Op constants are provided so that users of this package don't have to explicitly import "image/draw".

Variables

This section is empty.

Functions

func TransformMatrix added in v0.0.9

func TransformMatrix(dr image.Rectangle, sr image.Rectangle, rotDeg float32) math32.Matrix3

TransformMatrix returns a transformation matrix for the generic Draw function that scales, translates, and rotates the source image by the given degrees. to make it fit within the destination rectangle dr, given its original size sr (unrotated). To avoid scaling, ensure that the dr and sr are the same dimensions (post rotation). rotDeg = rotation degrees to apply in the mapping: 90 = left, -90 = right, 180 = invert

Types

type Drawer

type Drawer struct {

	// drawing system
	Sys vgpu.System

	// surface if render target
	Surf *vgpu.Surface

	// render frame if render target
	Frame *vgpu.RenderFrame

	// render so the Y axis points down, with 0,0 at the upper left, which is the Vulkan standard.  default is Y is up, with 0,0 at bottom left, which is OpenGL default.  this must be set prior to configuring, the surface, as it determines the rendering parameters.
	YIsDown bool

	// implementation state -- ignore
	Impl DrawerImpl

	// mutex on updating
	UpdateMu sync.Mutex `view:"-" copier:"-" json:"-" xml:"-"`
}

Drawer is the vDraw implementation, which draws Textures or Fills solid colors to a render target (Surface, RenderFrame). Image and color palette must be set prior to a given render pass. Multiple fill operations can be performed in one pass, but only one Image can be used at a time.

func (*Drawer) ConfigFrame

func (dw *Drawer) ConfigFrame(dev *vgpu.Device, size image.Point, maxTextures int)

ConfigFrame configures the Drawer to use a RenderFrame as a render target, of given size. Use dw.Frame.SetSize to resize later. Frame is owned and managed by the Drawer. Uses given Device -- if nil, one is made. If maxTextures > vgpu.MaxTexturesPerSet (16) then multiple descriptor sets are used to hold more textures.

func (*Drawer) ConfigImage

func (dw *Drawer) ConfigImage(idx int, fmt *vgpu.ImageFormat)

ConfigImage configures the draw image at given index to fit the given image format and number of layers as a drawing source.

func (*Drawer) ConfigImageDefaultFormat

func (dw *Drawer) ConfigImageDefaultFormat(idx int, width int, height int, layers int)

ConfigImageDefaultFormat configures the draw image at the given index to fit the default image format specified by the given width, height, and number of layers.

func (*Drawer) ConfigImageName

func (dw *Drawer) ConfigImageName(name string, fmt *vgpu.ImageFormat)

ConfigImageName configures the draw image at given name to fit the given image format as a drawing source.

func (*Drawer) ConfigMtxs

func (dw *Drawer) ConfigMtxs(src2dst math32.Matrix3, txsz image.Point, sr image.Rectangle, op draw.Op, flipY bool) *Mtxs

ConfigMtxs configures the draw matrix for given draw parameters: src2dst is the transform mapping source to destination coordinates (translation, scaling), txsz is the size of the texture to draw, sr is the source region (set to tex.Format.Bounds() for all) op is the drawing operation: Src = copy source directly (blit), Over = alpha blend with existing flipY inverts the Y axis of the source image.

func (*Drawer) ConfigPipeline

func (dw *Drawer) ConfigPipeline(pl *vgpu.Pipeline)

ConfigPipeline configures graphics settings on the pipeline

func (*Drawer) ConfigSurface

func (dw *Drawer) ConfigSurface(sf *vgpu.Surface, maxTextures int)

ConfigSurface configures the Drawer to use given surface as a render target. maxTextures is maximum number of images that can be used per pass. If maxTextures > vgpu.MaxTexturesPerSet (16) then multiple descriptor sets are used to hold more textures.

func (*Drawer) ConfigSys

func (dw *Drawer) ConfigSys()

ConfigSys configures the vDraw System and pipelines.

func (*Drawer) Copy

func (dw *Drawer) Copy(idx, layer int, dp image.Point, sr image.Rectangle, op draw.Op, flipY bool) error

Copy copies texture at given index and layer to render target. dp is the destination point, sr is the source region (set to image.ZR zero rect for all), op is the drawing operation: Src = copy source directly (blit), Over = alpha blend with existing flipY = flipY axis when drawing this image

func (*Drawer) DestBounds

func (dw *Drawer) DestBounds() image.Rectangle

DestBounds returns the bounds of the render destination

func (*Drawer) DestSize

func (dw *Drawer) DestSize() image.Point

DestSize returns the size of the render destination

func (*Drawer) Destroy

func (dw *Drawer) Destroy()

func (*Drawer) Draw

func (dw *Drawer) Draw(idx, layer int, src2dst math32.Matrix3, sr image.Rectangle, op draw.Op, flipY bool) error

Draw draws texture at index and layer to render target. Must have called StartDraw first. src2dst is the transform mapping source to destination coordinates (translation, scaling), sr is the source region (set to image.ZR for all) op is the drawing operation: Src = copy source directly (blit), Over = alpha blend with existing

func (*Drawer) EndDraw

func (dw *Drawer) EndDraw()

EndDraw ends image drawing rendering process on render target

func (*Drawer) EndFill

func (dw *Drawer) EndFill()

EndFill ends color filling rendering process on render target

func (*Drawer) Fill

func (dw *Drawer) Fill(clr color.Color, src2dst math32.Matrix3, reg image.Rectangle, op draw.Op) error

Fill fills given color to to render target. src2dst is the transform mapping source to destination coordinates (translation, scaling), reg is the region to fill op is the drawing operation: Src = copy source directly (blit), Over = alpha blend with existing

func (*Drawer) FillRect

func (dw *Drawer) FillRect(clr color.Color, reg image.Rectangle, op draw.Op) error

FillRect fills given color to render target, to given region. op is the drawing operation: Src = copy source directly (blit), Over = alpha blend with existing

func (*Drawer) GetImageValue added in v0.0.10

func (dw *Drawer) GetImageValue(idx int) *vgpu.Value

GetImageValue returns vgpu Value value of Image for given index

func (*Drawer) ImageIndexByName added in v0.0.10

func (dw *Drawer) ImageIndexByName(name string) int

ImageIndexByName returns index of image val by name. Logs error if not found, and returns 0.

func (*Drawer) MaxTextures

func (dw *Drawer) MaxTextures() int

MaxTextures returns the max number of textures for drawing, which is [Drawer.Impl.MaxTextures].

func (*Drawer) Scale

func (dw *Drawer) Scale(idx, layer int, dr image.Rectangle, sr image.Rectangle, op draw.Op, flipY bool, rotDeg float32) error

Scale copies texture at given index and layer to render target, scaling the region defined by src and sr to the destination such that sr in src-space is mapped to dr in dst-space. dr is the destination rectangle sr is the source region (set to image.Rectangle{} zero rect for all), op is the drawing operation: Src = copy source directly (blit), Over = alpha blend with existing flipY = flipY axis when drawing this image rotDeg = rotation degrees to apply in the mapping: 90 = left, -90 = right, 180 = invert

func (*Drawer) SelectPipeline

func (dw *Drawer) SelectPipeline(op draw.Op) *vgpu.Pipeline

SelectPipeline selects the pipeline based on draw op only changes if not last one used. Default is Src

func (*Drawer) SetFrameImage

func (dw *Drawer) SetFrameImage(idx int, fbi any)

SetFrameImage sets given vgpu.Framebuffer image as a drawing source at index, used in subsequent Draw methods. Must have already been configured to fit!

func (*Drawer) SetFrameImageName

func (dw *Drawer) SetFrameImageName(name string, fb *vgpu.Framebuffer)

SetFrameImageName sets given Framebuffer image as a drawing source at name, used in subsequent Draw methods. Must have already been configured to fit!

func (*Drawer) SetGoImage

func (dw *Drawer) SetGoImage(idx, layer int, img image.Image, flipY bool)

SetGoImage sets given Go image as a drawing source to given image index, and layer, used in subsequent Draw methods. A standard Go image is rendered upright on a standard Vulkan surface. Set flipY to true to flip.

func (*Drawer) SetGoImageName

func (dw *Drawer) SetGoImageName(name string, layer int, img image.Image, flipY bool)

SetGoImageName sets given Go image as a drawing source to given image name, and layer, used in subsequent Draw methods. (use SetImageName to set names) A standard Go image is rendered upright on a standard Vulkan surface. Set flipY to true to flip. This can be used directly without pre-configuring.

func (*Drawer) SetImageName

func (dw *Drawer) SetImageName(idx int, name string) error

SetImageName sets name of image at given index, to enable name-based access for subsequent calls. Returns error if name already exists.

func (*Drawer) SetMaxTextures

func (dw *Drawer) SetMaxTextures(maxTextures int)

SetMaxTextures updates the max number of textures for drawing Must call this prior to doing any allocation of images.

func (*Drawer) StartDraw

func (dw *Drawer) StartDraw(descIndex int) bool

StartDraw starts image drawing rendering process on render target No images can be added or set after this point. descIndex is the descriptor set to use -- choose this based on the bank of 16 texture values if number of textures > MaxTexturesPerSet. It returns false if rendering can not proceed.

func (*Drawer) StartFill

func (dw *Drawer) StartFill() bool

StartFill starts color fill drawing rendering process on render target. It returns false if rendering can not proceed.

func (*Drawer) Surface

func (dw *Drawer) Surface() any

func (*Drawer) SyncImages

func (dw *Drawer) SyncImages()

SyncImages must be called after images have been updated, to sync memory up to the GPU.

func (*Drawer) UseTextureSet

func (dw *Drawer) UseTextureSet(descIndex int)

UseTextureSet selects the descriptor set to use -- choose this based on the bank of 16 texture values if number of textures > MaxTexturesPerSet.

type DrawerImpl

type DrawerImpl struct {

	// surface index for current render process
	SurfIndex uint32

	// maximum number of images per pass -- set by user at config
	MaxTextures int

	// whether to render image with flipped Y
	FlipY bool

	// last draw operation used -- used for switching pipeline
	LastOp draw.Op
}

DrawerImpl contains implementation state -- ignore..

type Mtxs

type Mtxs struct {
	MVP math32.Matrix4
	UVP math32.Matrix4
}

Mtxs are the projection matricies

Jump to

Keyboard shortcuts

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