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've read this document about HLSL registers, but it's more of a syntax description and not a purpose explanation.

What are registers in HLSL, actually? How and when do I use them? What happens if I don't (what's the default)?

share|improve this question
add comment

3 Answers

A given shader model exposes a particular set of registers to HLSL; these registers are underlying hardware registers on the GPU, like CPU registers, but have more refined scopes (for example, there are registers dedicated to holding samplers).

Registers are where all your data is stored during the execution of your shader (with the exception of data, like texture or vertex buffer data, that is held in the GPU's RAM). On a GPU, you don't have the same flexibility in reading and manipulating memory as you do on a CPU. There's no "store this value to RAM for a moment," so everything has to be in registers. This is particular obvious in old shader assembly (from the shader model 1 era).

Data transfer can occur via these registers as well, which is why the keywords still exist in more modern shader models. If you're not using the effects framework, for example, you can manage the allocation of your sampler or constant registers yourself (for example via methods like SetVertexShaderConstant). Similarly, data from buffers can be mapped to registers; the vertex input registers typically get your vertex buffer data mapped to them prior to execution of your shader.

You can find a list of registers and their purpose on the MSDN. Here, for example, is the set for VS 5.0.

Often, you don't need to use them directly, much like CPU registers. They will be used automatically by the higher-level language constructs that you use in your shader code or in the API (such as the aforementioned effects framework).

share|improve this answer
add comment

The specific registers you deal with syntactically will be some form of I/O register. Josh explained excellently what registers are and why they're used on the GPU. The syntax you use to declare variables in specific registers is for transferring data. On the CPU you create an Input Layout (Vertex Array Object in GL) or set uniforms or samplers/textures. This maps values in your vertex buffers to specific registers (via semantics for vertex data). The C++ code and HLSL code must agree on which data will be in which register for input as well output for MRT and the like.

If you don't specify registers explicitly, the HLSL picks them for you. You should not rely on how it picks them; even if it seems stable it may change in a future version. You can use the shader introspection APIs to determine which registers the compiler picked so you can setup your input layouts appropriately. This is less efficient and more difficult than just declaring the registers in your shader code.

Essentially, the C++ says something like "the camera data is in constant buffer register 3." The shader then must read that buffer from register 3 or it won't work correctly. You can do it by name like old GL forced you to, but you shouldn't.

This is all the same for recent GL/GLSL too, though obviously with differing syntax and APIs.

share|improve this answer
add comment

Registers are a type of storage that is located on a processor, and accessible very rapidly. They are typically substantially smaller, and are used as input and output locations for processor instructions. HLSL provides a method for mapping up data to these registers from the CPU side, to be used within your shader on the GPU. They should be used whenever you have data that needs to be referenced within a GPU, but is only available at run time on the CPU(such as world view projection matrices), and thus must be copied to a GPU register. Here is an article describing registers in a non-hlsl context http://en.wikipedia.org/wiki/Processor_register

share|improve this answer
    
I know what a normal CPU register is. I'm specifically interested in the HLSL ones. For example, how can a texture (something pretty big) be mapped to a small processor register? –  NPS May 11 '13 at 14:38
1  
The texture sampler itself isn't likely stored entirely in the register; a reference to the sampler is. –  Josh Petrie May 11 '13 at 15:03
add comment

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.