16

I'm starting with OpenGL ES2.0 on Android (5.0.1), API Level 19. Where should I store the shader code? The first example encodes the shader directly as a string.

I'd like to have the shader code in a separate file for better usability. What's the best practice for storing and loading the vertex and fragment shaders?

Jonathan Eustace
  • 2,469
  • 12
  • 31
  • 54
anhoppe
  • 4,287
  • 3
  • 46
  • 58

2 Answers2

16

There are two main options:

  • Store them as text files in the assets folder of your project. To load the shader:

    1. Get the AssetManager with the getAssets() method of the context.
    2. Call open() on the AssetManager, passing in the file name of the shader. This gives you an InputStream.
    3. Read the shader code from the InputStream, and store it in a String.
    4. Call close() on the InputStream.
  • Store them in the res/raw folder of your project. To load the shader:

    1. Get the Resources with the getResources() method of the context.
    2. Call openRawResource() on the Resources, passing in the resource id (R.raw.<name>). This gives you an InputStream.
    3. (same as above)
    4. (same as above)

I don't believe there's a big reason to prefer one over the other. The main difference is that you use the file name to access assets, while you use the automatically assigned resource id for resources. It's a matter of preference which one you like better.

Reto Koradi
  • 53,228
  • 8
  • 93
  • 133
  • Thanks. I tried the 'Resource' way and it works fine. However, I find it rather difficult to read the file content as string. I found no other way then reading all lines and connecting them. But then I had to add the line break manually. And what is the best file extension for shaders? – anhoppe Dec 21 '14 at 20:21
  • 2
    if you use the ".fs" file extension Android will interpret it as a Renderscript filter script and gives you some syntax highlighting. – HPP Jul 25 '15 at 21:17
  • [These](https://www.khronos.org/opengles/sdk/tools/Reference-Compiler/) are the official extensions – elect Mar 13 '17 at 15:09
  • For users of IntelliJ (which includes Android Studio, but also the unmodified IntelliJ IDEA/other modifications) there is at least one plugin that handles .frag, .vert, .glsl and other extensions related to the shaders. This gives syntax highlighting and some suggestions. It is better than nothing – Zoe Jun 28 '17 at 13:29
  • As for resources vs assets, remember that **resources requires the shaders to have different names**. It ignores the extension. In those cases, it is a good idea to use .glsl extension and name them `shadername_v` or `shadername_f` depending on whether it is a vertex or a fragment shader – Zoe Jun 28 '17 at 13:36
  • @anhoppe: as to `InputStream` to `String`, see https://stackoverflow.com/a/35446009/889976 for a comparison of possible approaches. – Bernhard Bodenstorfer Jul 23 '18 at 19:33
4

Aloha! In case someone had problems opening the file with the .glsl extension in the res/raw folder, do the following actions: 1)open File > Settings 2)in inpit line write File types 3)find and choose OpenGL Shading Language (Check whether the GLSL Support plugin is installed) 4)add in Registred Patterns *.glsl wildcard 5) Apply and OK