mirror of
https://github.com/gnif/LookingGlass.git
synced 2025-08-06 10:44:01 +00:00
[client] egl: cleanup texture filtering/post-processing
This commit is contained in:
@@ -54,8 +54,8 @@ typedef struct RenderStep
|
||||
}
|
||||
RenderStep;
|
||||
|
||||
bool egl_textureInit(EGL * egl, EGL_Texture ** texture_,
|
||||
EGLDisplay * display, EGL_TexType type, bool streaming)
|
||||
bool egl_textureInit(EGL_Texture ** texture_, EGLDisplay * display,
|
||||
EGL_TexType type, bool streaming)
|
||||
{
|
||||
const EGL_TextureOps * ops;
|
||||
|
||||
@@ -85,7 +85,6 @@ bool egl_textureInit(EGL * egl, EGL_Texture ** texture_,
|
||||
|
||||
EGL_Texture * this = *texture_;
|
||||
memcpy(&this->ops, ops, sizeof(*ops));
|
||||
this->egl = egl;
|
||||
|
||||
glGenSamplers(1, &this->sampler);
|
||||
glSamplerParameteri(this->sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
@@ -99,52 +98,12 @@ void egl_textureFree(EGL_Texture ** tex)
|
||||
{
|
||||
EGL_Texture * this = *tex;
|
||||
|
||||
if (this->render)
|
||||
{
|
||||
RenderStep * step;
|
||||
while(ll_shift(this->render, (void **)&step))
|
||||
{
|
||||
if (step->fb)
|
||||
glDeleteFramebuffers(1, &step->fb);
|
||||
|
||||
glDeleteTextures(1, &step->tex);
|
||||
free(step);
|
||||
}
|
||||
ll_free(this->render);
|
||||
egl_modelFree(&this->model);
|
||||
ringbuffer_free(&this->textures);
|
||||
free(this->bindData);
|
||||
}
|
||||
|
||||
glDeleteSamplers(1, &this->sampler);
|
||||
|
||||
this->ops.free(this);
|
||||
*tex = NULL;
|
||||
}
|
||||
|
||||
bool setupRenderStep(EGL_Texture * this, RenderStep * step)
|
||||
{
|
||||
if (step->ready && (step->width > 0 || step->height > 0))
|
||||
return true;
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, step->tex);
|
||||
glTexImage2D(GL_TEXTURE_2D,
|
||||
0,
|
||||
this->format.intFormat,
|
||||
step->width > 0 ? step->width : this->format.width,
|
||||
step->height > 0 ? step->height : this->format.height,
|
||||
0,
|
||||
this->format.format,
|
||||
this->format.dataType,
|
||||
NULL);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
|
||||
step->ready = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool egl_textureSetup(EGL_Texture * this, enum EGL_PixelFormat pixFmt,
|
||||
size_t width, size_t height, size_t stride)
|
||||
{
|
||||
@@ -155,21 +114,10 @@ bool egl_textureSetup(EGL_Texture * this, enum EGL_PixelFormat pixFmt,
|
||||
.height = height,
|
||||
.stride = stride
|
||||
};
|
||||
this->size = height * stride;
|
||||
|
||||
if (!egl_texUtilGetFormat(&setup, &this->format))
|
||||
return false;
|
||||
|
||||
this->formatValid = true;
|
||||
|
||||
/* reconfigure any intermediate render steps */
|
||||
if (this->render)
|
||||
{
|
||||
RenderStep * step;
|
||||
for(ll_reset(this->render); ll_walk(this->render, (void **)&step); )
|
||||
step->ready = false;
|
||||
}
|
||||
|
||||
return this->ops.setup(this, &setup);
|
||||
}
|
||||
|
||||
@@ -181,13 +129,7 @@ bool egl_textureUpdate(EGL_Texture * this, const uint8_t * buffer)
|
||||
.buffer = buffer
|
||||
};
|
||||
|
||||
if (this->ops.update(this, &update))
|
||||
{
|
||||
atomic_store(&this->updated, true);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
return this->ops.update(this, &update);
|
||||
}
|
||||
|
||||
bool egl_textureUpdateFromFrame(EGL_Texture * this,
|
||||
@@ -202,13 +144,7 @@ bool egl_textureUpdateFromFrame(EGL_Texture * this,
|
||||
.rectCount = damageRectsCount,
|
||||
};
|
||||
|
||||
if (this->ops.update(this, &update))
|
||||
{
|
||||
atomic_store(&this->updated, true);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
return this->ops.update(this, &update);
|
||||
}
|
||||
|
||||
bool egl_textureUpdateFromDMA(EGL_Texture * this,
|
||||
@@ -221,54 +157,14 @@ bool egl_textureUpdateFromDMA(EGL_Texture * this,
|
||||
};
|
||||
|
||||
/* wait for completion */
|
||||
framebuffer_wait(frame, this->size);
|
||||
framebuffer_wait(frame, this->format.bufferSize);
|
||||
|
||||
if (this->ops.update(this, &update))
|
||||
{
|
||||
atomic_store(&this->updated, true);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
return this->ops.update(this, &update);
|
||||
}
|
||||
|
||||
enum EGL_TexStatus egl_textureProcess(EGL_Texture * this)
|
||||
{
|
||||
EGL_TexStatus status;
|
||||
if ((status = this->ops.process(this)) == EGL_TEX_STATUS_OK)
|
||||
{
|
||||
if (atomic_exchange(&this->updated, false))
|
||||
this->postProcessed = false;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
typedef struct BindInfo
|
||||
{
|
||||
GLuint tex;
|
||||
unsigned int width;
|
||||
unsigned int height;
|
||||
}
|
||||
BindInfo;
|
||||
|
||||
typedef struct BindData
|
||||
{
|
||||
GLuint sampler;
|
||||
GLuint dimensions[];
|
||||
}
|
||||
BindData;
|
||||
|
||||
static bool rbBindTexture(int index, void * value, void * udata)
|
||||
{
|
||||
BindInfo * bi = (BindInfo *)value;
|
||||
BindData * bd = (BindData *)udata;
|
||||
|
||||
glActiveTexture(GL_TEXTURE0 + index);
|
||||
glBindTexture(GL_TEXTURE_2D, bi->tex);
|
||||
glBindSampler(index, bd->sampler);
|
||||
bd->dimensions[index * 2 + 0] = bi->width;
|
||||
bd->dimensions[index * 2 + 1] = bi->height;
|
||||
return true;
|
||||
return this->ops.process(this);
|
||||
}
|
||||
|
||||
enum EGL_TexStatus egl_textureBind(EGL_Texture * this)
|
||||
@@ -276,206 +172,11 @@ enum EGL_TexStatus egl_textureBind(EGL_Texture * this)
|
||||
GLuint tex;
|
||||
EGL_TexStatus status;
|
||||
|
||||
if (!this->render)
|
||||
{
|
||||
if ((status = this->ops.get(this, &tex)) != EGL_TEX_STATUS_OK)
|
||||
return status;
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, tex);
|
||||
glBindSampler(0, this->sampler);
|
||||
return EGL_TEX_STATUS_OK;
|
||||
}
|
||||
|
||||
if (this->bindDataSize < ll_count(this->render))
|
||||
{
|
||||
free(this->bindData);
|
||||
|
||||
BindData * bd = (BindData *)calloc(1, sizeof(struct BindData) +
|
||||
sizeof(bd->dimensions[0]) * (ll_count(this->render)+1) * 2);
|
||||
bd->sampler = this->sampler;
|
||||
|
||||
this->bindData = bd;
|
||||
this->bindDataSize = ll_count(this->render);
|
||||
}
|
||||
|
||||
BindData * bd = (BindData *)this->bindData;
|
||||
RenderStep * step;
|
||||
|
||||
/* if the postProcessing has not yet been done */
|
||||
if (!this->postProcessed)
|
||||
{
|
||||
ringbuffer_reset(this->textures);
|
||||
|
||||
/* configure all the filters */
|
||||
for(ll_reset(this->render); ll_walk(this->render, (void **)&step); )
|
||||
{
|
||||
if (!step->enabled)
|
||||
continue;
|
||||
|
||||
if (!step->ready)
|
||||
setupRenderStep(this, step);
|
||||
}
|
||||
|
||||
if ((status = this->ops.get(this, &tex)) != EGL_TEX_STATUS_OK)
|
||||
return status;
|
||||
|
||||
struct Rect finalSz =
|
||||
{
|
||||
.x = this->format.width,
|
||||
.y = this->format.height
|
||||
};
|
||||
|
||||
ringbuffer_push(this->textures, &(BindInfo) {
|
||||
.tex = tex,
|
||||
.width = this->format.width,
|
||||
.height = this->format.height
|
||||
});
|
||||
|
||||
ringbuffer_forEach(this->textures, rbBindTexture, bd, true);
|
||||
|
||||
bool cleanup = false;
|
||||
for(ll_reset(this->render); ll_walk(this->render, (void **)&step); )
|
||||
{
|
||||
if (!step->enabled)
|
||||
continue;
|
||||
|
||||
cleanup = true;
|
||||
|
||||
/* create the framebuffer here as it must be in the same gl context as
|
||||
* it's usage */
|
||||
if (!step->fb)
|
||||
{
|
||||
glGenFramebuffers(1, &step->fb);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, step->fb);
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
|
||||
GL_TEXTURE_2D, step->tex, 0);
|
||||
glDrawBuffers(1, &(GLenum){GL_COLOR_ATTACHMENT0});
|
||||
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
|
||||
{
|
||||
DEBUG_ERROR("Failed to setup the shader framebuffer");
|
||||
return EGL_TEX_STATUS_ERROR;
|
||||
}
|
||||
}
|
||||
else
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, step->fb);
|
||||
|
||||
const struct Rect sz =
|
||||
{
|
||||
.x = step->width > 0 ? step->width : this->format.width,
|
||||
.y = step->height > 0 ? step->height : this->format.height
|
||||
};
|
||||
|
||||
glViewport(0, 0, sz.x, sz.y);
|
||||
|
||||
/* use the shader (also configures it's set uniforms) */
|
||||
egl_shaderUse(step->shader);
|
||||
|
||||
/* set the size uniforms */
|
||||
glUniform2uiv(step->uInRes,
|
||||
ringbuffer_getCount(this->textures), bd->dimensions);
|
||||
glUniform2ui(step->uOutRes, sz.x, sz.y);
|
||||
|
||||
/* render the scene */
|
||||
egl_modelRender(this->model);
|
||||
finalSz.x = sz.x;
|
||||
finalSz.y = sz.y;
|
||||
|
||||
/* push the details into the ringbuffer for the next pass */
|
||||
ringbuffer_push(this->textures, &(BindInfo) {
|
||||
.tex = step->tex,
|
||||
.width = sz.x,
|
||||
.height = sz.y
|
||||
});
|
||||
|
||||
/* bind the textures for the next pass */
|
||||
ringbuffer_forEach(this->textures, rbBindTexture, bd, true);
|
||||
}
|
||||
|
||||
/* restore the state and the viewport */
|
||||
if (cleanup)
|
||||
{
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
glUseProgram(0);
|
||||
egl_resetViewport(this->egl);
|
||||
}
|
||||
|
||||
this->finalWidth = finalSz.x;
|
||||
this->finalHeight = finalSz.y;
|
||||
this->postProcessed = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* bind the last texture */
|
||||
BindInfo * bi = (BindInfo *)ringBuffer_getLastValue(this->textures);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, bi->tex);
|
||||
glBindSampler(0, this->sampler);
|
||||
}
|
||||
if ((status = this->ops.get(this, &tex)) != EGL_TEX_STATUS_OK)
|
||||
return status;
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, tex);
|
||||
glBindSampler(0, this->sampler);
|
||||
return EGL_TEX_STATUS_OK;
|
||||
}
|
||||
|
||||
PostProcessHandle egl_textureAddFilter(EGL_Texture * this, EGL_Shader * shader,
|
||||
bool enabled)
|
||||
{
|
||||
if (!this->render)
|
||||
{
|
||||
this->render = ll_new();
|
||||
egl_modelInit(&this->model);
|
||||
egl_modelSetDefault(this->model, false);
|
||||
this->textures = ringbuffer_new(8, sizeof(BindInfo));
|
||||
}
|
||||
|
||||
RenderStep * step = calloc(1, sizeof(*step));
|
||||
glGenTextures(1, &step->tex);
|
||||
step->owner = this;
|
||||
step->shader = shader;
|
||||
step->uInRes = egl_shaderGetUniform(shader, "uInRes" );
|
||||
step->uOutRes = egl_shaderGetUniform(shader, "uOutRes");
|
||||
step->enabled = enabled;
|
||||
|
||||
ll_push(this->render, step);
|
||||
return (PostProcessHandle)step;
|
||||
}
|
||||
|
||||
void egl_textureEnableFilter(PostProcessHandle * handle, bool enable)
|
||||
{
|
||||
RenderStep * step = (RenderStep *)handle;
|
||||
if (step->enabled == enable)
|
||||
return;
|
||||
|
||||
step->enabled = enable;
|
||||
egl_textureInvalidate(step->owner);
|
||||
}
|
||||
|
||||
void egl_textureSetFilterRes(PostProcessHandle * handle,
|
||||
unsigned int x, unsigned int y)
|
||||
{
|
||||
RenderStep * step = (RenderStep *)handle;
|
||||
if (step->width == x && step->height == y)
|
||||
return;
|
||||
|
||||
step->width = x;
|
||||
step->height = y;
|
||||
step->ready = false;
|
||||
egl_textureInvalidate(step->owner);
|
||||
}
|
||||
|
||||
void egl_textureInvalidate(EGL_Texture * texture)
|
||||
{
|
||||
texture->postProcessed = false;
|
||||
}
|
||||
|
||||
void egl_textureGetFinalSize(EGL_Texture * this, struct Rect * rect)
|
||||
{
|
||||
if (!this->render)
|
||||
{
|
||||
rect->x = this->format.width;
|
||||
rect->y = this->format.height;
|
||||
return;
|
||||
}
|
||||
|
||||
rect->x = this->finalWidth;
|
||||
rect->y = this->finalHeight;
|
||||
}
|
||||
|
Reference in New Issue
Block a user