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:
Elias Naur
2020-02-16 17:48:58 +01:00
parent 49365dbcc5
commit f62725ea77
4 changed files with 11 additions and 28 deletions
+1 -7
View File
@@ -22,7 +22,7 @@ type Backend interface {
DefaultFramebuffer() Framebuffer
NilTexture() Texture
NewFramebuffer() Framebuffer
NewBuffer(typ BufferType) Buffer
NewBuffer(typ BufferType, data []byte) Buffer
NewProgram(vertexShader, fragmentShader string, attribMap []string) (Program, error)
SetupVertexArray(slot int, size int, dataType DataType, stride, offset int)
@@ -49,7 +49,6 @@ type TextureFilter uint8
type TextureFormat uint8
type BufferType uint8
type BufferUsage uint8
type DataType uint8
@@ -76,7 +75,6 @@ type Uniform interface{}
type Buffer interface {
Bind()
Upload(usage BufferUsage, data []byte)
Release()
}
@@ -116,10 +114,6 @@ const (
DataTypeShort
)
const (
BufferUsageStaticDraw BufferUsage = iota
)
const (
BufferTypeIndices BufferType = iota
BufferTypeData
+5 -14
View File
@@ -148,7 +148,7 @@ func (b *Backend) NewTexture(minFilter, magFilter gpu.TextureFilter) gpu.Texture
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()
var gltyp Enum
switch typ {
@@ -159,7 +159,10 @@ func (b *Backend) NewBuffer(typ gpu.BufferType) gpu.Buffer {
default:
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) {
@@ -344,18 +347,6 @@ func (b *gpuBuffer) Bind() {
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 {
if st := f.funcs.CheckFramebufferStatus(FRAMEBUFFER); st != FRAMEBUFFER_COMPLETE {
return fmt.Errorf("incomplete framebuffer, status = 0x%x, err = %d", st, f.funcs.GetError())
+3 -3
View File
@@ -380,14 +380,14 @@ func newBlitter(ctx Backend) *blitter {
if err != nil {
panic(err)
}
quadVerts := ctx.NewBuffer(BufferTypeData)
quadVerts.Upload(BufferUsageStaticDraw,
quadVerts := ctx.NewBuffer(BufferTypeData,
gunsafe.BytesView([]float32{
-1, +1, 0, 0,
+1, +1, 1, 0,
-1, -1, 0, 1,
+1, -1, 1, 1,
}))
}),
)
b := &blitter{
ctx: ctx,
prog: prog,
+2 -4
View File
@@ -143,8 +143,7 @@ func newStenciler(ctx Backend) *stenciler {
indices[i*6+4] = i*4 + 1
indices[i*6+5] = i*4 + 3
}
indexBuf := ctx.NewBuffer(BufferTypeIndices)
indexBuf.Upload(BufferUsageStaticDraw, gunsafe.BytesView(indices))
indexBuf := ctx.NewBuffer(BufferTypeIndices, gunsafe.BytesView(indices))
return &stenciler{
ctx: ctx,
defFBO: defFBO,
@@ -218,8 +217,7 @@ func (c *coverer) release() {
}
func buildPath(ctx Backend, p []byte) *pathData {
buf := ctx.NewBuffer(BufferTypeData)
buf.Upload(BufferUsageStaticDraw, p)
buf := ctx.NewBuffer(BufferTypeData, p)
return &pathData{
ncurves: len(p) / path.VertStride,
data: buf,