Share via


3D Pipeline Basics

The 3D graphics pipeline uses a graphics device to load resources and render a 3D scene using an effect.

In general, the 3D pipeline requires the following state for initialization:

  • World, view, and projection transforms to transform 3D vertices into a 2D space.
  • A vertex buffer which contains the geometry to render.
  • An effect that sets the render state necessary for drawing the geometry.

As you become comfortable with these ideas, you may want to learn more about the following: manipulating vertices, creating your own effects, applying textures, or improving performance by using index buffers.

The XNA Framework uses a shader-driven programmable pipeline. It requires a graphics card capable of at least Shader Model 2.0. The XNA Framework provides a class called BasicEffect that encapsulates most of these common operations.

The Graphics Device

When you create a game with XNA Game Studio, the XNA application model initializes a graphics device for you.

The GraphicsDeviceManager initializes the GraphicsDevice before you call Game.Initialize. Before you call Initialize, there are three ways to change the GraphicsDevice settings:

  1. Set the appropriate properties (e.g. PreferredBackBufferHeight, PreferredBackBufferWidth) on the GraphicsDeviceManager in your game's constructor.

  2. Handle the PreparingDeviceSettings event on the GraphicsDeviceManager, and change the PreparingDeviceSettingsEventArgs.GraphicsDeviceInformation.PresentationParameters member properties.

    Any changes made to the PreparingDeviceSettingsEventArgs will override the GraphicsDeviceManager preferred settings.

  3. Handle the DeviceCreated event on the GraphicsDeviceManager, and change the PresentationParameters of the GraphicsDevice directly.

When you call Game.Initialize, GraphicsDeviceManager creates and configures GraphicsDevice. You can safely access GraphicsDevice settings such as the backbuffer, depth/stencil buffer, viewport, and render states in Initialize.

After you call Game.Initialize, changes to the PresentationParameters of the GraphicsDevice will not take effect until you call GraphicsDeviceManager.ApplyChanges. Other changes, such as render states, will take effect immediately.

Resources

A resource is a collection of data stored in memory that can be accessed by the CPU or GPU. Types of resources that an application might use include render targets, vertex buffers, index buffers, and textures.

Based on the resource management mode that was used when a resource is created, it should be reloaded when the device is reset. For more information, see Loading Resources.

Vertex and Index Buffers

A vertex buffer contains a list of 3D vertices to be streamed to the graphics device. Each vertex in a vertex buffer may contain data about not only the 3D coordinate of the vertex, but also other information describing the vertex, such as the vertex normal, color, or texture coordinate. The XNA Framework contains several classes to describe common vertex declaration types, such as VertexPositionColor, VertexPositionColorTexture, VertexPositionNormalTexture, and VertexPositionTexture. Use the VertexElement class to compose custom vertex types.

Vertex buffers contain indexed or non-indexed vertex data.

If a vertex buffer is not indexed, all of the vertices are placed in the vertex buffer in the order they are to be rendered. Because 3D line lists or triangle lists often reference the same vertices multiple times, this can result in a large amount of redundant data.

Index buffers allow you to list each vertex only once in the vertex buffer. An index buffer is a list of indices into the vertex buffer, given in the order that you want the vertices to render.

To render a non-indexed vertex buffer, call the GraphicsDevice.DrawPrimitives Method or GraphicsDevice.DrawUserPrimitives Method. To render an indexed buffer, call the GraphicsDevice.DrawIndexedPrimitives Method or GraphicsDevice.DrawUserIndexedPrimitives Method.

Textures

A texture resource is a structured collection of texture data. The data in a texture resource is made up of one or more subresources that are organized into arrays and mipmap chains. Textures are filtered by a texture sampler as they are read. The type of texture influences how the texture is filtered.

You can apply textures by using the Texture property of the BasicEffect class, or choose to write your own effect methods to apply textures.

See Also

Concepts

Getting Started with 3D Games at App Hub Online
Shader Content Catalog at App Hub Online