zworld/engine/renderapi/buffer/item.go

48 lines
838 B
Go
Raw Permalink Normal View History

2024-01-14 22:56:06 +08:00
package buffer
import (
"fmt"
"reflect"
"zworld/engine/renderapi/device"
"zworld/engine/util"
)
type Item[K any] interface {
T
// Set the data in the buffer and flushes.
Set(data K)
}
type item[K any] struct {
T
}
// NewItem creates a new typed single-item buffer.
// When allocating items, the Size argument is ignored
func NewItem[K any](device device.T, args Args) Item[K] {
align, maxSize := GetBufferLimits(device, args.Usage)
var empty K
kind := reflect.TypeOf(empty)
element := util.Align(int(kind.Size()), align)
if element > maxSize {
panic(fmt.Sprintf("buffer is too large for the specified usage. size: %d, max: %d", element, maxSize))
}
args.Size = element
buffer := New(device, args)
return &item[K]{
T: buffer,
}
}
func (i *item[K]) Set(data K) {
ptr := &data
i.Write(0, ptr)
i.Flush()
}