Home

Awesome

BindBC-OpenGL

This project provides dynamic bindings to the OpenGL library. It supports OpenGL versions up to and including 4.6, along with numerous extensions (if an extension you need is not yet supported, please submit a PR or open an issue). BindBC-OpenGL is compatible with @nogc and nothrow and can be compiled with BetterC compatibility. This package is intended as a replacement of DerelictGL3, which does not provide the same level of compatibility.

NOTE: BindBC-OpenGL does not support static binding due to the nature of the OpenGL API.

NOTE: This documentation describes how to use BindBC-OpenGL. As the maintainer of this library, I do not provide instructions on using the OpenGL library. However, since this is a direct binding to the OpenGL API, existing OpenGL documentation and tutorials can be adapted to D with few modifications (those being minor differences in the language, such as array declaration syntax). The tutorial at learnopengl.com is a particularly good introduction to OpenGL for those who have little or no experience with the API. For documentation, I prefer the layout of docs.gl over that of the Khronos site.

Add BindBC-OpenGL to your project

By default, BindBC-OpenGL is configured to compile with BetterC compatibility disabled and with support only for up to OpenGL 2.1 core. To use BindBC-OpenGL in this default mode, simply add the package as a dependency in your dub.json or dub.sdl recipe.

Example: dub.json

"dependencies": {
	"bindbc-opengl": "~>1.1.0"
}

Example: dub.sdl

dependency "bindbc-opengl" version="~>1.1.0"

Enable BetterC support

If you are using BetterC, add the dynamicBC sub-configuration to your package recipe.

Example: dub.json

"subConfigurations": {
	"bindbc-opengl": "dynamicBC"
},

Example: dub.sdl

subConfiguration "bindbc-opengl" "dynamicBC"

Enable support for OpenGL 3.0 and higher

Support for OpenGL versions can be configured at compile time by adding the appropriate version to a versions directive in your package configuration file (or on the command line if you are building with a tool other than dub).

BindBC-OpenGL supports a D version identifier for each OpenGL version. The following table lists each identifier and the OpenGL versions they enable.

OpenGL versionsVersion identifier
1.0–2.1Default
1.0–3.0GL_30
1.0–3.1GL_31
1.0–3.2GL_32
1.0–3.3GL_33
1.0–4.0GL_40
1.0–4.1GL_41
1.0–4.2GL_42
1.0–4.3GL_43
1.0–4.4GL_44
1.0–4.5GL_45
1.0–4.6GL_46

[!NOTE]
If you use versions higher than OpenGL 4.1, then your application will not work on macOS; due to OpenGL being deprecated on macOS.

Adding one of these version identifiers to your package recipe will do two things:

To load all functions and enable all constants from "classic" OpenGL versions, including those that have been deprecated, set the version identifier GL_AllowDeprecated in your build system. Note that doing so means you may need to take special steps to configure your OpenGL context to ensure the deprecated functions are made available by the driver. Check the documentation of the API you are using for OpenGL context creation.

The following examples are configured to load the core functions from all OpenGL versions up to OpenGL 4.1:

dub.json

"dependencies": {
	"bindbc-opengl": "~>1.1.0"
}
"versions": [
	"GL_41"
],

dub.sdl

dependency "bindbc-opengl" version="~>1.1.0"
versions "GL_41"

With this configuration, client code can make use of all core OpenGL types and functions up to OpenGL 4.1. At run time, if the context supports OpenGL 4.1 or higher, the loader will attempt to load up to OpenGL 4.1. If the highest OpenGL version the context supports is lower than 4.1, the loader will attempt to load up to that version.

To enable the loading of deprecated functions in the same configuration:

dub.json

"dependencies": {
	"bindbc-opengl": "~>1.1.0"
}
"versions": [
	"GL_41", "GL_AllowDeprecated"
],

dub.sdl

dependency "bindbc-opengl" version="~>1.1.0"
versions "GL_41" "GL_AllowDeprecated"

With this, all OpenGL functions, both core and deprecated, will be loaded if the context has been configured appropriately and they are supported by the driver. Some deprecated features were removed from the specification in later versions of OpenGL.

GL_AllowDeprecated by itself enables support for deprecated functions and constants from OpenGL versions 1.0 - 2.1. When GL_AllowDeprecated is specified in conjunction with GL_30 or higher, support for deprecated constants from OpenGL version 3.0 will be enabled.

