5
votes

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)..

1
I'm not sure if I understand what you are trying to accomplish. The osd_color in the first example is black. Are you trying to paint all the pixels in the source texture to black?Walt Ritscher
Perhaps you can explain the scenario you are trying to accomplish in more details.Walt Ritscher
no I am not trying to paint it all black (osd_color is black here, but was just for testing). I would like to return both outline and osd_color, and have the ability for them to have different colors. So for example, osd_color is white (i.e the subtitles are white) and outline is black. Dno really how to do that, adding, subtracting etc the 2 will return a different float, that will affect both outline and osd_color and give them the same color.siz
Do you mean that you want to return the colors to your XAML code? Parameters in shaders are one way, You send then to the shader from your XAML app.Walt Ritscher
It's not for a XAML app. It's for a media player, right now with the above code, I have a text on screen (osd_color) and the same text at a different position (outline). The two text have the same color, doesn't matter what I change, atm they end out with the same color. If possible, I wan't the osd_color text be white and outline be black, so I could position outline behind osd_color, and get a sort of outline/stroke effect.siz

1 Answers

8
votes

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.