2021-08-02 13:37:33 +00:00
|
|
|
/**
|
|
|
|
* Looking Glass
|
2023-10-20 04:36:34 +00:00
|
|
|
* Copyright © 2017-2023 The Looking Glass Authors
|
2021-08-02 13:37:33 +00:00
|
|
|
* https://looking-glass.io
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 2 of the License, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., 59
|
|
|
|
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2021-08-09 04:08:10 +00:00
|
|
|
#include "texture.h"
|
2021-08-02 13:37:33 +00:00
|
|
|
#include "texture_util.h"
|
|
|
|
|
|
|
|
#include <EGL/egl.h>
|
|
|
|
#include <GLES3/gl3.h>
|
|
|
|
#include <GLES2/gl2ext.h>
|
|
|
|
|
|
|
|
#include "egldebug.h"
|
2021-08-19 02:55:23 +00:00
|
|
|
#include "egl_dynprocs.h"
|
2021-08-02 13:37:33 +00:00
|
|
|
|
2021-08-08 07:16:10 +00:00
|
|
|
bool egl_texUtilGetFormat(const EGL_TexSetup * setup, EGL_TexFormat * fmt)
|
2021-08-02 13:37:33 +00:00
|
|
|
{
|
2023-11-08 06:54:48 +00:00
|
|
|
fmt->pixFmt = setup->pixFmt;
|
|
|
|
fmt->width = setup->width;
|
|
|
|
fmt->height = setup->height;
|
|
|
|
fmt->stride = setup->stride;
|
|
|
|
fmt->pitch = setup->pitch;
|
|
|
|
|
2021-08-02 13:37:33 +00:00
|
|
|
switch(setup->pixFmt)
|
|
|
|
{
|
2023-11-02 20:03:32 +00:00
|
|
|
//EGL has no support for 24-bit formats, so we stuff it into a 32-bit
|
|
|
|
//texture to unpack with a shader later
|
2023-11-08 05:04:58 +00:00
|
|
|
case EGL_PF_BGR_32:
|
2023-11-02 20:03:32 +00:00
|
|
|
// fallthrough
|
|
|
|
|
2021-08-02 13:37:33 +00:00
|
|
|
case EGL_PF_BGRA:
|
|
|
|
fmt->bpp = 4;
|
|
|
|
fmt->format = GL_BGRA_EXT;
|
|
|
|
fmt->intFormat = GL_BGRA_EXT;
|
|
|
|
fmt->dataType = GL_UNSIGNED_BYTE;
|
|
|
|
fmt->fourcc = DRM_FORMAT_ARGB8888;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EGL_PF_RGBA:
|
|
|
|
fmt->bpp = 4;
|
|
|
|
fmt->format = GL_RGBA;
|
|
|
|
fmt->intFormat = GL_RGBA;
|
|
|
|
fmt->dataType = GL_UNSIGNED_BYTE;
|
|
|
|
fmt->fourcc = DRM_FORMAT_ABGR8888;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EGL_PF_RGBA10:
|
|
|
|
fmt->bpp = 4;
|
|
|
|
fmt->format = GL_RGBA;
|
|
|
|
fmt->intFormat = GL_RGB10_A2;
|
|
|
|
fmt->dataType = GL_UNSIGNED_INT_2_10_10_10_REV;
|
2023-11-02 20:03:32 +00:00
|
|
|
fmt->fourcc = DRM_FORMAT_ABGR2101010;
|
2021-08-02 13:37:33 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EGL_PF_RGBA16F:
|
|
|
|
fmt->bpp = 8;
|
|
|
|
fmt->format = GL_RGBA;
|
|
|
|
fmt->intFormat = GL_RGBA16F;
|
|
|
|
fmt->dataType = GL_HALF_FLOAT;
|
|
|
|
fmt->fourcc = DRM_FORMAT_ABGR16161616F;
|
|
|
|
break;
|
|
|
|
|
2023-11-08 06:54:48 +00:00
|
|
|
case EGL_PF_RGB_24:
|
|
|
|
fmt->bpp = 3;
|
|
|
|
fmt->format = GL_BGRA_EXT;
|
|
|
|
fmt->intFormat = GL_BGRA_EXT;
|
|
|
|
fmt->dataType = GL_UNSIGNED_BYTE;
|
|
|
|
fmt->fourcc = DRM_FORMAT_ARGB8888;
|
2023-11-18 22:42:37 +00:00
|
|
|
fmt->stride = fmt->pitch / 4;
|
2023-11-08 06:54:48 +00:00
|
|
|
break;
|
|
|
|
|
2021-08-02 13:37:33 +00:00
|
|
|
default:
|
|
|
|
DEBUG_ERROR("Unsupported pixel format");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-11-02 20:03:32 +00:00
|
|
|
if (!fmt->stride)
|
|
|
|
fmt->stride = setup->width;
|
|
|
|
|
|
|
|
if (!fmt->pitch)
|
|
|
|
fmt->pitch = fmt->stride * fmt->bpp;
|
|
|
|
|
2023-11-08 03:24:37 +00:00
|
|
|
fmt->dataSize = fmt->height * fmt->pitch;
|
2021-08-02 13:37:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-08 07:16:10 +00:00
|
|
|
bool egl_texUtilGenBuffers(const EGL_TexFormat * fmt, EGL_TexBuffer * buffers,
|
2021-08-02 13:37:33 +00:00
|
|
|
int count)
|
|
|
|
{
|
|
|
|
for(int i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
EGL_TexBuffer *buffer = &buffers[i];
|
|
|
|
|
2023-11-08 03:24:37 +00:00
|
|
|
buffer->size = fmt->dataSize;
|
2021-08-02 13:37:33 +00:00
|
|
|
glGenBuffers(1, &buffer->pbo);
|
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer->pbo);
|
2021-08-19 02:55:23 +00:00
|
|
|
g_egl_dynProcs.glBufferStorageEXT(
|
2021-08-02 13:37:33 +00:00
|
|
|
GL_PIXEL_UNPACK_BUFFER,
|
2023-11-08 03:24:37 +00:00
|
|
|
fmt->dataSize,
|
2021-08-02 13:37:33 +00:00
|
|
|
NULL,
|
|
|
|
GL_MAP_WRITE_BIT |
|
|
|
|
GL_MAP_PERSISTENT_BIT_EXT |
|
|
|
|
GL_MAP_COHERENT_BIT_EXT
|
|
|
|
);
|
|
|
|
|
2021-08-08 07:16:10 +00:00
|
|
|
if (!egl_texUtilMapBuffer(buffer))
|
2021-08-02 13:37:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-08 07:16:10 +00:00
|
|
|
void egl_texUtilFreeBuffers(EGL_TexBuffer * buffers, int count)
|
2021-08-02 13:37:33 +00:00
|
|
|
{
|
|
|
|
for(int i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
EGL_TexBuffer *buffer = &buffers[i];
|
|
|
|
|
|
|
|
if (!buffer->pbo)
|
|
|
|
continue;
|
|
|
|
|
2021-08-08 07:16:10 +00:00
|
|
|
egl_texUtilUnmapBuffer(buffer);
|
2021-08-02 13:37:33 +00:00
|
|
|
glDeleteBuffers(1, &buffer->pbo);
|
|
|
|
buffer->pbo = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-08 07:16:10 +00:00
|
|
|
bool egl_texUtilMapBuffer(EGL_TexBuffer * buffer)
|
2021-08-02 13:37:33 +00:00
|
|
|
{
|
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer->pbo);
|
|
|
|
buffer->map = glMapBufferRange(
|
|
|
|
GL_PIXEL_UNPACK_BUFFER,
|
|
|
|
0,
|
|
|
|
buffer->size,
|
|
|
|
GL_MAP_WRITE_BIT |
|
|
|
|
GL_MAP_UNSYNCHRONIZED_BIT |
|
|
|
|
GL_MAP_INVALIDATE_BUFFER_BIT |
|
|
|
|
GL_MAP_PERSISTENT_BIT_EXT |
|
|
|
|
GL_MAP_COHERENT_BIT_EXT);
|
|
|
|
|
|
|
|
if (!buffer->map)
|
|
|
|
DEBUG_GL_ERROR("glMapBufferRange failed of %lu bytes", buffer->size);
|
|
|
|
|
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
|
|
|
|
return buffer->map;
|
|
|
|
}
|
|
|
|
|
2021-08-08 07:16:10 +00:00
|
|
|
void egl_texUtilUnmapBuffer(EGL_TexBuffer * buffer)
|
2021-08-02 13:37:33 +00:00
|
|
|
{
|
|
|
|
if (!buffer->map)
|
|
|
|
return;
|
|
|
|
|
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer->pbo);
|
|
|
|
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
|
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
|
|
|
|
buffer->map = NULL;
|
|
|
|
}
|