   Array reference can not be used as an l-value
  I have to do the Filtering with the variable filter size. So after i calculate the filter size in the vertex shader, i pass it to the pixel shader and calculate the around pixel positionThis is my code to calculate the offset for each pixel around the current pixel for (int i = 0; i&lt; InitWidth/2; i++){for (int j = 0; j&lt; InitWidth/2; j++){TexCoord[k] = float2(i*TexelSize.x,j*TexelSize.y);k=k+1;}}InitWidth is the Filter size for current pixel. And it turn out an error like this "Array reference can not be used as an l-value, not native addressable, forcing lopp to unroll""Forced to unroll loop, but unrolling failed"Can anyone help?[Edited by - hl1202 on April 22, 2010 7:37:23 PM]
  - What shader model are you targeting?- Can you post enough of the shader so that other people can compile it for themselves without guessing at what the constants and variable declarations are?- What filter are you trying to apply? Can you decompose it into separate horizontal and vertical passes?   ;  -  I'm using 3.0 version-  I'm trying to apply an Averaging filter. It will sum all the value of the pixels around the current filter (within the filter size) and then divide by the total number of pixel.-  I cannot post the whole Vertex shader and Pixel Shader here, but i try to explain==========================================================In the vertex shader:+ The output struct of the vertex shaderstruct AverageData{ float4 oPos: POSITION; float2 oTex: TEXCOORD0; float4 vPosLight: TEXCOORD1; float  Width: TEXCOORD2;};+ The Width is the Filter size, d is the distance from the light, znear is the near clip planeOutput.Width = LightWidth*(d-znear)/d;==========================================================In the pixel shader:+Input with the Vertex shader output struct float4 PixAverage(AverageData Input):COLOR0+ Get the filter size and round it to the integer valuefloat Wi = abs(Input.Width);int InitWidth = Zround(Wi);+ Array's used to store the pixel position.float2 TexCoord[244]; int k = 0;+ This will run through 1/4 of the filter area , if this successfully, i will run 4 times like this for 3 other filter areas. At last i can obtain the whole data. for (int i = 0; i&lt; InitWidth/2; i++){for (int j = 0; j&lt; InitWidth/2; j++){    TexCoord[k] = float2(i*TexelSize.x,j*TexelSize.y); k=k+1;} }After we get the pixel coordinate offset, we will do the filtering===========================[Edited by - hl1202 on April 22, 2010 7:15:49 PM];  My guess is that the incrementation of k within a double loop is confusing the compiler. Try:int n = (InitWidth/2);for (int k = 0; i < n*n; i++){    int i=k/n, j=k%n;    TexCoord[k] = float2(i*TexelSize.x,j*TexelSize.y);}BTW, [code]code tags and [source]source tags[/source] are a good way to keep your code formatted here.   ;  As the compiler mentioned, I believe there's some weirdness with indexing into constant registers-- I don't think it's actually possible to implement what you're trying to do the way you're approaching it.Also, these weights are (AFAIK) going to be constant over the entire image. Why are you not precomputing these once on the CPU and uploading the result to the GPU? There's really a whole lot of redundant computation going on.EDIT: While you're at it, you might also be able to use the hardware's bilinear filtering capabilities to collect the results of 4 samples at a time-- just do some offsetting so that your reads are positioned halfway in between pixels in the source image and the texture hardware will automatically average the two (or four) adjacent texels for free.   ;  @Sneftel :I think you mean k in the for loop, it seems to be a good loop for me. I will try and inform the result.@InvalidPointer: Actually Filter size if calculated by Output.Width = LightWidth*(d-znear)/d;So it changes from this pixel to another pixel (i hope i am not wrong about this). So i can not pre-calculate the weights like you say.   ;  Assuming the UVs dont need to be offset, I would write it this way.  This will minmize the amount of math the shader ends up having to calculate#define NumSamplesOverTwo 8    // ideally this is either hardcoded or set by the c++ code before the shader is complieduniform float4 Samples[ NumSamplesOverTwo];void func(float2 TexelSize){    float4 TransformedSamples[NumSamplesOverTwo ];    [unroll]    for (int i=0; i<NumSamplesOverTwo ; ++i)    {   TransformedSamples[k] = Samples.xyzw * TexelSize.xyxy;    }    // .. code that uses TransformedSamples[].xy and TransformedSamples[].zw in pairs}If you need an offset to the samples it should be applied in the same register so it can be converted to a mad assembly instruction.#define NumSamples 16uniform float4 Samples[ NumSamples ];void func(float2 TexelSize){    float2 TransformedSamples[NumSamples];    [unroll]    for (int i=0; i<NumSamples; ++i)    {   TransformedSamples[k] = Samples.xy * TexelSize.xy + Samples.zw;    }    // .. code that uses TransformedSamples[].xy}