GwegZilla GwegZilla - 7 days ago 6
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):
https://developer.xamarin.com/samples/monodroid/GLTriangle20/

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);
GL.CompileShader(shader);

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());
}

GL.DeleteShader(shader);
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

Well,

The trick was quite simple :

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

ContextRenderingApi = GLVersion.ES2;

Problem solved.

Comments