2018-09-23 05:48:44 +00:00
|
|
|
/*
|
|
|
|
Looking Glass - KVM FrameRelay (KVMFR) Client
|
2019-02-22 11:16:14 +00:00
|
|
|
Copyright (C) 2017-2019 Geoffrey McRae <geoff@hostfission.com>
|
2018-09-23 05:48:44 +00:00
|
|
|
https://looking-glass.hostfission.com
|
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2018-12-12 05:39:04 +00:00
|
|
|
#include "texture.h"
|
2019-04-11 01:12:59 +00:00
|
|
|
#include "common/debug.h"
|
2019-10-01 13:17:20 +00:00
|
|
|
#include "common/framebuffer.h"
|
2019-08-30 01:40:38 +00:00
|
|
|
#include "debug.h"
|
2018-09-23 05:48:44 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2020-05-17 23:06:11 +00:00
|
|
|
#include <stdatomic.h>
|
2018-09-23 05:48:44 +00:00
|
|
|
|
|
|
|
#include <SDL2/SDL_egl.h>
|
|
|
|
|
2020-07-24 07:39:16 +00:00
|
|
|
/* this must be a multiple of 2 */
|
|
|
|
#define TEXTURE_COUNT 2
|
2020-05-19 07:34:24 +00:00
|
|
|
|
|
|
|
struct Tex
|
|
|
|
{
|
|
|
|
GLuint t[3];
|
|
|
|
bool hasPBO;
|
|
|
|
GLuint pbo;
|
|
|
|
void * map;
|
|
|
|
GLsync sync;
|
|
|
|
};
|
|
|
|
|
2020-07-24 07:39:16 +00:00
|
|
|
struct TexState
|
2020-05-19 07:34:24 +00:00
|
|
|
{
|
2020-07-24 07:39:16 +00:00
|
|
|
_Atomic(uint8_t) w, u, s, d;
|
2020-05-19 07:34:24 +00:00
|
|
|
};
|
|
|
|
|
2018-09-23 05:48:44 +00:00
|
|
|
struct EGL_Texture
|
|
|
|
{
|
2018-09-23 10:45:20 +00:00
|
|
|
enum EGL_PixelFormat pixFmt;
|
2020-04-14 03:27:07 +00:00
|
|
|
size_t width, height, stride;
|
|
|
|
size_t bpp;
|
2018-09-24 09:48:11 +00:00
|
|
|
bool streaming;
|
2019-06-12 12:36:00 +00:00
|
|
|
bool ready;
|
2018-09-23 05:56:47 +00:00
|
|
|
|
2020-05-19 07:34:24 +00:00
|
|
|
int planeCount;
|
2018-09-23 10:45:20 +00:00
|
|
|
GLuint samplers[3];
|
2020-05-19 07:34:24 +00:00
|
|
|
size_t planes [3][3];
|
|
|
|
GLintptr offsets [3];
|
2018-12-04 10:23:28 +00:00
|
|
|
GLenum intFormat;
|
2018-09-23 10:45:20 +00:00
|
|
|
GLenum format;
|
2018-12-04 10:23:28 +00:00
|
|
|
GLenum dataType;
|
2020-05-19 07:34:24 +00:00
|
|
|
size_t pboBufferSize;
|
2018-09-23 10:45:20 +00:00
|
|
|
|
2020-07-24 07:39:16 +00:00
|
|
|
struct TexState state;
|
|
|
|
int textureCount;
|
|
|
|
struct Tex tex[TEXTURE_COUNT];
|
2018-09-23 05:48:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
bool egl_texture_init(EGL_Texture ** texture)
|
|
|
|
{
|
|
|
|
*texture = (EGL_Texture *)malloc(sizeof(EGL_Texture));
|
|
|
|
if (!*texture)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to malloc EGL_Texture");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(*texture, 0, sizeof(EGL_Texture));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void egl_texture_free(EGL_Texture ** texture)
|
|
|
|
{
|
|
|
|
if (!*texture)
|
|
|
|
return;
|
|
|
|
|
2020-05-19 07:34:24 +00:00
|
|
|
if ((*texture)->planeCount > 0)
|
|
|
|
glDeleteSamplers((*texture)->planeCount, (*texture)->samplers);
|
2018-09-23 05:48:44 +00:00
|
|
|
|
2020-05-29 06:54:25 +00:00
|
|
|
for(int i = 0; i < (*texture)->textureCount; ++i)
|
2019-05-22 01:37:27 +00:00
|
|
|
{
|
2020-05-19 07:34:24 +00:00
|
|
|
struct Tex * t = &(*texture)->tex[i];
|
|
|
|
if (t->hasPBO)
|
2019-05-22 01:37:27 +00:00
|
|
|
{
|
2020-05-19 07:34:24 +00:00
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, t->pbo);
|
2020-05-22 07:47:19 +00:00
|
|
|
if ((*texture)->tex[i].map)
|
|
|
|
{
|
|
|
|
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
|
|
|
|
(*texture)->tex[i].map = NULL;
|
|
|
|
}
|
2020-05-19 07:34:24 +00:00
|
|
|
glDeleteBuffers(1, &t->pbo);
|
|
|
|
if (t->sync)
|
|
|
|
glDeleteSync(t->sync);
|
2019-05-22 01:37:27 +00:00
|
|
|
}
|
2020-05-19 07:34:24 +00:00
|
|
|
|
|
|
|
if ((*texture)->planeCount > 0)
|
|
|
|
glDeleteTextures((*texture)->planeCount, t->t);
|
2019-05-22 01:37:27 +00:00
|
|
|
}
|
2020-05-19 07:34:24 +00:00
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
|
2018-09-23 05:48:44 +00:00
|
|
|
|
|
|
|
free(*texture);
|
|
|
|
*texture = NULL;
|
|
|
|
}
|
|
|
|
|
2020-05-29 05:48:59 +00:00
|
|
|
static bool egl_texture_map(EGL_Texture * texture, uint8_t i)
|
2020-05-21 01:44:56 +00:00
|
|
|
{
|
2020-05-29 05:48:59 +00:00
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, texture->tex[i].pbo);
|
|
|
|
texture->tex[i].map = glMapBufferRange(
|
|
|
|
GL_PIXEL_UNPACK_BUFFER,
|
|
|
|
0,
|
|
|
|
texture->pboBufferSize,
|
|
|
|
GL_MAP_WRITE_BIT |
|
|
|
|
GL_MAP_UNSYNCHRONIZED_BIT |
|
|
|
|
GL_MAP_INVALIDATE_BUFFER_BIT
|
|
|
|
);
|
2020-05-29 06:54:25 +00:00
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
|
2020-05-29 05:48:59 +00:00
|
|
|
|
|
|
|
if (!texture->tex[i].map)
|
2020-05-21 01:44:56 +00:00
|
|
|
{
|
2020-05-29 05:48:59 +00:00
|
|
|
EGL_ERROR("glMapBufferRange failed for %d of %lu bytes", i, texture->pboBufferSize);
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-21 01:44:56 +00:00
|
|
|
|
2020-05-29 05:48:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void egl_texture_unmap(EGL_Texture * texture, uint8_t i)
|
2020-05-21 01:44:56 +00:00
|
|
|
{
|
2020-05-29 05:48:59 +00:00
|
|
|
if (!texture->tex[i].map)
|
|
|
|
return;
|
2020-05-22 07:47:19 +00:00
|
|
|
|
2020-05-29 05:48:59 +00:00
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, texture->tex[i].pbo);
|
|
|
|
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
|
2020-05-29 06:54:25 +00:00
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
|
2020-05-29 05:48:59 +00:00
|
|
|
texture->tex[i].map = NULL;
|
|
|
|
}
|
|
|
|
|
2019-01-01 23:30:19 +00:00
|
|
|
bool egl_texture_setup(EGL_Texture * texture, enum EGL_PixelFormat pixFmt, size_t width, size_t height, size_t stride, bool streaming)
|
2018-09-23 05:48:44 +00:00
|
|
|
{
|
2020-05-19 07:34:24 +00:00
|
|
|
int planeCount;
|
2018-09-23 10:45:20 +00:00
|
|
|
|
2020-05-29 06:54:25 +00:00
|
|
|
if (texture->streaming)
|
|
|
|
{
|
|
|
|
for(int i = 0; i < texture->textureCount; ++i)
|
|
|
|
{
|
|
|
|
egl_texture_unmap(texture, i);
|
|
|
|
if (texture->tex[i].hasPBO)
|
|
|
|
{
|
|
|
|
glDeleteBuffers(1, &texture->tex[i].pbo);
|
|
|
|
texture->tex[i].hasPBO = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
texture->pixFmt = pixFmt;
|
|
|
|
texture->width = width;
|
|
|
|
texture->height = height;
|
|
|
|
texture->stride = stride;
|
|
|
|
texture->streaming = streaming;
|
|
|
|
texture->textureCount = streaming ? TEXTURE_COUNT : 1;
|
|
|
|
texture->ready = false;
|
|
|
|
|
2020-07-24 07:39:16 +00:00
|
|
|
atomic_store_explicit(&texture->state.w, 0, memory_order_relaxed);
|
|
|
|
atomic_store_explicit(&texture->state.u, 0, memory_order_relaxed);
|
|
|
|
atomic_store_explicit(&texture->state.s, 0, memory_order_relaxed);
|
|
|
|
atomic_store_explicit(&texture->state.d, 0, memory_order_relaxed);
|
2018-09-23 05:48:44 +00:00
|
|
|
|
2018-09-23 10:45:20 +00:00
|
|
|
switch(pixFmt)
|
|
|
|
{
|
|
|
|
case EGL_PF_BGRA:
|
2020-05-19 07:34:24 +00:00
|
|
|
planeCount = 1;
|
2020-04-14 03:27:07 +00:00
|
|
|
texture->bpp = 4;
|
2019-01-01 23:30:19 +00:00
|
|
|
texture->format = GL_BGRA;
|
|
|
|
texture->planes[0][0] = width;
|
|
|
|
texture->planes[0][1] = height;
|
|
|
|
texture->planes[0][2] = stride / 4;
|
|
|
|
texture->offsets[0] = 0;
|
|
|
|
texture->intFormat = GL_BGRA;
|
|
|
|
texture->dataType = GL_UNSIGNED_BYTE;
|
|
|
|
texture->pboBufferSize = height * stride;
|
2018-12-04 10:23:28 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EGL_PF_RGBA:
|
2020-05-19 07:34:24 +00:00
|
|
|
planeCount = 1;
|
2020-04-14 03:27:07 +00:00
|
|
|
texture->bpp = 4;
|
2019-01-01 23:30:19 +00:00
|
|
|
texture->format = GL_RGBA;
|
|
|
|
texture->planes[0][0] = width;
|
|
|
|
texture->planes[0][1] = height;
|
|
|
|
texture->planes[0][2] = stride / 4;
|
|
|
|
texture->offsets[0] = 0;
|
|
|
|
texture->intFormat = GL_BGRA;
|
|
|
|
texture->dataType = GL_UNSIGNED_BYTE;
|
|
|
|
texture->pboBufferSize = height * stride;
|
2018-12-04 10:23:28 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EGL_PF_RGBA10:
|
2020-05-19 07:34:24 +00:00
|
|
|
planeCount = 1;
|
2020-04-14 03:27:07 +00:00
|
|
|
texture->bpp = 4;
|
2019-01-01 23:30:19 +00:00
|
|
|
texture->format = GL_RGBA;
|
|
|
|
texture->planes[0][0] = width;
|
|
|
|
texture->planes[0][1] = height;
|
|
|
|
texture->planes[0][2] = stride / 4;
|
|
|
|
texture->offsets[0] = 0;
|
|
|
|
texture->intFormat = GL_RGB10_A2;
|
|
|
|
texture->dataType = GL_UNSIGNED_INT_2_10_10_10_REV;
|
|
|
|
texture->pboBufferSize = height * stride;
|
2018-09-23 10:45:20 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EGL_PF_YUV420:
|
2020-05-19 07:34:24 +00:00
|
|
|
planeCount = 3;
|
2020-04-14 03:27:07 +00:00
|
|
|
texture->bpp = 4;
|
2019-01-01 23:30:19 +00:00
|
|
|
texture->format = GL_RED;
|
|
|
|
texture->planes[0][0] = width;
|
|
|
|
texture->planes[0][1] = height;
|
|
|
|
texture->planes[0][2] = stride;
|
|
|
|
texture->planes[1][0] = width / 2;
|
|
|
|
texture->planes[1][1] = height / 2;
|
|
|
|
texture->planes[1][2] = stride / 2;
|
|
|
|
texture->planes[2][0] = width / 2;
|
|
|
|
texture->planes[2][1] = height / 2;
|
|
|
|
texture->planes[2][2] = stride / 2;
|
|
|
|
texture->offsets[0] = 0;
|
|
|
|
texture->offsets[1] = stride * height;
|
|
|
|
texture->offsets[2] = texture->offsets[1] + (texture->offsets[1] / 4);
|
|
|
|
texture->dataType = GL_UNSIGNED_BYTE;
|
|
|
|
texture->pboBufferSize = texture->offsets[2] + (texture->offsets[1] / 4);
|
2018-09-23 10:45:20 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
DEBUG_ERROR("Unsupported pixel format");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-19 07:34:24 +00:00
|
|
|
if (planeCount > texture->planeCount)
|
2018-09-24 09:48:11 +00:00
|
|
|
{
|
2020-05-19 07:34:24 +00:00
|
|
|
if (texture->planeCount > 0)
|
|
|
|
glDeleteSamplers(texture->planeCount, texture->samplers);
|
|
|
|
|
2020-05-29 06:54:25 +00:00
|
|
|
for(int i = 0; i < texture->textureCount; ++i)
|
2018-09-24 09:48:11 +00:00
|
|
|
{
|
2020-05-19 07:34:24 +00:00
|
|
|
if (texture->planeCount > 0)
|
|
|
|
glDeleteTextures(texture->planeCount, texture->tex[i].t);
|
|
|
|
glGenTextures(planeCount, texture->tex[i].t);
|
2018-09-24 09:48:11 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 07:34:24 +00:00
|
|
|
glGenSamplers(planeCount, texture->samplers);
|
|
|
|
for(int p = 0; p < planeCount; ++p)
|
|
|
|
{
|
|
|
|
glSamplerParameteri(texture->samplers[p], GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glSamplerParameteri(texture->samplers[p], GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glSamplerParameteri(texture->samplers[p], GL_TEXTURE_WRAP_S , GL_CLAMP_TO_EDGE);
|
|
|
|
glSamplerParameteri(texture->samplers[p], GL_TEXTURE_WRAP_T , GL_CLAMP_TO_EDGE);
|
|
|
|
}
|
|
|
|
|
|
|
|
texture->planeCount = planeCount;
|
2018-09-24 09:48:11 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 06:54:25 +00:00
|
|
|
for(int i = 0; i < texture->textureCount; ++i)
|
2018-09-23 10:45:20 +00:00
|
|
|
{
|
2020-05-19 07:34:24 +00:00
|
|
|
for(int p = 0; p < planeCount; ++p)
|
|
|
|
{
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture->tex[i].t[p]);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, texture->intFormat, texture->planes[p][0],
|
|
|
|
texture->planes[p][1], 0, texture->format, texture->dataType, NULL);
|
|
|
|
}
|
2018-09-23 05:56:47 +00:00
|
|
|
}
|
2018-09-24 09:48:11 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
2018-09-23 05:48:44 +00:00
|
|
|
|
2020-05-29 06:54:25 +00:00
|
|
|
if (!streaming)
|
|
|
|
return true;
|
2020-05-21 01:44:56 +00:00
|
|
|
|
2020-05-29 06:54:25 +00:00
|
|
|
for(int i = 0; i < texture->textureCount; ++i)
|
2018-09-23 05:48:44 +00:00
|
|
|
{
|
2020-05-21 01:44:56 +00:00
|
|
|
glGenBuffers(1, &texture->tex[i].pbo);
|
|
|
|
texture->tex[i].hasPBO = true;
|
|
|
|
|
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, texture->tex[i].pbo);
|
|
|
|
glBufferStorage(
|
|
|
|
GL_PIXEL_UNPACK_BUFFER,
|
|
|
|
texture->pboBufferSize,
|
|
|
|
NULL,
|
|
|
|
GL_MAP_WRITE_BIT
|
|
|
|
);
|
2020-05-29 06:54:25 +00:00
|
|
|
}
|
2020-05-21 01:44:56 +00:00
|
|
|
|
2018-09-23 05:48:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-19 12:42:55 +00:00
|
|
|
static void egl_warn_slow()
|
|
|
|
{
|
|
|
|
static bool warnDone = false;
|
|
|
|
if (!warnDone)
|
|
|
|
{
|
|
|
|
warnDone = true;
|
|
|
|
DEBUG_BREAK();
|
2020-05-21 04:09:51 +00:00
|
|
|
DEBUG_WARN("The guest is providing updates faster then your computer can display them");
|
|
|
|
DEBUG_WARN("This is a hardware limitation, expect microstutters & frame skips");
|
2020-05-19 12:42:55 +00:00
|
|
|
DEBUG_BREAK();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-24 09:48:11 +00:00
|
|
|
bool egl_texture_update(EGL_Texture * texture, const uint8_t * buffer)
|
2018-09-23 05:48:44 +00:00
|
|
|
{
|
2018-09-24 09:48:11 +00:00
|
|
|
if (texture->streaming)
|
2018-09-23 10:45:20 +00:00
|
|
|
{
|
2020-07-24 07:39:16 +00:00
|
|
|
const uint8_t sw =
|
|
|
|
atomic_load_explicit(&texture->state.w, memory_order_acquire);
|
2018-09-24 09:48:11 +00:00
|
|
|
|
2020-08-15 12:37:54 +00:00
|
|
|
if (atomic_load_explicit(&texture->state.u, memory_order_acquire) == (uint8_t)(sw + 1))
|
2020-05-19 12:42:55 +00:00
|
|
|
{
|
|
|
|
egl_warn_slow();
|
2020-05-19 07:34:24 +00:00
|
|
|
return true;
|
2020-05-19 12:42:55 +00:00
|
|
|
}
|
2019-05-22 01:37:27 +00:00
|
|
|
|
2020-07-24 07:39:16 +00:00
|
|
|
const uint8_t t = sw % TEXTURE_COUNT;
|
|
|
|
if (!egl_texture_map(texture, t))
|
2020-05-30 06:50:27 +00:00
|
|
|
return EGL_TEX_STATUS_ERROR;
|
|
|
|
|
2020-07-24 07:39:16 +00:00
|
|
|
memcpy(texture->tex[t].map, buffer, texture->pboBufferSize);
|
|
|
|
atomic_fetch_add_explicit(&texture->state.w, 1, memory_order_release);
|
|
|
|
egl_texture_unmap(texture, t);
|
2018-09-24 09:48:11 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-19 07:34:24 +00:00
|
|
|
for(int p = 0; p < texture->planeCount; ++p)
|
2018-09-24 09:48:11 +00:00
|
|
|
{
|
2020-05-19 07:34:24 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, texture->tex[0].t[p]);
|
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, texture->planes[p][0]);
|
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, texture->planes[p][0], texture->planes[p][1],
|
|
|
|
texture->format, texture->dataType, buffer + texture->offsets[p]);
|
2018-09-24 09:48:11 +00:00
|
|
|
}
|
2018-10-03 14:09:10 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
2018-09-23 10:45:20 +00:00
|
|
|
}
|
2018-09-23 05:48:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-01-13 08:30:49 +00:00
|
|
|
bool egl_texture_update_from_frame(EGL_Texture * texture, const FrameBuffer * frame)
|
2019-10-01 13:17:20 +00:00
|
|
|
{
|
|
|
|
if (!texture->streaming)
|
|
|
|
return false;
|
|
|
|
|
2020-07-24 07:39:16 +00:00
|
|
|
const uint8_t sw =
|
|
|
|
atomic_load_explicit(&texture->state.w, memory_order_acquire);
|
2020-05-19 07:34:24 +00:00
|
|
|
|
2020-08-15 12:37:54 +00:00
|
|
|
if (atomic_load_explicit(&texture->state.u, memory_order_acquire) == (uint8_t)(sw + 1))
|
2020-05-19 12:42:55 +00:00
|
|
|
{
|
|
|
|
egl_warn_slow();
|
2019-10-01 13:17:20 +00:00
|
|
|
return true;
|
2020-05-19 12:42:55 +00:00
|
|
|
}
|
2019-10-01 13:17:20 +00:00
|
|
|
|
2020-07-24 07:39:16 +00:00
|
|
|
const uint8_t t = sw % TEXTURE_COUNT;
|
|
|
|
if (!egl_texture_map(texture, t))
|
2020-05-30 06:50:27 +00:00
|
|
|
return EGL_TEX_STATUS_ERROR;
|
|
|
|
|
2020-04-14 03:27:07 +00:00
|
|
|
framebuffer_read(
|
|
|
|
frame,
|
2020-07-24 07:39:16 +00:00
|
|
|
texture->tex[t].map,
|
2020-04-14 03:27:07 +00:00
|
|
|
texture->stride,
|
|
|
|
texture->height,
|
|
|
|
texture->width,
|
|
|
|
texture->bpp,
|
|
|
|
texture->stride
|
|
|
|
);
|
|
|
|
|
2020-07-24 07:39:16 +00:00
|
|
|
atomic_fetch_add_explicit(&texture->state.w, 1, memory_order_release);
|
|
|
|
egl_texture_unmap(texture, t);
|
2020-05-30 06:50:27 +00:00
|
|
|
|
2019-10-01 13:17:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-12 12:36:00 +00:00
|
|
|
enum EGL_TexStatus egl_texture_process(EGL_Texture * texture)
|
2018-09-23 05:48:44 +00:00
|
|
|
{
|
2019-06-12 12:36:00 +00:00
|
|
|
if (!texture->streaming)
|
|
|
|
return EGL_TEX_STATUS_OK;
|
|
|
|
|
2020-07-24 07:39:16 +00:00
|
|
|
const uint8_t su =
|
|
|
|
atomic_load_explicit(&texture->state.u, memory_order_acquire);
|
2020-05-19 07:34:24 +00:00
|
|
|
|
2020-07-24 07:39:16 +00:00
|
|
|
const uint8_t nextu = su + 1;
|
|
|
|
if (
|
|
|
|
su == atomic_load_explicit(&texture->state.w, memory_order_acquire) ||
|
|
|
|
nextu == atomic_load_explicit(&texture->state.s, memory_order_acquire) ||
|
|
|
|
nextu == atomic_load_explicit(&texture->state.d, memory_order_acquire))
|
2019-06-12 12:36:00 +00:00
|
|
|
return texture->ready ? EGL_TEX_STATUS_OK : EGL_TEX_STATUS_NOTREADY;
|
|
|
|
|
2020-05-19 07:34:24 +00:00
|
|
|
/* update the texture */
|
2020-07-24 07:39:16 +00:00
|
|
|
const uint8_t t = su % TEXTURE_COUNT;
|
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, texture->tex[t].pbo);
|
2020-05-19 07:34:24 +00:00
|
|
|
for(int p = 0; p < texture->planeCount; ++p)
|
2018-10-03 14:09:10 +00:00
|
|
|
{
|
2020-07-24 07:39:16 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, texture->tex[t].t[p]);
|
2020-05-19 07:34:24 +00:00
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, texture->planes[p][2]);
|
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, texture->planes[p][0], texture->planes[p][1],
|
|
|
|
texture->format, texture->dataType, (const void *)texture->offsets[p]);
|
2018-10-03 14:09:10 +00:00
|
|
|
|
2019-06-12 12:36:00 +00:00
|
|
|
}
|
2020-05-29 06:54:25 +00:00
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
|
2019-06-12 12:36:00 +00:00
|
|
|
|
2020-05-19 07:34:24 +00:00
|
|
|
/* create a fence to prevent usage before the update is complete */
|
2020-07-24 07:39:16 +00:00
|
|
|
texture->tex[t].sync = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
|
2019-06-12 12:36:00 +00:00
|
|
|
|
2020-05-22 07:47:19 +00:00
|
|
|
/* we must flush to ensure the sync is in the command buffer */
|
|
|
|
glFlush();
|
|
|
|
|
2020-05-29 05:48:59 +00:00
|
|
|
texture->ready = true;
|
2020-07-24 07:39:16 +00:00
|
|
|
atomic_fetch_add_explicit(&texture->state.u, 1, memory_order_release);
|
2019-06-12 12:36:00 +00:00
|
|
|
|
|
|
|
return EGL_TEX_STATUS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum EGL_TexStatus egl_texture_bind(EGL_Texture * texture)
|
|
|
|
{
|
2020-07-24 07:39:16 +00:00
|
|
|
uint8_t ss = atomic_load_explicit(&texture->state.s, memory_order_acquire);
|
|
|
|
uint8_t sd = atomic_load_explicit(&texture->state.d, memory_order_acquire);
|
2020-05-19 07:34:24 +00:00
|
|
|
|
|
|
|
if (texture->streaming)
|
|
|
|
{
|
|
|
|
if (!texture->ready)
|
|
|
|
return EGL_TEX_STATUS_NOTREADY;
|
|
|
|
|
2020-07-24 07:39:16 +00:00
|
|
|
const uint8_t t = ss % TEXTURE_COUNT;
|
|
|
|
if (texture->tex[t].sync != 0)
|
2020-05-19 07:34:24 +00:00
|
|
|
{
|
2020-07-24 07:39:16 +00:00
|
|
|
switch(glClientWaitSync(texture->tex[t].sync, 0, 20000000)) // 20ms
|
2020-05-19 07:34:24 +00:00
|
|
|
{
|
|
|
|
case GL_ALREADY_SIGNALED:
|
|
|
|
case GL_CONDITION_SATISFIED:
|
2020-07-24 07:39:16 +00:00
|
|
|
glDeleteSync(texture->tex[t].sync);
|
|
|
|
texture->tex[t].sync = 0;
|
2020-05-19 07:34:24 +00:00
|
|
|
|
2020-07-24 07:39:16 +00:00
|
|
|
ss = atomic_fetch_add_explicit(&texture->state.s, 1,
|
|
|
|
memory_order_release) + 1;
|
2020-05-19 07:34:24 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GL_TIMEOUT_EXPIRED:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GL_WAIT_FAILED:
|
2020-05-22 07:47:19 +00:00
|
|
|
case GL_INVALID_VALUE:
|
2020-07-24 07:39:16 +00:00
|
|
|
glDeleteSync(texture->tex[t].sync);
|
|
|
|
texture->tex[t].sync = 0;
|
2020-05-19 07:34:24 +00:00
|
|
|
EGL_ERROR("glClientWaitSync failed");
|
|
|
|
return EGL_TEX_STATUS_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2020-05-19 12:42:55 +00:00
|
|
|
|
2020-08-15 12:37:54 +00:00
|
|
|
if (ss != sd && ss != (uint8_t)(sd + 1))
|
2020-07-24 07:39:16 +00:00
|
|
|
sd = atomic_fetch_add_explicit(&texture->state.d, 1,
|
|
|
|
memory_order_release) + 1;
|
2020-05-19 07:34:24 +00:00
|
|
|
}
|
2018-10-03 14:09:10 +00:00
|
|
|
|
2020-07-24 07:39:16 +00:00
|
|
|
const uint8_t t = sd % TEXTURE_COUNT;
|
2020-05-19 07:34:24 +00:00
|
|
|
for(int i = 0; i < texture->planeCount; ++i)
|
2018-09-23 10:45:20 +00:00
|
|
|
{
|
|
|
|
glActiveTexture(GL_TEXTURE0 + i);
|
2020-07-24 07:39:16 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, texture->tex[t].t[i]);
|
2018-09-23 10:45:20 +00:00
|
|
|
glBindSampler(i, texture->samplers[i]);
|
|
|
|
}
|
2019-06-12 12:36:00 +00:00
|
|
|
|
|
|
|
return EGL_TEX_STATUS_OK;
|
2018-09-23 10:45:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int egl_texture_count(EGL_Texture * texture)
|
|
|
|
{
|
2020-05-19 07:34:24 +00:00
|
|
|
return texture->planeCount;
|
2020-04-14 03:27:07 +00:00
|
|
|
}
|