Class: Stagecraft::Renderer::FrameResources

Inherits:
Object
  • Object
show all
Defined in:
lib/stagecraft/renderer/frame_resources.rb

Defined Under Namespace

Classes: SkinBinding

Constant Summary collapse

OBJECT_SLOT_SIZE =
256
OBJECT_DATA_SIZE =
144
FRAMES_IN_FLIGHT =
3
LIGHT_SIZE =
80
GUARANTEED_SAMPLE_COUNTS =
[1, 4].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context:, stats:, sample_count:) ⇒ FrameResources

Returns a new instance of FrameResources.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/stagecraft/renderer/frame_resources.rb', line 25

def initialize(context:, stats:, sample_count:)
  @context = context
  @device = context.device
  @queue = context.queue
  @stats = stats
  @sample_count = self.class.guaranteed_sample_count(sample_count)
  @frame_number = 0
  @object_capacity = 1
  @light_capacity = 1
  @skin_bindings = {}
  create_layouts
  create_buffers
  create_shadow_resources
  create_fallback_textures
  resize(context.width, context.height)
rescue StandardError
  dispose_after_failed_initialization
  raise
end

Instance Attribute Details

#depth_textureObject (readonly)

Returns the value of attribute depth_texture.



13
14
15
# File 'lib/stagecraft/renderer/frame_resources.rb', line 13

def depth_texture
  @depth_texture
end

#depth_viewObject (readonly)

Returns the value of attribute depth_view.



13
14
15
# File 'lib/stagecraft/renderer/frame_resources.rb', line 13

def depth_view
  @depth_view
end

#deviceObject (readonly)

Returns the value of attribute device.



13
14
15
# File 'lib/stagecraft/renderer/frame_resources.rb', line 13

def device
  @device
end

#empty_groupObject (readonly)

Returns the value of attribute empty_group.



13
14
15
# File 'lib/stagecraft/renderer/frame_resources.rb', line 13

def empty_group
  @empty_group
end

#empty_layoutObject (readonly)

Returns the value of attribute empty_layout.



13
14
15
# File 'lib/stagecraft/renderer/frame_resources.rb', line 13

def empty_layout
  @empty_layout
end

#fallback_blackObject (readonly)

Returns the value of attribute fallback_black.



13
14
15
# File 'lib/stagecraft/renderer/frame_resources.rb', line 13

def fallback_black
  @fallback_black
end

#fallback_normalObject (readonly)

Returns the value of attribute fallback_normal.



13
14
15
# File 'lib/stagecraft/renderer/frame_resources.rb', line 13

def fallback_normal
  @fallback_normal
end

#fallback_whiteObject (readonly)

Returns the value of attribute fallback_white.



13
14
15
# File 'lib/stagecraft/renderer/frame_resources.rb', line 13

def fallback_white
  @fallback_white
end

#frame_groupObject (readonly)

Returns the value of attribute frame_group.



13
14
15
# File 'lib/stagecraft/renderer/frame_resources.rb', line 13

def frame_group
  @frame_group
end

#frame_layoutObject (readonly)

Returns the value of attribute frame_layout.



13
14
15
# File 'lib/stagecraft/renderer/frame_resources.rb', line 13

def frame_layout
  @frame_layout
end

#hdr_textureObject (readonly)

Returns the value of attribute hdr_texture.



13
14
15
# File 'lib/stagecraft/renderer/frame_resources.rb', line 13

def hdr_texture
  @hdr_texture
end

#hdr_viewObject (readonly)

Returns the value of attribute hdr_view.



13
14
15
# File 'lib/stagecraft/renderer/frame_resources.rb', line 13

def hdr_view
  @hdr_view
end

#heightObject (readonly)

Returns the value of attribute height.



13
14
15
# File 'lib/stagecraft/renderer/frame_resources.rb', line 13

def height
  @height
end

#object_groupObject (readonly)

Returns the value of attribute object_group.



13
14
15
# File 'lib/stagecraft/renderer/frame_resources.rb', line 13

def object_group
  @object_group
end

#object_layoutObject (readonly)

Returns the value of attribute object_layout.



13
14
15
# File 'lib/stagecraft/renderer/frame_resources.rb', line 13

def object_layout
  @object_layout
end

#output_textureObject (readonly)

Returns the value of attribute output_texture.



13
14
15
# File 'lib/stagecraft/renderer/frame_resources.rb', line 13

def output_texture
  @output_texture
end

#output_viewObject (readonly)

Returns the value of attribute output_view.



13
14
15
# File 'lib/stagecraft/renderer/frame_resources.rb', line 13

def output_view
  @output_view
end

#queueObject (readonly)

Returns the value of attribute queue.



13
14
15
# File 'lib/stagecraft/renderer/frame_resources.rb', line 13

def queue
  @queue
end

#sample_countObject (readonly)

Returns the value of attribute sample_count.



13
14
15
# File 'lib/stagecraft/renderer/frame_resources.rb', line 13

