OpenSceneGraph Forum Forum Index OpenSceneGraph Forum
Official forum which mirrors the existent OSG mailing lists. Messages posted here are forwarded to the mailing list and vice versa.
 
   FAQFAQ    SearchSearch    MemberlistMemberlist    RulesRules    UsergroupsUsergroups    RegisterRegister 
 Mail2Forum SettingsMail2Forum Settings  ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
   AlbumAlbum  OpenSceneGraph IRC ChatOpenSceneGraph IRC Chat   SmartFeedSmartFeed 

Pass an osg::Texture2D to CUDA driver api


 
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> General
View previous topic :: View next topic  
Author Message
mille25
User


Joined: 20 Apr 2016
Posts: 35

PostPosted: Wed Jun 15, 2016 12:37 pm    Post subject:
Pass an osg::Texture2D to CUDA driver api
Reply with quote

Hi,

I'm currently facing some issues in passing a texture2d created via OSG to the CUDA low level driver API. I'm trying to run a cuda kernel on a texture after calling viewer->renderingTraversals();

As far as I have understood, all thats required is getting the underlaying texture ID for OpenGL and passing it to CUDA for further processing. However, I immediately get a segmentation fault when calling cuGraphicsGLRegisterImage. (Note: NOT a cuda error)
This in itself is already sort of weird, because to my understanding a CUDA call should either work or return a proper error code. Anyways...

Using the debugger, I validated the following:

"openGLContextID" is set to 0, which seems correct. Im not sure if I am retrieving the context ID in the correct way tough. Im using multiple cameras and let OSG manage the graphics context by itself (I never explicitly create it).

"texid" is set to 2.
"texid2" is set to 78.

I've also double checked the texture type, getTextureObject()->target() returns the same numerical value as GL_TEXTURE_2D, so it can't be that either.

I have tested all my code previously in an OpenGL only program, and it worked perfectly.

Does anyone know whats wrong? Am I executing the CUDA stuff at the wrong place? (After rendering)

Relevant code (error checking boilerplate removed for readability):

Code:
radarShaderOutputTexture = new osg::Texture2D;
   radarShaderOutputTexture->setInternalFormat(GL_RGBA32F);
   radarShaderOutputTexture->setSourceFormat(GL_RGBA);
   radarShaderOutputTexture->setSourceType(GL_FLOAT);
//... more texture stuff.

cudaOutputTex = new osg::Texture2D;
//... more texture stuff.

unsigned int openGLContextID = viewer->getCamera()->getGraphicsContext()->getState()->getContextID();
      GLenum texid = radarShaderOutputTexture->getTextureObject(
            openGLContextID)->id();
      GLenum texid2 =
            cudaOutputTexture->getTextureObject(openGLContextID)->id();

      CUgraphicsResource cudaInputTex, cudaOutputTex;
      cuGraphicsGLRegisterImage(&cudaInputTex, texid,
      GL_TEXTURE_2D, CU_GRAPHICS_REGISTER_FLAGS_READ_ONLY);
      cuGraphicsGLRegisterImage(&cudaOutputTex, texid2,
      GL_TEXTURE_2D, CU_GRAPHICS_REGISTER_FLAGS_WRITE_DISCARD);


Thank you!

Cheers,
Philipp
Back to top
View user's profile Send private message
mille25
User


Joined: 20 Apr 2016
Posts: 35

PostPosted: Wed Jun 15, 2016 1:48 pm    Post subject:
Reply with quote

Hi,

figured it out.
One needs to use

Code:
viewer->setReleaseContextAtEndOfFrameHint(false);


to prevent the context from getting released after a frame is rendered.
That way, its resources, like textures, can still be accessed after the frame completes.

Also, seems like the viewer must be set to singleThreaded mode for this to work.

Thank you!

Cheers,
Philipp

PS: damn it, why does it always seem like I'm figuring stuff out right after posting the question...
Back to top
View user's profile Send private message
robertosfield
OSG Project Lead


Joined: 18 Mar 2009
Posts: 12264

PostPosted: Wed Jun 15, 2016 2:43 pm    Post subject:
Pass an osg::Texture2D to CUDA driver api
Reply with quote

Hi Philipp

On 15 June 2016 at 14:48, Philipp Meyer <> wrote:
Quote:
figured it out.
One needs to use


Code:
viewer->setReleaseContextAtEndOfFrameHint(false);



to prevent the context from getting released after a frame is rendered.
That way, its resources, like textures, can still be accessed after the frame completes.

I don't have CUDA experience so can't comment on this specifically.

On the OSG side the setReleaseContextAtEndOfFrameHint() is only useful
in when you have a single graphics context and are running your
application SingleThreaded.

There will be other ways to integrate CUDA rather than via the main
loop. You should be able to create a custom GraphicsOperation and
attach this to a GraphicsWidnow or Camera draw callback to invoke the
CUDA side from within a thread that has the graphics context current.

Robert.


------------------
Post generated by Mail2Forum
Back to top
View user's profile Send private message
mille25
User


Joined: 20 Apr 2016
Posts: 35

PostPosted: Fri Jun 24, 2016 12:00 pm    Post subject:
Re: Pass an osg::Texture2D to CUDA driver api
Reply with quote

