Imagination PowerVR SDK Blog

Problem about use struct as parameter or return value of function in fragment shader



hi all.

I am new to OpenGLES2.0. I use PowerVR in my project on win7 platform. Recently I wirte a fragment shader, it has a compile error but I do not know why.

This is my shader code:

<br />
struct STestStruct<br />
{<br />
mediump vec4 kValue;<br />
};<br />
void TestFun( out STestStruct kParam )<br />
{<br />
kParam.kValue = vec4(1,0,0,0);<br />
}<br />
void main ()<br />
{<br />
STestStruct kStruct;<br />
TestFun( kStruct );<br />
gl_FragColor = kStruct.kValue;<br />
};<br />

```<br />
The error message is: "Compile failed. ERROR: 0:1: 'TestFun' : no matching overloaded function found“.<br />
<br />
I do not know, so I change my shader code as below:<br />

struct STestStruct
mediump vec4 kValue;
STestStruct TestFun()
STestStruct kParam;
kParam.kValue = vec4(1,0,0,0);
return kParam;
void main ()
STestStruct kStruct = TestFun();
gl_FragColor = kStruct.kValue;
<br />
But there is also has a error : 'return' : function return is not matching type.<br />
<br />
Is there a problem with my shader code? Wish somebody can help me. Thanks very much!


oh… maybe I found the reason in the document “Structure declarations are not permitted as part of parameter declarations or return type declarations.”



You first example shader appears to be valid and compiles successfully with PVRShaderEditor and SGX 1.9 compilers (apart from the small syntax error appending the semicolon to the end of the main() scope braces). Did you solve this problem and/or are you targeting a particular device?