22 #include <android/log.h>
24 #include <GLES3/gl3.h>
34 #define LOG_TAG "libNative"
35 #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
36 #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
41 GLenum glError = glGetError(); \
42 if(glError != GL_NO_ERROR) { \
43 LOGE("glGetError() = %i (0x%.8x) at %s:%i\n", glError, glError, __FILE__, __LINE__); \
48 using namespace MaliSDK;
84 "#extension GL_OVR_multiview : enable\n"
86 "layout(num_views = 4) in;\n"
88 "in vec3 vertexPosition;\n"
89 "in vec3 vertexNormal;\n"
90 "uniform mat4 modelViewProjection[4];\n"
91 "uniform mat4 model;\n"
92 "out vec3 v_normal;\n"
96 " gl_Position = modelViewProjection[gl_ViewID_OVR] * vec4(vertexPosition, 1.0);\n"
97 " v_normal = (model * vec4(vertexNormal, 0.0f)).xyz;\n"
103 "precision highp float;\n"
105 "in vec3 v_normal;\n"
106 "out vec4 f_color;\n"
108 "vec3 light(vec3 n, vec3 l, vec3 c)\n"
110 " float ndotl = max(dot(n, l), 0.0);\n"
111 " return ndotl * c;\n"
116 " vec3 albedo = vec3(0.95, 0.84, 0.62);\n"
117 " vec3 n = normalize(v_normal);\n"
118 " f_color.rgb = vec3(0.0);\n"
119 " f_color.rgb += light(n, normalize(vec3(1.0)), vec3(1.0));\n"
120 " f_color.rgb += light(n, normalize(vec3(-1.0, -1.0, 0.0)), vec3(0.2, 0.23, 0.35));\n"
122 " f_color.a = 1.0;\n"
128 "in vec3 attributePosition;\n"
129 "in vec2 attributeLowResTexCoord;\n"
130 "in vec2 attributeHighResTexCoord;\n"
131 "out vec2 vLowResTexCoord;\n"
132 "out vec2 vHighResTexCoord;\n"
135 " vLowResTexCoord = attributeLowResTexCoord;\n"
136 " vHighResTexCoord = attributeHighResTexCoord;\n"
137 " gl_Position = vec4(attributePosition, 1.0);\n"
143 "precision mediump float;\n"
144 "precision mediump int;\n"
145 "precision mediump sampler2DArray;\n"
146 "in vec2 vLowResTexCoord;\n"
147 "in vec2 vHighResTexCoord;\n"
148 "out vec4 fragColor;\n"
149 "uniform sampler2DArray tex;\n"
150 "uniform int layerIndex;\n"
153 " vec4 lowResSample = texture(tex, vec3(vLowResTexCoord, layerIndex));\n"
154 " vec4 highResSample = texture(tex, vec3(vHighResTexCoord, layerIndex + 2));\n"
155 " // Using squared distance to middle of screen for interpolating.\n"
156 " vec2 distVec = vec2(0.5) - vHighResTexCoord;\n"
157 " float squaredDist = dot(distVec, distVec);\n"
158 " // Using the high res texture when distance from center is less than 0.5 in texture coordinates (0.25 is 0.5 squared).\n"
159 " // When the distance is less than 0.2 (0.04 is 0.2 squared), only the high res texture will be used.\n"
160 " float lerpVal = smoothstep(-0.25, -0.04, -squaredDist);\n"
161 " fragColor = mix(lowResSample, highResSample, lerpVal);\n"
312 GL_CHECK(glShaderSource(shader, 1, &shaderSource, NULL));
315 GL_CHECK(glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled));
316 if (compiled != GL_TRUE)
319 GL_CHECK(glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen));
323 char * logBuffer = (
char*) malloc(infoLen);
325 if (logBuffer != NULL)
327 GL_CHECK(glGetShaderInfoLog(shader, infoLen, NULL, logBuffer));
328 LOGE(
"Could not Compile Shader %d:\n%s\n", shaderType, logBuffer);
345 if (vertexShader == 0)
351 if (fragmentShader == 0)
360 GL_CHECK(glAttachShader(program, vertexShader));
361 GL_CHECK(glAttachShader(program, fragmentShader));
363 GLint linkStatus = GL_FALSE;
364 GL_CHECK(glGetProgramiv(program, GL_LINK_STATUS, &linkStatus));
365 if(linkStatus != GL_TRUE)
368 GL_CHECK(glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength));
371 char* logBuffer = (
char*) malloc(bufLength);
373 if (logBuffer != NULL)
375 GL_CHECK(glGetProgramInfoLog(program, bufLength, NULL, logBuffer));
376 LOGE(
"Could not link program:\n%s\n", logBuffer);
417 GLenum result =
GL_CHECK(glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER));
418 if (result != GL_FRAMEBUFFER_COMPLETE)
420 LOGE(
"Framebuffer incomplete at %s:%i\n", __FILE__, __LINE__);
422 GL_CHECK(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0));
433 const GLubyte* extensions =
GL_CHECK(glGetString(GL_EXTENSIONS));
434 const char *found_extension = strstr((
const char*)extensions,
"GL_OVR_multiview");
435 if (NULL == found_extension)
437 LOGI(
"OpenGL ES 3.0 implementation does not support GL_OVR_multiview extension.\n");
446 LOGI(
"Can not get proc address for glFramebufferTextureMultiviewOVR.\n");
462 LOGE (
"Could not create multiview FBO");
470 LOGE (
"Could not create textured quad program");
485 LOGE (
"Could not create multiview program");
509 GL_CHECK(glViewport(0, 0, width, height));
512 Vec3f leftCameraPos = {-0.5f, 0.0f, 5.0f};
513 Vec3f rightCameraPos = {0.5f, 0.0f, 5.0f};
514 Vec3f lookAt = {0.0f, 0.0f, 0.0f};
515 Vec3f upVec = {0.0f, 1.0f, 0.0f};
532 GL_CHECK(glViewport(0, 0, width, height));
537 GL_CHECK(glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT));
589 GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, 0));
601 GL_CHECK(glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT));
608 for (
int i = 0; i < 2; i++)
613 GL_CHECK(glActiveTexture(GL_TEXTURE0));
638 GL_CHECK(glDrawArrays(GL_TRIANGLES, 0, 6));
645 JNIEnv * env, jobject obj, jint
width, jint
height);
647 JNIEnv * env, jobject obj);
651 JNIEnv * env, jobject obj, jint
width, jint
height)
657 JNIEnv * env, jobject obj)
float texturedQuadLowResTexCoordinates[]
GLuint texturedQuadLowResTexCoordLocation
void renderToFBO(const int width, const int height, const GLuint frameBufferID)
float projectionMatrix[16]
GLuint multiviewVertexLocation
Matrix modelViewProjectionMatrix[VIEWS]
GLuint texturedQuadVertexLocation
GLint GLsizei GLsizei height
GLuint texturedQuadProgram
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_multiview_NativeLibrary_init(JNIEnv *env, jobject obj, jint width, jint height)
Functions for manipulating matrices.
GLfloat multiviewVertices[]
void(* PFNGLFRAMEBUFFERTEXTUREMULTIVIEWOVR)(GLenum, GLenum, GLuint, GLint, GLint, GLsizei)
static Matrix createTranslation(float x, float y, float z)
Create and return a translation matrix.
static const char texturedQuadVertexShader[]
static Matrix matrixPerspective(float FOV, float ratio, float zNear, float zFar)
Create and return a perspective projection matrix.
static const char multiviewVertexShader[]
static const char texturedQuadFragmentShader[]
GLuint frameBufferTextureId
A 3D floating point vector.
GLuint createProgram(const char *vertexSource, const char *fragmentSource)
GLuint texturedQuadLayerIndexLocation
static Matrix matrixCameraLookAt(Vec3f eye, Vec3f center, Vec3f up)
Create and return a camera matrix.
GLuint loadShader(GLenum shaderType, const char *shaderSource)
GLfloat multiviewNormals[]
GLuint multiviewModelLocation
static Matrix createRotationY(float angle)
Create and return a rotation matrix around the y-axis matrix.
GLuint frameBufferDepthTextureId
#define GL_TEXTURE_2D_ARRAY
bool setupFBO(int width, int height)
static const char multiviewFragmentShader[]
GLuint multiviewVertexNormalLocation
Matrix viewProjectionMatrix[VIEWS]
GLuint texturedQuadHighResTexCoordLocation
float texturedQuadCoordinates[]
GLushort multiviewIndices[]
typedef void(GL_APIENTRYP PFNGLBLENDBARRIERKHRPROC)(void)
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_multiview_NativeLibrary_step(JNIEnv *env, jobject obj)
GLuint frameBufferObjectId
typedef GLfloat(GL_APIENTRYP PFNGLGETPATHLENGTHNVPROC)(GLuint path
typedef GLenum(GL_APIENTRYP PFNGLGETGRAPHICSRESETSTATUSKHRPROC)(void)
typedef GLuint(GL_APIENTRYP PFNGLGETDEBUGMESSAGELOGKHRPROC)(GLuint count
static Matrix createRotationX(float angle)
Create and return a rotation matrix around the x-axis matrix.
GLuint texturedQuadSamplerLocation
PFNGLFRAMEBUFFERTEXTUREMULTIVIEWOVR glFramebufferTextureMultiviewOVR
float texturedQuadHighResTexCoordinates[]
GLuint multiviewModelViewProjectionLocation