mipmap generation using Cg & pbuffers

im trying to do the following: reduce a 1024x1024 texture (GL_TEXTURE_2D) to 1x1 (get the average value of the image).

for this i use 2 pbuffers, alternatively using one of them as texture source and the other as rendering target, reducing the resolution in each step to half of the original. the mipmapping itself is done by a small cg program that will calculate the average of 4 image pixels and write that to the color buffer (pbuffer).

the problems:

after i call wglMakeCurrent(pbuffer…) the textures from my ‘main’ device context are not available (so i can render stuff to the pbuffer, but without textures). is there a way of sharing textures across device/rendering contexts?

i also have to load the cg-porgram for each pbuffer separately, which makes everything pretty slow (>1000ms, compared to SGIS_auto_mipmap_generation: 30ms), although the program itself is pretty simple. so is there a way of sharing a cg-program across device/rendering contexts?

half4 mipmap(half4 color: COLOR,
half2 texcoord:TEXCOORD0,
uniform sampler2D image,
uniform half scale,
uniform half halfsizeX,
uniform half halfsizeY): COLOR

//ignore pixels outside mipmap bounds
if((texcoord.x > scalehalfsizeX) || (texcoord.y > scalehalfsizeY))
return half4(0,0,0,0);

//average of 4 pixels
half4 bl,br,tl,tr;
bl = h4tex2D(image, texcoord);
br = h4tex2D(image, texcoord + scalehalf2(halfsizeX, 0));
tl = h4tex2D(image, texcoord + scale
half2(0 ,halfsizeY));
tr = h4tex2D(image, texcoord + scale*half2(halfsizeX,halfsizeY));

	return ((bl+br+tl+tr)/4);


Did you call wglShareLists() when creating the pbuffer DC?

Anyway, a much easier way to get this would be to draw the data to a framebuffer, then CopyTexSubImage() into an image for which you have created MIP map levels, and for which you have enabled automatic MIP map generation.

Then glGet the texture image for the 1x1 MIP map level (10 I believe, in this case).

yes, i do call wglShareLists(), but this will only share display lists and no texture images, right?

automatic mipmap generation definately is much easier, but i’m looking into different ways of generating mipmaps - speed and accuracy are a big issue.

thanks anyway,

the name is a little misleading… sharelists shares not only display lists but also texture objects, vbos and even vertex and fragment programs :slight_smile:

If you call ShareLists() and it doesn’t share textures, then you’re calling it with the arguments in the wrong order.

Auto-generate MIP maps is likely to be faster than anything you can do on your own, because the driver can use special circuitry for it, and it may even be able to make it happen asynchronously, hiding the latency while you process other things.

i think it’s the pixelformat, because MSDN says that pbuffer pixelformats and the original pixelformat must be identical in order to share textures across RC’s.

but there’s sth strange:
wglChoosePixelFormat(…) gives you a list of PFs that match your criteria, so all of the PFs should be just as good and perform in the same way, right?
at first i used the first PF returned ([0] in the array) and then i can render to the pbuffer, but dont have any textures.
but since the first PF might be different from my main DC’s PF, i tried using the original PF (i get it with GetPixelFormat(hDC)) for the pbuffer as well, but then i can’t draw anything to the pbuffer (pbuffer contens always remain the same, even after pbuffer deletion + program restart), although this PF also is in the list returned by wglChoosePixelFormat(…) so it should support WGL_DRAW_TO_PBUFFER_ARB and all other criteria, right?

:confused: ,

ok, got it… really stupid mistake on my side:

forgot that i have to glBind(texture2d) for each DC separately. :smiley: