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 

[HELP] Properly using osg::VertexAttribDivisor for basic geometry instancing


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





PostPosted: Wed Aug 01, 2018 12:17 pm    Post subject:
[HELP] Properly using osg::VertexAttribDivisor for basic geometry instancing
Reply with quote

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?







------------------
Post generated by Mail2Forum
Back to top
mp3butcher (Julien Valentin)
Appreciator


Joined: 17 Feb 2010
Posts: 519
Location: France

PostPosted: Wed Aug 01, 2018 2:32 pm    Post subject:
Re: [HELP] Properly using osg::VertexAttribDivisor for basic geometry instancing
Reply with quote

Hi
perhaps it's because of transaltion array 's default binding (UNDEFINED)
try
translation->setArrayBinding(Array::PER_VERTEX)
Cheers

ppsychrite wrote:
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?







------------------
Post generated by Mail2Forum

_________________
Twirling twirling twirling toward freedom
Back to top
View user's profile Send private message Visit poster's website
wernerM
Appreciator


Joined: 06 Feb 2013
Posts: 105

PostPosted: Thu Aug 02, 2018 9:21 am    Post subject:
[HELP] Properly using osg::VertexAttribDivisor for basic geometry instancing
Reply with quote

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 -

Am 01.08.2018 um 16:32 schrieb Julien Valentin:

Quote:
Quote:
Hi
perhaps it's because of transaltion array 's default binding (UNDEFINED)
try
translation->setArrayBinding(Array::PER_VERTEX)
Cheers


ppsychrite wrote:
Quote:
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?





_______________________________________________
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





_______________________________________________
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

--
TEXION Software Solutions, Rotter Bruch 26a, D-52068 Aachen
Phone: +49 241 475757-0
Fax: +49 241 475757-29
Web: http://texion.eu
eMail: (
Only registered users can see emails on this board!
Get registred or enter the forums!
)

------------------
Post generated by Mail2Forum
Back to top
View user's profile Send private message
mp3butcher (Julien Valentin)
Appreciator


Joined: 17 Feb 2010
Posts: 519
Location: France

PostPosted: Thu Aug 02, 2018 3:19 pm    Post subject:
Re: [HELP] Properly using osg::VertexAttribDivisor for basic geometry instancing
Reply with quote

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


wernerM wrote:
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 -

Am 01.08.2018 um 16:32 schrieb Julien Valentin:

Quote:
Quote:
Hi
perhaps it's because of transaltion array 's default binding (UNDEFINED)
try
translation->setArrayBinding(Array::PER_VERTEX)
Cheers


ppsychrite wrote:
Quote:
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?





_______________________________________________
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





_______________________________________________
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

--
TEXION Software Solutions, Rotter Bruch 26a, D-52068 Aachen
Phone: +49 241 475757-0
Fax: +49 241 475757-29
Web: http://texion.eu
eMail: (
Only registered users can see emails on this board!
Get registred or enter the forums!
)

------------------
Post generated by Mail2Forum

_________________
Twirling twirling twirling toward freedom
Back to top
View user's profile Send private message Visit poster's website
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 Force OSG for GLES 2.0 to reenable ve... plamen_t General 4 Tue Jun 11, 2019 11:57 am View latest post
No new posts Layered rendering with a geometry shader AnyOldName3 General 8 Tue May 14, 2019 11:25 pm View latest post
No new posts Retrieve the triangles indices array ... Paradox General 1 Mon May 13, 2019 11:16 am View latest post
No new posts Really basic question light/material ... dhhabyc General 0 Tue Apr 09, 2019 4:53 pm View latest post
No new posts Looking for big datasets to properly ... robertosfield General 6 Wed Apr 03, 2019 2:02 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