Only recreate immutable texture when necessary for Android TensorsToSegmentationCalculator.
PiperOrigin-RevId: 568937611
This commit is contained in:
parent
787371cfba
commit
698b154ff4
|
@ -174,6 +174,9 @@ class TensorsToSegmentationCalculator : public CalculatorBase {
|
||||||
mediapipe::GlCalculatorHelper gpu_helper_;
|
mediapipe::GlCalculatorHelper gpu_helper_;
|
||||||
GLuint upsample_program_;
|
GLuint upsample_program_;
|
||||||
#if MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_31
|
#if MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_31
|
||||||
|
int cached_width_ = 0;
|
||||||
|
int cached_height_ = 0;
|
||||||
|
std::unique_ptr<tflite::gpu::gl::GlTexture> small_mask_texture_;
|
||||||
std::unique_ptr<GlProgram> mask_program_31_;
|
std::unique_ptr<GlProgram> mask_program_31_;
|
||||||
#else
|
#else
|
||||||
GLuint mask_program_20_;
|
GLuint mask_program_20_;
|
||||||
|
@ -307,6 +310,7 @@ absl::Status TensorsToSegmentationCalculator::Close(CalculatorContext* cc) {
|
||||||
upsample_program_ = 0;
|
upsample_program_ = 0;
|
||||||
#if MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_31
|
#if MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_31
|
||||||
mask_program_31_.reset();
|
mask_program_31_.reset();
|
||||||
|
small_mask_texture_.reset();
|
||||||
#else
|
#else
|
||||||
if (mask_program_20_) glDeleteProgram(mask_program_20_);
|
if (mask_program_20_) glDeleteProgram(mask_program_20_);
|
||||||
mask_program_20_ = 0;
|
mask_program_20_ = 0;
|
||||||
|
@ -448,21 +452,24 @@ absl::Status TensorsToSegmentationCalculator::ProcessGpu(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create initial working mask texture.
|
// Create initial working mask texture.
|
||||||
#if MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_31
|
#if !(MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_31)
|
||||||
tflite::gpu::gl::GlTexture small_mask_texture;
|
|
||||||
#else
|
|
||||||
mediapipe::GlTexture small_mask_texture;
|
mediapipe::GlTexture small_mask_texture;
|
||||||
#endif // MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_31
|
#endif // MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_31
|
||||||
|
|
||||||
// Run shader, process mask tensor.
|
// Run shader, process mask tensor.
|
||||||
#if MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_31
|
#if MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_31
|
||||||
{
|
{
|
||||||
|
// Only recreate if the size has changed. See b/297809673 for more details.
|
||||||
|
if (tensor_width != cached_width_ || tensor_height != cached_height_) {
|
||||||
MP_RETURN_IF_ERROR(CreateReadWriteRgbaImageTexture(
|
MP_RETURN_IF_ERROR(CreateReadWriteRgbaImageTexture(
|
||||||
tflite::gpu::DataType::UINT8, // GL_RGBA8
|
tflite::gpu::DataType::UINT8, // GL_RGBA8
|
||||||
{tensor_width, tensor_height}, &small_mask_texture));
|
{tensor_width, tensor_height}, small_mask_texture_.get()));
|
||||||
|
cached_width_ = tensor_width;
|
||||||
|
cached_height_ = tensor_height;
|
||||||
|
}
|
||||||
|
|
||||||
const int output_index = 0;
|
const int output_index = 0;
|
||||||
glBindImageTexture(output_index, small_mask_texture.id(), 0, GL_FALSE, 0,
|
glBindImageTexture(output_index, small_mask_texture_->id(), 0, GL_FALSE, 0,
|
||||||
GL_WRITE_ONLY, GL_RGBA8);
|
GL_WRITE_ONLY, GL_RGBA8);
|
||||||
|
|
||||||
auto read_view = input_tensors[0].GetOpenGlBufferReadView();
|
auto read_view = input_tensors[0].GetOpenGlBufferReadView();
|
||||||
|
@ -547,7 +554,7 @@ absl::Status TensorsToSegmentationCalculator::ProcessGpu(
|
||||||
gpu_helper_.BindFramebuffer(output_texture);
|
gpu_helper_.BindFramebuffer(output_texture);
|
||||||
glActiveTexture(GL_TEXTURE1);
|
glActiveTexture(GL_TEXTURE1);
|
||||||
#if MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_31
|
#if MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_31
|
||||||
glBindTexture(GL_TEXTURE_2D, small_mask_texture.id());
|
glBindTexture(GL_TEXTURE_2D, small_mask_texture_->id());
|
||||||
#else
|
#else
|
||||||
glBindTexture(GL_TEXTURE_2D, small_mask_texture.name());
|
glBindTexture(GL_TEXTURE_2D, small_mask_texture.name());
|
||||||
#endif // MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_31
|
#endif // MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_31
|
||||||
|
@ -854,6 +861,7 @@ void main() {
|
||||||
mask_program_31_ = absl::make_unique<GlProgram>();
|
mask_program_31_ = absl::make_unique<GlProgram>();
|
||||||
MP_RETURN_IF_ERROR(GlProgram::CreateWithShader(shader_without_previous,
|
MP_RETURN_IF_ERROR(GlProgram::CreateWithShader(shader_without_previous,
|
||||||
mask_program_31_.get()));
|
mask_program_31_.get()));
|
||||||
|
small_mask_texture_ = absl::make_unique<tflite::gpu::gl::GlTexture>();
|
||||||
#elif MEDIAPIPE_METAL_ENABLED
|
#elif MEDIAPIPE_METAL_ENABLED
|
||||||
id<MTLDevice> device = metal_helper_.mtlDevice;
|
id<MTLDevice> device = metal_helper_.mtlDevice;
|
||||||
NSString* library_source =
|
NSString* library_source =
|
||||||
|
|
Loading…
Reference in New Issue
Block a user