Skip to content

Commit

Permalink
fix opengl debug
Browse files Browse the repository at this point in the history
  • Loading branch information
danini-the-panini committed Nov 11, 2024
1 parent eb5c704 commit e062b5e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 37 deletions.
76 changes: 42 additions & 34 deletions lib/mittsu/renderers/opengl/opengl_debug.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
# GL::TABLE_TOO_LARGE => 'TABLE_TOO_LARGE'
}

OriginalGL = GL

module OpenGLDebug
class DebugShader
def initialize(handle)
Expand All @@ -28,50 +30,56 @@ def get_uniform_name(handle)
end

def self.load_lib(*args)
GL.load_lib(*args)
OriginalGL.load_lib(*args)
end

GL.constants.each do |c|
const_set c, GL.const_get(c)
OriginalGL.constants.each do |c|
const_set c, OriginalGL.const_get(c)
end

def call_debug_method m, called_from = caller[0], *args
if m.to_s.start_with?('GL.Uniform')
uniform_name = @@current_shader.get_uniform_name(args.first)
call = "#{m}('#{uniform_name}',#{args[1..-1].map { |s| s.to_s[0..20] }.join(', ')})"
else
call = "#{m}(#{args.map { |s| s.to_s[0..20] }.join(', ')})"
end
print call
r = GL.send(m, *args)
ret = r.nil? ? '' : " => #{r}"
puts "#{ret} (#{called_from})"
e = GL.GetError
raise "ERROR: #{m} => #{ERROR_STRINGS[e]}" unless e == GL::NO_ERROR
r
end
class << self
OriginalGL::GL_FUNCTION_SYMBOLS
.map { |m| m.to_s.gsub(/^gl/, '').to_sym }
.each do |m|
define_method(m) do |*args|
call_debug_method(m, caller[0], *args)
end
end

GL.instance_methods.each do |m|
define_method m do |*args|
self.call_debug_method(m, caller[0], *args)
def call_debug_method m, called_from = caller[0], *args
if m.to_s.start_with?('Uniform')
uniform_name = @@current_shader.get_uniform_name(args.first)
call = "#{m}('#{uniform_name}',#{args[1..-1].map { |s| s.to_s[0..20] }.join(', ')})"
else
call = "#{m}(#{args.map { |s| s.to_s[0..20] }.join(', ')})"
end
print call
r = OriginalGL.send(m, *args)
ret = r.nil? ? '' : " => #{r}"
puts "#{ret} (#{called_from})"
e = OriginalGL.GetError
raise "ERROR: #{m} => #{ERROR_STRINGS[e]}" unless e == GL::NO_ERROR
r
end
end

def CreateProgram
call_debug_method(:GL.CreateProgram, caller[0]).tap do |handle|
@@shaders ||= {}
@@shaders[handle] = DebugShader.new(handle)
def CreateProgram
call_debug_method(:CreateProgram, caller[0]).tap do |handle|
@@shaders ||= {}
@@shaders[handle] = DebugShader.new(handle)
end
end
end

def UseProgram(handle)
@@current_shader = @@shaders[handle]
call_debug_method(:UseProgram, caller[0], handle)
end
def UseProgram(handle)
@@current_shader = @@shaders[handle]
call_debug_method(:UseProgram, caller[0], handle)
end

def GetUniformLocation(program, name)
call_debug_method(:GetUniformLocation, caller[0], program, name).tap do |handle|
@@shaders[program].set_uniform(handle, name)
def GetUniformLocation(program, name)
call_debug_method(:GetUniformLocation, caller[0], program, name).tap do |handle|
@@shaders[program].set_uniform(handle, name)
end
end
end
end

GL = OpenGLDebug
4 changes: 1 addition & 3 deletions lib/mittsu/renderers/opengl_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
GL.load_lib(ENV["MITTSU_LIBGL_FILE"] || opengl_lib.file, ENV["MITTSU_LIBGL_PATH"] || opengl_lib.path)

require 'mittsu/renderers/glfw_window'
require 'mittsu/renderers/opengl/opengl_debug' if ENV['DEBUG']
require 'mittsu/renderers/opengl/opengl_implementations'
require 'mittsu/renderers/opengl/opengl_extensions'
require 'mittsu/renderers/opengl/opengl_debug'
require 'mittsu/renderers/opengl/opengl_helper'
require 'mittsu/renderers/opengl/opengl_program'
require 'mittsu/renderers/opengl/opengl_state'
Expand All @@ -21,8 +21,6 @@
require 'mittsu/renderers/opengl/plugins/sprite_plugin'
require 'mittsu/renderers/shaders/shader_lib'
require 'mittsu/renderers/shaders/uniforms_utils'

require 'mittsu/renderers/opengl/debug' if ENV['DEBUG']
include Mittsu::OpenGLHelper

require 'mittsu/renderers/opengl/opengl_mittsu_params'
Expand Down

0 comments on commit e062b5e

Please # to comment.