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 have a pixel shader that sets the output color based on the interaction of an input texture, sort of like:

if (case1)
{
    output.Color = float4(0, 0, 0, 0);
}
else if (case2)
{
    output.Color = float4(0.5f, 0, 0, 1);
}
... 

There can be up to four cases, meaning that the output color will be one of four different input colors, but 90% of the time only one or two colors will be used.

An additional detail that may be important is that the shader has a constant buffer with a few values that can change up to per-frame. These values, and the colors, are selected out of a dictionary that is roughly like:

  • StyleID: 1
  • Color1: 0, 0, 0, 0
  • Color2: 0.5, 0, 0, 1
  • ...two other colors...
  • Width: 0.8
  • ...other properties...

There are around a thousand different styles to select from. My question is: what's the best way of passing these values to the shader, and why? There are two designs that occur to me offhand.

  1. On the CPU side, the current style is looked up in the dictionary. It assigns the various properties to members of the cbuffer, including however many colors associated with that style (1 to 4). The shader uses these values.

  2. During initialization, the the entire style dictionary is built in to a separate, large constant buffer, something like:

.

cbuffer Styles  
{  
    float4 Color1[n];  
    float4 Color2[n];  
    float1 Width[n];  
}  

Where n is the number of different styles. Then the CPU just pushes the style ID to a different buffer variable (or perhaps to the unused color semantic), and the shader resolves the colors and properties to used based on this.

I'm using shader model 4.0 level 9_3.

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

If you only ever change the current color set between frames, then you should be able to do it much easier on the CPU. Smaller constant buffers means less time transferring data between CPU and GPU.

share|improve this answer
    
Yes, I only need to change the color set between frames. Since design two would transfer less data per-frame (after the initialization of the large buffer during startup), are you saying that would be better? I'm assuming that nothing but the styleId would be transferred per frame in that case; that since the style buffer is unchanged it is transferred to the GPU only once. –  fatcat1111 Apr 2 '13 at 18:00
    
Yeah, from what I understand of your problem, it seems like all you really need is the current color set for your shader. If you really want, you could set up two separate constant buffers, one with the large dictionary and then the other that just stores the index, that way when you update the index, the machine isn't inclined to recopy over all the data in the dictionary. However, I don't see how this would help/hinder performance. Also some GPU's have limited local memory so less is better. –  UnderscoreZero Apr 2 '13 at 20:07
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.