|
|
|
|
|
|
|
|
#include <png.h> |
|
|
#include <png.h> |
|
|
#include <EGL/egl.h> |
|
|
#include <EGL/egl.h> |
|
|
#include <GLES2/gl2.h> |
|
|
|
|
|
#include <GLES2/gl2ext.h> |
|
|
|
|
|
|
|
|
#include <GL/gl.h> |
|
|
|
|
|
#include <GL/glext.h> |
|
|
|
|
|
#include <GLES3/gl3.h> |
|
|
|
|
|
#include <GLES3/gl3ext.h> |
|
|
|
|
|
|
|
|
#include <fstream> |
|
|
#include <fstream> |
|
|
#include <iostream> |
|
|
#include <iostream> |
|
|
|
|
|
|
|
|
eglInitialize(display, nullptr, nullptr); |
|
|
eglInitialize(display, nullptr, nullptr); |
|
|
eglBindAPI(EGL_OPENGL_ES_API); |
|
|
eglBindAPI(EGL_OPENGL_ES_API); |
|
|
|
|
|
|
|
|
const EGLint context_attribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; |
|
|
|
|
|
const EGLint config_attribs[] = { EGL_SURFACE_TYPE, EGL_DONT_CARE, EGL_RENDERABLE_TYPE, |
|
|
|
|
|
EGL_OPENGL_ES2_BIT, EGL_NONE }; |
|
|
|
|
|
|
|
|
const EGLint config_attribs[] = { |
|
|
|
|
|
EGL_SURFACE_TYPE, EGL_DONT_CARE, |
|
|
|
|
|
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
|
|
|
|
|
EGL_NONE }; |
|
|
EGLConfig egl_config; |
|
|
EGLConfig egl_config; |
|
|
EGLint num_configs; |
|
|
EGLint num_configs; |
|
|
if (!eglChooseConfig(display, config_attribs, &egl_config, 1, &num_configs)) { |
|
|
if (!eglChooseConfig(display, config_attribs, &egl_config, 1, &num_configs)) { |
|
|
ERROR("Unable to choose EGL config"); |
|
|
ERROR("Unable to choose EGL config"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const EGLint context_attribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; |
|
|
EGLContext context = eglCreateContext(display, egl_config, EGL_NO_CONTEXT, context_attribs); |
|
|
EGLContext context = eglCreateContext(display, egl_config, EGL_NO_CONTEXT, context_attribs); |
|
|
if (context == EGL_NO_CONTEXT) { |
|
|
if (context == EGL_NO_CONTEXT) { |
|
|
ERROR("Failed to create GLES context"); |
|
|
ERROR("Failed to create GLES context"); |
|
|
|
|
|
|
|
|
GLuint color_buffer; |
|
|
GLuint color_buffer; |
|
|
glGenRenderbuffers(1, &color_buffer); |
|
|
glGenRenderbuffers(1, &color_buffer); |
|
|
glBindRenderbuffer(GL_RENDERBUFFER, color_buffer); |
|
|
glBindRenderbuffer(GL_RENDERBUFFER, color_buffer); |
|
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8_OES, kWidth, kHeight); |
|
|
|
|
|
|
|
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, kWidth, kHeight); |
|
|
|
|
|
|
|
|
GLuint depth_buffer; |
|
|
GLuint depth_buffer; |
|
|
glGenRenderbuffers(1, &depth_buffer); |
|
|
glGenRenderbuffers(1, &depth_buffer); |
|
|
|
|
|
|
|
|
return program; |
|
|
return program; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void Draw(GLuint program) { |
|
|
|
|
|
|
|
|
uint32_t Draw(GLuint program) { |
|
|
glViewport(0, 0, kWidth, kHeight); |
|
|
glViewport(0, 0, kWidth, kHeight); |
|
|
glClearColor(0.0f, 0.0f, 1.0f, 1.0f); |
|
|
glClearColor(0.0f, 0.0f, 1.0f, 1.0f); |
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
|
glEnable(GL_DEPTH_TEST); |
|
|
glEnable(GL_DEPTH_TEST); |
|
|
|
|
|
|
|
|
glUseProgram(program); |
|
|
glUseProgram(program); |
|
|
|
|
|
GLuint query_object; |
|
|
|
|
|
glGenQueries(1, &query_object); |
|
|
|
|
|
glBeginQuery(GL_SAMPLES_PASSED, query_object); |
|
|
|
|
|
if (glGetError()) { |
|
|
|
|
|
ERROR("GLES version does not support GL_SAMPLES_PASSED"); |
|
|
|
|
|
} |
|
|
glDrawArrays(GL_TRIANGLES, 0, 6); |
|
|
glDrawArrays(GL_TRIANGLES, 0, 6); |
|
|
|
|
|
glEndQuery(GL_SAMPLES_PASSED); |
|
|
|
|
|
|
|
|
|
|
|
uint32_t shaded_fragments = 0; |
|
|
|
|
|
glGetQueryObjectuiv(query_object, GL_QUERY_RESULT, &shaded_fragments); |
|
|
|
|
|
|
|
|
glFinish(); |
|
|
glFinish(); |
|
|
|
|
|
return shaded_fragments; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
GLubyte* ReadFramebuffer() { |
|
|
GLubyte* ReadFramebuffer() { |
|
|
|
|
|
|
|
|
SetUpFramebuffer(); |
|
|
SetUpFramebuffer(); |
|
|
GLuint program = CreateProgram(); |
|
|
GLuint program = CreateProgram(); |
|
|
|
|
|
|
|
|
Draw(program); |
|
|
|
|
|
|
|
|
uint32_t shaded_fragments = Draw(program); |
|
|
|
|
|
std::cout << "[Occlusion query] shaded fragments: " << shaded_fragments << std::endl; |
|
|
|
|
|
|
|
|
GLubyte* pixels = ReadFramebuffer(); |
|
|
GLubyte* pixels = ReadFramebuffer(); |
|
|
WriteImage(pixels); |
|
|
WriteImage(pixels); |