Class: Rugl::Shader

Inherits:
Object
  • Object
show all
Defined in:
lib/rugl/shader.rb

Constant Summary collapse

TYPE_MAP =
{
  vertex: :VERTEX_SHADER,
  fragment: :FRAGMENT_SHADER
}.freeze

Class Method Summary collapse

Class Method Details

.compile(gl:, type:, source:, debug: true) ⇒ Object

Raises:



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rugl/shader.rb', line 11

def compile(gl:, type:, source:, debug: true)
  shader_type = TYPE_MAP.fetch(type.to_sym) do
    raise ShaderCompileError, "Unsupported shader type: #{type.inspect}"
  end

  shader_id = Util.gl_call(gl, :CreateShader, Util.gl_const(shader_type, gl: gl))
  Util.gl_call(gl, :ShaderSource, shader_id, source.to_s)
  Util.gl_call(gl, :CompileShader, shader_id)

  return shader_id unless debug
  return shader_id if shader_compiled?(gl, shader_id)

  log = shader_info_log(gl, shader_id)
  raise ShaderCompileError, "#{type} shader compilation failed: #{log}"
end

.shader_compiled?(gl, shader_id) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
# File 'lib/rugl/shader.rb', line 27

def shader_compiled?(gl, shader_id)
  status = [0]
  Util.gl_call(gl, :GetShaderiv, shader_id, Util.gl_const(:COMPILE_STATUS, gl: gl), status)
  status.first == 1
rescue StandardError
  true
end

.shader_info_log(gl, shader_id) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/rugl/shader.rb', line 35

def shader_info_log(gl, shader_id)
  return "(shader info log unavailable)" unless gl.respond_to?(:GetShaderInfoLog) || gl.respond_to?(:get_shader_info_log)

  Util.gl_call(gl, :GetShaderInfoLog, shader_id).to_s
rescue StandardError
  "(shader info log unavailable)"
end