   Render to texture
  I need to render water in particular scene im creating, where water shader needs to know the distance between the water surface and the land below it. My first thought was this-- Render Whole scene without water to Texture, RGBMap- Render Whole scene without water to Texture, DepthInfoMap- Render RGBMap to a plane that fills the screen- Render water on top, using DepthInfoMap for the depth of water shadingBut that requires the scene to be rendered twice.My second thought was-- Render Whole scene without water to Texture, RGBAMap, with depth in the alpha- Render RGBAMap to a plane that fills the screen- Render water on top, using Alpha for the depth of water shadingBut, how do i then cope with refections? Such as having a boat on the water surface reflect onto the water.Also, why is it that you cant access the current depth of a pixel in a shader? If you're writing to it, why cant you access it?And is there anyway of getting the depth/stencil buffers for the standard back buffer? All the code ive read involves people rendering the scene to a texture and then using that as a depth map. But if you create your device with a D3DPRESENT_PARAMETERS that has AutoDepthStencilFormat set, can you not just get a depth map from that somehow?   
  In D3D9 your best option is to just render out depth to a texture.  Many games will do this during an early depth pass, so that later on only visible pixels are fully shaded.  It's also common to write out other useful information like normals or per-pixel velocity during the first pass.  Or if you wanted you could store depth in the alpha channel like you've suggested...although if you need better precision or you need the alpha channel for something else than using MRT to render depth to its own texture is probably a better idea.In D3D9 there is no way to sample the hardware depth buffer.  The reason why is because when D3D9 came out, it would have been unreasonable to force IHV's to store their hardware depth buffers in a format that would have allowed for a vendor-independent method of reading and interpreting its contents.  However in more modern D3D10-capable hardware depth buffers aren't that special anymore, which is why D3D10 supports sampling it.   ;  Are you looking for the vertical distance from the water surface to the ground beneath it, or are you looking for the distance that a ray would traverse from the time it hits the surface of the water to eventually hit the ground somewhere?  The it is the latter then you suggestions would work, if it is the former then you would need a different approach unless you are rendering from overhead.  If it is in fact the vertical distance that you want, you could sample a height map at the water pixel's location and subtract that from the water surfaces level to obtain the depth.   ;  Regarding what Jason said, using the actual depth could provide better performance, since the ground depth can be determined once.A couple of thoughts:- You can use multiple render targets if storing depth in alpha isn't accurate enough.- For two pass there's no need to render the full scene into the second buffer, just the ground.- You can render depth once even if you're calculating ray distance, by calculating in the shader based on the light direction what depth texel is hit and the distance to it.   ;Quote:although if you need better precision or you need the alpha channel for something else than using MRT to render depth to its own texture is probably a better idea.So render the scene twice, once for colour into colour map, and once for dpeth into depth map? Also, if im rendering the scene with a rendertarget of format D3DFMT_A8R8G8B8, and using the alpha for storing the depth, although in the shader, the depth calculation will be floating point, when its saved to the render target, isnt that going to only save the depth cast to a byte value?That is unless i use something like D3DFMT_A32B32G32R32F for the render target. The problem here though, is that i dont need the extra precision for the RGB values. So i guess rendering into 2 separate maps is the better option.Colour Map - With colour enabled and depth disabledDepth Map - Vice versaQuote:If it is in fact the vertical distance that you want, you could sample a height map at the water pixel's location and subtract that from the water surfaces level to obtain the depth.Quote:- For two pass there's no need to render the full scene into the second buffer, just the ground.Its not just the height between the land and water surface im after, its basically the distance between the water pixel being rendered, and any objects behind it, so i can calculate whether to add froth, and depth texturing. Such that the further away an object is from the water pixel depth, the deeper the colour of the water pixel.   ;Quote:Original post by maya18222So render the scene twice, once for colour into colour map, and once for dpeth into depth map? Also, if im rendering the scene with a rendertarget of format D3DFMT_A8R8G8B8, and using the alpha for storing the depth, although in the shader, the depth calculation will be floating point, when its saved to the render target, isnt that going to only save the depth cast to a byte value?No I'm saying render color to an A8R8G8B8 texture and depth to an R32F texture simultaneously by making the depth texture your second render target...that way you can do it all in one pass and still have good precision.  And yes, your result would be cast to an 8-bit value if you stored depth to the alpha channel and this typically doesn't give you enough precision for most effects.;  Two passes are your best option here. Using the alpha channel would reduce your depth to 8 bits precision, which is way too inprecise for realworld usage. Plus you sometimes want to use the alpha channel for different purposes in the scene, for example for rendering foliage.Other games use this occasion to render out additional data, too. If you're going to put depth into a rendertarget, you can pre-write the depth into the depth buffer, as well, which allows complete avoidance of any overdraw in the second pass. You can also put other per-pixel information, like MJP mentioned, for example view space normals for SSAO or relative movement speeds for motion blur.   ;Quote:No I'm saying render color to an A8R8G8B8 texture and depth to an R32F texture simultaneously by making the depth texture your second render target...that way you can do it all in one pass and still have good precision. And yes, your result would be cast to an 8-bit value if you stored depth to the alpha channel and this typically doesn't give you enough precision for most effects.Im sorry i dont understand. From my understanding you can only write to one rendertarget at a time. How would i write to the depth rendertarget and the colour rendertarget at the same time? I thought you had to do one after the other.   ;  Bind multiple render targets to your device, then set up a pixel shader output struct in your shader to write to each of them using the COLOR0, COLOR1... etc, semantics.   ;Quote:Original post by maya18222Im sorry i dont understand. From my understanding you can only write to one rendertarget at a time. How would i write to the depth rendertarget and the colour rendertarget at the same time? I thought you had to do one after the other.Direct3D devices support rendering to multiple render targets (MRT), and in the pixel shader you can write individual values to each one.  There's a brief summary here.  Any GPU I've ever worked with has supported up to 4 render targets, but you'll want to check the caps if you're targeting integrated GPU's or really old discrete GPU's (for instance the GeForce FX series doesn't support MRT).In your pixel shader your output to your RT's by using COLOR0 - COLOR3.  So for instance if you were writing color to the first RT and depth to the second, your pixel shader would look like this:void ColorDepthPS(in PixelShaderInput input, out float4 Color : COLOR0, out float4 Depth : COLOR1){    Color = DoColorCalculations();    Depth = float4(input.PositionClipSpace.z / input.PositionClipSpace.w, 1.0f, 1.0f, 1.0f);}