def sample_count
  @sample_count
end

#shadow_frame_groupObject (readonly)

Returns the value of attribute shadow_frame_group.



13
14
15
# File 'lib/stagecraft/renderer/frame_resources.rb', line 13

def shadow_frame_group
  @shadow_frame_group
end

#shadow_frame_layoutObject (readonly)

Returns the value of attribute shadow_frame_layout.



13
14
15
# File 'lib/stagecraft/renderer/frame_resources.rb', line 13

def shadow_frame_layout
  @shadow_frame_layout
end

#shadow_textureObject (readonly)

Returns the value of attribute shadow_texture.



13
14
15
# File 'lib/stagecraft/renderer/frame_resources.rb', line 13

def shadow_texture
  @shadow_texture
end

#shadow_viewObject (readonly)

Returns the value of attribute shadow_view.



13
14
15
# File 'lib/stagecraft/renderer/frame_resources.rb', line 13

def shadow_view
  @shadow_view
end

#statsObject (readonly)

Returns the value of attribute stats.



13
14
15
# File 'lib/stagecraft/renderer/frame_resources.rb', line 13

def stats
  @stats
end

#widthObject (readonly)

Returns the value of attribute width.



13
14
15
# File 'lib/stagecraft/renderer/frame_resources.rb', line 13

def width
  @width
end

Class Method Details

.guaranteed_sample_count(requested) ⇒ Object



19
20
21
22
23
# File 'lib/stagecraft/renderer/frame_resources.rb', line 19

def self.guaranteed_sample_count(requested)
  value = Integer(requested)
  GUARANTEED_SAMPLE_COUNTS.reverse.find { |count| count <= value } ||
    GUARANTEED_SAMPLE_COUNTS.first
end

Instance Method Details

#begin_frame(items, camera:, ambient:, lights:, light_vp:, time:) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/stagecraft/renderer/frame_resources.rb', line 54

def begin_frame(items, camera:, ambient:, lights:, light_vp:, time:)
  ensure_object_capacity(items.length)
  ensure_light_capacity(lights.length)
  write_frame(camera:, ambient:, lights:, light_vp:, time:)
  write_objects(items)
  write_skins(items)
  @frame_number += 1
  self
end

#disposeObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/stagecraft/renderer/frame_resources.rb', line 103

def dispose
  release_attachments
  release_resource(@shadow_sampler, @shadow_view)
  destroy_texture(@shadow_texture)
  [fallback_white, fallback_black, fallback_normal].compact.each do |resource|
    release_gpu_texture(resource)
  end
  release_resource(@frame_group, @shadow_frame_group, @object_group, @empty_group)
  @skin_bindings.each_value do |binding|
    release_resource(binding.bind_group)
    destroy_buffer(binding.buffer)
  end
  @skin_bindings.clear
  [@frame_buffer, @lights_buffer, @object_buffer, @joint_buffer].compact.each do |buffer|
    destroy_buffer(buffer)
  end
  release_resource(frame_layout, object_layout, empty_layout, shadow_frame_layout)
end

#frame_targetObject



97
98
99
100
101
# File 'lib/stagecraft/renderer/frame_resources.rb', line 97

def frame_target
  return [output_texture, output_view] unless @context.surface

  @context.current_target
end

#main_color_attachmentObject



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/stagecraft/renderer/frame_resources.rb', line 84

def main_color_attachment
  if sample_count > 1
    {
      view: @hdr_msaa_view,
      resolve_target: hdr_view,
      load_op: :clear,
      store_op: :discard
    }
  else
    { view: hdr_view, load_op: :clear, store_op: :store }
  end
end

#object_group_for(mesh) ⇒ Object



69
70
71
# File 'lib/stagecraft/renderer/frame_resources.rb', line 69

def object_group_for(mesh)
  mesh.skin ? @skin_bindings.fetch(mesh.object_id).bind_group : object_group
end

#object_offset(index) ⇒ Object



64
65
66
67
# File 'lib/stagecraft/renderer/frame_resources.rb', line 64

def object_offset(index)
  frame_slot = (@frame_number - 1) % FRAMES_IN_FLIGHT
  (frame_slot * @object_capacity * OBJECT_SLOT_SIZE) + (index * OBJECT_SLOT_SIZE)
end

#resize(width, height) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/stagecraft/renderer/frame_resources.rb', line 45

def resize(width, height)
  @width = Integer(width)
  @height = Integer(height)
  release_attachments
  create_hdr_attachments
  create_output_attachment unless @context.surface
  self
end

#resize_shadow(map_size) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/stagecraft/renderer/frame_resources.rb', line 73

def resize_shadow(map_size)
  next_size = Integer(map_size)
  return self if next_size == @shadow_size

  release_resource(@shadow_view)
  destroy_texture(@shadow_texture)
  create_shadow_texture(next_size)
  recreate_frame_groups
  self
end