Sign in to follow this  
ijk

extern var in Pixel Shader

Recommended Posts

Hi ! I want to do a Pixel Shader : int cpt = 0; float4 TextureColor(in float2 texCoord : TEXCOORD0) : COLOR0 { cpt += 1; return tex2D(samp2, texCoord); } Next : void MyRender() { MyPixelShader.cpt=0; ... DrawWithMyPixelShader(); ... DrawText("Number of pass : " + MyPixelShader.cpt); } Unfortunately in PixelShader, a variable has to be constant !? Can I do something of similar with a shader registre or else ? thank.

Share this post


Link to post
Share on other sites
no values do not have to be constant in a pixel shader, but if you are using shader model 1_x literal values will get clamped to 0,1.

If you're trying to accumulate into a variable (which is not dependent on a texture lookup) just use the vertex shader. Or increment cpt by 0.01 and then multiply by 100 in your application. Kindof a bandaid fix but...

Share this post


Link to post
Share on other sites
Thank Dhanij ! But did you tested ? Because this does not work.

Let :

float myVar = 0.1;

float4 TextureColor(in float2 texCoord : TEXCOORD0) : COLOR0
{
myVar = myVar*0.5;
return tex2D(samp2, texCoord)*myvar;
}

Next :

void MyRender()
{
MyPixelShader.myVar= 0.8;
...
DrawPolygonWithMyPixelShader();
...
DrawText("My variable : " + MyPixelShader.myVar);
}


The result :

The drawn polygon (with the Shader) "show" that myVar = 0.4

And !!!

The drawn text : "My variable : 0.8"

Conlusion

MyVar IS A CONSTANT. Unfortunately !

I'm using pixel shader model 2.0

How bypass this problem ?


Share this post


Link to post
Share on other sites
Quote:
Original post by ijk
Hi !

I want to do a Pixel Shader :

int cpt = 0;

float4 TextureColor(in float2 texCoord : TEXCOORD0) : COLOR0
{
cpt += 1;
return tex2D(samp2, texCoord);
}

Next :

void MyRender()
{
MyPixelShader.cpt=0;
...
DrawWithMyPixelShader();
...
DrawText("Number of pass : " + MyPixelShader.cpt);
}

Unfortunately in PixelShader, a variable has to be constant !?

Can I do something of similar with a shader registre or else ?

thank.


No, this cannot be done. Global scope paramters cannot be modified by shader code.

Share this post


Link to post
Share on other sites
The entire program is run for each fragment. This means that all "globals" are re-initialized for each fragment. Current graphics hardware does not have registers that persist across fragments, and, given the parallelism of shading, that would be very hard to build (because of concurrency races), or even to figure out what the definition of "correct" should be. Cards may/will shade fragments that won't actually show up on the screen, to get things like derivatives to use in MIP map selection, for example.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
thank.

You are true. My question had no-sense.

So another way...

If I want to do the Sum of pixels into DX Surface.

Is there a hardware way ?

Of-course there is the mip-map. But in that case is not effective.

If I have a Surface 1024x1024 and (If) I draw a polygone in the rectangle(340,256, 54, 17)

I can locked the surface and :

for(x=340; x<(340+54); ++x)
for(y=256; y<(256+17); ++y)
{
sum += pixels[x][y];
}

OR can I use a Shader !?

I create a texture 1x1. Next use the TextureLoader.FillTexture methode with the shader

for(x=340/1024.0; x<(340+54)/1024.0; x+=1/1024.0)
for(y=256/1024.0; y<(256+17)/1024.0; y+=1/1024.0)
{
sum += tex2D(texture_of_my_surface, xy);
}


Isn't There more effective ? In Hardware ?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
ghanij :

In my first example
MyPixelShader.myVar "translates" a GetValue() of myVar of my effect.

So, you can see that myValue NO change.
And in my loop, I had "myVar = myVar*0.5;". After several pixels. MyVar = 0.4 = 0.2 = 0.1 = 0.05 =...= 0. But NO !
Conclusion MyVar is constant.

This, there is a Pixel shader graphic shows the R/W :
http://www.ati.com/developer/ATI_EGDC_AdvancedShaders.pdf

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this