Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm new to using matrices, so I'd like some input on whether I've understood the use of them in the context described in the title.

Right now I have a small Direct3D 11-based game engine which renders a stack of scenes, or screens if you will, from bottom to top. I have a static 2D orthographic view-projection matrix that simply maps to screen coordinates from ( 0, 0 ) to ( screen width, screen height ). The position and resulting world matrix of the different entities are set with coordinates relative to this.

However, I'd like to expand on this by enabling different scenes to use different perspectives, so that I would be able to render a 3D scene with perspective, and then a flat 2D GUI on top of it. But since I'm very new to using matrices, I'd like confirmation on whether I have the correct idea of what will happen before I change things, because it takes a fair amount of work to refactor the engine with the way it is set up now.

Let's say I have Scene A as the 3D scene, and Scene B as my 2D overlay.

When entities in Scene A is rendered, the following matrices are passed to the shader:

  • View = camera position/orientation in the 3D world
  • Projection = perspective matrix with FoV, aspect ratio and near/far depth
  • World = the individual entity's position in the 3D world

Then, when I want to render Scene B, I pass these matrices instead:

  • View = identity matrix since the view is static
  • Projection = orthographic matrix with screen coordinates
  • World = the individual entity's position in the screen coordinate system

In both cases the vertex shader multiplies the matrices like so: world * view * projection

Is this correct? Will this draw my 3D scene as expected first, and then correctly draw the 2D elements relative to the screen coordinate system instead? Or have I misunderstood how the matrices work?

EDIT: scratch the question below; I found the answer under related questions...: Should the modelview and projection matrices be calculated in the shader or on the CPU?

Original question: And one more thing, would it be more efficient to compute the View-Projection-World matrix on the CPU end before passing it to the shader's constant buffer, or is the GPU better suited at receiving the three matrices and multiplying them on the shader side? I've seen both approaches used, but I haven't found any conclusions on which way is more practical/efficient. I have two constant buffers by the way, one with the View and Projection matrices which is updated once per scene, and one with the World matrix updated for each individual entity, so the memory bandwidth savings would be insignificant if I moved the multiplications to the CPU instead. But would that be faster in theory, and are there any eventual drawbacks to this?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

The matrix calculations, as long as they are constant for a frame being rendered, should be calculated by the CPU and sent to GPU. Multiplying matrices is not an expensive operation for the CPU as you might think. But if you pass it to the vertex shader, the same matrix result gets calculated n times, where n is the total number of 3D vertices being rendered for that frame, which is totally inefficient no matter how fast that might be. That is unless you have a method of calculating the matrix in vertex shader just once. If you do, then its great. Otherwise just 'keep it simple'. Calculate them in CPU and set them as constants to the shader.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.