   Direct3D/HLSL Render Texture and Co.
  Hey all,I am currently working on a cross API 3D game framework ( I am currently working on the Direct3D version), and have been concentrating on the shader pipeline lately. Generally everything works ok, except when it comes to using Render Textures and Render Targets, mandatory if I want to incorporate Shadow Mapping, reflections and so on.My framework is highly abstractive, and thus most of the Direct3D stuff is hidden and/or wrapped in custom classes, for efficiency, and easier/automated use.Although most of my shader system wraps the Direct3D effects and works properly, I am still having issues dealing with Render Textures and Render Targets. I have managed to make it work after trial and error, but I am not happy with the way it is designed and works, so I was hoping you guys could maybe help me understand the concepts a bit better, and give me advice on how to proceed.I have no problems writing the actual shader (I use RenderMonkey), and loading is not a problem either, however, it is when it comes to using shaders with render targets within Direct3D that I get confused...From what I've seen on the internet, most of the example code demonstrating the use of render targets and render textures are "shader specific" in the sense that the code is written to work with a particular shader only with specific passes etc render target swapping, etc. You end up with a huge chunk of code for a simple 2 pass shader, and I cannot afford that in terms of ergonomy...In the case of my framework however, I would like avoiding to have the user take care of the render targets/textures (calling BeginScene, clearing, etc.) , and would rather like to be able to use render textures as easily as sending a matrix or a texture to the shader using the effect "Set" function.This is what I would like to achieve ( Pseudo-code using my framework's syntax): ////////////////////////////////////////////////////////////////////shader->SetRenderTexture( "renderTexture_Tex", myRenderTexture );shader->CommitChanges();shader->Begin();for(int i = 0 ; i < shader->getNumberOfPasses() ; i++ ){shader->BeginPass(i);//Render my object(s)shader->EndPass();}shader->End();////////////////////////////////////////////////////////////////////How would you go about designing such a thing? I was thinking of maybe writing a RenderTarget manager which would take care of the swapping, since I want to incorporate easy creation of multiple viewports using different cameras ; but I would also like to keep the shader system independant. Also, how would I go about detecting when to switch to a different rendertarget since the shader should be unknown at compile-time? I could possibly parse the .fx file for info about render targets, but it just seems a bit overkill, and there probably is an easier solution.Is there a way around that "SetRenderTarget(...)" mechanism, or is there something I might have missed? I guess I could still wrap the swapping of render targets into user friendly methods, but this still limits the amount of control I have on its use, and argueably allows the user to slow down his program and/or mess up the pipeline by calling "BeginScene" and "EndScene" unefficiently.Anyway, I would be grateful to anyone who can give me some input on what he/she would do; tips, comments and criticism over what I am trying to achieve.   