Skip to main content

OpenGLMeshUploadManager Class Template

Manager that consumes mesh-upload commands and performs OpenGL buffer setup. More...

Declaration

template <typename THandle, typename TCommandBuffer = NullCommandBuffer> class helios::opengl::OpenGLMeshUploadManager<THandle, TCommandBuffer> { ... }

Public Member Typedefs Index

template <typename THandle, typename TCommandBuffer = NullCommandBuffer>
usingEngineRoleTag = ManagerRole

Engine role marker used by runtime registries. More...

Public Constructors Index

template <typename THandle, typename TCommandBuffer = NullCommandBuffer>
OpenGLMeshUploadManager (RenderResourceWorld &renderResourceWorld)

Constructs the manager with access to render-resource storage. More...

Public Member Functions Index

template <typename THandle, typename TCommandBuffer = NullCommandBuffer>
voidflush (UpdateContext &updateContext) noexcept

Uploads all queued meshes and clears processed queue entries. More...

template <typename THandle, typename TCommandBuffer = NullCommandBuffer>
boolsubmit (MeshBatchUploadCommand< THandle > &&command) noexcept

Queues mesh handles from a batch upload command. More...

template <typename THandle, typename TCommandBuffer = NullCommandBuffer>
voidinit (helios::engine::runtime::messaging::command::CommandHandlerRegistry &commandHandlerRegistry) noexcept

Registers mesh upload command handlers in the command registry. More...

Private Member Functions Index

template <typename TVertexInputRate>
voidbuildVertexAttributeLayout (const VertexAttributeLayoutComponent< THandle, TVertexInputRate > *layoutComponent) noexcept

Configures OpenGL vertex attribute pointers from one layout component. More...

template <typename THandle, typename TCommandBuffer = NullCommandBuffer>
AttributeFormatfromAttributeType (VertexAttributeType attributeType) const noexcept

Maps engine attribute type to OpenGL attribute format details. More...

template <typename THandle, typename TCommandBuffer = NullCommandBuffer>
boolupload (MeshEntity mesh) noexcept

Uploads one mesh to GPU buffers and configures vertex attributes. More...

Private Member Attributes Index

template <typename THandle, typename TCommandBuffer = NullCommandBuffer>
RenderResourceWorld &renderResourceWorld_

Render-resource world used to resolve mesh entities by handle. More...

template <typename THandle, typename TCommandBuffer = NullCommandBuffer>
std::vector< MeshHandle >meshHandles_

Pending mesh handles queued for upload during flush(...). More...

Private Static Attributes Index

template <typename THandle, typename TCommandBuffer = NullCommandBuffer>
static const Logger &logger_ = LogManager::loggerForScope(HELIOS_LOG_SCOPE)

Description

Manager that consumes mesh-upload commands and performs OpenGL buffer setup.

Template Parameters
THandle

Mesh handle type.

TCommandBuffer

Command buffer type (kept for compatibility with runtime wiring).

Definition at line 69 of file OpenGLMeshUploadManager.ixx.

Public Member Typedefs

EngineRoleTag

template <typename THandle, typename TCommandBuffer = NullCommandBuffer>
using helios::opengl::OpenGLMeshUploadManager< THandle, TCommandBuffer >::EngineRoleTag = ManagerRole

Engine role marker used by runtime registries.

Definition at line 253 of file OpenGLMeshUploadManager.ixx.

253 using EngineRoleTag = ManagerRole;

Public Constructors

OpenGLMeshUploadManager()

template <typename THandle, typename TCommandBuffer = NullCommandBuffer>
helios::opengl::OpenGLMeshUploadManager< THandle, TCommandBuffer >::OpenGLMeshUploadManager (RenderResourceWorld & renderResourceWorld)
inline explicit

Constructs the manager with access to render-resource storage.

Parameters
renderResourceWorld

Render-resource world used to resolve mesh entities.

Definition at line 245 of file OpenGLMeshUploadManager.ixx.

245 explicit OpenGLMeshUploadManager(RenderResourceWorld& renderResourceWorld)
246 :
247 renderResourceWorld_(renderResourceWorld)
248 { }

Public Member Functions

flush()

template <typename THandle, typename TCommandBuffer = NullCommandBuffer>
void helios::opengl::OpenGLMeshUploadManager< THandle, TCommandBuffer >::flush (UpdateContext & updateContext)
inline noexcept

Uploads all queued meshes and clears processed queue entries.

Parameters
updateContext

Frame-local update context.

Definition at line 261 of file OpenGLMeshUploadManager.ixx.

261 void flush(UpdateContext& updateContext) noexcept {
262
263 if (meshHandles_.empty()) {
264 return;
265 }
266
267 for (const auto& sourceHandle : meshHandles_) {
268 auto meshEntity = renderResourceWorld_.findEntity<THandle>(sourceHandle);
269
270 if (!meshEntity) {
271 logger_.error("Could not find mesh entity");
272 assert(false && "Could not find mesh entity");
273 continue;
274 }
275
276 if (!upload(*meshEntity)) {
277 logger_.error("Could not compile mesh");
278 } else {
279 meshEntity->template remove<MeshUploadRequestComponent<THandle>>();
280 }
281 }
282
283 meshHandles_.clear();
284 }

