diff --git a/gpu/caches.go b/gpu/caches.go index b44f7ead..0945e7a1 100644 --- a/gpu/caches.go +++ b/gpu/caches.go @@ -8,8 +8,13 @@ import ( "gioui.org/internal/f32" ) -type resourceCache struct { - res map[interface{}]resourceCacheValue +type textureCacheKey struct { + filter byte + handle any +} + +type textureCache struct { + res map[textureCacheKey]resourceCacheValue } type resourceCacheValue struct { @@ -37,13 +42,13 @@ type opCacheValue struct { keep bool } -func newResourceCache() *resourceCache { - return &resourceCache{ - res: make(map[interface{}]resourceCacheValue), +func newTextureCache() *textureCache { + return &textureCache{ + res: make(map[textureCacheKey]resourceCacheValue), } } -func (r *resourceCache) get(key interface{}) (resource, bool) { +func (r *textureCache) get(key textureCacheKey) (resource, bool) { v, exists := r.res[key] if !exists { return nil, false @@ -55,17 +60,17 @@ func (r *resourceCache) get(key interface{}) (resource, bool) { return v.resource, exists } -func (r *resourceCache) put(key interface{}, val resource) { +func (r *textureCache) put(key textureCacheKey, val resource) { v, exists := r.res[key] if exists && v.used { - panic(fmt.Errorf("key exists, %p", key)) + panic(fmt.Errorf("key exists, %v", key)) } v.used = true v.resource = val r.res[key] = v } -func (r *resourceCache) frame() { +func (r *textureCache) frame() { for k, v := range r.res { if v.used { v.used = false @@ -77,7 +82,7 @@ func (r *resourceCache) frame() { } } -func (r *resourceCache) release() { +func (r *textureCache) release() { for _, v := range r.res { v.resource.release() } diff --git a/gpu/caches_test.go b/gpu/caches_test.go deleted file mode 100644 index 411de5af..00000000 --- a/gpu/caches_test.go +++ /dev/null @@ -1,24 +0,0 @@ -// SPDX-License-Identifier: Unlicense OR MIT - -package gpu - -import "testing" - -func BenchmarkResourceCache(b *testing.B) { - offset := 0 - const N = 100 - - cache := newResourceCache() - for i := 0; i < b.N; i++ { - // half are the same and half updated - for k := 0; k < N; k++ { - cache.put(offset+k, nullResource{}) - } - cache.frame() - offset += N / 2 - } -} - -type nullResource struct{} - -func (nullResource) release() {} diff --git a/gpu/gpu.go b/gpu/gpu.go index f616fd29..362eb141 100644 --- a/gpu/gpu.go +++ b/gpu/gpu.go @@ -51,7 +51,7 @@ type GPU interface { } type gpu struct { - cache *resourceCache + cache *textureCache profile string timers *timers @@ -359,7 +359,7 @@ func NewWithDevice(d driver.Device) (GPU, error) { func newGPU(ctx driver.Device) (*gpu, error) { g := &gpu{ - cache: newResourceCache(), + cache: newTextureCache(), } g.drawOps.pathCache = newOpCache() if err := g.init(ctx); err != nil { @@ -460,12 +460,8 @@ func (g *gpu) Profile() string { return g.profile } -func (r *renderer) texHandle(cache *resourceCache, data imageOpData) driver.Texture { - type cachekey struct { - filter byte - handle any - } - key := cachekey{ +func (r *renderer) texHandle(cache *textureCache, data imageOpData) driver.Texture { + key := textureCacheKey{ filter: data.filter, handle: data.handle, } @@ -1211,7 +1207,7 @@ func (d *drawState) materialFor(rect f32.Rectangle, off f32.Point, partTrans f32 return m } -func (r *renderer) uploadImages(cache *resourceCache, ops []imageOp) { +func (r *renderer) uploadImages(cache *textureCache, ops []imageOp) { for i := range ops { img := &ops[i] m := img.material