1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "shadertoy_shader.h" 18 #include "utils.h" 19 20 #include <fstream> 21 #include <sstream> 22 #include <android/log.h> 23 24 int g_framebuffer_width = 0; 25 int g_framebuffer_height = 0; 26 GLuint g_quad_vao = 0; 27 28 ShadertoyShader shader; 29 30 #define LOG_TAG "GPUStressTestActivity" 31 #define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__) 32 #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__) 33 34 double NowInMs() { 35 timespec timeval; 36 clock_gettime(CLOCK_REALTIME, &timeval); 37 double time = 1000.0 * timeval.tv_sec + (double) timeval.tv_nsec / 1e6; 38 return time; 39 } 40 41 42 GLuint CreateFullscreenQuad() { 43 GLfloat quadVertices[] = { 44 // Positions 45 -1.0f, 1.0f, 46 -1.0f, -1.0f, 47 1.0f, -1.0f, 48 49 -1.0f, 1.0f, 50 1.0f, -1.0f, 51 1.0f, 1.0f, 52 }; 53 54 // Setup screen VAO 55 GLuint quadVAO, quadVBO; 56 glGenVertexArrays(1, &quadVAO); 57 glGenBuffers(1, &quadVBO); 58 glBindVertexArray(quadVAO); 59 glBindBuffer(GL_ARRAY_BUFFER, quadVBO); 60 glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), &quadVertices, GL_STATIC_DRAW); 61 glEnableVertexAttribArray(0); 62 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (GLvoid*)0); 63 glBindVertexArray(0); 64 65 return quadVAO; 66 } 67 68 void CreateShader() { 69 extern std::string g_shader; 70 shader.CreateShaderFromString(g_shader); 71 } 72 73 void Init(int width, int height) { 74 GLint num_extensions = 0; 75 glGetIntegerv(GL_NUM_EXTENSIONS, &num_extensions); 76 for (GLint i = 0; i < num_extensions; ++i) { 77 const char* extension = (char*)( 78 glGetStringi(GL_EXTENSIONS, i)); 79 } 80 81 g_framebuffer_width = width; 82 g_framebuffer_height = height; 83 84 85 CreateShader(); 86 g_quad_vao = CreateFullscreenQuad(); 87 } 88 89 void DrawFrame() { 90 static double previous_time = 0; 91 static float angle = 0.0f; 92 static double elapsed_time_sum = 0; 93 static double gpu_timer_elapsed_sum = 0; 94 static double start_time = NowInMs(); 95 96 // After how many frames to report the avg frame time. 97 int kFrameReportInterval = 1; 98 static int frame_count = 0; 99 100 frame_count++; 101 if (frame_count == kFrameReportInterval) { 102 LOGI("%f\n", elapsed_time_sum / (double)kFrameReportInterval); 103 104 frame_count = 0; 105 elapsed_time_sum = 0; 106 gpu_timer_elapsed_sum = 0; 107 } 108 109 double current_time = NowInMs(); 110 double elapsed_time = current_time - previous_time; 111 previous_time = current_time; 112 elapsed_time_sum += elapsed_time; 113 float global_time = (float)(NowInMs() - start_time); 114 115 glViewport(0, 0, g_framebuffer_width, g_framebuffer_height); 116 117 glClearColor(0.2f, 0.3f, 0.3f, 1.0f); 118 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 119 120 float fov = 45; 121 122 shader.PrepareForDraw(g_framebuffer_width, g_framebuffer_height, global_time, frame_count, (float)elapsed_time); 123 124 glBindVertexArray(g_quad_vao); 125 126 glDrawArrays(GL_TRIANGLES, 0, 6); 127 } 128 129 void Cleanup() { 130 131 } 132