Class: Stagecraft::Renderer::ResourceCache
- Inherits:
-
Object
- Object
- Stagecraft::Renderer::ResourceCache
show all
- Defined in:
- lib/stagecraft/renderer/resource_cache.rb
Defined Under Namespace
Classes: Entry, GPUGeometry, GPUTexture
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(device:, queue:, stats:, in_flight_frames: 3) ⇒ ResourceCache
Returns a new instance of ResourceCache.
12
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/stagecraft/renderer/resource_cache.rb', line 12
def initialize(device:, queue:, stats:, in_flight_frames: 3)
@device = device
@queue = queue
@stats = stats
@in_flight_frames = in_flight_frames
@geometries = {}
@textures = {}
@dispose_queue = []
@registered = {}
@frame = 0
end
|
Instance Attribute Details
#frame ⇒ Object
Returns the value of attribute frame.
10
11
12
|
# File 'lib/stagecraft/renderer/resource_cache.rb', line 10
def frame
@frame
end
|
Instance Method Details
#advance_frame ⇒ Object
62
63
64
65
66
67
|
# File 'lib/stagecraft/renderer/resource_cache.rb', line 62
def advance_frame
@frame += 1
ready, @dispose_queue = @dispose_queue.partition { |due, _resource| due <= frame }
ready.each { |_due, resource| destroy_resource(resource) }
self
end
|
#dispose_all ⇒ Object
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/stagecraft/renderer/resource_cache.rb', line 69
def dispose_all
resources = @geometries.values.concat(@textures.values).map(&:resource)
resources.concat(@dispose_queue.map(&:last))
resources.each { |resource| destroy_resource(resource) }
@geometries.clear
@textures.clear
@dispose_queue.clear
@registered.clear
self
end
|
#gpu_geometry(geometry) ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/stagecraft/renderer/resource_cache.rb', line 24
def gpu_geometry(geometry)
raise DisposedError, "cannot upload disposed geometry" if geometry.disposed?
existing = @geometries[geometry.object_id]
if existing&.owner&.equal?(geometry) && existing.version == geometry.version
return existing.resource
end
schedule_resource(existing.resource) if existing
resource = upload_geometry(geometry)
@geometries[geometry.object_id] = Entry.new(geometry, geometry.version, resource)
register_disposal(geometry)
resource
end
|
#gpu_texture(texture) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/stagecraft/renderer/resource_cache.rb', line 39
def gpu_texture(texture)
raise DisposedError, "cannot upload disposed texture" if texture.disposed?
existing = @textures[texture.object_id]
if existing&.owner&.equal?(texture) && existing.version == texture.version
return existing.resource
end
schedule_resource(existing.resource) if existing
resource = upload_texture(texture)
@textures[texture.object_id] = Entry.new(texture, texture.version, resource)
register_disposal(texture)
resource
end
|
#live_count ⇒ Object
80
81
82
|
# File 'lib/stagecraft/renderer/resource_cache.rb', line 80
def live_count
@geometries.length + @textures.length
end
|
#schedule_dispose(cpu_object) ⇒ Object
54
55
56
57
58
59
60
|
# File 'lib/stagecraft/renderer/resource_cache.rb', line 54
def schedule_dispose(cpu_object)
entry = delete_owned_entry(@geometries, cpu_object) ||
delete_owned_entry(@textures, cpu_object)
schedule_resource(entry.resource) if entry
@registered.delete(cpu_object.object_id)
self
end
|