-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathGLResourceMan.cpp
More file actions
160 lines (138 loc) · 5.33 KB
/
GLResourceMan.cpp
File metadata and controls
160 lines (138 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "GLResourceMan.h"
#include "ContentFile.h"
#include "RTEError.h"
#include "GLCheck.h"
#include "allegro.h"
#include <algorithm>
#include "tracy/Tracy.hpp"
#include "tracy/TracyOpenGL.hpp"
#include "raylib/raylib.h"
#include "raylib/rlgl.h"
using namespace RTE;
GLResourceMan::GLResourceMan() = default;
GLResourceMan::~GLResourceMan() = default;
void GLResourceMan::Clear() {
for (auto prog: m_Shaders) {
GL_CHECK(glDeleteProgram(prog));
}
for (auto& texture: m_StaticTextures) {
glDeleteTextures(1, &texture->m_Texture);
}
for (auto& shaderData: m_ShaderCache) {
glDeleteShader(shaderData.second.second);
}
}
void GLResourceMan::Destroy() { Clear(); }
void GLResourceMan::Initialize() { Clear(); }
GLuint GLResourceMan::CompileShader(const std::string& filename, ShaderType type) {
return 0;
}
GLuint GLResourceMan::MakeGLProgram() {
return glCreateProgram();
}
// std::shared_ptr<Shader> GLResourceMan::MakeShaderProgram(const std::string& name, const std::string& vertexShaderPath, const std::string& fragmentShaderPath) {
// if (m_Shaders.find(name) != m_Shaders.end()) {
// RTEAbort("Attempted to reregister Shader: " + name);
// } else {
// m_Shaders[name] = std::make_shared<Shader>(vertexShaderPath, fragmentShaderPath);
// return m_Shaders[name];
// }
// return nullptr;
// }
GLBitmapInfo* GLResourceMan::GetBitmapInfo(BITMAP* bitmap) {
return reinterpret_cast<GLBitmapInfo*>(bitmap->extra);
}
Texture2D GLResourceMan::GetStaticTextureFromFile(const std::string& filename) {
BITMAP* bitmap = ContentFile(filename.c_str()).GetAsBitmap();
return GetStaticTextureFromBitmap(bitmap);
}
GLBitmapInfo* GLResourceMan::MakeBitmapInfo() {
m_StaticTextures.emplace_back(new GLBitmapInfo);
m_StaticTextures.back()->m_ID = m_StaticTextures.size();
return m_StaticTextures.back().get();
}
Texture2D GLResourceMan::GetStaticTextureFromBitmap(BITMAP* bitmap) {
if (!bitmap->extra) {
m_StaticTextures.emplace_back(new GLBitmapInfo);
m_StaticTextures.back()->m_ID = m_StaticTextures.size();
bitmap->extra = reinterpret_cast<void*>(m_StaticTextures.back().get());
GL_CHECK(glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap_color_depth(bitmap) == 8 ? 1 : 4));
m_StaticTextures.back()->m_Texture = rlLoadTexture(bitmap->line[0], bitmap->w, bitmap->h, bitmap_color_depth(bitmap) == 8 ? PIXELFORMAT_UNCOMPRESSED_GRAYSCALE : PIXELFORMAT_UNCOMPRESSED_R8G8B8A8, 1);
return {
.id = m_StaticTextures.back()->m_Texture,
.width = bitmap->w,
.height = bitmap->h,
.mipmaps = 0,
.format = bitmap_color_depth(bitmap) == 8 ? PIXELFORMAT_UNCOMPRESSED_GRAYSCALE : PIXELFORMAT_UNCOMPRESSED_R8G8B8A8};
} else {
return {
.id = GetBitmapInfo(bitmap)->m_Texture,
.width = bitmap->w,
.height = bitmap->h,
.mipmaps = 0,
.format = 0};
}
return {0, 0, 0, 0, -1};
}
GLuint GLResourceMan::GetDynamicUploadBuffer(BITMAP* bitmap) {
if (!bitmap->extra) {
GetStaticTextureFromBitmap(bitmap);
}
GLBitmapInfo* info = GetBitmapInfo(bitmap);
if (!info->m_UpdateBuffer) {
GLuint updateBuffer;
glGenBuffers(1, &updateBuffer);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, updateBuffer);
glBufferData(GL_PIXEL_UNPACK_BUFFER, bitmap->w * bitmap->h * bitmap_color_depth(bitmap) / 8, nullptr, GL_DYNAMIC_DRAW);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
info->m_UpdateBuffer = updateBuffer;
}
return info->m_UpdateBuffer;
}
GLuint GLResourceMan::UpdateDynamicBitmap(BITMAP* bitmap, bool updated, const std::vector<Box>& updateRegions) {
ZoneScopedN("Bitmap Upload");
GLuint texture = GetStaticTextureFromBitmap(bitmap).id;
if (updated) {
glBindTexture(GL_TEXTURE_2D, texture);
glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap_color_depth(bitmap) == 8 ? 1 : 4);
if (updateRegions.size() == 0) {
GL_CHECK(glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bitmap->w, bitmap->h, bitmap_color_depth(bitmap) == 8 ? GL_RED : GL_RGBA, GL_UNSIGNED_BYTE, bitmap->line[0]));
} else {
int bytesPerPixel = bitmap_color_depth(bitmap) / 8;
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, GetDynamicUploadBuffer(bitmap));
std::vector<size_t> offsets = {0};
for (size_t i = 0; i < updateRegions.size(); ++i) {
std::vector<unsigned char> pixels(updateRegions[i].m_Width * updateRegions[i].m_Height * bytesPerPixel);
for (size_t y = 0; y < updateRegions[i].m_Height; y++) {
memcpy(
pixels.data() + y * static_cast<int>(updateRegions[i].m_Width) * bytesPerPixel,
bitmap->line[y + updateRegions[i].m_Corner.GetFloorIntY()] + updateRegions[i].m_Corner.GetFloorIntX(),
updateRegions[i].m_Width * bytesPerPixel);
}
glBufferSubData(GL_PIXEL_UNPACK_BUFFER, offsets[i], updateRegions[i].m_Width * updateRegions[i].m_Height * bytesPerPixel, pixels.data());
offsets.emplace_back(updateRegions[i].m_Width * updateRegions[i].m_Height * bytesPerPixel);
}
for (size_t i = 0; i < updateRegions.size(); ++i) {
GL_CHECK(glTexSubImage2D(
GL_TEXTURE_2D,
0,
updateRegions[i].m_Corner.GetFloorIntX(),
updateRegions[i].m_Corner.GetFloorIntY(),
updateRegions[i].m_Width,
updateRegions[i].m_Height,
bytesPerPixel == 1 ? GL_RED : GL_RGBA,
GL_UNSIGNED_BYTE,
(void*)offsets[i]
));
}
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
}
}
return texture;
}
void GLResourceMan::DestroyBitmapInfo(BITMAP* bitmap) {
GLBitmapInfo* info = GetBitmapInfo(bitmap);
if (info) {
rlUnloadTexture(info->m_Texture);
}
}