0

I'm wondering how to get the maximum data locality and performance for the following problem without data copy.

I've a std::vector< MyClass* > where MyClass is something like

class MyClass
{
public:
    MyClass(int n,double px,double py,double pz)
    {
        someField=n;
        x=px;
        y=py;
        z=pz;
        anotherField=100;
        anotherUnusefulField=-10.0;
    }

    int someField;
    int anotherField;


    double x;
    double y;
    double z;
    double anotherUnusefulField;
};

std::vector<MyClass*> myClassVector;
// add some values and set x,y,z

for (std::vector<MyClass*>::iterator iter = myClassVector.begin(); iter!=myClassVector.end();++iter)
{
    MyClass *tmp = *iter;
    tmp->x+=1.0;
    tmp->y+=2.0;
    tmp->z+=3.0;
}

I'm iterating frequently on these data and I also would like to enforce data locality. The data contained in the pointer to MyClass should be sent to a OpenGL vertex array, where the vertices are ONLY determined by x,y,z variables. As you may imagine is difficult to correctly set the strides, so I'm here to ask if there are other (portable) solution to this problem.

(p.s. I've already read the post VBOs with std::vector but my case is basically different because I have pointers and I also have other variables inside the class.)

Community
  • 1
  • 1
linello
  • 8,451
  • 18
  • 63
  • 109
  • which part of data copy? can you elaborate? – billz Jan 31 '13 at 23:09
  • 1
    If `MyClass` only contains member variables like you illustrate, then storing them directly in a vector will make them the most friendly to data locality. With the pointer approach you take here, I suspect you're thrashing the CPUs cache, let alone how useless the data are to OpenGL. – radical7 Jan 31 '13 at 23:21

1 Answers1

1

I have pointers

Those pointers are useless to OpenGL, as they're in client address space. Also OpenGL doesn't dereference second level pointers.

and I also have other variables inside the class.

Well, then don't do this. If you passed those class instances to OpenGL you'd copy a lot of useless data. I recommend you just store a index into a tightly packed std::vector or array in your class members, and a reference to the vector/array itself. You can use getter/setter/referencer member functions to abstract away the access to the vector, i.e.

class …
{

    // …
    std::vector<v_t> *v;
    size_t index_v;
    x_t getX() const { return (*v)[index_v]; }
    x_t setX(x_t x) { return (*v)[index_v] = x;}
    x_t &x() { return (*v)[index_v]; }

};
datenwolf
  • 159,371
  • 13
  • 185
  • 298