Lagrange
VertexBuffer.h
1/*
2 * Copyright 2019 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13
14#include <lagrange/ui/api.h>
15#include <lagrange/ui/types/GLContext.h>
16
17#include <stdint.h>
18#include <algorithm>
19#include <array>
20#include <vector>
21
22
23namespace lagrange {
24namespace ui {
25
26template <typename T>
28template <>
29struct type_traits<uint32_t>
30{
31 enum { type = GL_UNSIGNED_INT, integral = 1 };
32};
33template <>
34struct type_traits<int32_t>
35{
36 enum { type = GL_INT, integral = 1 };
37};
38template <>
39struct type_traits<uint16_t>
40{
41 enum { type = GL_UNSIGNED_SHORT, integral = 1 };
42};
43template <>
44struct type_traits<int16_t>
45{
46 enum { type = GL_SHORT, integral = 1 };
47};
48template <>
49struct type_traits<uint8_t>
50{
51 enum { type = GL_UNSIGNED_BYTE, integral = 1 };
52};
53template <>
54struct type_traits<int8_t>
55{
56 enum { type = GL_BYTE, integral = 1 };
57};
58template <>
59struct type_traits<double>
60{
61 enum { type = GL_DOUBLE, integral = 0 };
62};
63template <>
64struct type_traits<float>
65{
66 enum { type = GL_FLOAT, integral = 0 };
67};
68
69struct LA_UI_API VertexBuffer
70{
71 VertexBuffer(GLenum _target = GL_ARRAY_BUFFER);
72 GLenum target; // most common: GL_ARRAY_BUFFER or GL_ELEMENT_ARRAY_BUFFER
73 GLuint id;
74 GLuint size;
75 GLuint glType;
76 bool is_integral;
77 GLsizei count;
78
79 template <typename Matrix>
80 void upload(const Matrix& M);
81
82 template <typename T>
83 void upload(const std::vector<T>& arr, uint32_t component_count = 1);
84
85
86 void initialize();
87
89 {
90 GLsizei count;
91 bool integral;
92 GLenum gl_type;
93 };
94
95 void upload(const void* data, size_t byte_size, DataDescription& desc);
96
97 void
98 upload(GLuint byte_size, const uint8_t* data, GLsizei count, bool integral, GLenum gl_type);
99
100 void download(GLuint size_, uint8_t* data) const;
101
102 void free();
103};
104
105
106template <typename Matrix>
107void VertexBuffer::upload(const Matrix& M)
108{
109 uint32_t component_size = sizeof(typename Matrix::Scalar);
110 upload(
111 static_cast<uint32_t>(M.size()) * component_size,
112 reinterpret_cast<const uint8_t*>(M.data()),
113 static_cast<GLsizei>(M.rows()),
115 static_cast<GLuint>(type_traits<typename Matrix::Scalar>::type));
116}
117
118
119template <typename T>
120void VertexBuffer::upload(const std::vector<T>& arr, uint32_t component_count)
121{
122 uint32_t component_size = sizeof(T);
123 upload(
124 static_cast<uint32_t>(arr.size()) * component_size,
125 reinterpret_cast<const uint8_t*>(arr.data()),
126 static_cast<GLsizei>(arr.size() / component_count),
127 static_cast<bool>(type_traits<T>::integral),
128 static_cast<GLuint>(type_traits<T>::type));
129}
130
131
132struct LA_UI_API VAO
133{
134 VAO()
135 : id(0)
136 {}
137 GLuint id;
138
139 void init();
140 void free();
141};
142
143
144struct LA_UI_API GPUBuffer
145{
146 GPUBuffer(const GPUBuffer&) = delete;
147 GPUBuffer& operator=(const GPUBuffer&) = delete;
148
149 GPUBuffer(GPUBuffer&&) = default;
150 GPUBuffer& operator=(GPUBuffer&&) = default;
151
152 GPUBuffer(GLenum _target = GL_ARRAY_BUFFER)
153 : m_vbo(_target)
154 {
155 m_vbo.initialize();
156 }
157 ~GPUBuffer() { m_vbo.free(); }
158
159 VertexBuffer& operator*() { return m_vbo; }
160 VertexBuffer* operator->() { return &m_vbo; }
161
162 VertexBuffer& vbo() { return m_vbo; }
163
164private:
165 // todo combine these
166 VertexBuffer m_vbo;
167};
168
169
170} // namespace ui
171} // namespace lagrange
Lagrange UI Viewer and mini 3D engine.
Definition: AcceleratedPicking.h:22
Main namespace for Lagrange.
Definition: AABBIGL.h:30
Definition: VertexBuffer.h:145
Definition: VertexBuffer.h:133
Definition: VertexBuffer.h:89
Definition: VertexBuffer.h:70
Definition: VertexBuffer.h:27