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 

Use an osgCuda::Texture2D in a shader


 
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> osgCompute [osgCompute]
View previous topic :: View next topic  
Author Message
Aurelien
Appreciator


Joined: 03 Aug 2011
Posts: 297

PostPosted: Fri Oct 14, 2011 4:37 pm    Post subject:
Use an osgCuda::Texture2D in a shader
Reply with quote

Hi,

I'm working on the port of a OpenGL / Cuda application to OSG / osgCuda.

(OSG 3.0.1 with osgCuda from trunk)

One of our class use Cuda to compute a buffer and then :
- copy this buffer to a texture buffer (using cudaMemcpy)
- bind this texture to a texture unit (using glBindTexture)
- use this texture unit in a shader

I'm trying to do the same using OSG / osgCuda.
I don't use Computation classes for the moment, because we need a lot of time to port our Cuda code architecture to osgCuda.

So, we do :

- create an osgCuda::Texture2D as follow (our computed Cuda buffer contains float3 data) :

Code:

osg::ref_ptr<osgCuda::Texture2D> pCudaTexture = new osgCuda::Texture2D();

pCudaTexture->setDataVariance(osg::Object::DYNAMIC);
pCudaTexture->setNumMipmapLevels(0);
pCudaTexture->setUseHardwareMipMapGeneration(false);
pCudaTexture->setFilter(osg::Texture::MIN_FILTER , osg::Texture::LINEAR);
pCudaTexture->setFilter(osg::Texture::MAG_FILTER , osg::Texture::LINEAR);
pCudaTexture->setResizeNonPowerOfTwoHint(false);
pCudaTexture->setWrap(osg::Texture::WRAP_R, osg::Texture::REPEAT);
pCudaTexture->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
pCudaTexture->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT);
pCudaTexture->setInternalFormat(GL_RGB32F_ARB);
pCudaTexture->setSourceFormat(GL_RGB);
pCudaTexture->setSourceType(GL_FLOAT);
pCudaTexture->setTextureSize(m_width, m_height);
pCudaTexture->setUsage(osgCompute::GL_TARGET_COMPUTE_TARGET);


- use this texture as a StateAttribute :

Code:
pNode->getOrCreateStateSet()->setTextureAttribute(0, pCudaTexture, osg::StateAttribute::ON);


Then before rendering each frame :

- call our Cuda code, which fill a Cuda buffer => this part is working
- copy this buffer to the osgCuda::Texture2D : (dSourceCuda is a pointer to our computed Cuda buffer)

Code:
osgCompute::Memory* pMemory = pCudaTexture->getMemory();
void* dDestCuda = pMemory->map();

CUDA_SAFE_CALL( cudaMemcpy(dDestCuda, dSourceCuda, size, cudaMemcpyDeviceToDevice) );

pMemory->unmap();


This part seems to work : if we write the content of "dSourceCuda" buffer and "dDestCuda" buffer to a file, the data are identical and computed correctly by our Cuda code.


But the texture unit seems to always have 0.0 values : when we have a look to our textureUnit with GDebugger, it always contains only 0.0 values.


Is there any way to use the content of our computed Cuda buffer into a texture unit ?


Regards,
Aurelien
Back to top
View user's profile Send private message
J.P. Delport
Guest





PostPosted: Mon Oct 17, 2011 1:19 pm    Post subject:
Use an osgCuda::Texture2D in a shader
Reply with quote

Hi Aurelien,

I'm not sure how to get you manual method working. It might have to do
with multiple contexts? How are you running the shader? In pre-render
pass? If so, you'll have to make sure the cuda code also executes in the
pre-render stage.

All I can add is that I'm mixing GLSL passes and osgCuda passes (with
some GLSL after cuda and some before) currently and it works. See here too:
http://thread.gmane.org/gmane.comp.graphics.openscenegraph.user/71276

cheers
jp

On 14/10/2011 18:37, Aurelien Albert wrote:
Quote:
Hi,

I'm working on the port of a OpenGL / Cuda application to OSG / osgCuda.

(OSG 3.0.1 with osgCuda from trunk)

One of our class use Cuda to compute a buffer and then :
- copy this buffer to a texture buffer (using cudaMemcpy)
- bind this texture to a texture unit (using glBindTexture)
- use this texture unit in a shader

I'm trying to do the same using OSG / osgCuda.
I don't use Computation classes for the moment, because we need a lot of time to port our Cuda code architecture to osgCuda.

So, we do :

- create an osgCuda::Texture2D as follow (our computed Cuda buffer contains float3 data) :


Code:

osg::ref_ptr<osgCuda::Texture2D> pCudaTexture = new osgCuda::Texture2D();

pCudaTexture->setDataVariance(osg::Object::DYNAMIC);
pCudaTexture->setNumMipmapLevels(0);
pCudaTexture->setUseHardwareMipMapGeneration(false);
pCudaTexture->setFilter(osg::Texture::MIN_FILTER , osg::Texture::LINEAR);
pCudaTexture->setFilter(osg::Texture::MAG_FILTER , osg::Texture::LINEAR);
pCudaTexture->setResizeNonPowerOfTwoHint(false);
pCudaTexture->setWrap(osg::Texture::WRAP_R, osg::Texture::REPEAT);
pCudaTexture->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
pCudaTexture->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT);
pCudaTexture->setInternalFormat(GL_RGB32F_ARB);
pCudaTexture->setSourceFormat(GL_RGB);
pCudaTexture->setSourceType(GL_FLOAT);
pCudaTexture->setTextureSize(m_width, m_height);
pCudaTexture->setUsage(osgCompute::GL_TARGET_COMPUTE_TARGET);




- use this texture as a StateAttribute :


Code:
pNode->getOrCreateStateSet()->setTextureAttribute(0, pCudaTexture, osg::StateAttribute:ShockedN);



Then before rendering each frame :

- call our Cuda code, which fill a Cuda buffer => this part is working
- copy this buffer to the osgCuda::Texture2D : (dSourceCuda is a pointer to our computed Cuda buffer)


Code:
osgCompute::Memory* pMemory = pCudaTexture->getMemory();
void* dDestCuda = pMemory->map();

CUDA_SAFE_CALL( cudaMemcpy(dDestCuda, dSourceCuda, size, cudaMemcpyDeviceToDevice) );

pMemory->unmap();




This part seems to work : if we write the content of "dSourceCuda" buffer and "dDestCuda" buffer to a file, the data are identical and computed correctly by our Cuda code.


But the texture unit seems to always have 0.0 values : when we have a look to our textureUnit with GDebugger, it always contains only 0.0 values.


Is there any way to use the content of our computed Cuda buffer into a texture unit ?


Regards,
Aurelien

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







--
This message is subject to the CSIR's copyright terms and conditions, e-mail legal notice, and implemented Open Document Format (ODF) standard.
The full disclaimer details can be found at http://www.csir.co.za/disclaimer.html.

This message has been scanned for viruses and dangerous content by MailScanner,
and is believed to be clean.



------------------
Post generated by Mail2Forum
Back to top
Aurelien
Appreciator


Joined: 03 Aug 2011
Posts: 297

PostPosted: Mon Oct 17, 2011 1:26 pm    Post subject:
Reply with quote

Hi,

The Shader is executed as a "normal" osg shader program : by setting it on the node stateset.

The Cuda code is executed before any rendering (before the pViewer->frame() call)

By diggging in the osgCuda code source, it seems like "cudaGraphicsMapResources" and "cudaGraphicsGLRegisterImage" functions are never called...
Back to top
View user's profile Send private message
J.P. Delport
Guest





PostPosted: Mon Oct 17, 2011 1:36 pm    Post subject:
Use an osgCuda::Texture2D in a shader
Reply with quote

Hi,

On 17/10/2011 15:26, Aurelien Albert wrote:
Quote:
Hi,

