Skip to content
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

[SofaFramework] Isolate OpenGL code into a single module (Sofa.GL) #1649

Merged
merged 38 commits into from
Jan 6, 2021

Conversation

fredroy
Copy link
Contributor

@fredroy fredroy commented Dec 4, 2020

Fix #1645 (needs #1646 , #1644 , #1650 )

TL;DR

OpenGL code in the core is now enclosed in the new module Sofa.GL.
If you need to call OpenGL code, you will need to find_package() it and add it to target_link_library().

Description

OpenGL is spread a bit everywhere in the core, and it is difficult to bypass it if you dont need it (typically for batch mode or headless system).
The macro SOFA_NO_OPENGL is a really Quick and Dirty solution to this problem.
For example, people include sofa/helper/system/gl.h (or any gl-related header), even without needing OpenGL actually.
And in this header, SOFA_NO_OPENGL does include (or not) the good gl.h header according to your OS. So if you enable this header, you still include gl.h, which does nothing.
This is quite nonsensical.
Moreover, if the macro is not well handled in CMake, you may have some weird things happening.

Solution

This PR regroups all OpenGL related code in the core in a single package, needing only SofaHelper and SofaDefaulttype.
If you wish to be able to call OpenGL things, just find_package() it and link it to your library

find_package(Sofa.GL REQUIRED)
...
target_add_library(${Target} PUBLIC .... Sofa.GL)

You can even conditionnally test if OpenGL is present or not with

if(Sofa.GL_FOUND)
...
endif()

if needed (to compile specific components in your module for example).

And even more, if you want to compile specific parts of your code with OpenGL (not a good idea but still), you can use this like an external dependency.
Example with ColourPickingVisitor in SofaGuiCommon.
CMakeLists.txt:

sofa_find_package(Sofa.GL) # create the cmake var SOFAGUICOMMON.GL_HAVE_SOFA.GL

config.h.in

#define SOFAGUICOMMON_HAVE_SOFA_GL @SOFAGUICOMMON_HAVE_SOFA.GL@

ColourPickingVisitor.cpp

void ColourPickingVisitor::processTriangleModel(simulation::Node * node, sofa::component::collision::TriangleCollisionModel<sofa::defaulttype::Vec3Types> * tmodel)
{
#ifdef SOFAGUICOMMON_HAVE_SOFA_GL
    using namespace sofa::core::collision;
    using namespace sofa::defaulttype;
    glDisable(GL_LIGHTING);
    ....
#endif // SOFAGUICOMMON_HAVE_SOFA_GL

Compatibility with actual code and breaking things

This new module follows the new targeted architecture for Sofa(NG), hence the Sofa.GL syntax.
All classes/functions are now in the namespace sofa::gl and in the folder sofa/gl; e.g Texture,
sofa/gl/Texture.h

namespace sofa::gl
{
	class Texture {...}
}

To avoid breaking (a lot of) code, a compatibility layer has been implemented, so the existing code still works:

#include <sofa/helper/gl/Texture.h
...
sofa::helper::gl::Texture = ....;

and will either:

  • create an error if you did not link against Sofa.GL
  • generate a deprecation warning if you did link but still using old paths.
    So for 99.999% of code, all you have to do is to link against Sofa.GL (or even nothing if you code is already linking against modules linking now against Sofa.GL like SofaOpenGlVisual)

Real breaking code

ONE function has been removed from core::visual::VisualParam :
helper::gl::Framebuffer getFramebuffer()
which was used to get the current bound framebuffer in OpenGL (getting a Sofa utility class Framebuffer).
Two reasons why it has been removed:

  • nobody uses it in all the repository
  • one can just use a real OpenGL function to get the bound framebuffer, with
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &result);

for example.

Notes

The compilation of Sofa.GL is itself enabled with the CMake variable SOFA_NO_OPENGL (weird name but I wanted to keep the same "reasoning" for now)
Use of SOFA_NO_OPENGL macro in the code has been removed from the Sofa codebase only, not the plugins (in a future PR)

😅


By submitting this pull request, I acknowledge that
I have read, understand, and agree SOFA Developer Certificate of Origin (DCO).


