Skip to content

use Preferences.jl for debug settings #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ version = "1.1.8"

[deps]
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
Preferences = "21216c6a-2e73-6563-6e65-726566657250"

[compat]
julia = "1"
julia = "1.6"

[extras]
GLFW = "f7f18e0c-5ee9-5ccd-a5bf-e8befd85ed98"
Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ The philosophy is to keep this library strictly a low-level wrapper, so you won'

You can rebuild ModernGL to include debug error checks:
```Julia
ENV["MODERNGL_DEBUGGING"] = "true"; Pkg.build("ModernGL")
# or to get back the default behaviour:
ENV["MODERNGL_DEBUGGING"] = "false"; Pkg.build("ModernGL")
using ModernGL
ModernGL.enable_debugging!()
# ModernGL.disable_debugging!() to turn it off

# restart Julia
using ModernGL
ModernGL.OPENGL_DEBUG # should be true (false) now
```

### Installation notes
Expand Down
1 change: 1 addition & 0 deletions src/ModernGL.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module ModernGL

using Libdl
using Preferences

function glXGetProcAddress(glFuncName)
ccall((:glXGetProcAddress, "libGL.so.1"), Ptr{Cvoid}, (Ptr{UInt8},), glFuncName)
Expand Down
24 changes: 9 additions & 15 deletions src/functionloading.jl
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
function should_enable_opengl_debugging()
v = get(ENV, "MODERNGL_DEBUGGING", "false")
if v in ("true", "false")
return v == "true"
else
error("MODERNGL_DEBUGGING must be either 'true' or 'false'.")
end
end
const OPENGL_DEBUG = @load_preference("DEBUG", false)

# decide this early here to debug any workload precompilation *in this package* before __init__ is run
const enable_opengl_debugging = Ref{Bool}(should_enable_opengl_debugging())

function __init__()
# the env var may have changed since precompilation
enable_opengl_debugging[] = should_enable_opengl_debugging()
enable_debugging!() = set_debug!(true)
disable_debugging!() = set_debug!(false)
function set_debug!(value::Bool)
if value != OPENGL_DEBUG
@set_preferences!("DEBUG" => value)
@info "Changing the debug mode requires restarting Julia to take effect!"
end
end

gl_represent(x::GLenum) = GLENUM(x).name
gl_represent(x) = repr(x)

function debug_opengl_expr(func_name, args)
if enable_opengl_debugging[] && func_name != :glGetError
if OPENGL_DEBUG && func_name != :glGetError
quote
err = glGetError()
if err != GL_NO_ERROR
Expand Down