Enable support for extensions

Extension support is enabled on an as-needed basis. All supported ARB/KHR extensions can be enabled by adding the GL_ARB version identifier to your dub.json or dub.sdl. Supported NV (Nvidia) extensions are enabled via GL_NV.

For example, the following enables support for all core OpenGL functions up to and including GL 4.1, as well as all ARB/KHR extensions:

dub.json

"dependencies": {
	"bindbc-opengl": "~>1.1.0"
}
"versions": [
	"GL_41",
	"GL_ARB"
],

dub.sdl

dependency "bindbc-opengl" version="~>1.1.0"
versions "GL_41" "GL_ARB"

Extensions which were promoted to the core OpenGL API are loaded automatically along with the OpenGL version APIs to which they belong.

Specific extensions can be enabled using the extension's OpenGL name string as a version identifier. The name string for each extension is listed below. They the form of GL_ prefixed to the extension name.

Version added to coreOpenGL extensionVersion identifier
N/AARB_bindless_textureGL_ARB_bindless_texture
N/AARB_cl_eventGL_ARB_cl_event
N/AARB_compute_variable_group_sizeGL_ARB_compute_variable_group_size
N/AARB_debug_outputGL_ARB_debug_output
N/AARB_framebuffer_sRGBGL_ARB_framebuffer_sRGB
N/AARB_geometry_shader4GL_ARB_geometry_shader4
N/AARB_gl_spirvGL_ARB_gl_spirv
N/AARB_gpu_shader_int64GL_ARB_gpu_shader_int64
N/AARB_indirect_parametersGL_ARB_indirect_parameters
N/AARB_instanced_arraysGL_ARB_instanced_arrays
N/AARB_pipeline_statistics_queryGL_ARB_pipeline_statistics_query
N/AARB_robustness_isolationGL_ARB_robustness_isolation
N/AARB_sample_shadingGL_ARB_sample_shading
N/AARB_texture_buffer_objectGL_ARB_texture_buffer_object
N/AARB_texture_compression_bptcGL_ARB_texture_compression_bptc
N/AARB_texture_cube_map_arrayGL_ARB_texture_cube_map_array
N/AARB_texture_gatherGL_ARB_texture_gather
N/AARB_transform_feedback_overflow_queryGL_ARB_transform_feedback_overflow_query
N/AARB_sparse_textureGL_ARB_sparse_texture
N/AARB_sparse_texture2GL_ARB_sparse_texture2
N/AARB_sparse_texture_clampGL_ARB_sparse_texture_clamp
3.0ARB_depth_buffer_floatGL_ARB_depth_buffer_float
3.0ARB_half_float_vertexGL_ARB_half_float_vertex
3.0ARB_texture_compression_rgtcGL_ARB_texture_compression_rgtc
3.0ARB_texture_rgGL_ARB_texture_rg
3.0ARB_framebuffer_objectGL_ARB_framebuffer_object
3.0ARB_map_buffer_rangeGL_ARB_map_buffer_range
3.0ARB_vertex_array_objectGL_ARB_vertex_array_object
3.1ARB_copy_bufferGL_ARB_copy_buffer
3.1ARB_uniform_buffer_objectGL_ARB_uniform_buffer_object
3.2ARB_depth_clampGL_ARB_depth_clamp
3.2ARB_provoking_vertexGL_ARB_provoking_vertex
3.2ARB_seamless_cube_mapGL_ARB_seamless_cube_map
3.2ARB_draw_elements_base_vertexGL_ARB_draw_elements_base_vertex
3.2ARB_syncGL_ARB_sync
3.2ARB_texture_multisampleGL_ARB_texture_multisample
3.3ARB_occlusion_query2GL_ARB_occlusion_query2
3.3ARB_texture_rgb10_a2uiGL_ARB_texture_rgb10_a2ui
3.3ARB_texture_swizzleGL_ARB_texture_swizzle
3.3ARB_blend_func_extendedGL_ARB_blend_func_extended
3.3ARB_sampler_objectsGL_ARB_sampler_objects
3.3ARB_timer_queryGL_ARB_timer_query
3.3ARB_vertex_type_2_10_10_10_revGL_ARB_vertex_type_2_10_10_10_rev
4.0ARB_gpu_shader5GL_ARB_gpu_shader5
4.0ARB_draw_indirectGL_ARB_draw_indirect
4.0ARB_gpu_shader_fp64GL_ARB_gpu_shader_fp64
4.0ARB_shader_subroutineGL_ARB_shader_subroutine
4.0ARB_tessellation_shaderGL_ARB_tessellation_shader
4.0ARB_transform_feedback2GL_ARB_transform_feedback2
4.0ARB_transform_feedback3GL_ARB_transform_feedback3
4.1ARB_ES2_compatibilityGL_ARB_ES2_compatibility
4.1ARB_get_program_binaryGL_ARB_get_program_binary
4.1ARB_separate_shader_objectsGL_ARB_separate_shader_objects
4.1ARB_vertex_attrib_64bitGL_ARB_vertex_attrib_64bit
4.1ARB_viewport_arrayGL_ARB_viewport_array
4.2ARB_base_instanceGL_ARB_base_instance
4.2ARB_compressed_texture_pixel_storageGL_ARB_compressed_texture_pixel_storage
4.2ARB_internalformat_queryGL_ARB_internalformat_query
4.2ARB_map_buffer_alignmentGL_ARB_map_buffer_alignment
4.2ARB_shader_image_load_storeGL_ARB_shader_image_load_store
4.2ARB_texture_storageGL_ARB_texture_storage
4.2ARB_transform_feedback_instancedGL_ARB_transform_feedback_instanced
4.3ARB_clear_buffer_objectGL_ARB_clear_buffer_object
4.3ARB_compute_shaderGL_ARB_compute_shader
4.3ARB_copy_imageGL_ARB_copy_image
4.3ARB_ES3_compatibilityGL_ARB_ES3_compatibility
4.3ARB_explicit_uniform_locationGL_ARB_explicit_uniform_location
4.3ARB_framebuffer_no_attachmentsGL_ARB_framebuffer_no_attachments
4.3ARB_internalformat_query2GL_ARB_internalformat_query2
4.3ARB_invalidate_subdataGL_ARB_invalidate_subdata
4.3ARB_multi_draw_indirectGL_ARB_multi_draw_indirect
4.3ARB_program_interface_queryGL_ARB_program_interface_query
4.3ARB_shader_storage_buffer_objectGL_ARB_shader_storage_buffer_object
4.3ARB_stencil_texturingGL_ARB_stencil_texturing
4.3ARB_texture_buffer_rangeGL_ARB_texture_buffer_range
4.3ARB_texture_storage_multisampleGL_ARB_texture_storage_multisample
4.3ARB_texture_viewGL_ARB_texture_view
4.3ARB_vertex_attrib_bindingGL_ARB_vertex_attrib_binding
4.3KHR_debugGL_KHR_debug
4.4ARB_buffer_storageGL_ARB_buffer_storage
4.4ARB_clear_textureGL_ARB_clear_texture
4.4ARB_enhanced_layoutsGL_ARB_enhanced_layouts
4.4ARB_multi_bindGL_ARB_multi_bind
4.4ARB_query_buffer_objectGL_ARB_query_buffer_object
4.4ARB_texture_mirror_clamp_to_edgeGL_ARB_texture_mirror_clamp_to_edge
4.5ARB_clip_controlGL_ARB_clip_control
4.5ARB_conditional_render_invertedGL_ARB_conditional_render_inverted
4.5ARB_cull_distanceGL_ARB_cull_distance
4.5ARB_direct_state_accessGL_ARB_direct_state_access
4.5ARB_ES3_1_compatibilityGL_ARB_ES3_1_compatibility
4.5ARB_get_texture_sub_imageGL_ARB_get_texture_sub_image
4.5ARB_texture_barrierGL_ARB_texture_barrier
4.5KHR_context_flush_controlGL_KHR_context_flush_control
4.5KHR_robustnessGL_KHR_robustness
4.6ARB_polygon_offset_clampGL_ARB_polygon_offset_clamp
4.6ARB_texture_filter_anisotropicGL_ARB_texture_filter_anisotropic
N/AKHR_blend_equation_advancedGL_KHR_blend_equation_advanced
N/ANV_shader_buffer_loadGL_NV_shader_buffer_load
N/ANV_vertex_buffer_unified_memoryGL_NV_vertex_buffer_unified_memory
N/ANV_command_listGL_NV_command_list

