mirror of
https://github.com/gnif/LookingGlass.git
synced 2025-01-22 12:47:04 +00:00
[client] egl: reworked the streaming texture pipeline
This commit is contained in:
parent
aff19e13c7
commit
73ba325072
@ -109,7 +109,7 @@ static struct Option egl_options[] =
|
|||||||
.name = "doubleBuffer",
|
.name = "doubleBuffer",
|
||||||
.description = "Enable double buffering",
|
.description = "Enable double buffering",
|
||||||
.type = OPTION_TYPE_BOOL,
|
.type = OPTION_TYPE_BOOL,
|
||||||
.value.x_bool = true
|
.value.x_bool = false
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.module = "egl",
|
.module = "egl",
|
||||||
|
@ -31,6 +31,32 @@ Place, Suite 330, Boston, MA 02111-1307 USA
|
|||||||
|
|
||||||
#include <SDL2/SDL_egl.h>
|
#include <SDL2/SDL_egl.h>
|
||||||
|
|
||||||
|
#define TEXTURE_COUNT 4
|
||||||
|
|
||||||
|
struct Tex
|
||||||
|
{
|
||||||
|
GLuint t[3];
|
||||||
|
bool hasPBO;
|
||||||
|
GLuint pbo;
|
||||||
|
void * map;
|
||||||
|
GLsync sync;
|
||||||
|
};
|
||||||
|
|
||||||
|
union TexState
|
||||||
|
{
|
||||||
|
uint32_t v;
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* w = write
|
||||||
|
* u = upload
|
||||||
|
* s = schedule
|
||||||
|
* d = display
|
||||||
|
*/
|
||||||
|
uint8_t w, u, s, d;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
struct EGL_Texture
|
struct EGL_Texture
|
||||||
{
|
{
|
||||||
enum EGL_PixelFormat pixFmt;
|
enum EGL_PixelFormat pixFmt;
|
||||||
@ -39,23 +65,18 @@ struct EGL_Texture
|
|||||||
bool streaming;
|
bool streaming;
|
||||||
bool ready;
|
bool ready;
|
||||||
|
|
||||||
int textureCount;
|
int planeCount;
|
||||||
GLuint textures[3];
|
|
||||||
GLuint samplers[3];
|
GLuint samplers[3];
|
||||||
size_t planes [3][3];
|
size_t planes [3][3];
|
||||||
GLintptr offsets [3];
|
GLintptr offsets [3];
|
||||||
GLenum intFormat;
|
GLenum intFormat;
|
||||||
GLenum format;
|
GLenum format;
|
||||||
GLenum dataType;
|
GLenum dataType;
|
||||||
|
|
||||||
bool hasPBO;
|
|
||||||
GLuint pbo[2];
|
|
||||||
int pboRIndex;
|
|
||||||
int pboWIndex;
|
|
||||||
_Atomic(int) pboCount;
|
|
||||||
size_t pboBufferSize;
|
size_t pboBufferSize;
|
||||||
void * pboMap[2];
|
|
||||||
GLsync pboSync[2];
|
LG_Lock lock;
|
||||||
|
union TexState state;
|
||||||
|
struct Tex tex[TEXTURE_COUNT];
|
||||||
};
|
};
|
||||||
|
|
||||||
bool egl_texture_init(EGL_Texture ** texture)
|
bool egl_texture_init(EGL_Texture ** texture)
|
||||||
@ -68,7 +89,7 @@ bool egl_texture_init(EGL_Texture ** texture)
|
|||||||
}
|
}
|
||||||
|
|
||||||
memset(*texture, 0, sizeof(EGL_Texture));
|
memset(*texture, 0, sizeof(EGL_Texture));
|
||||||
|
LG_LOCK_INIT((*texture)->lock);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,33 +98,34 @@ void egl_texture_free(EGL_Texture ** texture)
|
|||||||
if (!*texture)
|
if (!*texture)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if ((*texture)->textureCount > 0)
|
if ((*texture)->planeCount > 0)
|
||||||
|
glDeleteSamplers((*texture)->planeCount, (*texture)->samplers);
|
||||||
|
|
||||||
|
for(int i = 0; i < ((*texture)->streaming ? TEXTURE_COUNT : 1); ++i)
|
||||||
{
|
{
|
||||||
glDeleteTextures((*texture)->textureCount, (*texture)->textures);
|
struct Tex * t = &(*texture)->tex[i];
|
||||||
glDeleteSamplers((*texture)->textureCount, (*texture)->samplers);
|
if (t->hasPBO)
|
||||||
|
{
|
||||||
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, t->pbo);
|
||||||
|
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
|
||||||
|
glDeleteBuffers(1, &t->pbo);
|
||||||
|
if (t->sync)
|
||||||
|
glDeleteSync(t->sync);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((*texture)->hasPBO)
|
if ((*texture)->planeCount > 0)
|
||||||
{
|
glDeleteTextures((*texture)->planeCount, t->t);
|
||||||
for(int i = 0; i < 2; ++i)
|
|
||||||
{
|
|
||||||
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, (*texture)->pbo[i]);
|
|
||||||
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
|
|
||||||
|
|
||||||
if ((*texture)->pboSync[i])
|
|
||||||
glDeleteSync((*texture)->pboSync[i]);
|
|
||||||
}
|
}
|
||||||
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
|
||||||
glDeleteBuffers(2, (*texture)->pbo);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
LG_LOCK_FREE((*texture)->lock);
|
||||||
free(*texture);
|
free(*texture);
|
||||||
*texture = NULL;
|
*texture = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool egl_texture_setup(EGL_Texture * texture, enum EGL_PixelFormat pixFmt, size_t width, size_t height, size_t stride, bool streaming)
|
bool egl_texture_setup(EGL_Texture * texture, enum EGL_PixelFormat pixFmt, size_t width, size_t height, size_t stride, bool streaming)
|
||||||
{
|
{
|
||||||
int textureCount;
|
int planeCount;
|
||||||
|
|
||||||
texture->pixFmt = pixFmt;
|
texture->pixFmt = pixFmt;
|
||||||
texture->width = width;
|
texture->width = width;
|
||||||
@ -111,11 +133,12 @@ bool egl_texture_setup(EGL_Texture * texture, enum EGL_PixelFormat pixFmt, size_
|
|||||||
texture->stride = stride;
|
texture->stride = stride;
|
||||||
texture->streaming = streaming;
|
texture->streaming = streaming;
|
||||||
texture->ready = false;
|
texture->ready = false;
|
||||||
|
texture->state.v = 0;
|
||||||
|
|
||||||
switch(pixFmt)
|
switch(pixFmt)
|
||||||
{
|
{
|
||||||
case EGL_PF_BGRA:
|
case EGL_PF_BGRA:
|
||||||
textureCount = 1;
|
planeCount = 1;
|
||||||
texture->bpp = 4;
|
texture->bpp = 4;
|
||||||
texture->format = GL_BGRA;
|
texture->format = GL_BGRA;
|
||||||
texture->planes[0][0] = width;
|
texture->planes[0][0] = width;
|
||||||
@ -128,7 +151,7 @@ bool egl_texture_setup(EGL_Texture * texture, enum EGL_PixelFormat pixFmt, size_
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case EGL_PF_RGBA:
|
case EGL_PF_RGBA:
|
||||||
textureCount = 1;
|
planeCount = 1;
|
||||||
texture->bpp = 4;
|
texture->bpp = 4;
|
||||||
texture->format = GL_RGBA;
|
texture->format = GL_RGBA;
|
||||||
texture->planes[0][0] = width;
|
texture->planes[0][0] = width;
|
||||||
@ -141,7 +164,7 @@ bool egl_texture_setup(EGL_Texture * texture, enum EGL_PixelFormat pixFmt, size_
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case EGL_PF_RGBA10:
|
case EGL_PF_RGBA10:
|
||||||
textureCount = 1;
|
planeCount = 1;
|
||||||
texture->bpp = 4;
|
texture->bpp = 4;
|
||||||
texture->format = GL_RGBA;
|
texture->format = GL_RGBA;
|
||||||
texture->planes[0][0] = width;
|
texture->planes[0][0] = width;
|
||||||
@ -154,7 +177,7 @@ bool egl_texture_setup(EGL_Texture * texture, enum EGL_PixelFormat pixFmt, size_
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case EGL_PF_YUV420:
|
case EGL_PF_YUV420:
|
||||||
textureCount = 3;
|
planeCount = 3;
|
||||||
texture->bpp = 4;
|
texture->bpp = 4;
|
||||||
texture->format = GL_RED;
|
texture->format = GL_RED;
|
||||||
texture->planes[0][0] = width;
|
texture->planes[0][0] = width;
|
||||||
@ -178,50 +201,57 @@ bool egl_texture_setup(EGL_Texture * texture, enum EGL_PixelFormat pixFmt, size_
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (textureCount > texture->textureCount)
|
if (planeCount > texture->planeCount)
|
||||||
{
|
{
|
||||||
if (texture->textureCount > 0)
|
if (texture->planeCount > 0)
|
||||||
|
glDeleteSamplers(texture->planeCount, texture->samplers);
|
||||||
|
|
||||||
|
for(int i = 0; i < TEXTURE_COUNT; ++i)
|
||||||
{
|
{
|
||||||
glDeleteTextures(texture->textureCount, texture->textures);
|
if (texture->planeCount > 0)
|
||||||
glDeleteSamplers(texture->textureCount, texture->samplers);
|
glDeleteTextures(texture->planeCount, texture->tex[i].t);
|
||||||
|
glGenTextures(planeCount, texture->tex[i].t);
|
||||||
}
|
}
|
||||||
|
|
||||||
texture->textureCount = textureCount;
|
glGenSamplers(planeCount, texture->samplers);
|
||||||
glGenTextures(texture->textureCount, texture->textures);
|
for(int p = 0; p < planeCount; ++p)
|
||||||
glGenSamplers(texture->textureCount, texture->samplers);
|
{
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
for(int i = 0; i < textureCount; ++i)
|
texture->planeCount = planeCount;
|
||||||
{
|
}
|
||||||
glSamplerParameteri(texture->samplers[i], GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
||||||
glSamplerParameteri(texture->samplers[i], GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
||||||
glSamplerParameteri(texture->samplers[i], GL_TEXTURE_WRAP_S , GL_CLAMP_TO_EDGE);
|
|
||||||
glSamplerParameteri(texture->samplers[i], GL_TEXTURE_WRAP_T , GL_CLAMP_TO_EDGE);
|
|
||||||
|
|
||||||
glBindTexture(GL_TEXTURE_2D, texture->textures[i]);
|
for(int i = 0; i < (streaming ? TEXTURE_COUNT : 1); ++i)
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, texture->intFormat, texture->planes[i][0], texture->planes[i][1],
|
{
|
||||||
0, texture->format, texture->dataType, NULL);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
glBindTexture(GL_TEXTURE_2D, 0);
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
|
|
||||||
if (streaming)
|
if (streaming)
|
||||||
{
|
{
|
||||||
if (texture->hasPBO)
|
// release old PBOs and delete and re-create the buffers
|
||||||
|
for(int i = 0; i < TEXTURE_COUNT; ++i)
|
||||||
{
|
{
|
||||||
// release old PBOs and delete the buffers
|
if (texture->tex[i].hasPBO)
|
||||||
for(int i = 0; i < 2; ++i)
|
|
||||||
{
|
{
|
||||||
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, texture->pbo[i]);
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, texture->tex[i].pbo);
|
||||||
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
|
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
|
||||||
}
|
glDeleteBuffers(1, &texture->tex[i].pbo);
|
||||||
glDeleteBuffers(2, texture->pbo);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
glGenBuffers(2, texture->pbo);
|
glGenBuffers(1, &texture->tex[i].pbo);
|
||||||
texture->hasPBO = true;
|
texture->tex[i].hasPBO = true;
|
||||||
for(int i = 0; i < 2; ++i)
|
|
||||||
{
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, texture->tex[i].pbo);
|
||||||
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, texture->pbo[i]);
|
|
||||||
glBufferStorage(
|
glBufferStorage(
|
||||||
GL_PIXEL_UNPACK_BUFFER,
|
GL_PIXEL_UNPACK_BUFFER,
|
||||||
texture->pboBufferSize,
|
texture->pboBufferSize,
|
||||||
@ -230,18 +260,17 @@ bool egl_texture_setup(EGL_Texture * texture, enum EGL_PixelFormat pixFmt, size_
|
|||||||
GL_MAP_WRITE_BIT
|
GL_MAP_WRITE_BIT
|
||||||
);
|
);
|
||||||
|
|
||||||
texture->pboMap[i] = glMapBufferRange(
|
texture->tex[i].map = glMapBufferRange(
|
||||||
GL_PIXEL_UNPACK_BUFFER,
|
GL_PIXEL_UNPACK_BUFFER,
|
||||||
0,
|
0,
|
||||||
texture->pboBufferSize,
|
texture->pboBufferSize,
|
||||||
GL_MAP_PERSISTENT_BIT |
|
GL_MAP_PERSISTENT_BIT |
|
||||||
GL_MAP_WRITE_BIT |
|
GL_MAP_WRITE_BIT |
|
||||||
GL_MAP_UNSYNCHRONIZED_BIT |
|
GL_MAP_UNSYNCHRONIZED_BIT |
|
||||||
GL_MAP_INVALIDATE_BUFFER_BIT |
|
GL_MAP_INVALIDATE_BUFFER_BIT
|
||||||
GL_MAP_FLUSH_EXPLICIT_BIT
|
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!texture->pboMap[i])
|
if (!texture->tex[i].map)
|
||||||
{
|
{
|
||||||
EGL_ERROR("glMapBufferRange failed for %d of %lu bytes", i, texture->pboBufferSize);
|
EGL_ERROR("glMapBufferRange failed for %d of %lu bytes", i, texture->pboBufferSize);
|
||||||
return false;
|
return false;
|
||||||
@ -259,28 +288,31 @@ bool egl_texture_update(EGL_Texture * texture, const uint8_t * buffer)
|
|||||||
{
|
{
|
||||||
/* NOTE: DO NOT use any gl commands here as streaming must be thread safe */
|
/* NOTE: DO NOT use any gl commands here as streaming must be thread safe */
|
||||||
|
|
||||||
if (atomic_load_explicit(&texture->pboCount, memory_order_acquire) == 2)
|
union TexState s;
|
||||||
|
LG_LOCK(texture->lock);
|
||||||
|
s.v = texture->state.v;
|
||||||
|
LG_UNLOCK(texture->lock);
|
||||||
|
|
||||||
|
const uint8_t next = (s.w + 1) % TEXTURE_COUNT;
|
||||||
|
if (next == s.u)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
/* update the GPU buffer */
|
memcpy(texture->tex[s.w].map, buffer, texture->pboBufferSize);
|
||||||
memcpy(texture->pboMap[texture->pboWIndex], buffer, texture->pboBufferSize);
|
|
||||||
texture->pboSync[texture->pboWIndex] = 0;
|
|
||||||
|
|
||||||
if (++texture->pboWIndex == 2)
|
LG_LOCK(texture->lock);
|
||||||
texture->pboWIndex = 0;
|
texture->state.w = next;
|
||||||
|
LG_UNLOCK(texture->lock);
|
||||||
atomic_fetch_add_explicit(&texture->pboCount, 1, memory_order_release);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* Non streaming, this is NOT thread safe */
|
/* Non streaming, this is NOT thread safe */
|
||||||
|
|
||||||
for(int i = 0; i < texture->textureCount; ++i)
|
for(int p = 0; p < texture->planeCount; ++p)
|
||||||
{
|
{
|
||||||
glBindTexture(GL_TEXTURE_2D, texture->textures[i]);
|
glBindTexture(GL_TEXTURE_2D, texture->tex[0].t[p]);
|
||||||
glPixelStorei(GL_UNPACK_ROW_LENGTH, texture->planes[i][0]);
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, texture->planes[p][0]);
|
||||||
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, texture->planes[i][0], texture->planes[i][1],
|
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, texture->planes[p][0], texture->planes[p][1],
|
||||||
texture->format, texture->dataType, buffer + texture->offsets[i]);
|
texture->format, texture->dataType, buffer + texture->offsets[p]);
|
||||||
}
|
}
|
||||||
glBindTexture(GL_TEXTURE_2D, 0);
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
}
|
}
|
||||||
@ -292,12 +324,18 @@ bool egl_texture_update_from_frame(EGL_Texture * texture, const FrameBuffer * fr
|
|||||||
if (!texture->streaming)
|
if (!texture->streaming)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (atomic_load_explicit(&texture->pboCount, memory_order_acquire) == 2)
|
union TexState s;
|
||||||
|
LG_LOCK(texture->lock);
|
||||||
|
s.v = texture->state.v;
|
||||||
|
LG_UNLOCK(texture->lock);
|
||||||
|
|
||||||
|
const uint8_t next = (s.w + 1) % TEXTURE_COUNT;
|
||||||
|
if (next == s.u)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
framebuffer_read(
|
framebuffer_read(
|
||||||
frame,
|
frame,
|
||||||
texture->pboMap[texture->pboWIndex],
|
texture->tex[s.w].map,
|
||||||
texture->stride,
|
texture->stride,
|
||||||
texture->height,
|
texture->height,
|
||||||
texture->width,
|
texture->width,
|
||||||
@ -305,12 +343,10 @@ bool egl_texture_update_from_frame(EGL_Texture * texture, const FrameBuffer * fr
|
|||||||
texture->stride
|
texture->stride
|
||||||
);
|
);
|
||||||
|
|
||||||
texture->pboSync[texture->pboWIndex] = 0;
|
LG_LOCK(texture->lock);
|
||||||
|
texture->state.w = next;
|
||||||
|
LG_UNLOCK(texture->lock);
|
||||||
|
|
||||||
if (++texture->pboWIndex == 2)
|
|
||||||
texture->pboWIndex = 0;
|
|
||||||
|
|
||||||
atomic_fetch_add_explicit(&texture->pboCount, 1, memory_order_release);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -319,65 +355,35 @@ enum EGL_TexStatus egl_texture_process(EGL_Texture * texture)
|
|||||||
if (!texture->streaming)
|
if (!texture->streaming)
|
||||||
return EGL_TEX_STATUS_OK;
|
return EGL_TEX_STATUS_OK;
|
||||||
|
|
||||||
int pboCount = atomic_load_explicit(&texture->pboCount, memory_order_acquire);
|
union TexState s;
|
||||||
if (pboCount == 0)
|
LG_LOCK(texture->lock);
|
||||||
|
s.v = texture->state.v;
|
||||||
|
LG_UNLOCK(texture->lock);
|
||||||
|
|
||||||
|
const uint8_t nextu = (s.u + 1) % TEXTURE_COUNT;
|
||||||
|
if (s.u == s.w || nextu == s.s || nextu == s.d)
|
||||||
return texture->ready ? EGL_TEX_STATUS_OK : EGL_TEX_STATUS_NOTREADY;
|
return texture->ready ? EGL_TEX_STATUS_OK : EGL_TEX_STATUS_NOTREADY;
|
||||||
|
|
||||||
/* process any buffers that have not yet been flushed */
|
/* update the texture */
|
||||||
int pos = texture->pboRIndex;
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, texture->tex[s.u].pbo);
|
||||||
for(int i = 0; i < pboCount; ++i)
|
for(int p = 0; p < texture->planeCount; ++p)
|
||||||
{
|
{
|
||||||
if (texture->pboSync[pos] == 0)
|
glBindTexture(GL_TEXTURE_2D, texture->tex[s.u].t[p]);
|
||||||
{
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, texture->planes[p][2]);
|
||||||
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, texture->pbo[pos]);
|
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, texture->planes[p][0], texture->planes[p][1],
|
||||||
glFlushMappedBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, texture->pboBufferSize);
|
texture->format, texture->dataType, (const void *)texture->offsets[p]);
|
||||||
texture->pboSync[pos] = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (++pos == 2)
|
|
||||||
pos = 0;
|
|
||||||
}
|
}
|
||||||
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
|
||||||
|
|
||||||
/* wait for the buffer to be ready */
|
/* create a fence to prevent usage before the update is complete */
|
||||||
pos = texture->pboRIndex;
|
texture->tex[s.u].sync =
|
||||||
switch(glClientWaitSync(texture->pboSync[pos], 0, 0))
|
glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
|
||||||
{
|
|
||||||
case GL_ALREADY_SIGNALED:
|
|
||||||
case GL_CONDITION_SATISFIED:
|
|
||||||
break;
|
|
||||||
|
|
||||||
case GL_TIMEOUT_EXPIRED:
|
LG_LOCK(texture->lock);
|
||||||
return texture->ready ? EGL_TEX_STATUS_OK : EGL_TEX_STATUS_NOTREADY;
|
texture->state.u = nextu;
|
||||||
|
LG_UNLOCK(texture->lock);
|
||||||
|
|
||||||
case GL_WAIT_FAILED:
|
|
||||||
glDeleteSync(texture->pboSync[pos]);
|
|
||||||
EGL_ERROR("glClientWaitSync failed");
|
|
||||||
return EGL_TEX_STATUS_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* delete the sync and bind the buffer */
|
|
||||||
glDeleteSync(texture->pboSync[pos]);
|
|
||||||
texture->pboSync[pos] = 0;
|
|
||||||
|
|
||||||
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, texture->pbo[pos]);
|
|
||||||
|
|
||||||
/* update the textures */
|
|
||||||
for(int i = 0; i < texture->textureCount; ++i)
|
|
||||||
{
|
|
||||||
glBindTexture(GL_TEXTURE_2D, texture->textures[i]);
|
|
||||||
glPixelStorei(GL_UNPACK_ROW_LENGTH, texture->planes[i][2]);
|
|
||||||
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, texture->planes[i][0], texture->planes[i][1],
|
|
||||||
texture->format, texture->dataType, (const void *)texture->offsets[i]);
|
|
||||||
}
|
|
||||||
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
|
|
||||||
glBindTexture(GL_TEXTURE_2D, 0);
|
|
||||||
|
|
||||||
/* advance the read index */
|
|
||||||
if (++texture->pboRIndex == 2)
|
|
||||||
texture->pboRIndex = 0;
|
|
||||||
|
|
||||||
atomic_fetch_sub_explicit(&texture->pboCount, 1, memory_order_release);
|
|
||||||
texture->ready = true;
|
texture->ready = true;
|
||||||
|
|
||||||
return EGL_TEX_STATUS_OK;
|
return EGL_TEX_STATUS_OK;
|
||||||
@ -385,14 +391,47 @@ enum EGL_TexStatus egl_texture_process(EGL_Texture * texture)
|
|||||||
|
|
||||||
enum EGL_TexStatus egl_texture_bind(EGL_Texture * texture)
|
enum EGL_TexStatus egl_texture_bind(EGL_Texture * texture)
|
||||||
{
|
{
|
||||||
/* if there are no new buffers ready, then just bind the textures */
|
union TexState s;
|
||||||
if (texture->streaming && !texture->ready)
|
LG_LOCK(texture->lock);
|
||||||
|
s.v = texture->state.v;
|
||||||
|
LG_UNLOCK(texture->lock);
|
||||||
|
|
||||||
|
if (texture->streaming)
|
||||||
|
{
|
||||||
|
if (!texture->ready)
|
||||||
return EGL_TEX_STATUS_NOTREADY;
|
return EGL_TEX_STATUS_NOTREADY;
|
||||||
|
|
||||||
for(int i = 0; i < texture->textureCount; ++i)
|
if (texture->tex[s.s].sync != 0)
|
||||||
|
{
|
||||||
|
switch(glClientWaitSync(texture->tex[s.s].sync, 0, 0))
|
||||||
|
{
|
||||||
|
case GL_ALREADY_SIGNALED:
|
||||||
|
case GL_CONDITION_SATISFIED:
|
||||||
|
glDeleteSync(texture->tex[s.s].sync);
|
||||||
|
texture->tex[s.s].sync = 0;
|
||||||
|
|
||||||
|
LG_LOCK(texture->lock);
|
||||||
|
texture->state.d = texture->state.s;
|
||||||
|
texture->state.s = (s.s + 1) % TEXTURE_COUNT;
|
||||||
|
LG_UNLOCK(texture->lock);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case GL_TIMEOUT_EXPIRED:
|
||||||
|
break;
|
||||||
|
|
||||||
|
case GL_WAIT_FAILED:
|
||||||
|
glDeleteSync(texture->tex[s.s].sync);
|
||||||
|
texture->tex[s.s].sync = 0;
|
||||||
|
EGL_ERROR("glClientWaitSync failed");
|
||||||
|
return EGL_TEX_STATUS_ERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i = 0; i < texture->planeCount; ++i)
|
||||||
{
|
{
|
||||||
glActiveTexture(GL_TEXTURE0 + i);
|
glActiveTexture(GL_TEXTURE0 + i);
|
||||||
glBindTexture(GL_TEXTURE_2D, texture->textures[i]);
|
glBindTexture(GL_TEXTURE_2D, texture->tex[s.d].t[i]);
|
||||||
glBindSampler(i, texture->samplers[i]);
|
glBindSampler(i, texture->samplers[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -401,5 +440,5 @@ enum EGL_TexStatus egl_texture_bind(EGL_Texture * texture)
|
|||||||
|
|
||||||
int egl_texture_count(EGL_Texture * texture)
|
int egl_texture_count(EGL_Texture * texture)
|
||||||
{
|
{
|
||||||
return texture->textureCount;
|
return texture->planeCount;
|
||||||
}
|
}
|
||||||
|
@ -156,7 +156,7 @@ static int renderThread(void * unused)
|
|||||||
// if our clock is too far out of sync, resync it
|
// if our clock is too far out of sync, resync it
|
||||||
// this can happen when switching to/from a TTY, or due to clock drift
|
// this can happen when switching to/from a TTY, or due to clock drift
|
||||||
// we only check this once every 100 frames
|
// we only check this once every 100 frames
|
||||||
if (++resyncCheck == 100)
|
if (state.frameTime > 0 && ++resyncCheck == 100)
|
||||||
{
|
{
|
||||||
resyncCheck = 0;
|
resyncCheck = 0;
|
||||||
|
|
||||||
@ -209,6 +209,18 @@ static int renderThread(void * unused)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!state.resizeDone && state.resizeTimeout < microtime())
|
||||||
|
{
|
||||||
|
SDL_SetWindowSize(
|
||||||
|
state.window,
|
||||||
|
state.dstRect.w,
|
||||||
|
state.dstRect.h
|
||||||
|
);
|
||||||
|
state.resizeDone = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (state.frameTime > 0)
|
||||||
|
{
|
||||||
uint64_t nsec = time.tv_nsec + state.frameTime;
|
uint64_t nsec = time.tv_nsec + state.frameTime;
|
||||||
if (nsec > 1e9)
|
if (nsec > 1e9)
|
||||||
{
|
{
|
||||||
@ -219,15 +231,6 @@ static int renderThread(void * unused)
|
|||||||
time.tv_nsec = nsec;
|
time.tv_nsec = nsec;
|
||||||
|
|
||||||
clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &time, NULL);
|
clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &time, NULL);
|
||||||
|
|
||||||
if (!state.resizeDone && state.resizeTimeout < microtime())
|
|
||||||
{
|
|
||||||
SDL_SetWindowSize(
|
|
||||||
state.window,
|
|
||||||
state.dstRect.w,
|
|
||||||
state.dstRect.h
|
|
||||||
);
|
|
||||||
state.resizeDone = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user