How to set the color of a single pixel in a Direct3D texture?

I'm trying to draw a 2D image on the screen in Direct3D, which I assume should be done by matching the texture with the rectangular polygon of the billboard designed to fill the screen. (I'm not interested or cannot use Direct2D.) All the text information that I found in the SDK describes loading a bitmap from a file and assigning a texture to use this bitmap, but I have not yet found a way to manipulate the texture as a bitmap pixel by pixels.

I would really like a feature like

void TextureBitmap::SetBitmapPixel(int x, int y, DWORD color);

If I cannot set the pixels directly in the texture object, do I need to save around the DWORD array, which is a bitmap, and then assign a texture to each frame?

Finally, although I assume that I will do this on the processor, the calculation of pixel colors can probably be done on the GPU. Is HLSL code that sets the color of a single pixel in a texture or are pixel shaders useful only for changing the displayed pixels?

Thanks.

+4
source share
1 answer

First, your direct question:

You can technically set the pixels in the texture. To do this, you will need to use LockRect and the UnlockRect API.

In the context of D3D, “locking” generally refers to transferring a resource from the GPU memory to system memory (thereby disabling its participation in rendering operations). After locking, you can change the filled buffer as you wish, and then unlock it - that is, transfer the changed data back to the GPU. Locking was generally considered a very expensive operation, but since PCIe 2.0 , this is probably not the main problem. You can also specify a small (even 1-pixel) RECT as the second LockRect argument, thereby requiring data transfer with a small amount of data, and we hope that the driver is really smart enough to transmit exactly that (I know that the fact is that in the old nVidia drivers it wasn’t).

A more efficient (and with code) way to achieve this, indeed, never leaves the GPU. If you create your texture as a RenderTarget (that is, specify D3DUSAGE_RENDERTARGET as use ), you could set it as the destination of the pipeline before making any call calls and write a shader (possibly passing parameters ) to paint your pixels. This use of rendering targets is considered standard, and you should be able to find a lot of code samples, but if you are not already experiencing performance issues, I would say that this is an excess for one 2D billboard.

NTN.

+4
source

All Articles