<div dir="ltr"><div class="prettyprint" style="background-color: rgb(250, 250, 250); border-color: rgb(187, 187, 187); border-style: solid; border-width: 1px; overflow-wrap: break-word;"><code class="prettyprint"><div class="subprettyprint"><div class="subprettyprint"><font color="#666600">/* -*-c++-*- OpenSceneGraph example, osgcomputeshaders.</font></div><div class="subprettyprint"><font color="#666600">*</font></div><div class="subprettyprint"><font color="#666600">*  Permission is hereby granted, free of charge, to any person obtaining a copy</font></div><div class="subprettyprint"><font color="#666600">*  of this software and associated documentation files (the "Software"), to deal</font></div><div class="subprettyprint"><font color="#666600">*  in the Software without restriction, including without limitation the rights</font></div><div class="subprettyprint"><font color="#666600">*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell</font></div><div class="subprettyprint"><font color="#666600">*  copies of the Software, and to permit persons to whom the Software is</font></div><div class="subprettyprint"><font color="#666600">*  furnished to do so, subject to the following conditions:</font></div><div class="subprettyprint"><font color="#666600">*</font></div><div class="subprettyprint"><font color="#666600">*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR</font></div><div class="subprettyprint"><font color="#666600">*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,</font></div><div class="subprettyprint"><font color="#666600">*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE</font></div><div class="subprettyprint"><font color="#666600">*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER</font></div><div class="subprettyprint"><font color="#666600">*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,</font></div><div class="subprettyprint"><font color="#666600">*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN</font></div><div class="subprettyprint"><font color="#666600">*  THE SOFTWARE.</font></div><div class="subprettyprint"><font color="#666600">*/</font></div><div class="subprettyprint"><font color="#666600"><br></font></div><div class="subprettyprint"><font color="#666600">// Written by Wang Rui</font></div><div class="subprettyprint"><font color="#666600">// This example can work only if GL version is 4.3 or greater</font></div><div class="subprettyprint"><font color="#666600"><br></font></div><div class="subprettyprint"><font color="#666600">#include <osg/Texture2D></font></div><div class="subprettyprint"><font color="#666600">#include <osg/BindImageTexture></font></div><div class="subprettyprint"><font color="#666600">#include <osg/DispatchCompute></font></div><div class="subprettyprint"><font color="#666600">#include <osg/Geode></font></div><div class="subprettyprint"><font color="#666600">#include <osgDB/ReadFile></font></div><div class="subprettyprint"><font color="#666600">#include <osgGA/StateSetManipulator></font></div><div class="subprettyprint"><font color="#666600">#include <osgViewer/Viewer></font></div><div class="subprettyprint"><font color="#666600">#include <osgViewer/ViewerEventHandlers></font></div><div class="subprettyprint"><font color="#666600"><br></font></div><div class="subprettyprint"><font color="#666600">static const char* computeSrc = {</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre"> </span>"#version 430\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">        </span>"precision highp float;\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">      </span>"precision highp int;\n"</font></div><div class="subprettyprint"><font color="#666600">    "layout (rgba32f, binding =0) highp uniform image2D uImageIn;\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">     </span>"layout (rgba32f, binding =1) highp uniform image2D uImageOut;\n"</font></div><div class="subprettyprint"><font color="#666600">    "layout (local_size_x = 16, local_size_y = 16, local_size_z = 1) in;\n"</font></div><div class="subprettyprint"><font color="#666600">    "void main() {\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre"> </span>"<span style="white-space:pre">       </span>ivec2 id   = ivec2(gl_GlobalInvocationID.xy);        <span style="white-space:pre">                                                                  </span>\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">  </span>"<span style="white-space:pre">       </span>ivec2 size = imageSize(uImageOut);<span style="white-space:pre">                                                                                                           </span>\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">  </span>"<span style="white-space:pre">       </span>if (id.x >= size.x || id.y >= size.y) {<span style="white-space:pre">                                                                                                        </span>\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">  </span>"<span style="white-space:pre">               </span>return;<span style="white-space:pre">                                                                                                                                                              </span>\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">  </span>"<span style="white-space:pre">       </span>}<span style="white-space:pre">                                                                                                                                                                            </span>\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">  </span>"   const int uSigma = 15;\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre"> </span>"<span style="white-space:pre">       </span>// 0.9544<span style="white-space:pre">                                                                                                                                                            </span>\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">  </span>"<span style="white-space:pre">       </span>int kernel   = int(ceil(2.0 * uSigma));<span style="white-space:pre">                                                                                                    </span>\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">  </span>"<span style="white-space:pre">                                                                                                                                                                                       </span>\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">  </span>"<span style="white-space:pre">       </span>vec4 pixel = vec4(0.0);<span style="white-space:pre">                                                                                                                                      </span>\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">  </span>"<span style="white-space:pre">       </span>float coef = 0.0;<span style="white-space:pre">                                                                                                                                            </span>\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">  </span>"<span style="white-space:pre">       </span>for (int dx = -kernel; dx <= kernel; dx++) {<span style="white-space:pre">                                                                                      </span>\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">  </span>"<span style="white-space:pre">               </span>for (int dy = -kernel; dy <= kernel; dy++) {<span style="white-space:pre">                                                                              </span>\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">  </span>"<span style="white-space:pre">                       </span>int x = id.x + dx;<span style="white-space:pre">                                                                                                                           </span>\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">  </span>"<span style="white-space:pre">                       </span>int y = id.y + dy;<span style="white-space:pre">                                                                                                                           </span>\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">  </span>"<span style="white-space:pre">                       </span>if (x < 0 || x >= size.x ||<span style="white-space:pre">                                                                                                            </span>\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">  </span>"<span style="white-space:pre">                               </span>y < 0 || y >= size.y) {<span style="white-space:pre">                                                                                                                </span>\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">  </span>"<span style="white-space:pre">                               </span>continue;<span style="white-space:pre">                                                                                                                                    </span>\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">  </span>"<span style="white-space:pre">                       </span>}<span style="white-space:pre">                                                                                                                                                            </span>\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">  </span>"<span style="white-space:pre">                       </span>float c = exp(-float(dx * dx + dy * dy) / (2.0 * uSigma * uSigma + 1e-5));<span style="white-space:pre">           </span>\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">  </span>"<span style="white-space:pre">                       </span>pixel += (imageLoad(uImageIn, ivec2(x, y)) * c);<span style="white-space:pre">                                                             </span>\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">  </span>"<span style="white-space:pre">                       </span>coef  += c;<span style="white-space:pre">                                                                                                                                         </span>\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">  </span>"<span style="white-space:pre">               </span>}<span style="white-space:pre">                                                                                                                                                                    </span>\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">  </span>"<span style="white-space:pre">       </span>}<span style="white-space:pre">                                                                                                                                                                            </span>\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">  </span>"<span style="white-space:pre">       </span>imageStore(uImageOut, id, pixel / (coef + 1e-5));<span style="white-space:pre">                                                                            </span>\n"</font></div><div class="subprettyprint"><font color="#666600">    "}\n"</font></div><div class="subprettyprint"><font color="#666600">};</font></div><div class="subprettyprint"><font color="#666600"><br></font></div><div class="subprettyprint"><font color="#666600">int main( int argc, char** argv )</font></div><div class="subprettyprint"><font color="#666600">{</font></div><div class="subprettyprint"><font color="#666600">    osg::ArgumentParser arguments( &argc, argv );</font></div><div class="subprettyprint"><font color="#666600"><br></font></div><div class="subprettyprint"><font color="#666600">    // Create the texture as both the output of compute shader and the input of a normal quad</font></div><div class="subprettyprint"><font color="#666600">    osg::ref_ptr<osg::Texture2D> tex2D = new osg::Texture2D;</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">        </span>osg::Image* pImage = osgDB::readImageFile("Images/man.png");</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">    </span>pImage->setDataVariance(osg::Object::DYNAMIC);</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre"> </span>tex2D->setImage(pImage);</font></div><div class="subprettyprint"><font color="#666600">    tex2D->setTextureSize( 512, 512 );</font></div><div class="subprettyprint"><font color="#666600">    tex2D->setFilter( osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR );</font></div><div class="subprettyprint"><font color="#666600">    tex2D->setFilter( osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR );</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">   </span>tex2D->setInternalFormat(GL_RGBA32F_ARB);</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">      </span>tex2D->setSourceFormat(GL_RGBA);</font></div><div class="subprettyprint"><font color="#666600">    tex2D->setSourceType( GL_FLOAT );</font></div><div class="subprettyprint"><font color="#666600">    // So we can use 'image2D' in the compute shader</font></div><div class="subprettyprint"><font color="#666600">    osg::ref_ptr<osg::BindImageTexture> imagbinding = new osg::BindImageTexture(0, tex2D.get(), osg::BindImageTexture::READ_WRITE, GL_RGBA32F_ARB);</font></div><div class="subprettyprint"><font color="#666600">    </font></div><div class="subprettyprint"><span style="white-space:pre"><font color="#666600"> </font></span></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">  </span>osg::ref_ptr<osg::Texture2D> tex2D2 = new osg::Texture2D;</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">   </span>tex2D2->setTextureSize(512, 512);</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">      </span>tex2D2->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR);</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre"> </span>tex2D2->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR);</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre"> </span>tex2D2->setInternalFormat(GL_RGBA32F_ARB);</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">     </span>tex2D2->setSourceFormat(GL_RGBA);</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">      </span>tex2D2->setSourceType(GL_FLOAT);</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">       </span>// So we can use 'image2D' in the compute shader</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">  </span>osg::ref_ptr<osg::BindImageTexture> imagbinding2 = new osg::BindImageTexture(0, tex2D2.get(), osg::BindImageTexture::READ_WRITE, GL_RGBA32F_ARB);</font></div><div class="subprettyprint"><font color="#666600"><br></font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">      </span>// The compute shader can't work with other kinds of shaders</font></div><div class="subprettyprint"><font color="#666600">    // It also requires the work group numbers. Setting them to 0 will disable the compute shader</font></div><div class="subprettyprint"><font color="#666600">    osg::ref_ptr<osg::Program> computeProg = new osg::Program;</font></div><div class="subprettyprint"><font color="#666600">    computeProg->addShader( new osg::Shader(osg::Shader::COMPUTE, computeSrc) );</font></div><div class="subprettyprint"><font color="#666600"><br></font></div><div class="subprettyprint"><font color="#666600">    // Create a node for outputting to the texture.</font></div><div class="subprettyprint"><font color="#666600">    // It is OK to have just an empty node here, but seems inbuilt uniforms like osg_FrameTime won't work then.</font></div><div class="subprettyprint"><font color="#666600">    // TODO: maybe we can have a custom drawable which also will implement glMemoryBarrier?</font></div><div class="subprettyprint"><font color="#666600">    osg::ref_ptr<osg::Node> sourceNode = new osg::DispatchCompute(512/16, 512/16, 1 );</font></div><div class="subprettyprint"><font color="#666600">    sourceNode->setDataVariance( osg::Object::DYNAMIC );</font></div><div class="subprettyprint"><font color="#666600">    sourceNode->getOrCreateStateSet()->setAttributeAndModes( computeProg.get() );</font></div><div class="subprettyprint"><font color="#666600">    sourceNode->getOrCreateStateSet()->addUniform( new osg::Uniform("uImageIn", (int)0) );</font></div><div class="subprettyprint"><font color="#666600">    sourceNode->getOrCreateStateSet()->setTextureAttributeAndModes(0, tex2D.get() );</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">   </span>sourceNode->getOrCreateStateSet()->addUniform(new osg::Uniform("uImageOut", (int)1));</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">     </span>sourceNode->getOrCreateStateSet()->setTextureAttributeAndModes(1, tex2D2.get());</font></div><div class="subprettyprint"><font color="#666600"><br></font></div><div class="subprettyprint"><font color="#666600">    // Display the texture on a quad. We will also be able to operate on the data if reading back to CPU side</font></div><div class="subprettyprint"><font color="#666600">    osg::Geometry* geom = osg::createTexturedQuadGeometry(</font></div><div class="subprettyprint"><font color="#666600">        osg::Vec3(), osg::Vec3(1.0f,0.0f,0.0f), osg::Vec3(0.0f,0.0f,1.0f) );</font></div><div class="subprettyprint"><font color="#666600">    osg::ref_ptr<osg::Geode> quad = new osg::Geode;</font></div><div class="subprettyprint"><font color="#666600">    quad->addDrawable( geom );</font></div><div class="subprettyprint"><font color="#666600">    quad->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );</font></div><div class="subprettyprint"><font color="#666600">    quad->getOrCreateStateSet()->setTextureAttributeAndModes( 0, tex2D2.get() );</font></div><div class="subprettyprint"><font color="#666600">    quad->getOrCreateStateSet()->setAttributeAndModes(imagbinding2.get());</font></div><div class="subprettyprint"><font color="#666600">    // Create the scene graph and start the viewer</font></div><div class="subprettyprint"><font color="#666600">    osg::ref_ptr<osg::Group> scene = new osg::Group;</font></div><div class="subprettyprint"><font color="#666600">    scene->addChild( sourceNode );</font></div><div class="subprettyprint"><font color="#666600">    scene->addChild( quad.get() );</font></div><div class="subprettyprint"><font color="#666600"><br></font></div><div class="subprettyprint"><font color="#666600">    osgViewer::Viewer viewer;</font></div><div class="subprettyprint"><font color="#666600">    viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );</font></div><div class="subprettyprint"><font color="#666600">    viewer.addEventHandler( new osgViewer::StatsHandler );</font></div><div class="subprettyprint"><font color="#666600">    viewer.addEventHandler( new osgViewer::WindowSizeHandler );</font></div><div class="subprettyprint"><font color="#666600">    viewer.setSceneData( scene.get() );</font></div><div class="subprettyprint"><font color="#666600">    return viewer.run();</font></div><div class="subprettyprint"><font color="#666600">}</font></div><div><br></div></div></code></div><br><br></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups "OpenSceneGraph Users" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="mailto:osg-users+unsubscribe@googlegroups.com">osg-users+unsubscribe@googlegroups.com</a>.<br />
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/osg-users/f560c951-678e-4b67-b7bb-e4c4569207cco%40googlegroups.com?utm_medium=email&utm_source=footer">https://groups.google.com/d/msgid/osg-users/f560c951-678e-4b67-b7bb-e4c4569207cco%40googlegroups.com</a>.<br />