0

I'm trying to get my head around 3D development with XNA and I have a seemingly simple query that I can't seem to find the answer to.

We have the vertex shader which, if I understand correctly, is for transforming vertices. But, I also have various methods inside the Matrix class such as CreateRotationY etc which seem to do the same thing.

What should I be using to transform my vertices? The vertex shader or the methods inside of the Matrix? Are there any differences between the two?

Walter
  • 664
  • 1
  • 6
  • 19

2 Answers2

1

Vertex shader runs on GPU, Matrix you're talking about is used to operate over object in your RAM, using CPU.

What should I be using to transform my vertices?

It depends.

Vertex shaders are very fast, as they run on GPU, so by running some astonishing effect you free CPU to do something else, and psuh all work load on GPU, which is tailored to focus exactly matrix and vector operations by its achitecture.

On CPU, using matrix you can do small and short not very ("very" is relative to your program performance concerns concept) computational intensive operation, as moving calculation to GPU, has its cost to. But it all depends what is your application doing, so you have to measure.

In general they suggest to use shaders whenever its possible for fast computing.

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • I see, can I go ahead and assume that transforms with the vertex shader will use the HT&L functionality sitting on the GPU whereas the Matrix methods will use the SSE functionality on the CPU? – Walter Jul 11 '13 at 09:33
  • @user1435899: HT&L is not mandatory present (afaik), so I would not concretise a *method* it applies a calculaton. – Tigran Jul 11 '13 at 09:55
1

various methods inside the Matrix class such as CreateRotationY etc which seem to do the same thing

When you use Matrix.CreateRotationY(), you are not actually transforming any vertices. You are creating (calculating) a matrix that will be eventually sent to the shader where the vertices will be transformed by it.

So the heavy lifting of vertex transformation is always done in a shader even when you use Matrix methods in C#. It does not make sense to think of these two options (Matrix methods in Xna vs HLSL) as two independent ways to accomplish the same goal. They both represent different and necessary pieces of the puzzle.

Steve H
  • 5,479
  • 4
  • 20
  • 26
  • Wait, so if I use Matrix.CreateRotationY() it will use the CPU to create the matrix and the GPU to do the final transform based on the matrix? So then regardless of whether I use HLSL or not, all vertices will eventually be sent through the vertex shader? – Walter Jul 12 '13 at 17:30
  • yes. Typically the un-transformed vertices of a `Model` are stored in GPU memory and they are never altered or accessed by your c#. Each frame a copy of them are sent through the GPU's shader process where they get transformed by whatever matrices are sent to the shader that frame from C# (the Draw method). The next frame, a new and likely different set of matrices are sent to the GPU to transform a new copy of the original vertices. Since the new copy is transformed differently than last frame's copy, the model appears to move or rotate. – Steve H Jul 13 '13 at 16:33