yuy2rgb-gl.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2017 Intel Corporation. All Rights Reserved.
3 
4 #include "../include/librealsense2/hpp/rs_sensor.hpp"
5 #include "../include/librealsense2/hpp/rs_processing.hpp"
6 #include "../include/librealsense2-gl/rs_processing_gl.hpp"
7 
9 #include "yuy2rgb-gl.h"
10 #include "option.h"
11 
12 #ifndef NOMINMAX
13 #define NOMINMAX
14 #endif
15 
16 #include <glad/glad.h>
17 
18 #include <iostream>
19 
20 #include <chrono>
21 #include <strstream>
22 
23 #include "synthetic-stream-gl.h"
24 
25 static const char* fragment_shader_text =
26 "#version 110\n"
27 "varying vec2 textCoords;\n"
28 "uniform sampler2D textureSampler;\n"
29 "uniform float opacity;\n"
30 "uniform float width;\n"
31 "uniform float height;\n"
32 "void main(void) {\n"
33 " float pixel_width = 1.0 / width;\n"
34 " float pixel_height = 1.0 / height;\n"
35 " float y = 0.0;\n"
36 " float u = 0.0;\n"
37 " float v = 0.0;\n"
38 " float tex_y = 1.0 - textCoords.y;\n"
39 " if (mod(floor(gl_FragCoord.x), 2.0) == 0.0){\n"
40 " vec2 tx1 = vec2(textCoords.x, tex_y);\n"
41 " vec4 px1 = texture2D(textureSampler, tx1);\n"
42 " vec2 tx2 = vec2(textCoords.x + pixel_width, tex_y);\n"
43 " vec4 px2 = texture2D(textureSampler, tx2);\n"
44 " y = px1.x; u = px1.y; v = px2.y;\n"
45 " }\n"
46 " else\n"
47 " {\n"
48 " vec2 tx1 = vec2(textCoords.x - pixel_width, tex_y);\n"
49 " vec4 px1 = texture2D(textureSampler, tx1);\n"
50 " vec2 tx2 = vec2(textCoords.x, tex_y);\n"
51 " vec4 px2 = texture2D(textureSampler, tx2);\n"
52 " y = px2.x; u = px1.y; v = px2.y;\n"
53 " }\n"
54 " //y *= 256.0; u *= 256.0; v *= 256.0;\n"
55 " float c = y - (16.0 / 256.0);\n"
56 " float d = u - 0.5;\n"
57 " float e = v - 0.5;\n"
58 " vec3 color = vec3(0.0);\n"
59 " //color.x = clamp(((298.0 / 256.0) * c + (409.0 / 256.0) * e + 0.5), 0.0, 1.0);\n"
60 " //color.y = clamp(((298.0 / 256.0) * c - (100.0 / 256.0) * d - (208.0/256.0) * e + 0.5), 0.0, 1.0);\n"
61 " //color.z = clamp(((298.0 / 256.0) * c + (516.0 / 256.0) * d + 0.5), 0.0, 1.0);\n"
62 " color.x = clamp((y + 1.40200 * (v - 0.5)), 0.0, 1.0);\n"
63 " color.y = clamp((y - 0.34414 * (u - 0.5) - 0.71414 * (v - 0.5)), 0.0, 1.0);\n"
64 " color.z = clamp((y + 1.77200 * (u - 0.5)), 0.0, 1.0);\n"
65 " gl_FragColor = vec4(color.xyz, opacity);\n"
66 "}";
67 
68 using namespace rs2;
69 using namespace librealsense::gl;
70 
72 {
73 public:
76  texture_2d_shader::default_vertex_shader(),
77  fragment_shader_text, "position", "textureCoords"))
78  {
79  _width_location = _shader->get_uniform_location("width");
80  _height_location = _shader->get_uniform_location("height");
81  }
82 
83  void set_size(int w, int h)
84  {
85  _shader->load_uniform(_width_location, (float)w);
86  _shader->load_uniform(_height_location, (float)h);
87  }
88 
89 private:
92 };
93 
94 void yuy2rgb::cleanup_gpu_resources()
95 {
96  _viz.reset();
97  _fbo.reset();
98  _enabled = 0;
99 }
100 
101 void yuy2rgb::create_gpu_resources()
102 {
103  _viz = std::make_shared<visualizer_2d>(std::make_shared<yuy2rgb_shader>());
104  _fbo = std::make_shared<fbo>(_width, _height);
105  _enabled = glsl_enabled() ? 1 : 0;
106 }
107 
108 yuy2rgb::yuy2rgb()
109  : stream_filter_processing_block("YUY Converter (GLSL)")
110 {
112 
113  auto opt = std::make_shared<librealsense::ptr_option<int>>(
114  0, 1, 0, 1, &_enabled, "GLSL enabled");
116 
117  initialize();
118 }
119 
121 {
122  perform_gl_action([&]()
123  {
125  }, []{});
126 }
127 
129 {
130  //scoped_timer t("yuy2rgb");
131 
132  if (f.get_profile().get() != _input_profile.get())
133  {
139  _width = vp.width(); _height = vp.height();
140 
141  perform_gl_action([&]()
142  {
143  _fbo = std::make_shared<fbo>(_width, _height);
144  }, [this] {
145  _enabled = false;
146  });
147  }
148 
149  rs2::frame res = f;
150 
151  perform_gl_action([&]()
152  {
153  //scoped_timer t("yuy2rgb.gl");
154 
156  if (!res) return;
157 
158  auto gf = dynamic_cast<gpu_addon_interface*>((frame_interface*)res.get());
159 
160  uint32_t yuy_texture;
161 
162  if (auto input_frame = f.as<rs2::gl::gpu_frame>())
163  {
164  yuy_texture = input_frame.get_texture_id(0);
165  }
166  else
167  {
168  glGenTextures(1, &yuy_texture);
169  glBindTexture(GL_TEXTURE_2D, yuy_texture);
173  }
174 
175  uint32_t output_rgb;
176  gf->get_gpu_section().output_texture(0, &output_rgb, TEXTYPE_RGB);
177  glBindTexture(GL_TEXTURE_2D, output_rgb);
181 
182  gf->get_gpu_section().set_size(_width, _height);
183 
186 
187  glBindTexture(GL_TEXTURE_2D, output_rgb);
188  _fbo->createTextureAttachment(output_rgb);
189 
190  _fbo->bind();
191  glClearColor(1, 0, 0, 1);
193 
194  auto& shader = (yuy2rgb_shader&)_viz->get_shader();
195  shader.begin();
196  shader.set_size(_width, _height);
197  shader.end();
198 
199  _viz->draw_texture(yuy_texture);
200 
201  _fbo->unbind();
202 
204 
205  if (!f.is<rs2::gl::gpu_frame>())
206  {
207  glDeleteTextures(1, &yuy_texture);
208  }
209  },
210  [this]{
211  _enabled = false;
212  });
213 
214  return res;
215 }
#define GL_TEXTURE_MAG_FILTER
#define GL_RG8
void add_extension(rs2_extension ex)
Definition: source.h:45
#define GL_TEXTURE_2D
#define GL_UNSIGNED_BYTE
uint32_t _width_location
Definition: yuy2rgb-gl.cpp:90
stream_profile get_profile() const
Definition: rs_frame.hpp:557
const void * get_data() const
Definition: rs_frame.hpp:545
Definition: cah-model.h:10
GLdouble GLdouble GLdouble w
#define GL_LINEAR
GLenum src
Definition: glext.h:1751
GLfloat GLfloat GLfloat GLfloat h
Definition: glext.h:1960
#define glTexImage2D
void register_option(rs2_option id, std::shared_ptr< option > option)
Definition: options.h:86
void set_size(int w, int h)
Definition: yuy2rgb-gl.cpp:83
frame allocate_video_frame(const stream_profile &profile, const frame &original, int new_bpp=0, int new_width=0, int new_height=0, int new_stride=0, rs2_extension frame_type=RS2_EXTENSION_VIDEO_FRAME) const
GLdouble f
bool is() const
Definition: rs_frame.hpp:570
#define glGenTextures
#define GL_COLOR_BUFFER_BIT
#define glBindFramebuffer
#define glTexParameteri
rs2::frame process_frame(const rs2::frame_source &source, const rs2::frame &f) override
Definition: yuy2rgb-gl.cpp:128
unsigned int uint32_t
Definition: stdint.h:80
void cleanup_gpu_resources() override
Definition: yuy2rgb-gl.cpp:94
#define glClear
#define GL_TEXTURE_MIN_FILTER
uint32_t _height_location
Definition: yuy2rgb-gl.cpp:91
int stream_index() const
Definition: rs_frame.hpp:34
#define GL_RG
#define glDeleteTextures
#define RS2_EXTENSION_VIDEO_FRAME_GL
rs2::stream_profile _output_profile
Definition: yuy2rgb-gl.h:40
const rs2_stream_profile * get() const
Definition: rs_frame.hpp:137
#define glBindTexture
void perform_gl_action(T action, S fallback)
std::shared_ptr< rs2::fbo > _fbo
Definition: yuy2rgb-gl.h:45
#define glClearColor
#define GL_COLOR_ATTACHMENT0
GLuint res
Definition: glext.h:8856
#define GL_RGB
#define glDrawBuffer
#define GL_NEAREST
rs2_stream stream_type() const
Definition: rs_frame.hpp:39
stream_profile clone(rs2_stream type, int index, rs2_format format) const
Definition: rs_frame.hpp:63
static const char * fragment_shader_text
Definition: yuy2rgb-gl.cpp:25
#define GL_FRAMEBUFFER
rs2::stream_profile _input_profile
Definition: yuy2rgb-gl.h:39
T as() const
Definition: rs_frame.hpp:580
std::shared_ptr< rs2::visualizer_2d > _viz
Definition: yuy2rgb-gl.h:44


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:50:23