NVIDIA DeepStream SDK API Reference

9.2 Release
sources/libs/ds3d/glprocess/gl_render_base_priv.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2023-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef DS3D_GLPROCESS_GL_RENDER_BASE_PRIV_H
19 #define DS3D_GLPROCESS_GL_RENDER_BASE_PRIV_H
20 
21 #include "ds3d/common/common.h"
22 #include "ds3d/common/hpp/datamap.hpp"
23 #include "ds3d/common/helper/cuda_utils.h"
24 #include "ds3d/glprocess/gl_program.h"
25 #include "ds3d/glprocess/gl_buf.h"
26 #include "ds3d/glprocess/gl_render_ctx.h"
27 #include "ds3d/glprocess/gl_cuda_utils.h"
28 
29 namespace ds3d { namespace gl {
30 
34  vec2f xy0 = {{0, 0}};
36  vec2f xy1 = {{0, 0}};
37 };
38 
40 struct ViewLayout {
42  vec2<int32_t> xy0 = {{0, 0}};
44  vec2<int32_t> xy1= {{0, 0}};
45 };
46 
47 struct ViewLookat {
48  vec3f position{{0.0f, 0.0f, 1.0f}};
49  vec3f target{{0.0f, 0.0f, -1.0f}};
50  vec3f up{{0.0f, 1.0f, 0.0f}};
51 };
52 
54  float fov = 90.0f; // degree
55  float ratio = 0.0f; // width / height set by default
56  float near = 0.01f;
57  float far = 100.0f;
58 };
59 
68  bool lookatEnabled = false;
70  bool perspectiveEnabled = false;
72  bool needColorClear = true;
73  bool is2dOnly = false;
74  int devId = 0;
75 };
76 
77 class BaseRenderPriv : public BaseRender {
78 protected:
82 
88 
89 public:
90  BaseRenderPriv() = default;
91  virtual ~BaseRenderPriv() = default;
92  ErrCode syncData() override
93  {
94  if (_cudaStream) {
95  DS3D_ERROR_RETURN(_cudaStream->sync(), "BaseRenderPriv sync cuda stream failed");
96  }
97  return ErrCode::kGood;
98  }
99 
100 protected:
101  Ptr<Program>& shader() { return _shader; }
102  Ptr<VertexArray>& vao() { return _vao; }
104  mat4f& model() { return _modelMat; }
105  mat4f& view() { return _viewlMat; }
106  mat4f& project() { return _projMat; }
107 
109  {
110  _cudaStream.reset(new CudaStream(cudaStreamDefault, conf.devId));
112  _cudaStream, ErrCode::kConfig, "BaseRenderPriv create cuda stream failed");
113  return ErrCode::kGood;
114  }
115 
117  {
118  if (_cudaStream) {
119  // skip error check for deinit
120  _cudaStream->sync();
121  _cudaStream.reset();
122  }
123  return ErrCode::kGood;
124  }
125 
127  {
129  }
130 
131  ErrCode buildShader(const std::vector<std::string>& scripts, const std::vector<GLenum>& types)
132  {
133  std::unique_ptr<Program> shader(BuildProgramFromSource(scripts, types));
134  DS3D_FAILED_RETURN(shader, ErrCode::kGL, "Build shaders failed");
136  return ErrCode::kGood;
137  }
138 
140  {
141  GuardGlWindow win = getWindow();
142  DS_ASSERT(win);
143  uint32_t winW = 0, winH = 0;
144  DS3D_ERROR_RETURN(win->getWindowSize(winW, winH), "get frame buffer size failed");
145  DS3D_FAILED_RETURN(winW && winH, ErrCode::kParam, "window size is 0");
146  normalizeViewPort(layout, winW, winH, _normalizedView);
147  return ErrCode::kGood;
148  }
149 
150  ErrCode calculateViewPort(int32_t& x, int32_t& y, uint32_t& width, uint32_t& height)
151  {
152  GuardGlWindow win = getWindow();
153  DS_ASSERT(win);
154  uint32_t winW = 0, winH = 0;
155  DS3D_ERROR_RETURN(win->getFrameBufferSize(winW, winH), "get frame buffer size failed");
156  DS3D_FAILED_RETURN(winW && winH, ErrCode::kParam, "window size is 0");
157  calculateViewPort(_normalizedView, winW, winH, x, y, width, height);
158 
159  return ErrCode::kGood;
160  }
161 
163  {
164  _vao.reset(VertexArray::create());
165  return ErrCode::kGood;
166  }
167 
168 public:
170  static ErrCode parseConfig(const std::string& yamlStr, BaseRenderConfig& config)
171  {
172  YAML::Node node = YAML::Load(yamlStr);
173  auto yLayout = node["layout"];
174  auto ycolorClear = node["color_clear"];
175 
176  auto yViewPos = node["view_position"];
177  auto yViewTarget = node["view_target"];
178  auto yViewUp = node["view_up"];
179  auto yNear = node["perspective_near"];
180  auto yFar = node["perspective_far"];
181  auto yFov = node["perspective_fov"];
182  auto yRatio = node["perspective_ratio"];
183  auto yModelMat = node["model_mat"];
184 
185  if (yLayout) {
186  auto vec = yLayout.as<std::vector<int32_t>>();
188  vec.size() == 4, ErrCode::kConfig, "layout must be list of [x0, y0, x1, y1]");
189  config.viewLayout.xy0 = {{vec[0], vec[1]}};
190  config.viewLayout.xy1 = {{vec[2], vec[3]}};
192  config.viewLayout.xy0.x <= config.viewLayout.xy1.x &&
193  config.viewLayout.xy0.y <= config.viewLayout.xy1.y,
194  ErrCode::kConfig, "layout xy1 must >= xy0 [x0, y0, x1, y1]");
195  }
196  if (ycolorClear) {
197  config.needColorClear = ycolorClear.as<bool>();
198  }
199  if (yViewPos) {
200  auto pos = yViewPos.as<std::vector<float>>();
202  pos.size() >= 3, ErrCode::kConfig, "view_position need x,y,z 3 values");
203  config.lookat.position = {{pos[0], pos[1], pos[2]}};
204  config.lookatEnabled = true;
205  }
206  if (yViewTarget) {
207  auto target = yViewTarget.as<std::vector<float>>();
209  target.size() >= 3, ErrCode::kConfig, "view_target need x,y,z 3 values");
210  config.lookat.target = {{target[0], target[1], target[2]}};
211  config.lookatEnabled = true;
212  }
213  if (yViewUp) {
214  auto up = yViewUp.as<std::vector<float>>();
215  DS3D_FAILED_RETURN(up.size() >= 3, ErrCode::kConfig, "view_up need x,y,z 3 values");
216  config.lookat.up = {{up[0], up[1], up[2]}};
217  config.lookatEnabled = true;
218  }
219  if (yNear) {
220  config.perspective.near = yNear.as<float>();
221  config.perspectiveEnabled = true;
222  }
223  if (yFar) {
224  config.perspective.far = yFar.as<float>();
225  config.perspectiveEnabled = true;
226  }
227  if (yFov) {
228  config.perspective.fov = yFov.as<float>();
229  config.perspectiveEnabled = true;
230  }
231  if (yRatio) {
232  config.perspective.ratio = yRatio.as<float>();
233  config.perspectiveEnabled = true;
234  }
235 
236  if (config.perspectiveEnabled && isNear(config.perspective.ratio, 0.0f)) {
237  int h = config.viewLayout.xy1.y - config.viewLayout.xy0.y;
238  int w = config.viewLayout.xy1.x - config.viewLayout.xy0.x;
239  if (h > 0) {
240  config.perspective.ratio = (float)(w) / (float)(h);
241  } else {
242  config.perspective.ratio = 1.0f;
243  }
244  }
245 
246  if (yModelMat) {
247  auto m4x4 = yModelMat.as<std::vector<float>>();
249  m4x4.size() == 16, ErrCode::kConfig,
250  "model_mat must be a list of 16(4x4) float values");
251  config.modelMat = eye4x4();
252  for (int r = 0; r < 4; ++r) {
253  config.modelMat[r] = {
254  {m4x4[r * 4], m4x4[r * 4 + 1], m4x4[r * 4 + 2], m4x4[r * 4 + 3]}};
255  }
256  }
257  return ErrCode::kGood;
258  }
259 
260  static void getMVPFrom(mat4f& m, mat4f& v, mat4f& p, const BaseRenderConfig& config)
261  {
262  v = eye4x4();
263  p = eye4x4();
264  if (!config.is2dOnly && config.lookatEnabled) {
265  glm::mat4 mat = glm::lookAt(
266  vec3ToGlm(config.lookat.position), vec3ToGlm(config.lookat.target),
267  vec3ToGlm(config.lookat.up));
268  v = glmMat4ToDS(mat);
269  }
270  if (!config.is2dOnly && config.perspectiveEnabled) {
271  glm::mat4 mat = glm::perspective(
272  glm::radians(config.perspective.fov), config.perspective.ratio,
273  config.perspective.near, config.perspective.far);
274  p = glmMat4ToDS(mat);
275  }
276  m = config.modelMat;
277  }
278 
279  static void normalizeViewPort(
280  const ViewLayout& layout, uint32_t winW, uint32_t winH,
282  {
283  DS_ASSERT(winW && winH);
284  normalizedView.xy0.x = layout.xy0.x / (float)winW;
285  normalizedView.xy0.y = 1.0 - layout.xy1.y / (float)winH;
286  normalizedView.xy1.x = layout.xy1.x / (float)winW;
287  normalizedView.xy1.y = 1.0 - layout.xy0.y / (float)winH;
288  }
289 
290  static void calculateViewPort(
291  const NormalViewPort& normalizedView, uint32_t winW, uint32_t winH, int32_t& x, int32_t& y,
292  uint32_t& width, uint32_t& height)
293  {
294  x = (int32_t)(normalizedView.xy0.x * winW);
295  y = (int32_t)(normalizedView.xy0.y * winH);
296 
297  int32_t w = (int32_t)(normalizedView.xy1.x * winW) - x;
298  int32_t h = (int32_t)(normalizedView.xy1.y * winH) - y;
299  w = std::max<int32_t>(0, w);
300  h = std::max<int32_t>(0, h);
301  width = (uint32_t)w;
302  height = (uint32_t)h;
303  }
304 };
305 
306 
307 }} // namespace ds3d::gl
308 
309 #endif // DS3D_GLPROCESS_GL_RENDER_BASE_PRIV_H
ds3d::gl::BaseRenderConfig::is2dOnly
bool is2dOnly
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:73
ds3d::CudaStream
Definition: includes/ds3d/common/helper/cuda_utils.h:29
width
JDIMENSION JDIMENSION * width
Definition: sources/libs/nvll_batch_jpeg_encode/oss/jpeglib.h:1001
ds3d::gl::ViewLookat::target
vec3f target
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:49
ds3d::gl::BaseRenderPriv::syncData
ErrCode syncData() override
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:92
ds3d::__xy::y
T y
Definition: includes/ds3d/common/idatatype.h:50
ds3d::__xyz
Definition: includes/ds3d/common/idatatype.h:39
ds3d::gl::BaseRenderPriv::_projMat
mat4f _projMat
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:87
ds3d::ErrCode::kGL
@ kGL
ds3d::gl::BaseRenderPriv::BaseRenderPriv
BaseRenderPriv()=default
ds3d::gl::BaseRenderConfig::modelMat
mat4f modelMat
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:71
ds3d::gl::mat4f
std::vector< vec4f > mat4f
Definition: sources/libs/ds3d/glprocess/gl_common.h:60
ds3d::gl::Window::getWindowSize
virtual ErrCode getWindowSize(uint32_t &width, uint32_t &height)=0
ds3d::gl::NormalViewPort::xy0
vec2f xy0
lower left in viewpoint coordinates
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:34
ds3d::ErrCode::kParam
@ kParam
ds3d::gl::VertexArray::create
static VertexArray * create()
ds3d::gl::BaseRenderPriv::storeViewPort
ErrCode storeViewPort(const ViewLayout &layout)
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:139
ds3d::ErrCode::kGood
@ kGood
ds3d::gl::PerspectiveParam::near
float near
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:56
ds3d::gl::BaseRender
Definition: sources/libs/ds3d/glprocess/gl_render_ctx.h:54
ds3d::gl::BaseRenderPriv::view
mat4f & view()
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:105
ds3d::gl::BaseRenderPriv::normalizeViewPort
static void normalizeViewPort(const ViewLayout &layout, uint32_t winW, uint32_t winH, NormalViewPort &normalizedView)
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:279
ds3d::gl::BaseRenderPriv::createVao
ErrCode createVao()
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:162
ds3d::gl::BaseRenderPriv::clearBase
ErrCode clearBase()
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:116
ds3d::gl::ViewLookat::position
vec3f position
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:48
ds3d::gl::NormalViewPort
Normalized view port positon in the window in range [0, 1].
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:32
DS3D_FAILED_RETURN
#define DS3D_FAILED_RETURN(condition, ret, fmt,...)
Definition: includes/ds3d/common/defines.h:69
ds3d::ErrCode
ErrCode
Definition: includes/ds3d/common/common.h:47
ds3d::gl::BaseRender::getWindow
GuardGlWindow getWindow() override
Definition: sources/libs/ds3d/glprocess/gl_render_ctx.h:57
ds3d::gl::BaseRenderPriv::initBase
ErrCode initBase(const BaseRenderConfig &conf)
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:108
ds3d::gl::NormalViewPort::xy1
vec2f xy1
upper right in viewpoint coordinates
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:36
ds3d::GuardDataT< gl::Window >
ds3d::gl::BaseRenderConfig::needColorClear
bool needColorClear
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:72
ds3d::gl::ViewLookat
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:47
ds3d::gl::BaseRenderPriv::getMVPFrom
static void getMVPFrom(mat4f &m, mat4f &v, mat4f &p, const BaseRenderConfig &config)
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:260
ds3d::gl::BaseRenderConfig
base config for all renders layout: [0, 0, 1280, 720] color_clear: True
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:65
ds3d::gl::BaseRenderPriv::_viewlMat
mat4f _viewlMat
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:86
ds3d::gl::BaseRenderPriv::_modelMat
mat4f _modelMat
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:85
ds3d::gl::vec3ToGlm
glm::vec3 vec3ToGlm(const vec3f &m)
Definition: sources/libs/ds3d/glprocess/gl_helper.h:63
ds3d::gl::BaseRenderPriv::vao
Ptr< VertexArray > & vao()
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:102
absl::move
constexpr absl::OTABSL_OPTION_NAMESPACE_NAME::remove_reference_t< T > && move(T &&t) noexcept
Definition: includes/opentelemetry/nostd/internal/absl/utility/utility.h:199
ds3d::gl::PerspectiveParam::ratio
float ratio
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:55
ds3d::isNear
bool isNear(float a, float b)
Definition: includes/ds3d/common/func_utils.h:234
ds3d::gl::PerspectiveParam::fov
float fov
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:54
ds3d::gl::BaseRenderPriv::project
mat4f & project()
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:106
ds3d::gl::PerspectiveParam
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:53
ds3d::gl::BaseRenderPriv::~BaseRenderPriv
virtual ~BaseRenderPriv()=default
ds3d::gl::BaseRenderPriv::initMvpFromConfig
void initMvpFromConfig(const BaseRenderConfig &conf)
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:126
ds3d::gl::ViewLookat::up
vec3f up
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:50
ds3d::gl::BaseRenderConfig::devId
int devId
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:74
ds3d::gl::BaseRenderPriv::_vao
Ptr< VertexArray > _vao
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:80
ds3d::ErrCode::kConfig
@ kConfig
ds3d::gl::BaseRenderPriv::parseConfig
static ErrCode parseConfig(const std::string &yamlStr, BaseRenderConfig &config)
static functions for backward compatibility
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:170
ds3d::gl::BaseRenderPriv::calculateViewPort
ErrCode calculateViewPort(int32_t &x, int32_t &y, uint32_t &width, uint32_t &height)
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:150
ds3d::gl::BaseRenderPriv::_cudaStream
Ptr< CudaStream > _cudaStream
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:81
ds3d::gl::BaseRenderConfig::lookat
ViewLookat lookat
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:67
ds3d::__xy
Definition: includes/ds3d/common/idatatype.h:47
ds3d::gl::BuildProgramFromSource
Program * BuildProgramFromSource(const std::vector< std::string > &sources, const std::vector< GLenum > &types)
ds3d::gl::BaseRenderConfig::perspective
PerspectiveParam perspective
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:69
ds3d::gl::BaseRenderPriv::model
mat4f & model()
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:104
DS3D_ERROR_RETURN
#define DS3D_ERROR_RETURN(code, fmt,...)
Definition: includes/ds3d/common/defines.h:77
ds3d::Ptr
ShrdPtr< T > Ptr
Definition: includes/ds3d/common/hpp/obj.hpp:37
ds3d::gl::BaseRenderPriv::_normalizedView
NormalViewPort _normalizedView
normalized viewport posion in the window
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:84
ds3d::gl::glmMat4ToDS
mat4f glmMat4ToDS(const glm::mat4 &m)
Definition: sources/libs/ds3d/glprocess/gl_helper.h:45
ds3d::gl::BaseRenderPriv::calculateViewPort
static void calculateViewPort(const NormalViewPort &normalizedView, uint32_t winW, uint32_t winH, int32_t &x, int32_t &y, uint32_t &width, uint32_t &height)
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:290
ds3d::gl::BaseRenderPriv::buildShader
ErrCode buildShader(const std::vector< std::string > &scripts, const std::vector< GLenum > &types)
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:131
ds3d::gl::PerspectiveParam::far
float far
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:57
ds3d::gl::ViewLayout::xy1
vec2< int32_t > xy1
buttom right in window coordinates
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:44
ds3d::gl::BaseRenderConfig::lookatEnabled
bool lookatEnabled
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:68
ds3d::gl::BaseRenderPriv::shader
Ptr< Program > & shader()
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:101
ds3d::gl::eye4x4
mat4f eye4x4()
Definition: sources/libs/ds3d/glprocess/gl_helper.h:32
ds3d
Definition: includes/ds3d/common/abi_dataprocess.h:25
ds3d::__xy::x
T x
Definition: includes/ds3d/common/idatatype.h:50
ds3d::gl::ViewLayout
layout position in window coordinates
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:40
ds3d::gl::BaseRenderPriv
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:77
ds3d::gl::BaseRenderPriv::normalizedView
NormalViewPort & normalizedView()
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:103
ds3d::gl::BaseRenderConfig::viewLayout
ViewLayout viewLayout
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:66
ds3d::gl::ViewLayout::xy0
vec2< int32_t > xy0
top left in window coordinates
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:42
DS_ASSERT
#define DS_ASSERT(...)
Definition: includes/ds3d/common/defines.h:36
ds3d::gl::BaseRenderConfig::perspectiveEnabled
bool perspectiveEnabled
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:70
ds3d::gl::Window::getFrameBufferSize
virtual ErrCode getFrameBufferSize(uint32_t &width, uint32_t &height)=0
ds3d::gl::BaseRenderPriv::_shader
Ptr< Program > _shader
Definition: sources/libs/ds3d/glprocess/gl_render_base_priv.h:79