Render to texture, RTT

Source: Internet
Author: User
Render to texture, RTT

Tags:HLSL Rendering Graphics
Category:Game programming
RTT is a basic technology used in many special effects. It is very simple and important to implement it. However, It is puzzling that only a few articles have been found on the internet for a long time. I don't know if it is too simple or because the technology has been around for a long time. In short, I had a lot of twists and turns when I was exploring this thing. Now let's write down the specific implementation method.

Rendering to a texture, as its name implies, is to change the rendering target from the frame cache to a texture. In this way, you can render a scenario and perform post process to make various popular special effects. In addition, when using GPU for general-purpose computing, the program also exchanges data with the GPU through RTT.

Steps:

  1. Declare Variables
    Lpdirect3dtexture9 prendertexture = NULL; // target texture
    Lpdirect3dsurface9 prendersurface = NULL, pbackbuffer = NULL, ptempsurface;
    // Prendersurface is the surface corresponding to prendertexture
    // Pbackbuffer is used to save the original render target
  2. Create a texture as the rendering target)
    // Note that the third parameter must be d3dusage_rendertarget.
    // The fourth parameter determines the texture format. Different scenarios require different formats.
    Pd3ddevice-> createtexture (tex_width, tex_height, 1, d3dusage_rendertarget, d3dfmt_r5g6b5, d3dpool_default, & prendertexture, null );

    // Obtain the surface corresponding to prendertexture
    Prendertexture-> getsurfacelevel (0, & prendersurface );

  3. Rendering scenario
    // Save the original render target and restore it after RTT.
    Pd3ddevice-> getrendertarget (0, & pbackbuffer );
    If (succeeded (pd3ddevice-> beginscene ()))
    {
    // Set our texture to render target
    Pd3ddevice-> setrendertarget (0, prendersurface );
    Pd3ddevice-> clear (0, null, d3dclear_target | d3dclear_zbuffer, d3dxcolor (0.0f, 0.00f, 0.00f, 1.00f), 1.0f, 0 );

    // Set render target to frame cache again
    Pd3ddevice-> setrendertarget (0, pbackbuffer );

    Pd3ddevice-> endscene ();
    Pbackbuffer-> release ();
    }

  4. Aftercare
    Safe_release (prendersurface );
    Safe_release (prendertexture );

Notes:

  • Select the correct texture format during rendering. If you want to save a high-precision floating point number in the texture. Generally, in the a8r8g8b8 format, each color component has only eight digits and can only represent 0-255. For more information, see d3dformat in DirectX SDK help.
  • If the aspect ratio of your texture is different from that of the frame cache, You need to reset the projection matrix after switching the rendertarget. Otherwise, the rendered image is stretched.
  • The texture size cannot be too large. The experiment found that the size of the window and texture in the window mode cannot exceed the size of the screen minus the taskbar. If it is exceeded, it does not seem to have any effect on Texture operations.
  • If you want to verify that the texture you have rendered is correct, you can use d3dxsavetexturetofile to save the texture as an image.
  • If you want to directly access the values in the texture, it will be a bit more difficult. According to the SDK documentation, the texture used as the rendertarget is stored in the video memory and cannot be locked or unlocked. Perform the following operations to access the value:
    Lpdirect3dtexture9 text;
    Lpdirect3dsurface9 surf;
    D3dlocked_rect lockbits;
    Pd3ddevice-> createtexture (tex_width, tex_height, 1, 0, d3dfmt_r5g6b5, d3dpool_systemmem, & text, null );
    Text-> getsurfacelevel (0, & SURF );
    If (pd3ddevice-> getrendertargetdata (prendersurface, surf) = d3d_ OK)
    If (surf-> lockrect (& lockbits, null, d3dlock_readonly) = d3d_ OK)
    {
    Prendersurface-> unlockrect ();
    Float * bits = (float *) (lockbits. pbits );
    // Save bits to text file
    File * ofile = fopen ("output.txt", "W ");
    For (INT I = 0; I <64; I ++)
    {
    For (Int J = 0; j <64; j ++)
    Fprintf (ofile, "(% 2.2f, % 2.2f, % 2.2f)", BITs [I * 64*4 + J * 4], bits [I * 64*4 + J * 4 + 1], BITs [I * 64*4 + J * 4 + 2]);
    Fprintf (ofile, "/N ");
    }
    Fclose (ofile );
    }
    Text-> release ();
    Surf-> release ();

This technique can be used to deliver rendering results in multi-channel rendering. For example, you can use the RTT result as the texture in the second part of the rendering, so as to realize the effect of water surface reflection. In addition, data can be stored in general computing. For example, you can save the GPU numerical computation results in the texture, use the preceding method to read the numbers. (If you really want to do this, do not forget to set the texture format to a format with high precision, for example, a32b32g32r32f ).Problem about obtaining the backbuffer address.

The pseudo code is described as follows:
1: d3dpp. Flags = d3dpresentflag_lockable_backbuffer; // you can lock the backup buffer.
2: Device-> getbackbuffer (0, d3dbackbuffer_type_mono, & surface)
3: d3dlocked_rect d3dl_rect; (this is a 32-bit color version, it is very easy to change to 16-bit color)
Surface-> lockrect (& d3dl_rect, null, null );
Int pitch32 = d3dl_rect.pitch/4;
DWORD * P = (DWORD *) d3dl_rect.pbits; // This P is what you want. Please try F!
Surface-> unlockrect (); multi-channel rendering generally setrendertarget (0, prt0); setrendertarget (1, prt1); setrendertarget (2, Prt2); corresponds to colorbuffer, from color0, corresponding to color1 and color2 respectively

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.