Jump to content
  • Advertisement
Sign in to follow this  
ijk

extern var in Pixel Shader

This topic is 5139 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
woops. I guess I dint think of that. well what about using the option where it saves the shader state call to call and then the GetValue member of effect to read it out?

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!