gpt4 book ai didi

opengl-es - 当我在 NDK 程序中使用 Eglimage 替换 glreadpixels 时出现什么问题?

转载 作者:行者123 更新时间:2023-12-02 23:20:07 25 4
gpt4 key购买 nike

//step 2. Create the Android Graphic Buffer
GraphicBuffer* buffer = new GraphicBuffer(w, h,
HAL_PIXEL_FORMAT_RGBA_8888,
GraphicBuffer::USAGE_HW_TEXTURE |
GraphicBuffer::USAGE_HW_2D |
GRALLOC_USAGE_SW_READ_OFTEN |
GRALLOC_USAGE_SW_WRITE_OFTEN);
// Init the buffer
status_t err = buffer->initCheck();
if (err != NO_ERROR)
{
LOGE("Error: %s\n", strerror(-err));
return ;
}

// Retrieve andorid native buffer
android_native_buffer_t* anb = buffer->getNativeBuffer();

//step 3. Create the EGLImage
const EGLint attrs[] = {
EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
EGL_NONE, EGL_NONE
};
EGLImageKHR pEGLImage = _eglCreateImageKHR(eglGetCurrentDisplay(), EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID, (EGLClientBuffer)anb, attrs);
if (pEGLImage == EGL_NO_IMAGE_KHR) {
EGLint error = eglGetError();
LOGE("Error (%#x): Creating EGLImageKHR at %s:%i\n", error, __FILE__, __LINE__);
}

// Create GL texture, bind to GL_TEXTURE_2D, etc.
GLuint texture, renderBuffer, frameBuffer;
printGLString("Version", GL_VERSION);
printGLString("Vendor", GL_VENDOR);
printGLString("Renderer", GL_RENDERER);

//lglActiveTexture(GL_TEXTURE0);
glGenTextures(1, &texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
LOG_INFO("Input Image Texture id %d\n", texture);
glBindTexture(GL_TEXTURE_2D, texture);

//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

// Attach the EGLImage to whatever texture is bound to GL_TEXTURE_2D
_glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, pEGLImage);
LOGE("glEGLImageTargetTexture2DOES");



glGenFramebuffers(1, &frameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,texture, 0);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
glDeleteTextures(1, &texture);
glDeleteFramebuffers(1, &frameBuffer);
glDeleteRenderbuffers(1, &renderBuffer);
LOG_ERROR("Image Handler initImageFBO failed!\n");
return;
}
glViewport(0, 0, w, h);

////////////////////////////////////
GLuint vsh, fsh, program;
vsh = glCreateShader(GL_VERTEX_SHADER);
fsh = glCreateShader(GL_FRAGMENT_SHADER);
program = glCreateProgram();
glShaderSource(vsh, 1, (const GLchar**)&g_defaultVertexShaderString, NULL);
glShaderSource(fsh, 1, (const GLchar**)&g_defaultFragmentShaderString, NULL);
glCompileShader(vsh);
glCompileShader(fsh);
glAttachShader(program, vsh);
glAttachShader(program, fsh);
glLinkProgram(program);
glDeleteShader(vsh);
glDeleteShader(fsh);
glUseProgram(program);
GLuint vPosition = glGetAttribLocation(program, "vPosition");
checkGLError("glGetAttribLocation");
LOG_INFO("glGetAttribLocation(\"vPosition\") = %d\n", vPosition);
glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0, g_vertices);
glEnableVertexAttribArray(vPosition);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);


//glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, row);



void* vaddr = NULL;
buffer->lock(GraphicBuffer::USAGE_SW_READ_OFTEN, &vaddr);
if(vaddr)
{
memcpy(row, vaddr, w * h * 4);
LOGE("copy memory");
}
buffer->unlock();

我不知道出了什么问题,vaddr的内存数据错误,屏幕上什么也没有显示。但我使用 glTexImage2D 和 glReadPixels 结果是正确的

最佳答案

您需要调用 glFinish() 来告诉驱动程序进行实际绘图。 glReadPixels() 调用导致事情正常进行,因为它强制进行渲染 - 您已经告诉驱动程序您想要读回像素,因此它会暂停,直到渲染完成。

关于opengl-es - 当我在 NDK 程序中使用 Eglimage 替换 glreadpixels 时出现什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25564203/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com