Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HLSL Pixel Shader - global variables?

I am new to HLSL and shaders. I can't seem to replace the color I retrieve. It's for use in 2D text, i.e. subtitles. The problem is if I set osd_color outside main() it doesn't display anything. I am using Shazzam Shader Editor 1.4 to quickly see the effect, however same thing happens in the program..

sampler2D texture0 : register(s0);

float4 osd_color = float4(0,0,0,1);
struct PixelShaderInput
{
    float2 uv0: TEXCOORD0;          
    float4 color: COLOR;
};

float4 main(PixelShaderInput input): COLOR {
float4 color = tex2D(texture0, input.uv0) * osd_color;
return color;
}

Hope you can help.

Edit:

While I'm at it, if I'd want to add a shadow/outline and returns its color as well, how would I do that? Let's say every variable works. And osd_color is white and a float4 outline is black. I've tried:

float4 outline = tex2D(texture0, (input.uv0 * 1.1) ) * outline_color;
return color + outline;

With this all I get is a white color (osd_color)..

like image 789
siz Avatar asked Oct 15 '12 11:10

siz


1 Answers

You have to manage the memory of non-static variables yourself. A static variable will save your day:

static float4 osd_color = float4(0,0,0,1);

When using static everything works as expected, since the compiler cares about reserving some memory for the color value. If the static is not present you have to manage the memory yourself - or your application - which means that you have to retrieve the default value of the variable and copy it by hand to constant buffer for instance.

like image 137
Vertexwahn Avatar answered Oct 21 '22 05:10

Vertexwahn