0

I cannot get this empty compute shader to compile using OpenGL ES 3.1 on a Pixel C tablet running Android 8.1.0:

"shader.comp"

#version 310 es

void main()
{

}

I am using the Asset manager to reader this source from a file as follows and all my GL calls are using the Java bindings rather than native code and the NDK:

String shaderSource;
AssetManager assets = context.getAssets();
InputStream shaderStream;
try
{
    shaderStream = assets.open("shader.comp");
} // catch etc...
shaderSource = shaderStream.toString();

// Compile source
int shaderObject = GLES31.glCreateShader(GLES31.GL_COMPUTE_SHADER);
GLES31.glShaderSource(shaderObject, shaderSource);
GLES31.glCompileShader(shaderObject);

// Get info about compilation
GLES31.glGetShaderiv(shaderObject, GLES31.GL_COMPILE_STATUS, status, 0);
if (status[0] != GLES31.GL_TRUE)
{
    Log.e("ComputeShader",
            "Compute Shader did not compile: " +
                    GLES31.glGetShaderInfoLog(shaderObject));
}

    // Link etc..

The error I get is confusing:

Compute Shader did not compile: 0(1) : error C0000: syntax error, unexpected '.', expecting "::" at token "."

There is no "." in the shader source. Do you think this might be related to the encoding used by the .toString() conversion of the input stream? The Java documentation for OpenGL ES on Android is largely incomplete so there is little help there. Is there any other potential reasons for this type of error?

CodingLumis
  • 594
  • 2
  • 22
  • 1
    Are you sure that you are correctly reading the file? `InputStream.toString()` would get the `Object.toString()` of that object not the contents of the stream. See: https://stackoverflow.com/questions/16110002/read-assets-file-as-string – Morrison Chang Feb 21 '18 at 18:32
  • @MorrisonChang thank you for this, I stupidly had assumed that the `toString()` method was like the `StringBuilder.toString()` method as clearly it is the inherited method. Lesson: check the documentation. If you wanted to post an answer I'll accept it. You can quote this answer: https://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string/5445161 for reference if you want. Cheers. – CodingLumis Feb 22 '18 at 11:17

1 Answers1

0

As mentioned by Morrison Chang, the issue here is that the .toString() method I was attempting to use to extract InputStream contents into a String was the one inherited from java.lang.Object and not some specific implementation. The lesson here is to always check the documentation rather than assume you know how a method is implemented from its name.

To perform the action required I replaced the shaderSource = shaderStream.toString() line with the following:

BufferedReader reader = new BufferedReader(new InputStreamReader(shaderStream, "UTF-8"));

// Read each line
String line;
StringBuilder contents = new StringBuilder();
while ((line = reader.readLine()) != null)
{
    // Add line to the file contents with a new line character at the end as they are stripped out by the readLine() method
    contents.append(line);
    contents.append('\n');
}
shaderSource = contents.toString();

The StringBuilder.toString() method is the override I wanted.

CodingLumis
  • 594
  • 2
  • 22