Reviewers will merge this pull-request only if

  • it builds with SUCCESS for all platforms on the CI.
  • it does not generate new warnings.
  • it does not generate new unit test failures.
  • it does not generate new scene test failures.
  • it does not break API compatibility.
  • it is more than 1 week old (or has fast-merge label).

@fredroy fredroy added enhancement About a possible enhancement pr: breaking Change possibly inducing a compilation error pr: status wip Development in the pull-request is still in progress refactoring Refactor code pr: clean Cleaning the code NG2: pluginization See https://github.com/sofa-framework/sofa/issues/1527 labels Dec 4, 2020
@fredroy fredroy changed the title [SofaFramework] Isolate OpenGL into a single module (Sofa.GL) [SofaFramework] Isolate OpenGL code into a single module (Sofa.GL) Dec 4, 2020
@fredroy
Copy link
Contributor Author

fredroy commented Dec 4, 2020

[ci-build][with-all-tests]

@fredroy fredroy added pr: status to review To notify reviewers to review this pull-request and removed pr: status wip Development in the pull-request is still in progress labels Dec 8, 2020
{

class SOFA_HELPER_API Capture
class SOFA_SOFA_GL_API Capture
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

normal SOFA_SOFA_*?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

40 times

add_library(${PROJECT_NAME} SHARED ${HEADER_FILES} ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} PUBLIC SofaHelper SofaDefaultType)

target_compile_definitions(${PROJECT_NAME} PRIVATE SOFA_BUILD_SOFA_GL) # To remove once sofa_add_targets_to_package remove the dot in the generated definition
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guparan just not to be forgotten for the update of sofa_add_targets_to_package

@hugtalbot hugtalbot added pr: status wip Development in the pull-request is still in progress and removed pr: status to review To notify reviewers to review this pull-request labels Dec 9, 2020
@@ -0,0 +1,106 @@
cmake_minimum_required(VERSION 3.12)
project(Sofa.GL LANGUAGES CXX)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we define versions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Package version follows Sofa's version; you can see that this is set in the macro with (... PACKAGE_VERSION ${Sofa_VERSION}... )

@fredroy
Copy link
Contributor Author

fredroy commented Dec 10, 2020

[ci-build][force-full-build]

@fredroy
Copy link
Contributor Author

fredroy commented Dec 15, 2020

[ci-build][with-all-tests]

@fredroy
Copy link
Contributor Author

fredroy commented Dec 15, 2020

[ci-build][force-full-build][with-all-tests]

@fredroy fredroy added pr: status to review To notify reviewers to review this pull-request and removed pr: status wip Development in the pull-request is still in progress labels Dec 16, 2020
@fredroy
Copy link
Contributor Author

fredroy commented Dec 23, 2020

[ci-build][force-full-build][with-all-tests]

foreach(TARGET ${SOFAFRAMEWORK_TARGETS})
message("Adding ${TARGET}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this for debugging purposes?


//
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove?

@hugtalbot hugtalbot added pr: status ready Approved a pull-request, ready to be squashed and removed pr: status to review To notify reviewers to review this pull-request labels Jan 6, 2021
@fredroy fredroy added the pr: do not squash To merge instead of squash a pull-request label Jan 6, 2021
@jnbrunet
Copy link
Contributor

jnbrunet commented Jan 6, 2021

MacOS CI stalled for some reason. But the CI passed just before and the subsequent changes were minimal, so it should be safe to merge.

@jnbrunet jnbrunet merged commit c8e99f3 into sofa-framework:master Jan 6, 2021
@fredroy fredroy deleted the isolate_gl branch January 6, 2021 16:08
@guparan guparan added this to the v21.06 milestone Jan 18, 2021
guparan added a commit to guparan/sofa that referenced this pull request Jun 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement About a possible enhancement NG2: pluginization See https://github.com/sofa-framework/sofa/issues/1527 pr: breaking Change possibly inducing a compilation error pr: clean Cleaning the code pr: do not squash To merge instead of squash a pull-request pr: status ready Approved a pull-request, ready to be squashed refactoring Refactor code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Isolate OpenGL code in SofaFramework
4 participants