Xbox LIVE Indie Games
Sort Discussions: Previous Discussion Next Discussion
Page 1 of 1 (6 posts)

Get result of vertex shader ?

Last post 7/25/2009 4:14 PM by Stainless. 5 replies.
  • 7/25/2009 9:25 AM

    Get result of vertex shader ?

    I have a terrain system based on generated noise in the vertex shader.

    I feed in a vertex buffer with the x,z, and texture coords set, then generate the y coord in the vertex shader.

    Is it possible to get the post vertex shader positions back into c# ?
  • 7/25/2009 9:37 AM In reply to

    Re: Get result of vertex shader ?

    Realistically, it's often too difficult to make it feasible. But it depends on your needs.

    You can draw the data into a texture, then read back the data - however this really can't be done in realtime. However if you only need the data in later rendering, you can just keep it in the texture.
    Otherwise, you could consider implementing the same shader logic in C# using the source data, unless you are using complex texture lookups then this will probably be your best option.

  • 7/25/2009 11:34 AM In reply to

    Re: Get result of vertex shader ?

    It is possible to do this in realtime on an xbox. You can write (as StatusUnknown mentioned) the calculated information to your current rendertarget and afterwards resolve the rendertarget. Finally you get a texture that you can process with the cpu or use as an input for the next shader pass. On the xbox and various graphicscard you can even make a texture fetch in the vertex shader. This means you can use the texture values to change the vertex values of the vertexbuffer in the vertex processer (e.g. change the heigth values of a flat grid). Since the xbox has no geometry processor you can not generate vertices on the gpu side. This technique ist often used e.g. for calclulating physics on the gpu side.

    But if I understand you correct you only want to change the surface height randomly. In this case I would calculate the random values on the cpu side and put these values into a texture as input for the shader. Maybe you should take a look at the GPU Gems 2 article "Using Vertex Texture Displacement for Realistic Water Rendering" - this can be found online at the NVidia website. In this article is described how a texture can be used to change the vertices of the vertexbuffer.

  • 7/25/2009 2:24 PM In reply to

    Re: Get result of vertex shader ?

    Yes I can write to textures, I do it all the time, but specifically I wanted to recover the post vertex shader coordinates.

    The problem is this, I can generate terrain on a planetary scale with a vertex shader, I feed it a vertexbuffer with the x and z coordinates set then generate the y coordinate.

    Works fine, but when you want to populate the terrain with things like trees, planets, animals, you end up doing all the calculations again, which is just too slow.

    So I implemented a new shader which renders the terrain to a render target which I recover as a texture in vector4 format (on the pc)

    The problem is the two do not match up. For some reason I cannot discover they are completly different.

    So I tried using pixwin to compare the results of the two shaders, and they are identical.

    Which means that the reference graphic driver works, but the graphics driver for my video card does not.

    So I don't have a bug in my code, I have a problem with my approach.

    IF I can recover the post vertex shader coordinates of my working shader, I can use that directly, or use it to compare with the results of the render to texture to figure out why it isn't working.

    Pixwin does it, so there must be a way.

  • 7/25/2009 3:31 PM In reply to

    Re: Get result of vertex shader ?

    If I remember correctly Pix for Windows runs the vertex shaders on the CPU for this view.

    Beside of this do you planning to use this on the PC or the XBox? I am asking because transferring data from the video memory back to the main memory is a slow high latency operation on the PC.

    I would recommend another approach to solve your problem. If you already have a shader code snippet that could calculate the terrain high for a give point use it in your object shader. Transfer the reference point as a constant to the vertex shader. Calculate the right high and add it to the y coordinate of your objects.

  • 7/25/2009 4:14 PM In reply to

    Re: Get result of vertex shader ?

    As a test I took my vertex shader that works, and modded it to output the height instead of rendering a pixel.

    This works, though doing all the calculations twice grinds my pc.

    So it has to be something to do with either the way texture coords are interpolated, or something I haven't considered.

    the working approach has a 128 by 128 vertex buffer populated with x,z,and texcoords, I calculate a spherical coordinate based on the planetary position and the x,z coord, and use that to generate the terrain.

    the failing approach uses a vertex buffer with 4 coords, I ignore the input position and generate output position and the spherical coordinate based on the texture coords

    when I test in pixwin both approaches agree, when I run the code they do not.
Page 1 of 1 (6 posts) Previous Discussion Next Discussion