fix simple-es2 to save back buffer to disk

This commit is contained in:
caoxiong 2024-11-26 18:29:46 +08:00
parent 67d5b5737e
commit 22e1a81574

View File

@ -17,6 +17,7 @@
#include <stdbool.h>
#include <threads.h>
#include <sys/time.h>
#include <signal.h>
#include "GLES2/gl2.h"
@ -53,6 +54,9 @@ GLuint g_hProjMatrixLoc = 0;
GLuint g_hVertexLoc = 0;
GLuint g_hColorLoc = 1;
int fbo_width = 800;
int fbo_height = 600;
#define ASSERT_EQ(exp1, exp2) do { auto e1 = (exp1);auto e2 = (exp2);\
if((e1) != (e2)) { \
printf("assert value: %p, %p\n", e1, e2); \
@ -116,7 +120,7 @@ float VertexPositions[] =
void render(float w, float h)
{
static int ttt = 0;
// Clear the colorbuffer and depth-buffer
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glViewport(0, 0, w, h);
@ -132,7 +136,7 @@ void render(float w, float h)
// Rotate and translate the model view matrix
matModelView[ 0] = 1.0;
matModelView[ 2] = 0.0f;
matModelView[ 2] = sinf((ttt++)*0.3);
matModelView[ 5] = 1.0f;
matModelView[ 8] = 0.0f;
matModelView[10] = 0.0f;
@ -179,11 +183,12 @@ EGLDisplay gbm_init(const char* devicePath) {
return eglGetDisplay(gbmDevice);
}
#define PRINT_ERR(_str) { int _ret = glGetError();if ((_ret)!=GL_NO_ERROR) {printf(_str " ret: 0x%x\n", _ret);} }
void fbo_test() {
int width = 800;
int height = 600;
uint32_t ret;
// 创建并绑定帧缓冲对象
glGenFramebuffers(1, &framebuffer);
@ -194,17 +199,21 @@ void fbo_test() {
glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
// 分配存储空间
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, fbo_width, fbo_height);
// PRINT_ERR("glRenderbufferStorage");
// 将渲染缓冲对象附着到帧缓冲的深度和模板附着点
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, renderbuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderbuffer);
// PRINT_ERR("glFramebufferRenderbuffer");
// 创建并绑定纹理对象用于颜色附着点
glGenTextures(1, &texId);
glBindTexture(GL_TEXTURE_2D, texId);
// 分配存储空间
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, fbo_width, fbo_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
// PRINT_ERR("glTexImage2D");
// 设置纹理参数
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
@ -215,6 +224,7 @@ void fbo_test() {
// 将纹理对象附着到帧缓冲的颜色附着点
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texId, 0);
// PRINT_ERR("glFramebufferTexture2D");
// 检查 FBO 完整性
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
@ -265,7 +275,7 @@ int init(void)
}
int configIndex = matchConfigToVisual(egldisplay, GBM_FORMAT_XRGB8888, configs, numConfigs);
int configIndex = matchConfigToVisual(egldisplay, GBM_FORMAT_ARGB8888, configs, numConfigs);
if (configIndex < 0)
{
fprintf(stderr, "Failed to find matching EGL config! Error: 0x%x\n", eglGetError());
@ -475,24 +485,18 @@ void writeToFile(const char* filename, unsigned char* data, long dataLen) {
}
}
#define DURATION(_t1, _t2) (((_t1).tv_usec - (_t2).tv_usec))
int main (void)
{
EGLint width = 800;
EGLint height = 600;
// init_signal();
init();
// eglQuerySurface(egldisplay, eglsurface, EGL_WIDTH, &width);
// eglQuerySurface(egldisplay, eglsurface, EGL_HEIGHT, &height);
printf("w=%d h=%d\n", fbo_width, fbo_height);
printf("w=%d h=%d\n",width,height);
// thrd_t thr;
// thrd_create(&thr, event_handle, NULL);
unsigned char* buffer = (unsigned char*)malloc(width * height * 4);
unsigned char* buffer = (unsigned char*)malloc(fbo_width * fbo_height * 4);
fbo_test();
struct timeval start, end;
while (1) {
glBindTexture(GL_TEXTURE_2D, texId);
@ -500,14 +504,19 @@ int main (void)
glBindTexture(GL_TEXTURE_2D, 0);
// glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
render(width,height);
gettimeofday(&start, NULL);
render(fbo_width, fbo_height);
gettimeofday(&end, NULL);
printf("render time: %ldus, %.1lf fps\n", DURATION(end, start), (1000000.0/(DURATION(end, start))));
// printf("render time: %ldms, %.1f fps\n", (end - start), (1000.0/(end - start)));
// glBindFramebuffer(GL_FRAMEBUFFER, 0);
printf("glReadPixels start \n");
glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
glReadPixels( 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer );
glReadPixels( 0, 0, fbo_width, fbo_height, GL_RGBA, GL_UNSIGNED_BYTE, buffer );
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
printf("glReadPixels end \n");
writeToFile("image.rgba", buffer, width * height * 4);
writeToFile("image.rgba", buffer, fbo_width * fbo_height * 4);
// eglSwapBuffers(egldisplay, eglsurface);
sleep(1);
}