Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - karagal

Pages: [1]
1
General / How to read pixels asynchronously using OpenGL & PBO
« on: January 31, 2022, 07:31:28 pm »
Hello!

I am drawing to a sf::RenderTexture and need to get the drawn image at each frame into an array.
Something like this but faster:

renderTexture.getTexture().copyToImage().getPixelsPtr();

I encountered Pixel Buffer Objects (PBOs) that would allow me to do this by copying from framebuffer to PBO asynchronously and alternating between two PBOs (ie i'd get the pixels with a 1 frame delay). However I'm not familiar with these and my current implementation is not working.

Here is what I am currently doing from what I've found so far:

initialization
GLuint pbo[2];
int index = 0;
int nextIndex = 1;

then at every frame
// several calls to renderTexture.draw(...);

index = (index + 1) % 2;
nextIndex = (nextIndex + 1) % 2;

glReadBuffer(GL_FRONT); // set target framebuffer to read

// read pixels from framebuffer to PBO asynchronously
glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo[index]);
glReadPixels(0, 0, textureWidth, textureHeight, GL_BGRA, GL_UNSIGNED_BYTE, nullptr);

// now read other PBO which should be already in CPU memory
glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo[nextIndex]);
unsigned char* ptr = (unsigned char*)glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);

if (ptr)
{    
    pixels_vector.assign(ptr, ptr + textureWidth * textureHeight * 4);
    glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
}

// back to conventional pixel operation
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);

 Currenty my code never enters the condition, meaning nothing is drawn to the buffer. I tried doing

glBindTexture(GL_TEXTURE_2D, renderTexture.getTexture().getNativeHandle());


but I feel like I'm missing something, everything doesn't seem to be bound together correctly. In particular I can't figure out how to make it so that GL_FRONT is linked to the texture I'm drawing to. If I could get help or pointers that would be very appreciated, as I haven't yet found an answer to that online and I'm not sure where to look anymore.

Thanks a lot!

2
General / Re: Efficient way to draw and get pixels
« on: January 30, 2022, 01:48:20 pm »
Thanks!

I've managed to avoid copying the pixel arrays several time with that. However the time I'm saving is tiny compared to the cost of moving from GPU to CPU.

Thing is in the deep learning process, those pixels are moved back to GPU for faster computations. Guess I have to figure out a way to make it stay on the GPU, but I have no idea how for now. Not very familiar with GPU computations, plus I'd have to pass GPU data from C++ SFML to Python.

Well, if someone here ever did something somewhat similar, please let me know!

3
General / Efficient way to draw and get pixels
« on: January 28, 2022, 11:32:43 am »
Hello,

I have been using SFML to render a driving simulator in C++, which is interfaced to Python using pybind11 for a deep learning project. At each frame I am drawing to a render texture, updating it, copying it to an image, getting the unsigned char pixel array (80*80*4), and copying it into a vector which I then return (I'm copying it to a vector so that the pixels can be moved to Python without copy). Right now this happens about 1000 times per second and is quite slow.

I'm coming here to see if anyone sees a more efficient way to do this, or if SFML is just not the right library for this task. Is there a way to avoid that costly GPU-to-CPU operation? (I just want the pixels, not to render to the screen, but I'm not sure it changes anything).

Thanks! :)

Pages: [1]