crondog

My understanding is that a vertex shader works on a single vertex at a time. If that's the case, I'm confused about how to call a shader using values that are in an array.

Say I have a mesh of ten vertices, and I have a an array of 40 vector3's. Each index in this array contains a unique vector value and corresponds to a vertex in the mesh, so, the array represents four frames of data that I want to use in a calculation within a vertex shader How is it done

Like I said my understanding is that the shader operates on a single vertex at a time, so I can't pass the whole vector3 array to the shader. I have to get the right index, and pass that value to a vector3 global in the shader, which doesn't sound like a big deal, right The problem is knowing what vertex the shader is currently operating on. Can anyone help me here Am I completely misunderstanding the way vertex shaders work



Re: XNA Game Studio Express Using an array in a vertex shader

CodePfo

Vertex shaders do work on one Vertex at a time. Basically, you can view a vertex shader as the body of a for loop, you write the body that processes each vertex. You don't have to actually send the vertices to the shader, it will do that itself. Don't worry about indices and all that either. Because this is out of your control, you cannot assume which vertex you are working on. You shouldn't need to anyway, why would you treat one vertex any different than another one for a given shader If you really need to treat different vertices differently, I think you can create a custom vertex definition and embed integers into it (or dummy Vector2's or Vector3's) and in the body of your shader, check these values to handle them appropriately.

If you want some good reference on shaders and HLSL, check out ati.amd.com, go to developer and check their reference section. The ShaderX2 book is online, and has great information about how all of this works.




Re: XNA Game Studio Express Using an array in a vertex shader

AOEA

Well, I'm used to writing shaders for Renderman or Mantra in RSL or VEX. There, it's trivial to send the value of the corresponding vertex index in an arbitrary array into the shader loop. As for why you would want to do this... deformers. If you want to apply a specific deformation to a mesh you can store the deformation in an array and apply it in a shader on vertex at a time. You could additionally use arbitrary attributes to blend between different deformations. You could even implement advanced animation techniques like pose based deformers in a fast vertex shader.

So, I guess what I have to do is attach the array for the current frame as an arbitrary attribute to the mesh each time I update before calling the shader. Is this possible Can anyone give me a pointer on how to do this




Re: XNA Game Studio Express Using an array in a vertex shader

jpolivares

Sounds like you may want to look into Vertex Streams to do this. It's like combining two "arrays" of vertices together, one is your source mesh and the other(s) can be your current deformer(s). You can change out one or more of the vertex streams before rendering to update the deformer.

For example, if you have 10 vertices in the source mesh and 4 sets of deformers (10 vertices each), you can set stream 0 to the source mesh and stream 1 to a deformer set. This will match the source vertices 1-1 with the deformer vertices. To the shader it kind of looks like the two sets of data are coming in as one vertex. Then you can apply whatever calculations between the data to make your output. And before rendering each frame you can change out which deformer is in stream 1.

That make any sense I have no XNA documentation with me right now so I can't give any example code, but maybe I'll post something later.

-Javier




Re: XNA Game Studio Express Using an array in a vertex shader

AOEA

It makes perfect sense. I came across the concept of vertex streams last night, in the overview, I think. It sounds like exactly what I need. I'll look into this tonight and let you know how it goes.