NVIDIA DeepStream SDK API Reference

9.2 Release
9.2/sources/libs/ds3d/glprocess/gl_buf.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2021-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_GL_PROCESS_GL_BUFS_H
19 #define DS3D_GL_PROCESS_GL_BUFS_H
20 
21 #include "ds3d/common/common.h"
22 #include "ds3d/glprocess/gl_common.h"
23 
24 namespace ds3d { namespace gl {
25 
26 class VertexBuf {
27  GLuint _val = 0;
28  GLenum _bufType = GL_ARRAY_BUFFER;
29  size_t _maxBytes = 0;
30  DS3D_DISABLE_CLASS_COPY(VertexBuf);
31  explicit VertexBuf(GLenum type) : _bufType(type) {}
32 
33 public:
34  // type: GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER
35  static VertexBuf* create(GLenum type = GL_ARRAY_BUFFER);
36  ~VertexBuf() { reset(); }
37  VertexBuf(VertexBuf&& o);
38 
39  void bind()
40  {
41  DS_ASSERT(get());
42  glBindBuffer(_bufType, get());
43  }
44  void unbind() { glBindBuffer(_bufType, 0); }
45 
46  // usage: GL_STATIC_DRAW, GL_DYNAMIC_DRAW
47  template <typename DT>
48  ErrCode upload(size_t size, const DT* data, GLenum usage = GL_STATIC_DRAW)
49  {
50  DS_ASSERT(get());
51  bind();
52  glBufferData(_bufType, size * sizeof(DT), data, usage);
53  _maxBytes = size * sizeof(DT);
54  unbind();
55  DS3D_RETURN_GL_ERROR("upload buffer failed");
56  return ErrCode::kGood;
57  }
58 
59  template <typename DT>
60  ErrCode allocate(size_t num, GLenum usage = GL_STATIC_DRAW)
61  {
62  return upload<DT>(num, nullptr, usage);
63  }
64 
65  ErrCode uploadSubData(int64_t offset, size_t bytes, const void* data)
66  {
67  DS_ASSERT(get());
68  DS_ASSERT(offset + bytes <= _maxBytes);
69  bind();
70  glBufferSubData(_bufType, offset, bytes, data);
71  unbind();
72  DS3D_RETURN_GL_ERROR("upload buffer failed");
73  return ErrCode::kGood;
74  }
75 
76  void attribPtr(
77  GLuint location, int compSize, size_t stride, GLenum type = GL_FLOAT,
78  GLboolean normalized = GL_FALSE, void* offset = 0)
79  {
80  DS_ASSERT(get());
81  bind();
82  glEnableVertexAttribArray(location);
83  glVertexAttribPointer(location, compSize, GL_FLOAT, normalized, stride, offset);
84  // glVertexAttribPointer(location, 3, GL_FLOAT, GL_FALSE,
85  // sizeof(vertices[0]), (void*) (sizeof(float) * 2));
86  unbind();
87  }
88 
89  GLuint get() const { return _val; }
90  void reset();
91 };
92 
93 class VertexArray {
94  GLuint _val = 0;
95  DS3D_DISABLE_CLASS_COPY(VertexArray);
96  VertexArray() {}
97 
98 public:
99  static VertexArray* create();
102 
103  void bind()
104  {
105  DS_ASSERT(get());
106  glBindVertexArray(get());
107  }
108  void unbind() { glBindVertexArray(0); }
109  void enableAttrib(GLuint index) { glEnableVertexAttribArray(index); }
110 
111  GLuint get() const { return _val; }
112  void reset();
113 };
114 
115 class Program;
116 
117 class Texture {
118  GLuint _val = 0;
119  uint32_t _width = 0;
120  uint32_t _height = 0;
121  DS3D_DISABLE_CLASS_COPY(Texture);
122  Texture() {}
123 
124 public:
125  static Texture* create();
126  ~Texture() { reset(); }
127  Texture(Texture&& o);
128 
129  uint32_t w() const { return _width; }
130  uint32_t h() const { return _height; }
131 
132  void bind(GLenum target = GL_TEXTURE_2D)
133  {
134  DS_ASSERT(_val);
135  glBindTexture(target, _val);
136  }
137  void unbind(GLenum target = GL_TEXTURE_2D) { glBindTexture(target, 0); }
138 
140  int width, int height, void* data, GLenum type = GL_UNSIGNED_BYTE,
141  int internalFormat = GL_RGBA, GLenum format = GL_RGBA,
142  std::function<void()> preParam = nullptr, std::function<void()> postParam = nullptr);
143 
145  int width, int height, GLenum type, int internalFormat, int format,
146  std::function<void()> preParam = nullptr, std::function<void()> postParam = nullptr)
147  {
148  return uploadImage(
149  width, height, nullptr, type, internalFormat, format, std::move(preParam),
150  std::move(postParam));
151  }
152 
153  // ErrCode render(Program& p);
154 
155 
156  GLuint get() const { return _val; }
157  void reset();
158 };
159 
160 }} // namespace ds3d::gl
161 
162 #endif // DS3D_GL_PROCESS_GL_BUFS_H
ds3d::gl::Texture::w
uint32_t w() const
Definition: 9.2/sources/libs/ds3d/glprocess/gl_buf.h:129
width
JDIMENSION JDIMENSION * width
Definition: sources/libs/nvll_batch_jpeg_encode/oss/jpeglib.h:1001
ds3d::gl::VertexBuf::bind
void bind()
Definition: 9.2/sources/libs/ds3d/glprocess/gl_buf.h:39
ds3d::gl::Texture::unbind
void unbind(GLenum target=GL_TEXTURE_2D)
Definition: 9.2/sources/libs/ds3d/glprocess/gl_buf.h:137
ds3d::gl::VertexArray::unbind
void unbind()
Definition: 9.2/sources/libs/ds3d/glprocess/gl_buf.h:108
ds3d::gl::VertexBuf::allocate
ErrCode allocate(size_t num, GLenum usage=GL_STATIC_DRAW)
Definition: 9.2/sources/libs/ds3d/glprocess/gl_buf.h:60
ds3d::gl::VertexArray::create
static VertexArray * create()
ds3d::gl::VertexBuf::uploadSubData
ErrCode uploadSubData(int64_t offset, size_t bytes, const void *data)
Definition: 9.2/sources/libs/ds3d/glprocess/gl_buf.h:65
ds3d::gl::VertexBuf
Definition: sources/libs/ds3d/glprocess/gl_buf.h:26
ds3d::ErrCode::kGood
@ kGood
ds3d::gl::VertexArray::bind
void bind()
Definition: 9.2/sources/libs/ds3d/glprocess/gl_buf.h:103
ds3d::gl::VertexBuf::~VertexBuf
~VertexBuf()
Definition: 9.2/sources/libs/ds3d/glprocess/gl_buf.h:36
ds3d::ErrCode
ErrCode
Definition: includes/ds3d/common/common.h:47
ds3d::gl::VertexArray
Definition: sources/libs/ds3d/glprocess/gl_buf.h:93
ds3d::gl::Texture::get
GLuint get() const
Definition: 9.2/sources/libs/ds3d/glprocess/gl_buf.h:156
ds3d::gl::Texture::reset
void reset()
ds3d::gl::VertexBuf::reset
void reset()
ds3d::gl::Texture::uploadImage
ErrCode uploadImage(int width, int height, void *data, GLenum type=GL_UNSIGNED_BYTE, int internalFormat=GL_RGBA, GLenum format=GL_RGBA, std::function< void()> preParam=nullptr, std::function< void()> postParam=nullptr)
ds3d::gl::VertexBuf::upload
ErrCode upload(size_t size, const DT *data, GLenum usage=GL_STATIC_DRAW)
Definition: 9.2/sources/libs/ds3d/glprocess/gl_buf.h:48
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::Texture
Definition: sources/libs/ds3d/glprocess/gl_buf.h:117
pydeepstreamdoc::tensor::TensorDoc::size
constexpr const char * size
Definition: service-maker/sources/python/src/bindings/docstrings/tensordoc.h:46
ds3d::gl::Texture::bind
void bind(GLenum target=GL_TEXTURE_2D)
Definition: 9.2/sources/libs/ds3d/glprocess/gl_buf.h:132
ds3d::gl::Texture::create
static Texture * create()
ds3d::gl::VertexArray::enableAttrib
void enableAttrib(GLuint index)
Definition: 9.2/sources/libs/ds3d/glprocess/gl_buf.h:109
ds3d::gl::Texture::h
uint32_t h() const
Definition: 9.2/sources/libs/ds3d/glprocess/gl_buf.h:130
ds3d::gl::VertexBuf::unbind
void unbind()
Definition: 9.2/sources/libs/ds3d/glprocess/gl_buf.h:44
ds3d::gl::VertexBuf::get
GLuint get() const
Definition: sources/libs/ds3d/glprocess/gl_buf.h:89
ds3d::gl::Texture::allocateImage
ErrCode allocateImage(int width, int height, GLenum type, int internalFormat, int format, std::function< void()> preParam=nullptr, std::function< void()> postParam=nullptr)
Definition: 9.2/sources/libs/ds3d/glprocess/gl_buf.h:144
data
JSAMPIMAGE data
Definition: sources/libs/nvll_batch_jpeg_encode/oss/jpeglib.h:967
DS3D_RETURN_GL_ERROR
#define DS3D_RETURN_GL_ERROR(fmt,...)
Definition: sources/libs/ds3d/glprocess/gl_common.h:56
ds3d::gl::VertexBuf::create
static VertexBuf * create(GLenum type=GL_ARRAY_BUFFER)
ds3d
Definition: includes/ds3d/common/abi_dataprocess.h:25
ds3d::gl::VertexBuf::attribPtr
void attribPtr(GLuint location, int compSize, size_t stride, GLenum type=GL_FLOAT, GLboolean normalized=GL_FALSE, void *offset=0)
Definition: 9.2/sources/libs/ds3d/glprocess/gl_buf.h:76
DS_ASSERT
#define DS_ASSERT(...)
Definition: includes/ds3d/common/defines.h:36
ds3d::gl::Texture::~Texture
~Texture()
Definition: 9.2/sources/libs/ds3d/glprocess/gl_buf.h:126
ds3d::gl::VertexArray::~VertexArray
~VertexArray()
Definition: 9.2/sources/libs/ds3d/glprocess/gl_buf.h:100
ds3d::v2xinfer::format
static std::string format(const char *fmt,...)
Definition: sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp:31
ds3d::gl::VertexArray::reset
void reset()
ds3d::gl::VertexArray::get
GLuint get() const
Definition: sources/libs/ds3d/glprocess/gl_buf.h:111