# GlProgram

Artifact: [androidx.media3:media3-common](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/common/util/GlProgram.java+class:androidx.media3.common.util.GlProgram)

*** ** * ** ***


```
@UnstableApi
public final class GlProgram
```

<br />

*** ** * ** ***

Represents a GLSL shader program.

After constructing a program, keep a reference for its lifetime and call `https://developer.android.com/reference/androidx/media3/common/util/GlProgram#delete()` (or release the current GL context) when it's no longer needed.

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/media3/common/util/GlProgram#GlProgram(java.lang.String,java.lang.String)(https://developer.android.com/reference/java/lang/String.html vertexShaderGlsl, https://developer.android.com/reference/java/lang/String.html fragmentShaderGlsl)` Creates a GL shader program from vertex and fragment shader GLSL GLES20 code. |
| `https://developer.android.com/reference/androidx/media3/common/util/GlProgram#GlProgram(android.content.Context,java.lang.String,java.lang.String)( https://developer.android.com/reference/android/content/Context.html context, https://developer.android.com/reference/java/lang/String.html vertexShaderFilePath, https://developer.android.com/reference/java/lang/String.html fragmentShaderFilePath )` Compiles a GL shader program from vertex and fragment shader GLSL GLES20 code. |
| `https://developer.android.com/reference/androidx/media3/common/util/GlProgram#GlProgram(android.content.Context,int,int)( https://developer.android.com/reference/android/content/Context.html context, int vertexShaderResId, int fragmentShaderResId )` Compiles a GL shader program from vertex and fragment shader GLSL GLES20 code. |
| `https://developer.android.com/reference/androidx/media3/common/util/GlProgram#GlProgram(android.content.Context,int,int,int...)( https://developer.android.com/reference/android/content/Context.html context, int vertexShaderResId, int firstFragmentShaderResId, int[] additionalFragmentShaderResIds )` Compiles a GL shader program from a vertex shader resource and one or more fragment shader resource IDs. |

| ### Public methods |
|---|---|
| `void` | `https://developer.android.com/reference/androidx/media3/common/util/GlProgram#bindAttributesAndUniforms()()` Binds all attributes and uniforms in the program. |
| `void` | `https://developer.android.com/reference/androidx/media3/common/util/GlProgram#delete()()` Deletes the program. |
| `int` | `https://developer.android.com/reference/androidx/media3/common/util/GlProgram#getAttributeArrayLocationAndEnable(java.lang.String)(https://developer.android.com/reference/java/lang/String.html attributeName)` Returns the location of an `https://developer.android.com/reference/androidx/media3/common/util/GlProgram.Attribute`, which has been enabled as a vertex attribute array. |
| `int` | `https://developer.android.com/reference/androidx/media3/common/util/GlProgram#getUniformLocation(java.lang.String)(https://developer.android.com/reference/java/lang/String.html uniformName)` Returns the location of a `https://developer.android.com/reference/androidx/media3/common/util/GlProgram.Uniform`. |
| `void` | `https://developer.android.com/reference/androidx/media3/common/util/GlProgram#setBufferAttribute(java.lang.String,float[],int)(https://developer.android.com/reference/java/lang/String.html name, float[] values, int size)` Sets a float buffer type attribute. |
| `void` | `https://developer.android.com/reference/androidx/media3/common/util/GlProgram#setExternalTexturesRequireNearestSampling(boolean)( boolean externalTexturesRequireNearestSampling )` Sets whether to sample external textures with GL_NEAREST. |
| `void` | `https://developer.android.com/reference/androidx/media3/common/util/GlProgram#setFloatUniform(java.lang.String,float)(https://developer.android.com/reference/java/lang/String.html name, float value)` Sets a `float` type uniform. |
| `void` | `https://developer.android.com/reference/androidx/media3/common/util/GlProgram#setFloatsUniform(java.lang.String,float[])(https://developer.android.com/reference/java/lang/String.html name, float[] value)` Sets a `float[]` type uniform. |
| `void` | `https://developer.android.com/reference/androidx/media3/common/util/GlProgram#setFloatsUniformIfPresent(java.lang.String,float[])(https://developer.android.com/reference/java/lang/String.html name, float[] value)` Sets a `float[]` type uniform if `name` is present, no-op otherwise. |
| `void` | `https://developer.android.com/reference/androidx/media3/common/util/GlProgram#setIntUniform(java.lang.String,int)(https://developer.android.com/reference/java/lang/String.html name, int value)` Sets an `int` type uniform. |
| `void` | `https://developer.android.com/reference/androidx/media3/common/util/GlProgram#setIntsUniform(java.lang.String,int[])(https://developer.android.com/reference/java/lang/String.html name, int[] value)` Sets a `int[]` type uniform. |
| `void` | `https://developer.android.com/reference/androidx/media3/common/util/GlProgram#setSamplerTexIdUniform(java.lang.String,int,int)(https://developer.android.com/reference/java/lang/String.html name, int texId, int texUnitIndex)` Sets a texture sampler type uniform. |
| `void` | `https://developer.android.com/reference/androidx/media3/common/util/GlProgram#setSamplerTexIdUniform(java.lang.String,int,int,int)( https://developer.android.com/reference/java/lang/String.html name, int texId, int texUnitIndex, @https://developer.android.com/reference/androidx/media3/common/C.TextureMinFilter int texMinFilter )` Sets a texture sampler type uniform. |
| `void` | `https://developer.android.com/reference/androidx/media3/common/util/GlProgram#use()()` Uses the program. |

