Class: Engine::OpenGL::ComputeShader

Inherits:
Object
  • Object
show all
Defined in:
lib/engine/opengl/compute_shader.rb

Instance Method Summary collapse

Constructor Details

#initialize(shader_path) ⇒ ComputeShader

Returns a new instance of ComputeShader.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/engine/opengl/compute_shader.rb', line 6

def initialize(shader_path)
  @uniform_locations = {}
  @compute_shader = compile_shader(shader_path)
  @program = Engine::GL.CreateProgram
  Engine::GL.AttachShader(@program, @compute_shader)
  Engine::GL.LinkProgram(@program)

  linked_buf = ' ' * 4
  Engine::GL.GetProgramiv(@program, Engine::GL::LINK_STATUS, linked_buf)
  linked = linked_buf.unpack('L')[0]
  if linked == 0
    compile_log = ' ' * 1024
    Engine::GL.GetProgramInfoLog(@program, 1023, nil, compile_log)
    compute_log = ' ' * 1024
    Engine::GL.GetShaderInfoLog(@compute_shader, 1023, nil, compute_log)
    raise "Shader program failed to link:\n#{compile_log.strip}\n#{compute_log.strip}"
  end
  @uniform_cache = {}
  @uniform_locations = {}
end

Instance Method Details

#dispatch(width, height, depth, textures: [], floats: {}, ints: {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/engine/opengl/compute_shader.rb', line 27

def dispatch(width, height, depth, textures: [], floats: {}, ints: {})
  # Extract gl_texture from ComputeTexture objects
  textures.each_with_index do |texture, slot|
    gl_tex = texture.respond_to?(:gl_texture) ? texture.gl_texture : texture
    set_texture(slot, gl_tex)
  end

  Engine::GL.UseProgram(@program)

  floats.each { |name, value| set_float(name, value) }
  ints.each { |name, value| set_int(name, value) }

  Engine::GL.DispatchCompute(width, height, depth)
  Engine::GL.MemoryBarrier(Engine::GL::SHADER_IMAGE_ACCESS_BARRIER_BIT)
end