For example, the following configurations enable support for OpenGL 4.1 and the extensions ARB_base_instance and ARB_compressed_texture_pixel_storage, both of which were promoted to core in OpenGL 4.2:

dub.json

"dependencies": {
	"bindbc-opengl": "~>1.1.0"
}
"versions": [
	"GL_41",
	"GL_ARB_base_instance",
	"GL_ARB_compressed_texture_pixel_storage"
],

dub.sdl

dependency "bindbc-opengl" version="~>1.1.0"
versions "GL_41" "GL_ARB_base_instance" "GL_ARB_compressed_texture_pixel_storage"

The loadOpenGL function (described in the next section) will attempt to load all extensions configured in this manner. No errors will be reported on failure. To determine if an extension was loaded, use the has* property for each extension, like so:

import bindbc.opengl;

// Create the OpenGL context and load OpenGl
...
// Check for required extensions
if(hasARBBaseInstance){
	// configure renderer for GL_ARB_base_instance support
}
if(hasARBCompressedTexturePixelStorage){
	// configure renderer for GL_ARB_compressed_texture_pixel_storage support
}

If you have a need for an extension that is not currently supported then please submit a pull request.

Loading OpenGL

The loadOpenGL function is used to load all supported OpenGL functions and extensions. In order for this function to succeed, an OpenGL context must be created before it is called. The return value of loadOpenGL can be used to determine which version of OpenGL actually loaded.

