mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-05 17:35:36 +00:00
gpu: make Buffers immutable
The GPU implementation only uses immutable buffers so far, so let's make it easy and performant for the backends. Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
+1
-7
@@ -22,7 +22,7 @@ type Backend interface {
|
|||||||
DefaultFramebuffer() Framebuffer
|
DefaultFramebuffer() Framebuffer
|
||||||
NilTexture() Texture
|
NilTexture() Texture
|
||||||
NewFramebuffer() Framebuffer
|
NewFramebuffer() Framebuffer
|
||||||
NewBuffer(typ BufferType) Buffer
|
NewBuffer(typ BufferType, data []byte) Buffer
|
||||||
NewProgram(vertexShader, fragmentShader string, attribMap []string) (Program, error)
|
NewProgram(vertexShader, fragmentShader string, attribMap []string) (Program, error)
|
||||||
SetupVertexArray(slot int, size int, dataType DataType, stride, offset int)
|
SetupVertexArray(slot int, size int, dataType DataType, stride, offset int)
|
||||||
|
|
||||||
@@ -49,7 +49,6 @@ type TextureFilter uint8
|
|||||||
type TextureFormat uint8
|
type TextureFormat uint8
|
||||||
|
|
||||||
type BufferType uint8
|
type BufferType uint8
|
||||||
type BufferUsage uint8
|
|
||||||
|
|
||||||
type DataType uint8
|
type DataType uint8
|
||||||
|
|
||||||
@@ -76,7 +75,6 @@ type Uniform interface{}
|
|||||||
|
|
||||||
type Buffer interface {
|
type Buffer interface {
|
||||||
Bind()
|
Bind()
|
||||||
Upload(usage BufferUsage, data []byte)
|
|
||||||
Release()
|
Release()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,10 +114,6 @@ const (
|
|||||||
DataTypeShort
|
DataTypeShort
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
BufferUsageStaticDraw BufferUsage = iota
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
BufferTypeIndices BufferType = iota
|
BufferTypeIndices BufferType = iota
|
||||||
BufferTypeData
|
BufferTypeData
|
||||||
|
|||||||
+5
-14
@@ -148,7 +148,7 @@ func (b *Backend) NewTexture(minFilter, magFilter gpu.TextureFilter) gpu.Texture
|
|||||||
return tex
|
return tex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Backend) NewBuffer(typ gpu.BufferType) gpu.Buffer {
|
func (b *Backend) NewBuffer(typ gpu.BufferType, data []byte) gpu.Buffer {
|
||||||
obj := b.funcs.CreateBuffer()
|
obj := b.funcs.CreateBuffer()
|
||||||
var gltyp Enum
|
var gltyp Enum
|
||||||
switch typ {
|
switch typ {
|
||||||
@@ -159,7 +159,10 @@ func (b *Backend) NewBuffer(typ gpu.BufferType) gpu.Buffer {
|
|||||||
default:
|
default:
|
||||||
panic("unsupported buffer type")
|
panic("unsupported buffer type")
|
||||||
}
|
}
|
||||||
return &gpuBuffer{funcs: b.funcs, obj: obj, typ: gltyp}
|
buf := &gpuBuffer{funcs: b.funcs, obj: obj, typ: gltyp}
|
||||||
|
buf.Bind()
|
||||||
|
b.funcs.BufferData(gltyp, data, STATIC_DRAW)
|
||||||
|
return buf
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Backend) bindTexture(unit int, t *gpuTexture) {
|
func (b *Backend) bindTexture(unit int, t *gpuTexture) {
|
||||||
@@ -344,18 +347,6 @@ func (b *gpuBuffer) Bind() {
|
|||||||
b.funcs.BindBuffer(b.typ, b.obj)
|
b.funcs.BindBuffer(b.typ, b.obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *gpuBuffer) Upload(usage gpu.BufferUsage, data []byte) {
|
|
||||||
b.Bind()
|
|
||||||
var glusage Enum
|
|
||||||
switch usage {
|
|
||||||
case gpu.BufferUsageStaticDraw:
|
|
||||||
glusage = STATIC_DRAW
|
|
||||||
default:
|
|
||||||
panic("unsupported buffer usage")
|
|
||||||
}
|
|
||||||
b.funcs.BufferData(b.typ, data, glusage)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *gpuFramebuffer) IsComplete() error {
|
func (f *gpuFramebuffer) IsComplete() error {
|
||||||
if st := f.funcs.CheckFramebufferStatus(FRAMEBUFFER); st != FRAMEBUFFER_COMPLETE {
|
if st := f.funcs.CheckFramebufferStatus(FRAMEBUFFER); st != FRAMEBUFFER_COMPLETE {
|
||||||
return fmt.Errorf("incomplete framebuffer, status = 0x%x, err = %d", st, f.funcs.GetError())
|
return fmt.Errorf("incomplete framebuffer, status = 0x%x, err = %d", st, f.funcs.GetError())
|
||||||
|
|||||||
+3
-3
@@ -380,14 +380,14 @@ func newBlitter(ctx Backend) *blitter {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
quadVerts := ctx.NewBuffer(BufferTypeData)
|
quadVerts := ctx.NewBuffer(BufferTypeData,
|
||||||
quadVerts.Upload(BufferUsageStaticDraw,
|
|
||||||
gunsafe.BytesView([]float32{
|
gunsafe.BytesView([]float32{
|
||||||
-1, +1, 0, 0,
|
-1, +1, 0, 0,
|
||||||
+1, +1, 1, 0,
|
+1, +1, 1, 0,
|
||||||
-1, -1, 0, 1,
|
-1, -1, 0, 1,
|
||||||
+1, -1, 1, 1,
|
+1, -1, 1, 1,
|
||||||
}))
|
}),
|
||||||
|
)
|
||||||
b := &blitter{
|
b := &blitter{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
prog: prog,
|
prog: prog,
|
||||||
|
|||||||
+2
-4
@@ -143,8 +143,7 @@ func newStenciler(ctx Backend) *stenciler {
|
|||||||
indices[i*6+4] = i*4 + 1
|
indices[i*6+4] = i*4 + 1
|
||||||
indices[i*6+5] = i*4 + 3
|
indices[i*6+5] = i*4 + 3
|
||||||
}
|
}
|
||||||
indexBuf := ctx.NewBuffer(BufferTypeIndices)
|
indexBuf := ctx.NewBuffer(BufferTypeIndices, gunsafe.BytesView(indices))
|
||||||
indexBuf.Upload(BufferUsageStaticDraw, gunsafe.BytesView(indices))
|
|
||||||
return &stenciler{
|
return &stenciler{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
defFBO: defFBO,
|
defFBO: defFBO,
|
||||||
@@ -218,8 +217,7 @@ func (c *coverer) release() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func buildPath(ctx Backend, p []byte) *pathData {
|
func buildPath(ctx Backend, p []byte) *pathData {
|
||||||
buf := ctx.NewBuffer(BufferTypeData)
|
buf := ctx.NewBuffer(BufferTypeData, p)
|
||||||
buf.Upload(BufferUsageStaticDraw, p)
|
|
||||||
return &pathData{
|
return &pathData{
|
||||||
ncurves: len(p) / path.VertStride,
|
ncurves: len(p) / path.VertStride,
|
||||||
data: buf,
|
data: buf,
|
||||||
|
|||||||
Reference in New Issue
Block a user