Two faults found on the first run against a real DXGI multi-monitor host, each of which left the composed view black while every geometry check passed: - Compose setup ran on the frame thread via onFrameFormat, but the VAO and FBO it creates are container objects that only exist in the creating context. The render thread drew with empty ones. Setup is now deferred to the render thread and picked up before the next draw (composeSetupPending), and a post-process pass is forced afterwards so a frame that arrived before setup completed is not presented stale. - A DMA-imported atlas is an external image and must be sampled with samplerExternalOES. Compose only compiled the plain sampler2D variant, so the DMA path sampled black. Both variants are now compiled and setup selects one, mirroring what the desktop shaders already do. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WxYTZ8oBdHX3YrHGgdLKwC
302 lines
9.0 KiB
C
302 lines
9.0 KiB
C
/**
|
|
* Looking Glass
|
|
* Copyright © 2017-2026 The Looking Glass Authors
|
|
* 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
|
|
*/
|
|
|
|
#include "compose.h"
|
|
|
|
#include "framebuffer.h"
|
|
#include "shader.h"
|
|
#include "state.h"
|
|
#include "texture.h"
|
|
|
|
#include "common/debug.h"
|
|
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include <GLES3/gl3.h>
|
|
|
|
#include "compose.vert.h"
|
|
#include "compose.frag.h"
|
|
|
|
/* Two triangles per display, as an indexed quad. */
|
|
#define VERTS_PER_DISPLAY 4
|
|
#define INDICES_PER_DISPLAY 6
|
|
/* each vertex is x, y, u, v */
|
|
#define FLOATS_PER_VERTEX 4
|
|
|
|
struct EGL_Compose
|
|
{
|
|
EGL_Framebuffer * framebuffer;
|
|
/* [0] samples a plain texture, [1] an imported DMA buffer, which needs the
|
|
* external image sampler. Which one runs is chosen at setup. */
|
|
EGL_Shader * shader [2];
|
|
EGL_Uniform * uViewSize [2];
|
|
EGL_Uniform * uAtlasSize[2];
|
|
bool useDMA;
|
|
|
|
GLuint vao;
|
|
GLuint buffers[2];
|
|
|
|
bool configured;
|
|
unsigned int viewWidth, viewHeight;
|
|
unsigned int atlasWidth, atlasHeight;
|
|
int quadCount;
|
|
};
|
|
|
|
bool egl_composeInit(EGL_Compose ** compose)
|
|
{
|
|
*compose = calloc(1, sizeof(**compose));
|
|
if (!*compose)
|
|
{
|
|
DEBUG_ERROR("Failed to allocate the display composition");
|
|
return false;
|
|
}
|
|
|
|
EGL_Compose * this = *compose;
|
|
|
|
if (!egl_framebufferInit(&this->framebuffer))
|
|
{
|
|
DEBUG_ERROR("Failed to initialize the display composition");
|
|
egl_composeFree(compose);
|
|
return false;
|
|
}
|
|
|
|
for(int i = 0; i < 2; ++i)
|
|
{
|
|
if (!egl_shaderInit(&this->shader[i]) ||
|
|
!egl_shaderCompile(this->shader[i],
|
|
b_shader_compose_vert, b_shader_compose_vert_size,
|
|
b_shader_compose_frag, b_shader_compose_frag_size,
|
|
i == 1, NULL))
|
|
{
|
|
DEBUG_ERROR("Failed to initialize the display composition");
|
|
egl_composeFree(compose);
|
|
return false;
|
|
}
|
|
|
|
this->uViewSize [i] = egl_shaderGetUniform(this->shader[i], "viewSize" );
|
|
this->uAtlasSize[i] = egl_shaderGetUniform(this->shader[i], "atlasSize");
|
|
}
|
|
|
|
glGenVertexArrays(1, &this->vao);
|
|
egl_stateBindVertexArray(this->vao);
|
|
|
|
glGenBuffers(2, this->buffers);
|
|
egl_stateBindBuffer(GL_ARRAY_BUFFER, this->buffers[0]);
|
|
glBufferData(GL_ARRAY_BUFFER,
|
|
KVMFR_MAX_DISPLAYS * VERTS_PER_DISPLAY * FLOATS_PER_VERTEX *
|
|
sizeof(GLfloat),
|
|
NULL, GL_STREAM_DRAW);
|
|
|
|
// location 0 is the view position, location 1 the atlas position
|
|
glEnableVertexAttribArray(0);
|
|
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE,
|
|
FLOATS_PER_VERTEX * sizeof(GLfloat), (void *)0);
|
|
glEnableVertexAttribArray(1);
|
|
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
|
|
FLOATS_PER_VERTEX * sizeof(GLfloat),
|
|
(void *)(2 * sizeof(GLfloat)));
|
|
|
|
GLushort indices[KVMFR_MAX_DISPLAYS * INDICES_PER_DISPLAY];
|
|
for(int i = 0; i < KVMFR_MAX_DISPLAYS; ++i)
|
|
{
|
|
indices[INDICES_PER_DISPLAY * i + 0] = VERTS_PER_DISPLAY * i + 0;
|
|
indices[INDICES_PER_DISPLAY * i + 1] = VERTS_PER_DISPLAY * i + 1;
|
|
indices[INDICES_PER_DISPLAY * i + 2] = VERTS_PER_DISPLAY * i + 2;
|
|
indices[INDICES_PER_DISPLAY * i + 3] = VERTS_PER_DISPLAY * i + 0;
|
|
indices[INDICES_PER_DISPLAY * i + 4] = VERTS_PER_DISPLAY * i + 2;
|
|
indices[INDICES_PER_DISPLAY * i + 5] = VERTS_PER_DISPLAY * i + 3;
|
|
}
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->buffers[1]);
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof indices, indices,
|
|
GL_STATIC_DRAW);
|
|
|
|
return true;
|
|
}
|
|
|
|
void egl_composeFree(EGL_Compose ** compose)
|
|
{
|
|
if (!*compose)
|
|
return;
|
|
|
|
EGL_Compose * this = *compose;
|
|
|
|
if (this->vao)
|
|
{
|
|
glDeleteVertexArrays(1, &this->vao);
|
|
glDeleteBuffers(2, this->buffers);
|
|
egl_stateInvalidateShared();
|
|
}
|
|
|
|
egl_shaderFree(&this->shader[0]);
|
|
egl_shaderFree(&this->shader[1]);
|
|
egl_framebufferFree(&this->framebuffer);
|
|
free(this);
|
|
*compose = NULL;
|
|
}
|
|
|
|
bool egl_composeSetup(EGL_Compose * this, const LG_RendererFormat * format,
|
|
bool useDMA)
|
|
{
|
|
this->configured = false;
|
|
this->quadCount = 0;
|
|
this->useDMA = useDMA;
|
|
|
|
if (!format->displayCount)
|
|
return false;
|
|
|
|
if (format->displayCount > KVMFR_MAX_DISPLAYS)
|
|
{
|
|
DEBUG_ERROR("Frame reports %u displays, the maximum is %d",
|
|
format->displayCount, KVMFR_MAX_DISPLAYS);
|
|
return false;
|
|
}
|
|
|
|
if (!format->viewWidth || !format->viewHeight)
|
|
{
|
|
DEBUG_ERROR("Frame reports an empty view region");
|
|
return false;
|
|
}
|
|
|
|
/* The composed image is the same pixel format as the atlas, so a display is
|
|
* copied without conversion and HDR handling downstream is unaffected. */
|
|
enum EGL_PixelFormat pixFmt;
|
|
switch(format->type)
|
|
{
|
|
case FRAME_TYPE_BGRA : pixFmt = EGL_PF_BGRA ; break;
|
|
case FRAME_TYPE_RGBA : pixFmt = EGL_PF_RGBA ; break;
|
|
case FRAME_TYPE_RGBA10 : pixFmt = EGL_PF_RGBA10 ; break;
|
|
case FRAME_TYPE_RGBA16F: pixFmt = EGL_PF_RGBA16F; break;
|
|
|
|
default:
|
|
DEBUG_ERROR("Frame type %d cannot be composed", format->type);
|
|
return false;
|
|
}
|
|
|
|
if (!egl_framebufferSetup(this->framebuffer, pixFmt,
|
|
format->viewWidth, format->viewHeight))
|
|
{
|
|
DEBUG_ERROR("Failed to create the %ux%u composition target",
|
|
format->viewWidth, format->viewHeight);
|
|
return false;
|
|
}
|
|
|
|
this->viewWidth = format->viewWidth;
|
|
this->viewHeight = format->viewHeight;
|
|
this->atlasWidth = format->frameWidth;
|
|
this->atlasHeight = format->frameHeight;
|
|
|
|
/* Build one quad per display that intersects the view, mapping its atlas
|
|
* rectangle onto its place in the view. Displays fully outside the view are
|
|
* skipped, which is what makes selecting a single display cost nothing
|
|
* extra to draw. */
|
|
GLfloat verts[KVMFR_MAX_DISPLAYS * VERTS_PER_DISPLAY * FLOATS_PER_VERTEX];
|
|
int quads = 0;
|
|
|
|
for(unsigned i = 0; i < format->displayCount; ++i)
|
|
{
|
|
const KVMFRDisplay * d = &format->displays[i];
|
|
|
|
// position within the view, which may be a single display's region
|
|
const int vx = d->desktopX - format->viewX;
|
|
const int vy = d->desktopY - format->viewY;
|
|
|
|
if (vx + (int)d->desktopWidth <= 0 ||
|
|
vy + (int)d->desktopHeight <= 0 ||
|
|
vx >= (int)format->viewWidth ||
|
|
vy >= (int)format->viewHeight)
|
|
continue;
|
|
|
|
if (d->atlasX + d->atlasWidth > format->frameWidth ||
|
|
d->atlasY + d->atlasHeight > format->frameHeight)
|
|
{
|
|
DEBUG_ERROR("Display %u (%s) lies outside the %ux%u frame",
|
|
i, d->name, format->frameWidth, format->frameHeight);
|
|
return false;
|
|
}
|
|
|
|
const GLfloat x0 = vx, y0 = vy;
|
|
const GLfloat x1 = vx + (GLfloat)d->desktopWidth;
|
|
const GLfloat y1 = vy + (GLfloat)d->desktopHeight;
|
|
|
|
const GLfloat u0 = d->atlasX, v0 = d->atlasY;
|
|
const GLfloat u1 = d->atlasX + (GLfloat)d->atlasWidth;
|
|
const GLfloat v1 = d->atlasY + (GLfloat)d->atlasHeight;
|
|
|
|
GLfloat * v = verts + quads * VERTS_PER_DISPLAY * FLOATS_PER_VERTEX;
|
|
v[ 0] = x0; v[ 1] = y0; v[ 2] = u0; v[ 3] = v0;
|
|
v[ 4] = x1; v[ 5] = y0; v[ 6] = u1; v[ 7] = v0;
|
|
v[ 8] = x1; v[ 9] = y1; v[10] = u1; v[11] = v1;
|
|
v[12] = x0; v[13] = y1; v[14] = u0; v[15] = v1;
|
|
++quads;
|
|
}
|
|
|
|
if (!quads)
|
|
{
|
|
DEBUG_ERROR("No display intersects the requested view region");
|
|
return false;
|
|
}
|
|
|
|
egl_stateBindVertexArray(this->vao);
|
|
egl_stateBindBuffer(GL_ARRAY_BUFFER, this->buffers[0]);
|
|
glBufferSubData(GL_ARRAY_BUFFER, 0,
|
|
quads * VERTS_PER_DISPLAY * FLOATS_PER_VERTEX * sizeof(GLfloat), verts);
|
|
|
|
this->quadCount = quads;
|
|
this->configured = true;
|
|
return true;
|
|
}
|
|
|
|
EGL_Texture * egl_composeRun(EGL_Compose * this, EGL_Texture * atlas)
|
|
{
|
|
if (!this->configured)
|
|
return NULL;
|
|
|
|
egl_framebufferBind(this->framebuffer);
|
|
|
|
/* Displays never overlap and the view is not guaranteed to be fully
|
|
* covered, so clear first to keep any gap between monitors black rather
|
|
* than showing the previous frame there. */
|
|
egl_stateBlend(false);
|
|
egl_stateScissor(false);
|
|
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
const int variant = this->useDMA ? 1 : 0;
|
|
egl_textureBind(atlas);
|
|
egl_uniform2f(this->uViewSize [variant], this->viewWidth , this->viewHeight );
|
|
egl_uniform2f(this->uAtlasSize[variant], this->atlasWidth, this->atlasHeight);
|
|
egl_shaderUse(this->shader[variant]);
|
|
|
|
egl_stateBindVertexArray(this->vao);
|
|
glDrawElements(GL_TRIANGLES, this->quadCount * INDICES_PER_DISPLAY,
|
|
GL_UNSIGNED_SHORT, NULL);
|
|
|
|
return egl_framebufferGetTexture(this->framebuffer);
|
|
}
|
|
|
|
void egl_composeGetSize(EGL_Compose * this,
|
|
unsigned int * width, unsigned int * height)
|
|
{
|
|
*width = this->viewWidth;
|
|
*height = this->viewHeight;
|
|
}
|