## Public constructors

### GlProgram

```
public GlProgram(String vertexShaderGlsl, String fragmentShaderGlsl)
```

Creates a GL shader program from vertex and fragment shader GLSL GLES20 code.

This involves slow steps, like compiling, linking, and switching the GL program, so do not call this in fast rendering loops.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/lang/String.html vertexShaderGlsl` | The vertex shader program. |
| `https://developer.android.com/reference/java/lang/String.html fragmentShaderGlsl` | The fragment shader program. |

### GlProgram

```
public GlProgram(
    Context context,
    String vertexShaderFilePath,
    String fragmentShaderFilePath
)
```

Compiles a GL shader program from vertex and fragment shader GLSL GLES20 code.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/content/Context.html context` | The `https://developer.android.com/reference/android/content/Context.html`. |
| `https://developer.android.com/reference/java/lang/String.html vertexShaderFilePath` | The path to a vertex shader program. |
| `https://developer.android.com/reference/java/lang/String.html fragmentShaderFilePath` | The path to a fragment shader program. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/io/IOException.html` | When failing to read shader files. |

### GlProgram

```
public GlProgram(
    Context context,
    int vertexShaderResId,
    int fragmentShaderResId
)
```

Compiles a GL shader program from vertex and fragment shader GLSL GLES20 code.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/content/Context.html context` | The `https://developer.android.com/reference/android/content/Context.html`. |
| `int vertexShaderResId` | The resource ID of a vertex shader program. |
| `int fragmentShaderResId` | The resource ID of a fragment shader program. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/io/IOException.html` | When failing to read shader files. |

### GlProgram

```
public GlProgram(
    Context context,
    int vertexShaderResId,
    int firstFragmentShaderResId,
    int[] additionalFragmentShaderResIds
)
```

Compiles a GL shader program from a vertex shader resource and one or more fragment shader resource IDs.

When multiple fragment shader resource IDs are passed, their sources are concatenated in order.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/content/Context.html context` | The `https://developer.android.com/reference/android/content/Context.html`. |
| `int vertexShaderResId` | The resource ID of a vertex shader program. |
| `int firstFragmentShaderResId` | The resource ID of the primary fragment shader program. |
| `int[] additionalFragmentShaderResIds` | Optional additional resource IDs of fragment shader programs. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/io/IOException.html` | When failing to read shader files. |

## Public methods

### bindAttributesAndUniforms

```
public void bindAttributesAndUniforms()
```

Binds all attributes and uniforms in the program.

| Throws |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/util/GlUtil.GlException` |   |

### delete

```
public void delete()
```

Deletes the program. Deleted programs cannot be used again.

| Throws |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/util/GlUtil.GlException` |   |

### getAttributeArrayLocationAndEnable

```
public int getAttributeArrayLocationAndEnable(String attributeName)
```

Returns the location of an `https://developer.android.com/reference/androidx/media3/common/util/GlProgram.Attribute`, which has been enabled as a vertex attribute array.

| Throws |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/util/GlUtil.GlException` |   |

### getUniformLocation

```
public int getUniformLocation(String uniformName)
```

Returns the location of a `https://developer.android.com/reference/androidx/media3/common/util/GlProgram.Uniform`.

### setBufferAttribute

```
public void setBufferAttribute(String name, float[] values, int size)
```

Sets a float buffer type attribute.

### setExternalTexturesRequireNearestSampling

```
public void setExternalTexturesRequireNearestSampling(
    boolean externalTexturesRequireNearestSampling
)
```

Sets whether to sample external textures with GL_NEAREST.

The default value is `false`.

### setFloatUniform

```
public void setFloatUniform(String name, float value)
```

Sets a `float` type uniform.

### setFloatsUniform

```
public void setFloatsUniform(String name, float[] value)
```

Sets a `float[]` type uniform.

### setFloatsUniformIfPresent

```
public void setFloatsUniformIfPresent(String name, float[] value)
```

Sets a `float[]` type uniform if `name` is present, no-op otherwise.

### setIntUniform

```
public void setIntUniform(String name, int value)
```

Sets an `int` type uniform.

### setIntsUniform

```
public void setIntsUniform(String name, int[] value)
```

Sets a `int[]` type uniform.

### setSamplerTexIdUniform

```
public void setSamplerTexIdUniform(String name, int texId, int texUnitIndex)
```

Sets a texture sampler type uniform.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/lang/String.html name` | The uniform's name. |
| `int texId` | The texture identifier. |
| `int texUnitIndex` | The texture unit index. Use a different index (0, 1, 2, ...) for each texture sampler in the program. |

### setSamplerTexIdUniform

```
public void setSamplerTexIdUniform(
    String name,
    int texId,
    int texUnitIndex,
    @C.TextureMinFilter int texMinFilter
)
```

Sets a texture sampler type uniform.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/lang/String.html name` | The uniform's name. |
| `int texId` | The texture identifier. |
| `int texUnitIndex` | The texture unit index. Use a different index (0, 1, 2, ...) for each texture sampler in the program. |
| `@https://developer.android.com/reference/androidx/media3/common/C.TextureMinFilter int texMinFilter` | The `https://developer.android.com/reference/androidx/media3/common/C.TextureMinFilter`. |

### use

```
public void use()
```

Uses the program.

Call this in the rendering loop to switch between different programs.

| Throws |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/util/GlUtil.GlException` |   |