For example, assume you've configured BindBC-OpenGL to support up to OpenGL 4.1, but you've designed your renderer to work with both 4.1 and 3.3. You can create a 4.1 or 3.3 context in one part of your code, then load OpenGL in another part, and configure your renderer based upon the return value.

import bindbc.opengl;

// Create OpenGL context
...
// Load supported OpenGL version + supported extensions
GLSupport retVal = loadOpenGL();
if(retVal == GLSupport.gl41){
	// configure renderer for OpenGL 4.1
}else if(retVal == GLSupport.gl33){
	// configure renderer for OpenGL 3.3
}else{
	// Error
}

[!NOTE]
loadOpenGL returns the highest version that was loaded. On some systems, deprecated OpenGL functions (e.g. from OpenGL 1.2) may be missing, so if you rely on these you should check that they were loaded using versionLoadedStatus:

if((versionLoadedStatus(GLSupport.gl12) & GLLoadStatus.loaded) == 0){
  //OpenGL 1.2 functions were not loaded!
}

On error, loadOpenGL will return one of the following:

The following functions are provided for convenience:

Note that when working with multiple contexts, it may be necessary to call loadOpenGL on every context switch. On Windows in particular, a context switch may cause context-dependent functions, i.e., core functions above 1.1 and all extensions, to become invalid in some circumstances.

The error reporting API in bindbc.loader can be used to facilitate logging error messages.

// Import the dependent package
import bindbc.opengl;

/*
 Import the sharedlib module for error handling. Assigning an alias ensures the function names do not conflict with
 other public APIs. This isn't strictly necessary, but the API names are common enough that they could appear in other
 packages.
*/
import loader = bindbc.loader.sharedlib;

// Create the OpenGL context before calling this function.
bool loadLib(){
	/*
	Compare the return value of loadGL with the global `glSupport` constant to determine if the version of GLFW
	configured at compile time is the version that was loaded.
	*/
	auto ret = loadOpenGL();
	if(ret != glSupport){
		// Log the error info
		foreach(info; loader.errors){
			/*
			A hypothetical logging function. Note that `info.error` and `info.message` are `const(char)*`, not
			`string`.
			*/
			logError(info.error, info.message);
		}
		
		// Optionally construct a user-friendly error message for the user
		string msg;
		if(ret == GLSupport.noLibrary){
			msg = "This application requires OpenGL.";
		}else if(ret == GLSupport.badLibrary){
			msg = "The version of OpenGL on your system is too low. Please upgrade."
		}else{
			// GLSupport.noContext
			msg = "This program has encountered a graphics configuration error. Please report it to the developers."
		}
		// A hypothetical message box function
		showMessageBox(msg);
		return false;
	}
	return true;
}