Hi Robert,

thanks for the input!
When thinking about this, I really want to find another approach than the "main loop" because I need to execute the CUDA code at a very specific point in time. (After a RTT Camera Texture has been written, but prior to rendering another texture).

Therefore, I think I cannot use a camera postDrawCallback, because that doesnt solve the problem of displaying the CUDA output after it has been produced in the same frame.

My idea was to create a "fake" drawable node and use a DrawCallback together with renderBins to make my custom CUDA code execute at a very specific time during rendering traversal.

However, I'm having trouble getting that approach to work. If I create a very simple scene graph, only consisting of a root node and 10 of my custom drawable nodes attached to it, each with another render bin number, the execution order of my drawCallbacks does not follow the renderbin number. Instead, the drawCallbacks always execute in the same order that I have added the drawables to the root node.

Does anyone know why that happens? From my understanding, the renderbin number should determine the order of draw (and therefore also of the drawCallback?) operations.

Also, Im having the issue that my drawCallback is only executed during the first frame and then skipped. Ive disabled culling and depth testing.

robertosfield wrote:
Hi Philipp

On 15 June 2016 at 14:48, Philipp Meyer <> wrote:
Quote:
figured it out.
One needs to use


Code:
viewer->setReleaseContextAtEndOfFrameHint(false);



to prevent the context from getting released after a frame is rendered.
That way, its resources, like textures, can still be accessed after the frame completes.

I don't have CUDA experience so can't comment on this specifically.

On the OSG side the setReleaseContextAtEndOfFrameHint() is only useful
in when you have a single graphics context and are running your
application SingleThreaded.

There will be other ways to integrate CUDA rather than via the main
loop. You should be able to create a custom GraphicsOperation and
attach this to a GraphicsWidnow or Camera draw callback to invoke the
CUDA side from within a thread that has the graphics context current.

Robert.


------------------
Post generated by Mail2Forum
Back to top
View user's profile Send private message
scrawl
Appreciator


Joined: 14 Dec 2014
Posts: 175

PostPosted: Fri Jun 24, 2016 4:20 pm    Post subject:
Reply with quote

Hi Philipp,

Quote:

Also, Im having the issue that my drawCallback is only executed during the first frame and then skipped. Ive disabled culling and depth testing.

You need to disable display lists on your drawable. With display lists enabled it will only draw once and then use the display list. This makes no sense when using Cuda obviously.

Quote:

Does anyone know why that happens? From my understanding, the renderbin number should determine the order of draw (and therefore also of the drawCallback?) operations.

I think this may also be related to the display list being enabled, the compileGLObjects() that is run in the first frame will compile display lists but not necessarily in the order of the render bins.

Cheers,
Jannik
Back to top
View user's profile Send private message
cbuchner1
Appreciator


Joined: 14 Mar 2012
Posts: 316

PostPosted: Fri Jun 24, 2016 4:31 pm    Post subject:
Pass an osg::Texture2D to CUDA driver api
Reply with quote

I am wondering if the default setting to enable use of Display lists in OSG still makes a lot of sense nowadays.


Christian




2016-06-24 18:20 GMT+02:00 Jannik Heller < (
Only registered users can see emails on this board!
Get registred or enter the forums!
)>:
Quote:
Hi Philipp,


Quote:

Also, Im having the issue that my drawCallback is only executed during the first frame and then skipped. Ive disabled culling and depth testing.


You need to disable display lists on your drawable. With display lists enabled it will only draw once and then use the display list. This makes no sense when using Cuda obviously.


Quote:

Does anyone know why that happens? From my understanding, the renderbin number should determine the order of draw (and therefore also of the drawCallback?) operations.


I think this may also be related to the display list being enabled, the compileGLObjects() that is run in the first frame will compile display lists but not necessarily in the order of the render bins.

Cheers,
Jannik

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=67803#67803





_______________________________________________
osg-users mailing list
(
Only registered users can see emails on this board!
Get registred or enter the forums!
)
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org




------------------
Post generated by Mail2Forum
Back to top
View user's profile Send private message
mille25
User


Joined: 20 Apr 2016
Posts: 35

PostPosted: Mon Jun 27, 2016 7:11 am    Post subject:
Reply with quote

Hi,

setting useDisplayLists to false indeed fixed both issues. Thank you very much.

Cheers,
Philipp
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum

Similar Topics
Topic Author Forum Replies Posted
No new posts pass mat4 as attribute for glsl sirian_ye General 4 Tue Nov 27, 2018 4:58 am View latest post
No new posts Possibility to cast/convert TextureRe... Tare General 4 Mon Jun 25, 2018 10:02 am View latest post
No new posts Creating depth texture in pre render ... wernerM General 0 Fri Mar 16, 2018 2:31 pm View latest post
No new posts [ShadowScene traversal] program in S... mp3butcher General 1 Fri Dec 08, 2017 6:07 pm View latest post
No new posts How to use openscenegraph to realize ... qiaokun General 1 Tue May 23, 2017 6:34 am View latest post


Board Security Anti Bot Question MOD - phpBB MOD against Spam Bots
Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP