<div dir="ltr"><div>sorry.My environment is win10 osg3.6.4 GTX1660Ti.<br></div><div><div>I tried to write a Gaussian blur computeshader,but I don't know why display error.</div><div><br></div><p class="separator" style="text-align: center; clear: both;"><img src="cid:c355dc23-9fdf-4266-8113-a2d1c3b429f6" alt="QQ图片20200623183156.png" style="margin-left: 1em; margin-right: 1em;" width="320" height="147"></p><div><br></div><div>The following code can be run directly.</div></div><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"><p class="separator" style="text-align: center; clear: both;"></p><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"><span style="white-space:pre">      </span>"uniform int      radius;\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre"> </span>"uniform float    width;\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">   </span>"uniform float    height;\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">  </span>"const float pi  = 3.1415926;\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">       </span>"float sigma     = float(radius) * 0.25;\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">  </span>"float s         = 2 * sigma * sigma;\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">   </span>"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"><span style="white-space:pre">       </span>"layout (local_size_x = 16, local_size_y = 16, local_size_z = 1) in;\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre"> </span>"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>"   vec2 scale = vec2(1.0) / vec2(width,height);                                            \n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre"> </span>"   vec4  pixel         = vec4(0.0);                                                        \n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">     </span>"<span style="white-space:pre">       </span>float weightSum     = 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 weight        = 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>vec2  offset        = vec2(0.0);<span style="white-space:pre">                                                                                                                </span>\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">  </span>"   for (int i = -radius / 2; i < radius / 2; i++)                                          \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>for (int j = -radius / 2; j < radius / 2; j++)<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>offset = vec2(i, j);<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>weight = exp(-(offset.x * offset.x + offset.y * offset.y) / s) / (pi * s);<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) + scale * offset) * weight;<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>weightSum += weight;<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>\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">  </span>"   pixel /= weightSum;<span style="white-space:pre">                                                                                                                                               </span>\n"</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">  </span>"   imageStore(uImageOut, id, pixel);<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>"}\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"><span style="white-space:pre">   </span>osg::ArgumentParser arguments(&argc, argv);</font></div><div class="subprettyprint"><font color="#666600"><br></font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">      </span>// 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"><span style="white-space:pre"> </span>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.jpg");</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"><span style="white-space:pre">       </span>tex2D->setTextureSize(512, 512);</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">       </span>tex2D->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR);</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">  </span>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_RGB32F_ARB);</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">       </span>tex2D->setSourceFormat(GL_RGB);</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">        </span>tex2D->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> imagbinding = new osg::BindImageTexture(0, tex2D.get(), osg::BindImageTexture::READ_WRITE, GL_RGB32F_ARB);</font></div><div class="subprettyprint"><font color="#666600"><br></font></div><div class="subprettyprint"><font color="#666600"><br></font></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_RGB32F_ARB);</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">      </span>tex2D2->setSourceFormat(GL_RGB);</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_RGB32F_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"><span style="white-space:pre">  </span>// It also requires the work group numbers. Setting them to 0 will disable the compute shader</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">     </span>osg::ref_ptr<osg::Program> computeProg = new osg::Program;</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">  </span>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"><span style="white-space:pre">        </span>// Create a node for outputting to the texture.</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">   </span>// 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"><span style="white-space:pre">   </span>// TODO: maybe we can have a custom drawable which also will implement glMemoryBarrier?</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">   </span>osg::ref_ptr<osg::Node> sourceNode = new osg::DispatchCompute(512 / 16, 512 / 16, 1);</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">       </span>sourceNode->setDataVariance(osg::Object::DYNAMIC);</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">     </span>sourceNode->getOrCreateStateSet()->setAttributeAndModes(computeProg.get());</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre"> </span>sourceNode->getOrCreateStateSet()->addUniform(new osg::Uniform("uImageIn", (int)0));</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">      </span>sourceNode->getOrCreateStateSet()->setTextureAttributeAndModes(0, tex2D.get());</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">     </span>//</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">        </span>osg::Uniform* radius0 = new osg::Uniform("radius", 15);</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre"> </span>sourceNode->getOrCreateStateSet()->addUniform(radius0);</font></div><div class="subprettyprint"><font color="#666600"><br></font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">        </span>osg::Uniform* width0 = new osg::Uniform("width", (float)pImage->s());</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">        </span>sourceNode->getOrCreateStateSet()->addUniform(width0);</font></div><div class="subprettyprint"><font color="#666600"><br></font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre"> </span>osg::Uniform* height0 = new osg::Uniform("height", (float)pImage->t());</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">      </span>sourceNode->getOrCreateStateSet()->addUniform(height0);</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">     </span>//</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"><span style="white-space:pre">       </span>// 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"><span style="white-space:pre"> </span>osg::Geometry* geom = osg::createTexturedQuadGeometry(</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">            </span>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"><span style="white-space:pre">   </span>osg::ref_ptr<osg::Geode> quad = new osg::Geode;</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">     </span>quad->addDrawable(geom);</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">       </span>quad->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">        </span>quad->getOrCreateStateSet()->setTextureAttributeAndModes(0, tex2D2.get());</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">  </span>quad->getOrCreateStateSet()->setAttributeAndModes(imagbinding2.get());</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">      </span>// Create the scene graph and start the viewer</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">    </span>osg::ref_ptr<osg::Group> scene = new osg::Group;</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">    </span>scene->addChild(sourceNode);</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">   </span>scene->addChild(quad.get());</font></div><div class="subprettyprint"><font color="#666600"><br></font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">      </span>osgViewer::Viewer viewer;</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre"> </span>viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">     </span>viewer.addEventHandler(new osgViewer::StatsHandler);</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre">      </span>viewer.addEventHandler(new osgViewer::WindowSizeHandler);</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre"> </span>viewer.setSceneData(scene.get());</font></div><div class="subprettyprint"><font color="#666600"><span style="white-space:pre"> </span>return viewer.run();</font></div><div class="subprettyprint"><font color="#666600">}</font></div><div><br></div></div></code></div><br><br><br>在 2020年6月19日星期五 UTC+8下午11:05:18,Robert Osfield写道:<blockquote class="gmail_quote" style="margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir="ltr"><div>When you write</div><div><br></div><div>    "Why does the computeshaderblur program not display properly?"</div><div><br></div><div>What about this third party example is not displaying properly?</div><div><br></div><div>What hardware and drivers are you using?  What OSG version are you using?  What do you see on screen?</div></div></blockquote></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/eb91b72b-1f75-4724-ab14-cb8350b3317ao%40googlegroups.com?utm_medium=email&utm_source=footer">https://groups.google.com/d/msgid/osg-users/eb91b72b-1f75-4724-ab14-cb8350b3317ao%40googlegroups.com</a>.<br />