Text + MXL logo pattern
This commit is contained in:
+56
-10
@@ -35,6 +35,8 @@ type WGPUGenerator struct {
|
||||
out *wgpu.Buffer
|
||||
host *wgpu.Buffer
|
||||
uniform *wgpu.Buffer
|
||||
logo *wgpu.Buffer
|
||||
logoPlane []uint32
|
||||
params []byte
|
||||
width int
|
||||
height int
|
||||
@@ -44,13 +46,27 @@ type WGPUGenerator struct {
|
||||
|
||||
var _ FrameGenerator = (*WGPUGenerator)(nil)
|
||||
|
||||
func NewWGPUGenerator(width, height uint, kernelPath string) (*WGPUGenerator, error) {
|
||||
// WGPUOption customizes NewWGPUGenerator.
|
||||
type WGPUOption func(*WGPUGenerator)
|
||||
|
||||
// WithLogo attaches a packed logo plane (see BuildLogoPlane) as read-only
|
||||
// storage binding 2 for kernels that sample it (v210_dvd_logo.wgsl).
|
||||
func WithLogo(plane []uint32) WGPUOption {
|
||||
return func(g *WGPUGenerator) {
|
||||
g.logoPlane = plane
|
||||
}
|
||||
}
|
||||
|
||||
func NewWGPUGenerator(width, height uint, kernelPath string, opts ...WGPUOption) (*WGPUGenerator, error) {
|
||||
g := &WGPUGenerator{
|
||||
width: int(width),
|
||||
height: int(height),
|
||||
blocks: int(width*height) / 6,
|
||||
params: make([]byte, 16),
|
||||
}
|
||||
for _, opt := range opts {
|
||||
opt(g)
|
||||
}
|
||||
g.frameSize = uint64(g.blocks) * 16
|
||||
binary.LittleEndian.PutUint32(g.params[0:], uint32(width))
|
||||
binary.LittleEndian.PutUint32(g.params[4:], uint32(height))
|
||||
@@ -105,22 +121,48 @@ func NewWGPUGenerator(width, height uint, kernelPath string) (*WGPUGenerator, er
|
||||
g.Close()
|
||||
return nil, fmt.Errorf("wgpu: write params: %w", err)
|
||||
}
|
||||
bglEntries := []gputypes.BindGroupLayoutEntry{
|
||||
{Binding: 0, Visibility: wgpu.ShaderStageCompute, Buffer: &gputypes.BufferBindingLayout{Type: gputypes.BufferBindingTypeStorage}},
|
||||
{Binding: 1, Visibility: wgpu.ShaderStageCompute, Buffer: &gputypes.BufferBindingLayout{Type: gputypes.BufferBindingTypeUniform}},
|
||||
}
|
||||
bgEntries := []wgpu.BindGroupEntry{
|
||||
{Binding: 0, Buffer: g.out, Size: g.frameSize},
|
||||
{Binding: 1, Buffer: g.uniform, Size: uint64(len(g.params))},
|
||||
}
|
||||
if g.logoPlane != nil {
|
||||
logoBytes := make([]byte, 4*len(g.logoPlane))
|
||||
for i, v := range g.logoPlane {
|
||||
binary.LittleEndian.PutUint32(logoBytes[4*i:], v)
|
||||
}
|
||||
if g.logo, err = g.device.CreateBuffer(&wgpu.BufferDescriptor{
|
||||
Label: "v210-logo", Size: uint64(len(logoBytes)),
|
||||
Usage: wgpu.BufferUsageStorage | wgpu.BufferUsageCopyDst,
|
||||
}); err != nil {
|
||||
g.Close()
|
||||
return nil, fmt.Errorf("wgpu: logo buffer: %w", err)
|
||||
}
|
||||
if err := g.queue.WriteBuffer(g.logo, 0, logoBytes); err != nil {
|
||||
g.Close()
|
||||
return nil, fmt.Errorf("wgpu: write logo: %w", err)
|
||||
}
|
||||
bglEntries = append(bglEntries, gputypes.BindGroupLayoutEntry{
|
||||
Binding: 2, Visibility: wgpu.ShaderStageCompute,
|
||||
Buffer: &gputypes.BufferBindingLayout{Type: gputypes.BufferBindingTypeReadOnlyStorage},
|
||||
})
|
||||
bgEntries = append(bgEntries, wgpu.BindGroupEntry{
|
||||
Binding: 2, Buffer: g.logo, Size: uint64(len(logoBytes)),
|
||||
})
|
||||
}
|
||||
if g.bgl, err = g.device.CreateBindGroupLayout(&wgpu.BindGroupLayoutDescriptor{
|
||||
Label: "v210-bgl",
|
||||
Entries: []gputypes.BindGroupLayoutEntry{
|
||||
{Binding: 0, Visibility: wgpu.ShaderStageCompute, Buffer: &gputypes.BufferBindingLayout{Type: gputypes.BufferBindingTypeStorage}},
|
||||
{Binding: 1, Visibility: wgpu.ShaderStageCompute, Buffer: &gputypes.BufferBindingLayout{Type: gputypes.BufferBindingTypeUniform}},
|
||||
},
|
||||
Label: "v210-bgl",
|
||||
Entries: bglEntries,
|
||||
}); err != nil {
|
||||
g.Close()
|
||||
return nil, fmt.Errorf("wgpu: bind group layout: %w", err)
|
||||
}
|
||||
if g.bg, err = g.device.CreateBindGroup(&wgpu.BindGroupDescriptor{
|
||||
Label: "v210-bg", Layout: g.bgl,
|
||||
Entries: []wgpu.BindGroupEntry{
|
||||
{Binding: 0, Buffer: g.out, Size: g.frameSize},
|
||||
{Binding: 1, Buffer: g.uniform, Size: uint64(len(g.params))},
|
||||
},
|
||||
Entries: bgEntries,
|
||||
}); err != nil {
|
||||
g.Close()
|
||||
return nil, fmt.Errorf("wgpu: bind group: %w", err)
|
||||
@@ -212,6 +254,10 @@ func (g *WGPUGenerator) Close() error {
|
||||
g.uniform.Release()
|
||||
g.uniform = nil
|
||||
}
|
||||
if g.logo != nil {
|
||||
g.logo.Release()
|
||||
g.logo = nil
|
||||
}
|
||||
if g.host != nil {
|
||||
g.host.Release()
|
||||
g.host = nil
|
||||
|
||||
Reference in New Issue
Block a user