Discussion:
[osg-users] [HELP] Properly using osg::VertexAttribDivisor for basic geometry instancing
p***@cock.li
2018-08-01 12:16:50 UTC
Permalink
hello sorry if i did this wrong but i dont know how to use mailing lists

so ive been trying to do modern glsl/opengl 3.3+ with osg::Geometry and
osg::Stateset and it's been working OK for basic triangle except for
implementing instanced array data
am I doing it the wrong way? here's the .cpp code for it and for me it
seems well enough and fine, position and color works fine


osg::Vec3 coords[] = {
{ -0.05f,0.05f,0.f },
{ 0.05f,-0.05f,0.f },
{ 0.05f,0.05f,1.f }
};
osg::Vec3 colors[] = {
{1.f,0.f,0.f},
{0.f,1.f,0.f},
{0.f,0.f,1.f}
};

osg::Vec2 instancetranslations[] = {
{ 0.0f, 0.0f },
{ 0.1f, 0.0f },
{ 0.2f, 0.0f }
};

osg::Vec3Array *pos = new osg::Vec3Array(sizeof(coords) /
sizeof(osg::Vec3), coords);
osg::Vec3Array *color = new osg::Vec3Array(sizeof(colors) /
sizeof(osg::Vec3), colors);
osg::Vec2Array *translation = new
osg::Vec2Array(sizeof(instancetranslations) / sizeof(osg::Vec2),
instancetranslations);

osg::Geometry *vao = new osg::Geometry();
vao->setUseDisplayList(false);
vao->setUseVertexArrayObject(true);
vao->setUseVertexBufferObjects(true);

vao->setVertexAttribArray(0, pos, osg::Array::BIND_PER_VERTEX);
vao->setVertexAttribArray(1, color, osg::Array::BIND_PER_VERTEX);
vao->setVertexAttribArray(2, translation);

vao->addPrimitiveSet(new osg::DrawArrays(GL_TRIANGLES, 0, 3, 3)); //3
instances

osg::ref_ptr<osg::Program> program = new osg::Program;

program->addShader(osgDB::readShaderFile("shaders/basic.vert"));
program->addShader(osgDB::readShaderFile("shaders/basic.frag"));

osg::StateSet *stateset = geom->getOrCreateStateSet();
stateset->setAttributeAndModes(program.get());
stateset->setAttribute(new osg::VertexAttribDivisor(2, 1)); //set 3rd
array as per-instance

here's the vertex shader code for it:


#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTrans;

out vec3 color;

void main()
{
color = aColor;
gl_Position = vec4(aPos.x+ aTrans.x,aPos.y+ aTrans.y,aPos.z,1.0);
}


aTrans doesn't move the triangle at all, it's just one. I know it isn't
the shaders fault because instead of adding aTrans to aPos I've done
(gl_InstanceID * 0.1f) and it's worked perfectly, seeing 3 instanced
triangles

what am i doing wrong/how could i get per-instanced array working?
Julien Valentin
2018-08-01 14:32:37 UTC
Permalink
Hi
perhaps it's because of transaltion array 's default binding (UNDEFINED)
try
translation->setArrayBinding(Array::PER_VERTEX)
Cheers
Post by p***@cock.li
hello sorry if i did this wrong but i dont know how to use mailing lists
so ive been trying to do modern glsl/opengl 3.3+ with osg::Geometry and
osg::Stateset and it's been working OK for basic triangle except for
implementing instanced array data
am I doing it the wrong way? here's the .cpp code for it and for me it
seems well enough and fine, position and color works fine
osg::Vec3 coords[] = {
{ -0.05f,0.05f,0.f },
{ 0.05f,-0.05f,0.f },
{ 0.05f,0.05f,1.f }
};
osg::Vec3 colors[] = {
{1.f,0.f,0.f},
{0.f,1.f,0.f},
{0.f,0.f,1.f}
};
osg::Vec2 instancetranslations[] = {
{ 0.0f, 0.0f },
{ 0.1f, 0.0f },
{ 0.2f, 0.0f }
};
osg::Vec3Array *pos = new osg::Vec3Array(sizeof(coords) /
sizeof(osg::Vec3), coords);
osg::Vec3Array *color = new osg::Vec3Array(sizeof(colors) /
sizeof(osg::Vec3), colors);
osg::Vec2Array *translation = new
osg::Vec2Array(sizeof(instancetranslations) / sizeof(osg::Vec2),
instancetranslations);
osg::Geometry *vao = new osg::Geometry();
vao->setUseDisplayList(false);
vao->setUseVertexArrayObject(true);
vao->setUseVertexBufferObjects(true);
vao->setVertexAttribArray(0, pos, osg::Array::BIND_PER_VERTEX);
vao->setVertexAttribArray(1, color, osg::Array::BIND_PER_VERTEX);
vao->setVertexAttribArray(2, translation);
vao->addPrimitiveSet(new osg::DrawArrays(GL_TRIANGLES, 0, 3, 3)); //3
instances
osg::ref_ptr<osg::Program> program = new osg::Program;
program->addShader(osgDB::readShaderFile("shaders/basic.vert"));
program->addShader(osgDB::readShaderFile("shaders/basic.frag"));
osg::StateSet *stateset = geom->getOrCreateStateSet();
stateset->setAttributeAndModes(program.get());
stateset->setAttribute(new osg::VertexAttribDivisor(2, 1)); //set 3rd
array as per-instance
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTrans;
out vec3 color;
void main()
{
color = aColor;
gl_Position = vec4(aPos.x+ aTrans.x,aPos.y+ aTrans.y,aPos.z,1.0);
}
aTrans doesn't move the triangle at all, it's just one. I know it isn't
the shaders fault because instead of adding aTrans to aPos I've done
(gl_InstanceID * 0.1f) and it's worked perfectly, seeing 3 instanced
triangles
what am i doing wrong/how could i get per-instanced array working?
_______________________________________________
osg-users mailing list
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
------------------
Post generated by Mail2Forum
------------------------
Twirling twirling twirling toward freedom

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=74452#74452
Werner Modenbach
2018-08-02 09:20:06 UTC
Permalink
Hi,

