Internal change

PiperOrigin-RevId: 519674123
This commit is contained in:
MediaPipe Team 2023-03-27 03:36:47 -07:00 committed by Copybara-Service
parent 5ccf986513
commit 9f888435b7
3 changed files with 49 additions and 27 deletions

View File

@ -858,7 +858,7 @@ cc_library(
":scale_mode_cc_proto", ":scale_mode_cc_proto",
":shader_util", ":shader_util",
"//mediapipe/framework/port:ret_check", "//mediapipe/framework/port:ret_check",
"//mediapipe/framework/port:status", "@com_google_absl//absl/status",
], ],
) )

View File

@ -87,6 +87,17 @@ absl::Status QuadRenderer::GlSetup(
glGenVertexArrays(1, &vao_); glGenVertexArrays(1, &vao_);
glGenBuffers(2, vbo_); glGenBuffers(2, vbo_);
glBindVertexArray(vao_);
glEnableVertexAttribArray(ATTRIB_VERTEX);
glEnableVertexAttribArray(ATTRIB_TEXTURE_POSITION);
glBindBuffer(GL_ARRAY_BUFFER, vbo_[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(mediapipe::kBasicTextureVertices),
kBasicTextureVertices, GL_STATIC_DRAW);
glVertexAttribPointer(ATTRIB_TEXTURE_POSITION, 2, GL_FLOAT, 0, 0, nullptr);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
return absl::OkStatus(); return absl::OkStatus();
} }
@ -104,6 +115,7 @@ void QuadRenderer::GlTeardown() {
vbo_[0] = 0; vbo_[0] = 0;
vbo_[1] = 0; vbo_[1] = 0;
} }
rotation_ = std::nullopt;
} }
absl::Status QuadRenderer::GlRender(float frame_width, float frame_height, absl::Status QuadRenderer::GlRender(float frame_width, float frame_height,
@ -145,12 +157,42 @@ absl::Status QuadRenderer::GlRender(float frame_width, float frame_height,
break; break;
} }
if (flip_texture) {
switch (rotation) {
case FrameRotation::kNone: // Fall-through intended.
case FrameRotation::k180:
flip_vertical = !flip_vertical;
break;
case FrameRotation::k90:
flip_horizontal = !flip_horizontal;
break;
case FrameRotation::k270:
flip_horizontal = !flip_horizontal;
break;
}
}
const int h_flip_factor = flip_horizontal ? -1 : 1; const int h_flip_factor = flip_horizontal ? -1 : 1;
const int v_flip_factor = flip_vertical ? -1 : 1; const int v_flip_factor = flip_vertical ? -1 : 1;
GLfloat scale[] = {scale_width * h_flip_factor, scale_height * v_flip_factor, GLfloat scale[] = {scale_width * h_flip_factor, scale_height * v_flip_factor,
1.0, 1.0}; 1.0, 1.0};
glUniform4fv(scale_unif_, 1, scale); glUniform4fv(scale_unif_, 1, scale);
// Draw.
glBindVertexArray(vao_);
if (rotation != rotation_) {
rotation_ = rotation;
UpdateVertices(rotation);
}
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindVertexArray(0);
return absl::OkStatus();
}
void QuadRenderer::UpdateVertices(FrameRotation rotation) const {
// Choose vertices for rotation. // Choose vertices for rotation.
const GLfloat* vertices; // quad used to render the texture. const GLfloat* vertices; // quad used to render the texture.
switch (rotation) { switch (rotation) {
@ -168,36 +210,11 @@ absl::Status QuadRenderer::GlRender(float frame_width, float frame_height,
break; break;
} }
// Draw.
// TODO: In practice, our vertex attributes almost never change, so
// convert this to being actually static, with initialization done in the
// GLSetup.
glBindVertexArray(vao_);
glEnableVertexAttribArray(ATTRIB_VERTEX);
glBindBuffer(GL_ARRAY_BUFFER, vbo_[0]); glBindBuffer(GL_ARRAY_BUFFER, vbo_[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(mediapipe::kBasicSquareVertices), glBufferData(GL_ARRAY_BUFFER, sizeof(mediapipe::kBasicSquareVertices),
vertices, GL_STATIC_DRAW); vertices, GL_STATIC_DRAW);
glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, nullptr); glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, nullptr);
glEnableVertexAttribArray(ATTRIB_TEXTURE_POSITION);
glBindBuffer(GL_ARRAY_BUFFER, vbo_[1]);
glBufferData(
GL_ARRAY_BUFFER,
flip_texture ? sizeof(mediapipe::kBasicTextureVerticesFlipY)
: sizeof(mediapipe::kBasicTextureVertices),
flip_texture ? kBasicTextureVerticesFlipY : kBasicTextureVertices,
GL_STATIC_DRAW);
glVertexAttribPointer(ATTRIB_TEXTURE_POSITION, 2, GL_FLOAT, 0, 0, nullptr);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDisableVertexAttribArray(ATTRIB_VERTEX);
glDisableVertexAttribArray(ATTRIB_TEXTURE_POSITION);
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
return absl::OkStatus();
} }
absl::Status FrameRotationFromInt(FrameRotation* rotation, int degrees_ccw) { absl::Status FrameRotationFromInt(FrameRotation* rotation, int degrees_ccw) {

View File

@ -15,7 +15,9 @@
#ifndef MEDIAPIPE_GPU_GL_QUAD_RENDERER_H_ #ifndef MEDIAPIPE_GPU_GL_QUAD_RENDERER_H_
#define MEDIAPIPE_GPU_GL_QUAD_RENDERER_H_ #define MEDIAPIPE_GPU_GL_QUAD_RENDERER_H_
#include "mediapipe/framework/port/status.h" #include <optional>
#include "absl/status/status.h"
#include "mediapipe/gpu/gl_base.h" #include "mediapipe/gpu/gl_base.h"
#include "mediapipe/gpu/scale_mode.pb.h" #include "mediapipe/gpu/scale_mode.pb.h"
@ -78,11 +80,14 @@ class QuadRenderer {
void GlTeardown(); void GlTeardown();
private: private:
void UpdateVertices(FrameRotation rotation) const;
GLuint program_ = 0; GLuint program_ = 0;
GLint scale_unif_ = -1; GLint scale_unif_ = -1;
std::vector<GLint> frame_unifs_; std::vector<GLint> frame_unifs_;
GLuint vao_ = 0; // vertex array object GLuint vao_ = 0; // vertex array object
GLuint vbo_[2] = {0, 0}; // for vertex buffer storage GLuint vbo_[2] = {0, 0}; // for vertex buffer storage
mutable std::optional<FrameRotation> rotation_;
}; };
absl::Status FrameRotationFromInt(FrameRotation* rotation, int degrees_ccw); absl::Status FrameRotationFromInt(FrameRotation* rotation, int degrees_ccw);