The Shader is executed as a "normal" osg shader program : by setting
it on the node stateset.

The Cuda code is executed before any rendering (before the
pViewer->frame() call)

By diggging in the osgCuda code source, it seems like
"cudaGraphicsMapResources" and "cudaGraphicsGLRegisterImage"
functions are never called...

I know these are called in my app (at least for
GL_TARGET_DEVICE_SOURCE), because I've traced through the code when it
executed. AFAIK it normally happens in the map() call.

You can check out this example I've made. It uses the output texture of
the cuda pass to display on a rect. If you set a shader on the rect it
should work too.

http://code.google.com/p/flitr/source/browse/#svn%2Ftrunk%2Fexamples%2Fcuda_auto_contrast

Maybe you can trace through this and see what is different between the
example and your code.

cheers
jp


Quote:

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





_______________________________________________ osg-users mailing
list
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

--
This message is subject to the CSIR's copyright terms and conditions, e-mail legal notice, and implemented Open Document Format (ODF) standard.
The full disclaimer details can be found at http://www.csir.co.za/disclaimer.html.

This message has been scanned for viruses and dangerous content by MailScanner,
and is believed to be clean.



------------------
Post generated by Mail2Forum
Back to top
Aurelien
Appreciator


Joined: 03 Aug 2011
Posts: 297

PostPosted: Mon Oct 17, 2011 1:37 pm    Post subject:
Reply with quote

To be more specific, I just need one function :

"copyCudaBufferToOsgTexture"

I thought osgCuda provide it, and I hope this is the first step for me before a complete migration from "hand-made" Cuda to osgCuda code.
Back to top
View user's profile Send private message
J.P. Delport
Guest





PostPosted: Mon Oct 17, 2011 1:46 pm    Post subject:
Use an osgCuda::Texture2D in a shader
Reply with quote

Hi,

I think search in the osgCuda code for Memcpy2D. I think the cuda
"shadow buffer" that osgCuda makes is copied to the texture in the
unmap(). There is possibly some other things like updating the texture
dirty count as well so that OSG knows the texture has been changed.

I'm sorry I can't help more, I've not used osgCuda in the manual way
that you are trying.

jp

On 17/10/2011 15:37, Aurelien Albert wrote:
Quote:
To be more specific, I just need one function :

"copyCudaBufferToOsgTexture"

I thought osgCuda provide it, and I hope this is the first step for me before a complete migration from "hand-made" Cuda to osgCuda code.

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







--
This message is subject to the CSIR's copyright terms and conditions, e-mail legal notice, and implemented Open Document Format (ODF) standard.
The full disclaimer details can be found at http://www.csir.co.za/disclaimer.html.

This message has been scanned for viruses and dangerous content by MailScanner,
and is believed to be clean.



------------------
Post generated by Mail2Forum
Back to top
jens.svt
User


Joined: 16 Mar 2009
Posts: 30

PostPosted: Fri Oct 28, 2011 12:22 pm    Post subject:
Reply with quote

Hi Aurelien,

have you tried to uncomment
Code:

...
pCudaTexture->setUsage(osgCompute::GL_TARGET_COMPUTE_TARGET);

because what you really need is:
Code:

...
pCudaTexture->setUsage(osgCompute::GL_SOURCE_COMPUTE_TARGET);

which is set by default.
Another option is to take a look at our osgTexDemo.

Best regards,
Jens
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> osgCompute [osgCompute] 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 draw primitive sets and geometry shad... wernerM General 0 Wed May 24, 2017 3:20 pm View latest post
No new posts passing material properties to shader... gpantar General [forum] 0 Mon May 15, 2017 1:17 pm View latest post
No new posts Scale problem while transforming obje... YuraIU02 General 1 Sat Apr 15, 2017 4:26 pm View latest post
No new posts shader 崔 献军 General 1 Fri Apr 07, 2017 1:55 am View latest post
No new posts Merge Shader::Program from differents... Paradox General 1 Tue Feb 28, 2017 3:53 pm 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