what we do is packing our instance parameters in a uniform array and
access it via gl_InstanceID as an index into the array.

- Werner -
Post by Julien Valentin
Hi
perhaps it's because of transaltion array 's default binding (UNDEFINED)
try
translation->setArrayBinding(Array::PER_VERTEX)
Cheers
Post by p***@cock.li
hello sorry if i did this wrong but i dont know how to use mailing lists
so ive been trying to do modern glsl/opengl 3.3+ with osg::Geometry and
osg::Stateset and it's been working OK for basic triangle except for
implementing instanced array data
am I doing it the wrong way? here's the .cpp code for it and for me it
seems well enough and fine, position and color works fine
osg::Vec3 coords[] = {
{ -0.05f,0.05f,0.f },
{ 0.05f,-0.05f,0.f },
{ 0.05f,0.05f,1.f }
};
osg::Vec3 colors[] = {
{1.f,0.f,0.f},
{0.f,1.f,0.f},
{0.f,0.f,1.f}
};
osg::Vec2 instancetranslations[] = {
{ 0.0f, 0.0f },
{ 0.1f, 0.0f },
{ 0.2f, 0.0f }
};
osg::Vec3Array *pos = new osg::Vec3Array(sizeof(coords) /
sizeof(osg::Vec3), coords);
osg::Vec3Array *color = new osg::Vec3Array(sizeof(colors) /
sizeof(osg::Vec3), colors);
osg::Vec2Array *translation = new
osg::Vec2Array(sizeof(instancetranslations) / sizeof(osg::Vec2),
instancetranslations);
osg::Geometry *vao = new osg::Geometry();
vao->setUseDisplayList(false);
vao->setUseVertexArrayObject(true);
vao->setUseVertexBufferObjects(true);
vao->setVertexAttribArray(0, pos, osg::Array::BIND_PER_VERTEX);
vao->setVertexAttribArray(1, color, osg::Array::BIND_PER_VERTEX);
vao->setVertexAttribArray(2, translation);
vao->addPrimitiveSet(new osg::DrawArrays(GL_TRIANGLES, 0, 3, 3)); //3
instances
osg::ref_ptr<osg::Program> program = new osg::Program;
program->addShader(osgDB::readShaderFile("shaders/basic.vert"));
program->addShader(osgDB::readShaderFile("shaders/basic.frag"));
osg::StateSet *stateset = geom->getOrCreateStateSet();
stateset->setAttributeAndModes(program.get());
stateset->setAttribute(new osg::VertexAttribDivisor(2, 1)); //set 3rd
array as per-instance
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTrans;
out vec3 color;
void main()
{
color = aColor;
gl_Position = vec4(aPos.x+ aTrans.x,aPos.y+ aTrans.y,aPos.z,1.0);
}
aTrans doesn't move the triangle at all, it's just one. I know it isn't
the shaders fault because instead of adding aTrans to aPos I've done
(gl_InstanceID * 0.1f) and it's worked perfectly, seeing 3 instanced
triangles
what am i doing wrong/how could i get per-instanced array working?
_______________________________________________
osg-users mailing list
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
------------------
Post generated by Mail2Forum
------------------------
Twirling twirling twirling toward freedom
------------------
http://forum.openscenegraph.org/viewtopic.php?p=74452#74452
_______________________________________________
osg-users mailing list
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
--
*TEXION Software Solutions, Rotter Bruch 26a, D-52068 Aachen*
Phone: +49 241 475757-0
Fax: +49 241 475757-29
Web: http://texion.eu
eMail: ***@texion.eu
Julien Valentin
2018-08-02 15:20:26 UTC
Permalink
Hi Werner
vertex attribute pulling (your method) has good perf since number of instances stay low. However with growing number of instances you would see the perf diff compared to vertex attribute.
(I saw benchmark on a youtube nvidia speech but don't remember where)
No, using attrib divisor is still a better approach for perf perspective...
Cheers
Hi,
what we do is packing our instance parameters in a uniform array and access it via gl_InstanceID as an index into the array.
- Werner -
Post by Julien Valentin
Hi
perhaps it's because of transaltion array 's default binding (UNDEFINED)
try
translation->setArrayBinding(Array::PER_VERTEX)
Cheers
Post by p***@cock.li
hello sorry if i did this wrong but i dont know how to use mailing lists
so ive been trying to do modern glsl/opengl 3.3+ with osg::Geometry and
osg::Stateset and it's been working OK for basic triangle except for
implementing instanced array data
am I doing it the wrong way? here's the .cpp code for it and for me it
seems well enough and fine, position and color works fine
osg::Vec3 coords[] = {
{ -0.05f,0.05f,0.f },
{ 0.05f,-0.05f,0.f },
{ 0.05f,0.05f,1.f }
};
osg::Vec3 colors[] = {
{1.f,0.f,0.f},
{0.f,1.f,0.f},
{0.f,0.f,1.f}
};
osg::Vec2 instancetranslations[] = {
{ 0.0f, 0.0f },
{ 0.1f, 0.0f },
{ 0.2f, 0.0f }
};
osg::Vec3Array *pos = new osg::Vec3Array(sizeof(coords) /
sizeof(osg::Vec3), coords);
osg::Vec3Array *color = new osg::Vec3Array(sizeof(colors) /
sizeof(osg::Vec3), colors);
osg::Vec2Array *translation = new
osg::Vec2Array(sizeof(instancetranslations) / sizeof(osg::Vec2),
instancetranslations);
osg::Geometry *vao = new osg::Geometry();
vao->setUseDisplayList(false);
vao->setUseVertexArrayObject(true);
vao->setUseVertexBufferObjects(true);
vao->setVertexAttribArray(0, pos, osg::Array::BIND_PER_VERTEX);
vao->setVertexAttribArray(1, color, osg::Array::BIND_PER_VERTEX);
vao->setVertexAttribArray(2, translation);
vao->addPrimitiveSet(new osg::DrawArrays(GL_TRIANGLES, 0, 3, 3)); //3
instances
osg::ref_ptr<osg::Program> program = new osg::Program;
program->addShader(osgDB::readShaderFile("shaders/basic.vert"));
program->addShader(osgDB::readShaderFile("shaders/basic.frag"));
osg::StateSet *stateset = geom->getOrCreateStateSet();
stateset->setAttributeAndModes(program.get());
stateset->setAttribute(new osg::VertexAttribDivisor(2, 1)); //set 3rd
array as per-instance
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTrans;
out vec3 color;
void main()
{
color = aColor;
gl_Position = vec4(aPos.x+ aTrans.x,aPos.y+ aTrans.y,aPos.z,1.0);
}
aTrans doesn't move the triangle at all, it's just one. I know it isn't
the shaders fault because instead of adding aTrans to aPos I've done
(gl_InstanceID * 0.1f) and it's worked perfectly, seeing 3 instanced
triangles
what am i doing wrong/how could i get per-instanced array working?
_______________________________________________
osg-users mailing list
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org (http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org)
------------------
Post generated by Mail2Forum
------------------------
Twirling twirling twirling toward freedom
------------------
http://forum.openscenegraph.org/viewtopic.php?p=74452#74452 (http://forum.openscenegraph.org/viewtopic.php?p=74452#74452)
_______________________________________________
osg-users mailing list
()
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org (http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org)
--
TEXION Software Solutions, Rotter Bruch 26a, D-52068 Aachen
Phone: +49 241 475757-0
Fax: +49 241 475757-29
Web: http://texion.eu (http://texion.eu)
eMail: ()
------------------
Post generated by Mail2Forum
------------------------
Twirling twirling twirling toward freedom

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

Loading...