GwegZilla GwegZilla - 1 year ago 116
C# Question

Xamarin AndroidGameView : can't figure out why VertexShader / FragmentShader does not compile

I'm trying to work with shaders with this Xamarin Sample (I have already work a bit on shaders with SFML):

I'm trying to make a new project and re-create a AndroidGameView class (such as PaintingView in this sample).

But I can't compile my VertexShader and my Fragment shader.

What I have done so far :

  • My FrameBuffer is created.

  • OnLoad base function has been called.

  • I created a string where my shaders are stored before compilation (same shaders in the sample)

  • Create a shader with GL.CreateShader => result is not 0

  • Store my sourceCode in the vertex shader handle with GL.ShaderSource

  • Try to compile with GL.CompileShader => GL.GetShader indicates that it did not compile.

here is the vertex shader string :

string vertexShaderSrc = "attribute vec4 vPosition; \n" +
"void main() \n" +
"{ \n" +
" gl_Position = vPosition; \n" +
"} \n";

and here is the loading function called after declaration of the string, where "type = ShaderType.VertexShader" and "source = vertexShaderSrc" :

int LoadShader(ShaderType type, string source)
int shader = GL.CreateShader(ShaderType.VertexShader);

if (shader == 0)
throw new InvalidOperationException("Unable to create shader");

int[] length = { 0 };

GL.ShaderSource(shader, 1, new string[] { source }, (int[])null);

int[] compiled = { 0 } ;
GL.GetShader(shader, ShaderParameter.CompileStatus, compiled);
if (compiled[0] == 0)
length[0] = 0;
GL.GetShader(shader, ShaderParameter.InfoLogLength, length);
if (length[0] > 0)
var log = new StringBuilder(length[0]);
GL.GetShaderInfoLog(shader, length[0], length, log);
Log.Debug("GL2", "Couldn't compile shader: " + log.ToString());

throw new InvalidOperationException("Unable to compile shader of type : " + type.ToString());

return shader;

What step do I miss ? Is there a particular setting to do in Xamarin ? I don't think that the problem is related to the shader itself because it's the same in the sample.

Thank you for your time.

Answer Source


The trick was quite simple :

Don"t forget to add the following line in CreateFrameBuffer :

ContextRenderingApi = GLVersion.ES2;

Problem solved.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download