D3D Render To Texture Target

render_to_target-300x237

This demo shows how to apply the render to texture target feature with D3D API. The whole content of the Quad geometry was rendered into a texture, and after a cube mesh take this texture as it’s diffuse texture.

 

Render to Target is a very useful technology. Dynamic shadow maps, dynamic cube maps, some real time mirror effect, all kind of post-process effects and so on. All of them are based on this tech.

 

Pre-compile vertex shader & pixel shader. We could compile shaders at the running time, but pre-compile shaders into byte code files should be a much better way. On the one hand, this could save us some running time; on the other side, complied byte code shaders is not so easy to read and it is very hard to crack. This could be used to protect our assets especially when we deliver with a game.

 

To pre-compile D3D HLSL shaders, you coudl refer to “fxc.exe” (This tool should be located under the DirectX install directory). For more details about how to compile shaders, you could check it’s command line help.

1) To compile a pixel shader with optimization on:

fxc /T ps_2_0 /E Main /Fo Quad.ps.bin Quad.ps

2) To compile a pixel shader with debug option on:

fxc /Od /Zi /T ps_2_0 /E Main Quad.ps.bin Quad.ps

3) You could compile the vertex shaders with the very similar command line, but replace “ps_2_0” with “vs_2_0” and give the vertex shader source file. like following:

fxc /T vs_2_0 /E Main /Fo Quad.vs.bin Quad.vs

 

The process of using real time compiling shaders and process of pre-compiled shaders is almost the same.

1) To use real time compiling shaders:

Call “D3DXCompileShaderFromFile()” first to create the shader buffer. With the shader buffer filled already, call “CreatePixelShader” or “CreateVeratexShader” directly.

2) To use pre-compiled shaders:

Here things become more easier. Load the shader byte code binary file content into memory, then call “CreatePixelShader” or “CreateVertexShader” directly.

 

The full source code could be download from here.

posted @ 2012-08-24 08:42  opencoder  阅读(545)  评论(0编辑  收藏  举报