v0.3.0 API Reference
OGLResourceCache.h
1//
2// OGLResourceCache.h
3// avara3d
4//
5// Created by Morgan Davis on 12/31/25.
6// Copyright © 2026 Morgan K Davis. All rights reserved.
7//
8
9#ifndef AVARA3D_RENDER_BACKEND_OPENGL_OGLRESOURCECACHE_H
10#define AVARA3D_RENDER_BACKEND_OPENGL_OGLRESOURCECACHE_H
11
12#include <array>
13#include <cstdint>
14#include <unordered_map>
15#include <vector>
16
17#include "a3d/render/PipelineDesc.h"
18#include "a3d/render/backend/opengl/GLTypes.h"
19
20namespace a3d {
21
22class Material;
23class MeshElement;
24class OGLMemoryTracker;
25class Texture;
26
27// [Internal Types]
28
29struct OGLPipeline {
30 PipelineDesc desc = {};
31 gl::uint_t program = 0;
32};
33
34struct OGLMeshElement {
35 gl::uint_t vao = 0;
36 gl::uint_t vbo = 0;
37 gl::uint_t ebo = 0;
38 gl::enum_t indexType = gl::value::unsigned_int;
39 uint32_t indexCount = 0;
40 uint32_t vertexCount = 0;
41 VertexLayout vertexLayoutKey = VertexLayout::None;
42};
43
44static constexpr size_t NUM_MATERIAL_PROPERTY_SLOTS = 4;
45
46struct OGLMaterial {
47 std::array<gl::uint_t, NUM_MATERIAL_PROPERTY_SLOTS> tex = {};
48 float specularExponent = 75.0f;
49 float uvScale = 1.0f;
50};
51
52struct OGLTexture {
53 gl::uint_t id = 0;
54};
55
56class OGLResourceCache {
57
58public:
59 // [Internal Lifecycle Functions]
60
61 explicit OGLResourceCache(OGLMemoryTracker& memoryTracker);
62
63 OGLResourceCache(const OGLResourceCache&) = delete;
64 OGLResourceCache& operator=(const OGLResourceCache&) = delete;
65
66 OGLResourceCache(OGLResourceCache&&) = delete;
67 OGLResourceCache& operator=(OGLResourceCache&&) = delete;
68
69 ~OGLResourceCache();
70
71 // [Internal Member Functions]
72
73 PipelineId ensurePipeline(const PipelineDesc& desc, gl::uint_t program);
74 const OGLPipeline& pipeline(PipelineId pipelineId) const;
75 const OGLMeshElement& ensureMeshElement(MeshElement& element);
76 const OGLMaterial& ensureMaterial(Material& material);
77 const OGLTexture& ensureTexture(Texture& texture);
78
79private:
80 // [Private Member Variables]
81
82 OGLMemoryTracker& _memoryTracker;
83 std::unordered_map<PipelineDesc, PipelineId, PipelineDescHash> _pipelineMap;
84 std::vector<OGLPipeline> _pipelineList;
85 std::unordered_map<MeshElement*, OGLMeshElement> _meshElementMap;
86 std::unordered_map<Material*, OGLMaterial> _materialMap;
87 std::unordered_map<Texture*, OGLTexture> _textureMap;
88};
89
90} // namespace a3d
91
92#endif // AVARA3D_RENDER_BACKEND_OPENGL_OGLRESOURCECACHE_H