Draw video if everything bypassed

This commit is contained in:
Aiden
2026-05-12 18:41:48 +10:00
parent 0a8b335048
commit 957c0be05a
3 changed files with 140 additions and 0 deletions

View File

@@ -3,8 +3,43 @@
#include "GLExtensions.h"
#include <algorithm>
#include <array>
#include <cmath>
namespace
{
constexpr GLuint kInputTextureUnit = 0;
const char* kTextureVertexShader = R"GLSL(
#version 430 core
out vec2 vTexCoord;
void main()
{
vec2 positions[3] = vec2[3](
vec2(-1.0, -1.0),
vec2( 3.0, -1.0),
vec2(-1.0, 3.0));
vec2 texCoords[3] = vec2[3](
vec2(0.0, 0.0),
vec2(2.0, 0.0),
vec2(0.0, 2.0));
gl_Position = vec4(positions[gl_VertexID], 0.0, 1.0);
vTexCoord = texCoords[gl_VertexID];
}
)GLSL";
const char* kTextureFragmentShader = R"GLSL(
#version 430 core
layout(binding = 0) uniform sampler2D uInputTexture;
in vec2 vTexCoord;
out vec4 fragColor;
void main()
{
fragColor = texture(uInputTexture, clamp(vTexCoord, vec2(0.0), vec2(1.0)));
}
)GLSL";
}
bool SimpleMotionRenderer::InitializeGl(unsigned width, unsigned height)
{
mWidth = width;
@@ -43,8 +78,100 @@ void SimpleMotionRenderer::RenderFrame(uint64_t frameIndex)
glDisable(GL_SCISSOR_TEST);
}
void SimpleMotionRenderer::RenderTexture(GLuint texture)
{
if (texture == 0 || !EnsureTextureProgram())
{
RenderFrame(0);
return;
}
glViewport(0, 0, static_cast<GLsizei>(mWidth), static_cast<GLsizei>(mHeight));
glDisable(GL_SCISSOR_TEST);
glDisable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
glActiveTexture(GL_TEXTURE0 + kInputTextureUnit);
glBindTexture(GL_TEXTURE_2D, texture);
glUseProgram(mTextureProgram);
glBindVertexArray(mTextureVertexArray);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
glUseProgram(0);
glBindTexture(GL_TEXTURE_2D, 0);
glActiveTexture(GL_TEXTURE0);
}
void SimpleMotionRenderer::ShutdownGl()
{
DestroyTextureProgram();
mWidth = 0;
mHeight = 0;
}
bool SimpleMotionRenderer::EnsureTextureProgram()
{
if (mTextureProgram != 0)
return true;
if (!CompileShader(GL_VERTEX_SHADER, kTextureVertexShader, mTextureVertexShader))
return false;
if (!CompileShader(GL_FRAGMENT_SHADER, kTextureFragmentShader, mTextureFragmentShader))
{
DestroyTextureProgram();
return false;
}
mTextureProgram = glCreateProgram();
glAttachShader(mTextureProgram, mTextureVertexShader);
glAttachShader(mTextureProgram, mTextureFragmentShader);
glLinkProgram(mTextureProgram);
GLint linkResult = GL_FALSE;
glGetProgramiv(mTextureProgram, GL_LINK_STATUS, &linkResult);
if (linkResult == GL_FALSE)
{
DestroyTextureProgram();
return false;
}
glUseProgram(mTextureProgram);
const GLint inputLocation = glGetUniformLocation(mTextureProgram, "uInputTexture");
if (inputLocation >= 0)
glUniform1i(inputLocation, static_cast<GLint>(kInputTextureUnit));
glUseProgram(0);
glGenVertexArrays(1, &mTextureVertexArray);
return mTextureVertexArray != 0;
}
void SimpleMotionRenderer::DestroyTextureProgram()
{
if (mTextureProgram != 0)
glDeleteProgram(mTextureProgram);
if (mTextureVertexShader != 0)
glDeleteShader(mTextureVertexShader);
if (mTextureFragmentShader != 0)
glDeleteShader(mTextureFragmentShader);
if (mTextureVertexArray != 0)
glDeleteVertexArrays(1, &mTextureVertexArray);
mTextureProgram = 0;
mTextureVertexShader = 0;
mTextureFragmentShader = 0;
mTextureVertexArray = 0;
}
bool SimpleMotionRenderer::CompileShader(GLenum shaderType, const char* source, GLuint& shader)
{
shader = glCreateShader(shaderType);
glShaderSource(shader, 1, &source, nullptr);
glCompileShader(shader);
GLint compileResult = GL_FALSE;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compileResult);
if (compileResult != GL_FALSE)
return true;
glDeleteShader(shader);
shader = 0;
return false;
}