4
\$\begingroup\$

I want to draw different per-pixel data on the screen, where each pixel has a specific value according to my needs. An example may be a random noise pattern where each pixel is randomly generated.

I'm not sure what is the correct and fastest way to do this. Locking a texture/surface and manipulating the raw pixel data? How is this done in modern graphics programming?

I'm currently trying to do this in Pygame but realized I will face the same problem if I go for C/SDL or OpenGL/DirectX.

\$\endgroup\$
9
  • \$\begingroup\$ What are you actually trying to do? The only answer possible here is like one line long and the only good answer would be "it depends." \$\endgroup\$ Jun 11, 2013 at 0:54
  • 1
    \$\begingroup\$ I'm trying to draw and array of heights from their height to the bottom of the screen, while changing the color of every pixel on the way down (creating some retro ground-like effect). I've just tried SDL 2 and streaming textures and it seems to be working nicely. \$\endgroup\$
    – Amir Eldor
    Jun 11, 2013 at 1:05
  • 1
    \$\begingroup\$ @stas - this doesnt seem like premature optimization, it seems more a case of doing things the sensible way to begin with. \$\endgroup\$ Jun 11, 2013 at 8:04
  • 1
    \$\begingroup\$ You want to change the texture each frame? Then the fastest version might be compute shaders that manipulate the texture directly on the graphics card without the need of uploading the texture each frame from the cpu to gpu. However, you might still have to upload some state data to the compute shader each frame, i.e. the noise generator state or whatever, but this might be significantally less than uploading the entire texture, plus compute is able to manipulate the texture much faster in parallel than a single cpu thread can. \$\endgroup\$ Jun 11, 2013 at 8:10
  • 1
    \$\begingroup\$ @stas - even so, it still doesn't seem like premature optimization. It was wrong to just throw the old Knuth chestnut at this question; not all performance improvements are premature optimization (even Knuth acknowledges this in the full context of the quote); some are a basic requirement of what a person is trying to do, and in this case it seems to fall squarely into that latter bracket. \$\endgroup\$ Jun 11, 2013 at 17:30

1 Answer 1

2
\$\begingroup\$

I'm not sure what is the correct and fastest way to do this. Locking a texture/surface and manipulating the raw pixel data? How is this done in modern graphics programming?

If this is something to recompute each frame, if the only input you need is the pixels position, and if you only need to output this directly to the screen, I suppose the modern and fastest way to to this is to simply use a pixel shader on the GPU. Render a full-screen quad with your favorite engine, with a pixel shader that computes a color from the pixel's position. This is similar to doing a full-screen post-process.

If you only need to recompute this one in a while, or if you need to output to something else than the screen, you could render to a texture for later use.

If you need more inputs than simply a pixel's position, you'll have to find a way to pass this info to your pixel shader. You can use uniforms, textures etc. for this purpose.

There are cases where you could use the CPU for the sake of simplicity. It will be easier to handle inputs/outputs, and you could save some texture copying time in some cases, but you'll need to workaround the fact that CPUs aren't really made to work on arrays of pixels. To avoid hiccups, you'll have to do it during a loading time, on a different thread, or even offline.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .