Re: [info-performer] bin-user-data

Date view Thread view Subject view Author view

From: Paolo Farinelli (paolo++at++sgi.com)
Date: 11/02/2005 10:14:47


Hi Alexej,
pls see my comments below.
Regards,
Paolo

On Wed, 2 Nov 2005 11:29:37 +0100 (MET), Alexej Fink <alexejfink++at++gmx.net>
wrote:

> Hello,
>
> we have a problem submitting a userdata-pointer to bin pre/post funcs:
> ---- code ----------------------------------------------------
> //virtual
> void avMPShaderProgram::setupBin( int bin, pfChannel& chan) {
> chan.setBinCallBack( bin, PFBIN_CALLBACK_PRE_DRAW,
> &avMPShaderProgram::pre);
> chan.setBinCallBack( bin, PFBIN_CALLBACK_POST_DRAW,
> &avMPShaderProgram::post);
> chan.setBinUserData( bin, this, sizeof(avMPShaderProgram));
> ...
> --------------------------------------------------------------
>
> in the callback function "&avMPShaderProgram::pre":
> ---- code ----------------------------------------------------
> //static
> void avMPShaderProgram::pre ( void *userData)
> {
> avMPShaderProgram *sp = (avMPShaderProgram*) userData;
> std::cout << "avMPShaderProgram::pre-userData: " << sp << std::endl <<
> std::flush;
> sp->pre_impl(); // virtual member func.
> }
> --------------------------------------------------------------
>
> we get a different/wrong pointer. Any suggestions?

setBinUserData will actually make a copy of the "object" you
assign it, and provide a pointer to this copy to your bin callbacks.
[This is why you also need to provide a size value].

In your case, try something like this:

  void avMPShaderProgram::setupBin( int bin, pfChannel& chan) {
      chan.setBinCallBack( bin, PFBIN_CALLBACK_PRE_DRAW,
                           &avMPShaderProgram::pre);
      chan.setBinCallBack( bin, PFBIN_CALLBACK_POST_DRAW,
                           &avMPShaderProgram::post);

      /* allocate a pointer-to-your-class in shared arena */
      avMPShaderProgram** ptrToThis = (avMPShaderProgram**)pfCalloc(1,
                          sizeof(avMPShaderProgram*), pfGetSharedArena());

      /* set this pointer to point to 'this' */
      *ptrToThis = this;

      /* provide a pointer to the pointer-to-your-class;
         note that size is only the sizeof a pointer */
      chan.setBinUserData( bin, ptrToThis, sizeof(avMPShaderProgram*));
    ...
  }

  void avMPShaderProgram::pre ( void *userData)
  {
    /* userData is a pointer to a pointer-to-your-class. cast and
       dereference appropriately to obtain the pointer you need. */
    avMPShaderProgram *sp = *((avMPShaderProgram**)userData);
    std::cout << "avMPShaderProgram::pre-userData: " << sp << std::endl <<
std::flush;
    sp->pre_impl(); // virtual member func.
  }

Let me know if this does not work.

> and one more time:
>
> - why is bin-managing "done" by channel-instances?
>

Binning is tightly related to the cull and draw traversals.
Both are done at the channel level (see manpages for pfCull
and pfDraw).

> ============================================================
>
> btw. in man pfObject, there is a strange sentence:
>
> Note that memory from pfMemory::malloc is not an considered a pfObject so
> user-data pointers are not provided for pfMalloc'ed memory.

I think this means that the setUserData and getUserData methods
are implemented by the pfObject class, and thus cannot be used
directly on a pointer returned by a pfMalloc call.
Reference-counting on the other hand is implemented at a lower
level, and also applies to pfMalloc'ed memory.

 From the pfMemory manpage:

     The data pointer returned by pfMalloc, pfCalloc, and
     pfRealloc is actually part of a pfMemory object that,
     among other things, provides a reference count. Reference
     counts are used to keep track of how many times each allo-
     cated block of memory is referenced or instanced. All
     OpenGL Performer libpr objects (pfMemory) are created with
     pfMalloc so their reference counts are updated by appro-
     priate libpr routines. ...

> Alexej
>

-- 
Paolo Farinelli <paolo++at++sgi.com>
OpenGL|Performer - Silicon Graphics Inc.


Date view Thread view Subject view Author view

This archive was generated by hypermail 2b29 : Wed Nov 02 2005 - 10:09:14 PST