Class: Engine::ComputeShader

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

Instance Method Summary collapse

Constructor Details

#initialize(compute_shader) ⇒ ComputeShader

Returns a new instance of ComputeShader.



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

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

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

Instance Method Details

#compile_shader(shader) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/engine/compute_shader.rb', line 26

def compile_shader(shader)
  handle = GL.CreateShader(GL::COMPUTE_SHADER)
  path = File.expand_path(File.join(GAME_DIR, shader))
  s_srcs = [File.read(path)].pack('p')
  s_lens = [File.size(path)].pack('I')
  GL.ShaderSource(handle, 1, s_srcs, s_lens)
  GL.CompileShader(handle)
  handle
end

#dispatch(x, y, z, floats: {}, textures: [], ints: {}, vec3s: {}, vec4s: {}, mat4s: {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/engine/compute_shader.rb', line 36

def dispatch(x, y, z, floats: {}, textures: [], ints: {}, vec3s: {}, vec4s: {}, mat4s: {})
  textures.each_with_index { |texture, slot| set_texture(slot, texture) }

  GL.UseProgram(@program)

  floats.each { |name, value| set_float(name, value) }
  ints.each { |name, value| set_int(name, value) }
  vec3s.each { |name, value| set_vec3(name, value) }
  vec4s.each { |name, value| set_vec4(name, value) }
  mat4s.each { |name, value| set_mat4(name, value) }

  GL.DispatchCompute(x, y, z)
  GL.MemoryBarrier(GL::SHADER_IMAGE_ACCESS_BARRIER_BIT)
end