PDA

View Full Version : Render targets and pixels



NecroDOME
24-03-2009, 08:40 AM
Hi there,

I'm stuck again. I try to get a pixel (just one per frame) from a render target. How can I do that without much overhead.

What I do now is (every frame):
- Create new texture
- Copy render target surface to that texture
- Get pixel from copied textured
- Destroy texture
(Yes it works, but is slow as hell)

A normal texture can just be locked and get some data out of out, but a render target is a different story...

So is there a better way to just get one pixel? (I cannot render my scene to a 1x1 texture!!... well I can, but I don't want to end up looking at one big-ass pixel)

ps. I'm using DirectX 9 and shaders.

JSoftware
24-03-2009, 08:59 AM
Can't you use some sort of mip mapping feature to make the RT texture 1x1 pixel and then just copy that?

Why is it that you can't lock the RT texture btw?

NecroDOME
24-03-2009, 09:17 AM
Render targets are somehow "special". I also stumbled upon this one: http://www.gamedev.net/community/forums/topic.asp?topic_id=351396

Create a new render target. Render the rectangle you need to this new render target, and copy it to system memory.

I can copy it to 1x1 texture, but I was wondering if there was a better approach.

Dan
24-03-2009, 12:11 PM
You dont really need to copy your render target to a texture, just copy it to an offscreen surface and read a pixel from there, that might be just a bit faster.

Another approach - you can create a lockable backbuffer, then draw a rectangle with your texture, lock the backbuffer and read your pixel.

Why would you want to read a pixel of a render target anyway? Perhaps there is another high level approach?

NecroDOME
24-03-2009, 12:19 PM
Well, I use it for picking. And this way I don't have to cast a ray, don't have to iterate trough every triangle etc. etc., but I simply use an off screen render target for lookups. And I have it this way pixel-perfect!

User137
24-03-2009, 01:50 PM
Don't know about reading pixels, but here is old function i have in libs that can be used to pick a specified object from OpenGL screen. It is pixel perfect, doesn't make a ray and is as fast as your RenderProc which renders single triangle or a full object on screen. You can do this test after or during flipping main screen and draw unflipped and without any textures and lighting.


type
TRenderProc = procedure(index: integer);
...
// Returns render index if cursor points it
// or -1 if no match
function MousePick(mx, my, PickRadius: double;
Count: integer; RenderProc: TRenderProc): integer;
var SelBuffer: array[0..511] of TGLUint;
i,hits: integer; viewport: TGLVectori4;
d1: double;
begin
result:=-1;
if count<1 then exit;
glGetIntegerv(GL_VIEWPORT,@viewPort);

glSelectBuffer(512, @SelBuffer);
glRenderMode(GL_SELECT);
glInitNames; glPushName(0);
glMatrixMode(GL_PROJECTION);
glPushMatrix; glLoadIdentity;
gluPickMatrix(mx,my,PickRadius,PickRadius,viewport );
gluPerspective(45, AspectRatio, TGL_NEARZ, TGL_FARZ);
glMatrixMode(GL_MODELVIEW);

for i:=0 to Count-1 do begin
glLoadName(i);
RenderProc(i);
end;

glMatrixMode(GL_PROJECTION);
glPopMatrix;
glMatrixMode(GL_MODELVIEW);

hits:=glRenderMode(GL_RENDER);
if hits>0 then begin
d1:=selBuffer[1]; result:=selBuffer[3];
for i:=1 to hits-1 do
if selBuffer[i*4+1]<d1 then begin
result:=selBuffer[i*4+3]; d1:=selBuffer[i*4+1];
end;
end;
end;