init()

template <typename THandle, typename TCommandBuffer = NullCommandBuffer>
void helios::opengl::OpenGLMeshUploadManager< THandle, TCommandBuffer >::init (helios::engine::runtime::messaging::command::CommandHandlerRegistry & commandHandlerRegistry)
inline noexcept

Registers mesh upload command handlers in the command registry.

Parameters
commandHandlerRegistry

Registry used for command-handler registration.

Definition at line 306 of file OpenGLMeshUploadManager.ixx.

306 void init(helios::engine::runtime::messaging::command::CommandHandlerRegistry& commandHandlerRegistry) noexcept {
307 commandHandlerRegistry.registerHandler<MeshBatchUploadCommand<THandle>>(*this);
308 }

submit()

template <typename THandle, typename TCommandBuffer = NullCommandBuffer>
bool helios::opengl::OpenGLMeshUploadManager< THandle, TCommandBuffer >::submit (MeshBatchUploadCommand< THandle > && command)
inline noexcept

Queues mesh handles from a batch upload command.

Parameters
command

Batch command containing mesh handles to upload (consumed by value).

Returns

true when the command was accepted.

Definition at line 292 of file OpenGLMeshUploadManager.ixx.

292 bool submit(MeshBatchUploadCommand<THandle>&& command) noexcept {
293 meshHandles_.reserve(meshHandles_.size() + command.meshHandles.size());
294
295 for (const auto& meshHandle : command.meshHandles) {
296 meshHandles_.push_back(std::move(meshHandle));
297 }
298 return true;
299 }

Private Member Functions

buildVertexAttributeLayout()

template <typename TVertexInputRate>
void helios::opengl::OpenGLMeshUploadManager< THandle, TCommandBuffer >::buildVertexAttributeLayout (const VertexAttributeLayoutComponent< THandle, TVertexInputRate > * layoutComponent)
inline noexcept

Configures OpenGL vertex attribute pointers from one layout component.

Template Parameters
TVertexInputRate

Input-rate tag (PerVertex or PerInstance).

Parameters
layoutComponent

Attribute layout component to translate to OpenGL calls.

Definition at line 100 of file OpenGLMeshUploadManager.ixx.

100 void buildVertexAttributeLayout(const VertexAttributeLayoutComponent<THandle, TVertexInputRate>* layoutComponent) noexcept {
101
102 const auto& active = layoutComponent->active();
103 const auto& layouts = layoutComponent->layouts();
104
105 for (std::size_t idx = 0; idx < active.size(); idx++) {
106
107 if (!active.test(idx)) {
108 continue;
109 }
110
111 const auto& layout = layouts[idx];
112
113 const auto format = fromAttributeType(layout.attribute.type);
114
115 for (std::uint32_t i = 0; i < format.locationCount; i++) {
116 const auto location = layout.location + i;
117
118 glEnableVertexAttribArray(location);
119 glVertexAttribPointer(
120 location,
121 format.componentsPerLocation,
122 OpenGLEnumMapper::toOpenGLBaseType(layout.attribute.type),
123 GL_FALSE,
124 static_cast<GLsizei>(layout.stride),
125 reinterpret_cast<void*>(layout.offset + (i * format.locationStrideBytes))
126 );
127 glVertexAttribDivisor(location, layout.divisor);
128 }
129 }
130
131 }

fromAttributeType()

template <typename THandle, typename TCommandBuffer = NullCommandBuffer>
AttributeFormat helios::opengl::OpenGLMeshUploadManager< THandle, TCommandBuffer >::fromAttributeType (VertexAttributeType attributeType)
inline noexcept

Maps engine attribute type to OpenGL attribute format details.

Parameters
attributeType

Engine-side vertex attribute type.

Returns

Expanded format used to configure one or multiple attribute locations.

Definition at line 140 of file OpenGLMeshUploadManager.ixx.

140 [[nodiscard]] AttributeFormat fromAttributeType(VertexAttributeType attributeType) const noexcept {
141
142 switch (attributeType) {
143 case VertexAttributeType::Float:
144 return AttributeFormat{1, 1, 0};
145 case VertexAttributeType::Vec3f:
146 return AttributeFormat{1, 3, 0};
147 case VertexAttributeType::Vec4f:
148 return AttributeFormat{1, 4, 0};
149 case VertexAttributeType::Mat4f:
150 return AttributeFormat{4, 4, sizeof(float) * 4};
151 default:
152 std::unreachable();
153 }
154
155 }

upload()

template <typename THandle, typename TCommandBuffer = NullCommandBuffer>
bool helios::opengl::OpenGLMeshUploadManager< THandle, TCommandBuffer >::upload (MeshEntity mesh)
inline noexcept

Uploads one mesh to GPU buffers and configures vertex attributes.

Parameters
mesh

Mesh entity containing upload request and mesh data.

Returns

true if upload succeeded, otherwise false.

Definition at line 163 of file OpenGLMeshUploadManager.ixx.

163 bool upload(MeshEntity mesh) noexcept {
164
165 using Handle = typename MeshEntity::Handle_type;
166
167 logger_.info("Uploading mesh data for MeshEntity {0}...", mesh.handle().entityId);
168
169 if (!mesh.template get<MeshUploadRequestComponent<Handle>>()) {
170 logger_.error("MeshUpload not requested by this entity");
171 assert(false && "MeshUpload not requested by this entity");
172 return false;
173 }
174
175
176 if (mesh.template get<OpenGLMeshComponent<Handle>>()) {
177 logger_.error("Mesh already has a MeshComponent");
178 assert(false && "Mesh already has a MeshComponent");
179 return false;
180 }
181
182 auto* meshDataComponent = mesh.template get<MeshDataComponent<Handle>>();
183 auto& meshData = meshDataComponent->meshData;
184
185 auto& openglMesh = mesh.template add<OpenGLMeshComponent<Handle>>();
186
187 openglMesh.indexCount = meshData.indices.size();
188 openglMesh.primitiveType = OpenGLEnumMapper::toOpenGL(meshData.primitiveType);
189
190 auto* vertexAttributeLayoutComponent = mesh.template get<VertexAttributeLayoutComponent<Handle, PerVertex>>();
191 assert(vertexAttributeLayoutComponent && "Expected a VertexAttributeLayoutComponent for PerVertex attributes");
192 auto* instancedAttributeLayoutComponent = mesh.template get<VertexAttributeLayoutComponent<Handle, PerInstance>>();
193
194 glGenVertexArrays(1, &openglMesh.vao);
195 glGenBuffers(1, &openglMesh.vbo);
196 glGenBuffers(1, &openglMesh.ebo);
197
198 if (instancedAttributeLayoutComponent) {
199 glGenBuffers(1, &openglMesh.instanceVbo);
200 }
201
202 glBindVertexArray(openglMesh.vao);
203
204 // vertex buffer
205 glBindBuffer(GL_ARRAY_BUFFER, openglMesh.vbo);
206 glBufferData(
207 GL_ARRAY_BUFFER,
208 meshData.vertices.size() * sizeof(Vertex),
209 &(meshData.vertices)[0],
210 GL_STATIC_DRAW
211 );
212
213 // element buffer
214 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, openglMesh.ebo);
215 glBufferData(
216 GL_ELEMENT_ARRAY_BUFFER,
217 meshData.indices.size() * sizeof(unsigned int),
218 &(meshData.indices)[0],
219 GL_STATIC_DRAW
220 );
221
222 // per instance buffer
223 buildVertexAttributeLayout<PerVertex>(vertexAttributeLayoutComponent);
224 mesh.template remove<VertexAttributeLayoutComponent<Handle, PerVertex>>();
225
226 if (instancedAttributeLayoutComponent) {
227 glBindBuffer(GL_ARRAY_BUFFER, openglMesh.instanceVbo);
228 buildVertexAttributeLayout<PerInstance>(instancedAttributeLayoutComponent);
229 mesh.template remove<VertexAttributeLayoutComponent<Handle, PerInstance>>();
230 }
231
232
233 glBindVertexArray(0);
234 glBindBuffer(GL_ARRAY_BUFFER, 0);
235 return true;
236 }

Private Member Attributes

meshHandles_

template <typename THandle, typename TCommandBuffer = NullCommandBuffer>
std::vector<MeshHandle> helios::opengl::OpenGLMeshUploadManager< THandle, TCommandBuffer >::meshHandles_

Pending mesh handles queued for upload during flush(...).

Definition at line 79 of file OpenGLMeshUploadManager.ixx.

79 std::vector<MeshHandle> meshHandles_;

renderResourceWorld_

template <typename THandle, typename TCommandBuffer = NullCommandBuffer>
RenderResourceWorld& helios::opengl::OpenGLMeshUploadManager< THandle, TCommandBuffer >::renderResourceWorld_

Render-resource world used to resolve mesh entities by handle.

Definition at line 74 of file OpenGLMeshUploadManager.ixx.

74 RenderResourceWorld& renderResourceWorld_;

Private Static Attributes

logger_

template <typename THandle, typename TCommandBuffer = NullCommandBuffer>
const Logger& helios::opengl::OpenGLMeshUploadManager< THandle, TCommandBuffer >::logger_ = LogManager::loggerForScope(HELIOS_LOG_SCOPE)
static

Definition at line 82 of file OpenGLMeshUploadManager.ixx.

82 inline static const Logger& logger_ = LogManager::loggerForScope(HELIOS_LOG_SCOPE);

The documentation for this class was generated from the following file:


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.