Class: Stagecraft::Renderer::PipelineFactory

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

Defined Under Namespace

Classes: MaterialBinding

Constant Summary collapse

ATTRIBUTE_LOCATIONS =
{
  position: 0, normal: 1, uv: 2, tangent: 3, color: 4, joints: 5, weights: 6,
  uv1: 7
}.freeze
FORMAT_SIZES =
{
  float32: 4, float32x2: 8, float32x3: 12, float32x4: 16,
  sint8x2: 2, sint8x4: 4, snorm8x2: 2, snorm8x4: 4,
  sint16x2: 4, sint16x4: 8, snorm16x2: 4, snorm16x4: 8,
  uint16: 2, uint16x2: 4, uint16x4: 8,
  unorm16x2: 4, unorm16x4: 8,
  uint32: 4, uint32x2: 8, uint32x3: 12, uint32x4: 16,
  unorm8x2: 2, unorm8x4: 4, uint8x2: 2, uint8x4: 4
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(device:, queue:, resources:, resource_cache:, stats:, sample_count:) ⇒ PipelineFactory

Returns a new instance of PipelineFactory.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/stagecraft/renderer/pipeline_factory.rb', line 24

def initialize(device:, queue:, resources:, resource_cache:, stats:, sample_count:)
  @device = device
  @queue = queue
  @resources = resources
  @resource_cache = resource_cache
  @sample_count = sample_count
  @stats = stats
  @cache = PipelineCache.new(on_change: ->(amount) { stats.increment(:pipelines, amount) })
  @material_layouts = {}
  @material_bindings = {}
end

Instance Method Details

#disposeObject



125
126
127
128
129
130
131
132
# File 'lib/stagecraft/renderer/pipeline_factory.rb', line 125

def dispose
  @cache.clear
  @material_bindings.each_value { |binding| release_material_binding(binding) }
  @material_bindings.clear
  @material_layouts.each_value { |layout| layout.release if layout.respond_to?(:release) }
  @material_layouts.clear
  @post_layout&.release
end

#material_binding(material, features) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/stagecraft/renderer/pipeline_factory.rb', line 53

def material_binding(material, features)
  signature = material_layout_signature(material)
  key = [material.object_id, signature]
  purge_obsolete_material_bindings(material, key)
  texture_versions = material_texture_versions(material)
  value_signature = material_value_signature(material)
  existing = @material_bindings[key]
  if existing&.owner&.equal?(material) &&
     existing.version == material.version &&
     existing.texture_versions == texture_versions &&
     existing.value_signature == value_signature
    return existing.bind_group
  end

  bytes, textures = material_bytes_and_textures(material)
  if existing&.owner&.equal?(material) &&
     existing.buffer.size == bytes.bytesize &&
     existing.texture_versions == texture_versions
    @queue.write_buffer(existing.buffer, 0, bytes)
    existing.version = material.version
    existing.value_signature = value_signature
    return existing.bind_group
  end

  release_material_binding(existing)
  buffer = @device.create_buffer_with_data(
    label: "stagecraft material",
    data: bytes.empty? ? "\0".b * 16 : bytes,
    usage: %i[uniform copy_dst]
  )
  @stats.increment(:buffers)
  layout = material_layout(material)
  bind_group = @device.create_bind_group(
    layout:,
    entries: material_entries(buffer, textures, material)
  )
  @material_bindings[key] = MaterialBinding.new(
    material, material.version, texture_versions, value_signature,
    buffer, bind_group, signature
  )
  bind_group
end

#pipeline_for(item, shadow: false) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/stagecraft/renderer/pipeline_factory.rb', line 36

def pipeline_for(item, shadow: false)
  material = item.mesh.material
  layout_signature = material_layout_signature(material)
  key = PipelineCache::Key.new(
    material_class: material_pipeline_signature(material, layout_signature),
    feature_bits: Features.bits(item.features),
    vertex_layout_id: Features.vertex_layout_id(item.mesh.geometry),
    blend_state: material.blend,
    depth_state: [material.depth_test, material.depth_write],
    cull_mode: material.side,
    sample_count: shadow ? 1 : @sample_count,
    color_format: shadow ? :depth32_float : :rgba16_float,
    shadow_pass: shadow
  )
  @cache.fetch(key) { build_pipeline(item, shadow:) }
end

#post_layoutObject



116
117
118
119
120
121
122
123
# File 'lib/stagecraft/renderer/pipeline_factory.rb', line 116

def post_layout
  @post_layout ||= @device.create_bind_group_layout(
    entries: [
      { binding: 0, visibility: :fragment, texture: { sample_type: :float } },
      { binding: 1, visibility: :fragment, sampler: { type: :filtering } }
    ]
  )
end

#post_pipeline(format) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/stagecraft/renderer/pipeline_factory.rb', line 96

def post_pipeline(format)
  defines = format.to_s.end_with?("_srgb") ? Set.new : Set[:ENCODE_SRGB]
  key = [:post, format, defines.hash]
  @cache.fetch(key) do
    shader = @device.create_shader_module(code: Shaders.compose("tonemap.wgsl", defines:))
    layout = @device.create_pipeline_layout(bind_group_layouts: [post_layout])
    @device.create_render_pipeline(
      label: "stagecraft tonemap",
      layout:,
      vertex: { module: shader, entry_point: "vs_main" },
      primitive: { topology: :triangle_list, cull_mode: :none },
      fragment: {
        module: shader,
        entry_point: "fs_main",
        targets: [{ format: }]
      }
    )
  end
end