Class: Engine::Metal::ComputeShader
- Inherits:
-
Object
- Object
- Engine::Metal::ComputeShader
- Includes:
- Fiddle
- Defined in:
- lib/engine/metal/compute_shader.rb
Instance Method Summary collapse
- #dispatch(width, height, depth, textures: [], floats: {}, ints: {}) ⇒ Object
-
#initialize(shader_path) ⇒ ComputeShader
constructor
A new instance of ComputeShader.
Constructor Details
#initialize(shader_path) ⇒ ComputeShader
Returns a new instance of ComputeShader.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/engine/metal/compute_shader.rb', line 10 def initialize(shader_path) @device = Device.instance @uniform_buffer_data = {} path = File.(File.join(GAME_DIR, shader_path)) source = File.read(path) @library = @device.new_library_with_source(source) @pipeline = @device.new_compute_pipeline(@library, 'computeMain') # Get thread execution width for optimal dispatch @thread_width = ObjC.msg(@pipeline, 'threadExecutionWidth').to_i @thread_height = ObjC.msg(@pipeline, 'maxTotalThreadsPerThreadgroup').to_i / @thread_width end |
Instance Method Details
#dispatch(width, height, depth, textures: [], floats: {}, ints: {}) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/engine/metal/compute_shader.rb', line 25 def dispatch(width, height, depth, textures: [], floats: {}, ints: {}) command_buffer = @device.new_command_buffer encoder = ObjC.msg(command_buffer, 'computeCommandEncoder') # Set the pipeline state ObjC.msg(encoder, 'setComputePipelineState:', @pipeline) # Set textures (extract metal_texture from ComputeTexture objects) textures.each_with_index do |texture, index| next if texture.nil? = texture.respond_to?(:metal_texture) ? texture. : texture ObjC.msg(encoder, 'setTexture:atIndex:', , index) end # Create uniform buffer with floats and ints if floats.any? || ints.any? uniform_data = create_uniform_buffer(floats, ints) uniform_buffer = (uniform_data) ObjC.msg(encoder, 'setBuffer:offset:atIndex:', uniform_buffer, 0, 0) end # Calculate thread groups # Use dispatchThreads for simpler dispatch (Metal 2.0+) threads = create_mtl_size_ptr(width, height, depth) threads_per_group = create_mtl_size_ptr(@thread_width, @thread_height, 1) # Use objc_msgSend_stret variant for struct-passing methods dispatch_threads(encoder, threads, threads_per_group) ObjC.msg(encoder, 'endEncoding') ObjC.msg(command_buffer, 'commit') ObjC.msg(command_buffer, 'waitUntilCompleted') # Sync textures to make results available for OpenGL rendering textures.each { |tex| tex.sync if tex.respond_to?(:sync) } end |