GL.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. #include "rendering/GL.h"
  2. #include "GL/glew.h"
  3. #include "data/Array.h"
  4. #include "utils/Logger.h"
  5. #include "utils/Utility.h"
  6. static_assert(Core::IsSame<GL::Shader, GLuint>, "shader has invalid type");
  7. static_assert(Core::IsSame<GL::Program, GLuint>, "p has invalid type");
  8. static_assert(Core::IsSame<char, GLchar>, "char has invalid type");
  9. static_assert(Core::IsSame<int, GLint>, "int has invalid type");
  10. static_assert(Core::IsSame<float, GLfloat>, "float has invalid type");
  11. static_assert(Core::IsSame<GL::Texture, GLuint>, "texture has invalid type");
  12. static_assert(Core::IsSame<GL::Framebuffer, GLuint>,
  13. "framebuffer has invalid type");
  14. static_assert(Core::IsSame<GL::ColorAttachment, GLenum>,
  15. "color attachment has invalid type");
  16. static_assert(Core::IsSame<GL::VertexArray, GLuint>,
  17. "vertex array has invalid type");
  18. static_assert(Core::IsSame<GL::Buffer, GLuint>, "buffer has invalid type");
  19. GL::Attribute::Attribute(int count_, int size_, int type_, bool normalized_)
  20. : count(count_), size(size_), type(type_), normalized(normalized_) {
  21. }
  22. GL::Attribute GL::Attribute::newFloat(int count) {
  23. return GL::Attribute(count, sizeof(float), GL_FLOAT, false);
  24. }
  25. GL::Attribute GL::Attribute::newColor(int count) {
  26. return GL::Attribute(count, sizeof(unsigned char), GL_UNSIGNED_BYTE, true);
  27. }
  28. GL::Attribute GL::Attribute::newDummy() {
  29. return GL::Attribute(0, 0, -1, false);
  30. }
  31. bool GL::Attribute::isDummy() const {
  32. return type == -1;
  33. }
  34. int GL::Attribute::getSize() const {
  35. return count * size;
  36. }
  37. GL::TextureFormat::TextureFormat(int internalformat_, int format_, int type_)
  38. : internalformat(internalformat_), format(format_), type(type_) {
  39. }
  40. GL::TextureFormat GL::TextureFormat::color8(int channels) {
  41. switch(channels) {
  42. case 1: return TextureFormat(GL_RED, GL_RED, GL_UNSIGNED_BYTE);
  43. case 2: return TextureFormat(GL_RG, GL_RG, GL_UNSIGNED_BYTE);
  44. case 3: return TextureFormat(GL_RGB, GL_RGB, GL_UNSIGNED_BYTE);
  45. case 4: return TextureFormat(GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE);
  46. }
  47. LOG_ERROR(StringBuffer<50>(channels).append(
  48. " is not a valid amount of channels"));
  49. return unknown();
  50. }
  51. GL::TextureFormat GL::TextureFormat::float16(int channels) {
  52. switch(channels) {
  53. case 1: return TextureFormat(GL_R16F, GL_RED, GL_FLOAT);
  54. case 2: return TextureFormat(GL_RG16F, GL_RG, GL_FLOAT);
  55. case 3: return TextureFormat(GL_RGB16F, GL_RGB, GL_FLOAT);
  56. case 4: return TextureFormat(GL_RGBA16F, GL_RGBA, GL_FLOAT);
  57. }
  58. LOG_ERROR(StringBuffer<50>(channels).append(
  59. " is not a valid amount of channels"));
  60. return unknown();
  61. }
  62. GL::TextureFormat GL::TextureFormat::float32(int channels) {
  63. switch(channels) {
  64. case 1: return TextureFormat(GL_R32F, GL_RED, GL_FLOAT);
  65. case 2: return TextureFormat(GL_RG32F, GL_RG, GL_FLOAT);
  66. case 3: return TextureFormat(GL_RGB32F, GL_RGB, GL_FLOAT);
  67. case 4: return TextureFormat(GL_RGBA32F, GL_RGBA, GL_FLOAT);
  68. }
  69. LOG_ERROR(StringBuffer<50>(channels).append(
  70. " is not a valid amount of channels"));
  71. return unknown();
  72. }
  73. GL::TextureFormat GL::TextureFormat::depth16() {
  74. return TextureFormat(GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_FLOAT);
  75. }
  76. GL::TextureFormat GL::TextureFormat::depth32() {
  77. return TextureFormat(GL_DEPTH_COMPONENT32, GL_DEPTH_COMPONENT, GL_FLOAT);
  78. }
  79. GL::TextureFormat GL::TextureFormat::unknown() {
  80. return TextureFormat(-1, -1, -1);
  81. }
  82. bool GL::printError(const char* message) {
  83. GLenum error = glGetError();
  84. if(error != GL_NO_ERROR) {
  85. (void)message;
  86. LOG_ERROR(StringBuffer<100>(message).append(": ").append(error));
  87. return true;
  88. }
  89. return false;
  90. }
  91. Error GL::getError(const char* message) {
  92. GLenum error = glGetError();
  93. if(error != GL_NO_ERROR) {
  94. Error e = {message};
  95. e.message.append(": ").append(error);
  96. return e;
  97. }
  98. return {};
  99. }
  100. void GL::enableDepthTesting() {
  101. glEnable(GL_DEPTH_TEST);
  102. }
  103. void GL::disableDepthTesting() {
  104. glDisable(GL_DEPTH_TEST);
  105. }
  106. void GL::bindMainFramebuffer() {
  107. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  108. }
  109. void GL::clear() {
  110. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  111. }
  112. void GL::enableBlending() {
  113. glEnable(GL_BLEND);
  114. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  115. glBlendEquation(GL_FUNC_ADD);
  116. }
  117. void GL::disableBlending() {
  118. glDisable(GL_BLEND);
  119. }
  120. void GL::setViewport(int width, int height) {
  121. glViewport(0, 0, width, height);
  122. }
  123. void GL::vertexAttribPointer(int index, const Attribute& a, int stride,
  124. int offset) {
  125. glVertexAttribPointer(static_cast<GLuint>(index), a.count,
  126. static_cast<GLenum>(a.type), a.normalized, stride,
  127. static_cast<char*>(0) + offset);
  128. glEnableVertexAttribArray(static_cast<GLuint>(index));
  129. }
  130. GL::Program GL::createProgram() {
  131. return glCreateProgram();
  132. }
  133. void GL::attachShader(Program p, Shader s) {
  134. glAttachShader(p, s);
  135. }
  136. void GL::linkProgram(Program p) {
  137. glLinkProgram(p);
  138. }
  139. Error GL::getLinkerError(Program p) {
  140. GLint linked;
  141. glGetProgramiv(p, GL_LINK_STATUS, &linked);
  142. if(!linked) {
  143. Array<char, 256> log;
  144. glGetProgramInfoLog(p, log.getLength(), nullptr, log.begin());
  145. Error e = {"linker log: "};
  146. e.message.append(static_cast<const char*>(log.begin()));
  147. return e;
  148. }
  149. return {};
  150. }
  151. void GL::deleteShader(Shader s) {
  152. if(s != 0) {
  153. glDeleteShader(s);
  154. }
  155. }
  156. void GL::deleteProgram(Program p) {
  157. if(p != 0) {
  158. glDeleteProgram(p);
  159. }
  160. }
  161. GL::Shader GL::createShader(ShaderType type) {
  162. static constexpr GLenum MAP[] = {0,
  163. GL_VERTEX_SHADER,
  164. GL_FRAGMENT_SHADER,
  165. GL_GEOMETRY_SHADER,
  166. GL_TESS_CONTROL_SHADER,
  167. GL_TESS_EVALUATION_SHADER};
  168. static constexpr int MAP_SIZE =
  169. static_cast<int>(sizeof(MAP) / sizeof(GLenum));
  170. int index = static_cast<int>(type);
  171. GLenum realTyp = index < 0 || index >= MAP_SIZE ? MAP[0] : MAP[index];
  172. return glCreateShader(realTyp);
  173. }
  174. void GL::compileShader(Shader s, const char* code) {
  175. glShaderSource(s, 1, &code, nullptr);
  176. glCompileShader(s);
  177. }
  178. Error GL::getCompileError(Shader s) {
  179. GLint compiled;
  180. glGetShaderiv(s, GL_COMPILE_STATUS, &compiled);
  181. if(!compiled) {
  182. Array<char, 256> log;
  183. glGetShaderInfoLog(s, log.getLength(), nullptr, log.begin());
  184. Error e = {"compiler log: "};
  185. e.message.append(static_cast<const char*>(log.begin()));
  186. return e;
  187. }
  188. return {};
  189. }
  190. void GL::useProgram(Program p) {
  191. glUseProgram(p);
  192. }
  193. void GL::setMatrix(Program p, const char* name, const float* data) {
  194. glUniformMatrix4fv(glGetUniformLocation(p, name), 1, GL_TRUE, data);
  195. }
  196. void GL::setInt(Program p, const char* name, int data) {
  197. glUniform1i(glGetUniformLocation(p, name), data);
  198. }
  199. void GL::setFloat(Program p, const char* name, float data) {
  200. glUniform1f(glGetUniformLocation(p, name), data);
  201. }
  202. void GL::set2Float(Program p, const char* name, const float* data) {
  203. glUniform2fv(glGetUniformLocation(p, name), 1, data);
  204. }
  205. void GL::set3Float(Program p, const char* name, const float* data) {
  206. glUniform3fv(glGetUniformLocation(p, name), 1, data);
  207. }
  208. void GL::set4Float(Program p, const char* name, const float* data) {
  209. glUniform4fv(glGetUniformLocation(p, name), 1, data);
  210. }
  211. void GL::texImage2D(const TextureFormat& tf, int width, int height,
  212. const void* data, int level) {
  213. glTexImage2D(GL_TEXTURE_2D, level, tf.internalformat, width, height, 0,
  214. static_cast<GLenum>(tf.format), static_cast<GLenum>(tf.type),
  215. data);
  216. }
  217. GL::Texture GL::genTexture() {
  218. Texture t;
  219. glGenTextures(1, &t);
  220. return t;
  221. }
  222. void GL::deleteTexture(Texture t) {
  223. if(t != 0) {
  224. glDeleteTextures(1, &t);
  225. }
  226. }
  227. void GL::setNearFilter2D() {
  228. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  229. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  230. }
  231. void GL::setMipMapNearFilter2D() {
  232. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  233. GL_NEAREST_MIPMAP_LINEAR);
  234. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  235. }
  236. void GL::setLinearFilter2D() {
  237. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  238. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  239. }
  240. void GL::setMipMapLinearFilter2D() {
  241. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  242. GL_LINEAR_MIPMAP_LINEAR);
  243. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  244. }
  245. void GL::setClampWrap2D() {
  246. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  247. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  248. }
  249. void GL::setRepeatWrap2D() {
  250. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  251. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  252. }
  253. void GL::bindTexture2D(Texture t) {
  254. glBindTexture(GL_TEXTURE_2D, t);
  255. }
  256. void GL::activeTexture(int index) {
  257. glActiveTexture(static_cast<GLenum>(GL_TEXTURE0 + index));
  258. }
  259. void GL::generateMipmap2D(int maxLevels) {
  260. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
  261. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, maxLevels);
  262. glGenerateMipmap(GL_TEXTURE_2D);
  263. }
  264. void GL::deleteFramebuffers(Framebuffer fb) {
  265. if(fb != 0) {
  266. glDeleteFramebuffers(1, &fb);
  267. }
  268. }
  269. GL::Framebuffer GL::genFramebuffer() {
  270. Framebuffer fb;
  271. glGenFramebuffers(1, &fb);
  272. return fb;
  273. }
  274. void GL::bindFramebuffer(Framebuffer fb) {
  275. glBindFramebuffer(GL_FRAMEBUFFER, fb);
  276. }
  277. void GL::framebufferDepthTexture2D(Texture t) {
  278. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D,
  279. t, 0);
  280. }
  281. GL::ColorAttachment GL::framebufferColorTexture2D(Texture t, int index) {
  282. GLenum c = static_cast<GLenum>(GL_COLOR_ATTACHMENT0 + index);
  283. glFramebufferTexture2D(GL_FRAMEBUFFER, c, GL_TEXTURE_2D, t, 0);
  284. return c;
  285. }
  286. void GL::drawBuffers(int length, ColorAttachment* c) {
  287. glDrawBuffers(length, c);
  288. }
  289. Error GL::getFramebufferError() {
  290. GLenum error = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  291. if(error != GL_FRAMEBUFFER_COMPLETE) {
  292. Error e = {"framebuffer error: "};
  293. e.message.append(error);
  294. return e;
  295. }
  296. return {};
  297. }
  298. GL::VertexArray GL::genVertexArray() {
  299. VertexArray va;
  300. glGenVertexArrays(1, &va);
  301. return va;
  302. }
  303. GL::Buffer GL::genBuffer() {
  304. Buffer b;
  305. glGenBuffers(1, &b);
  306. return b;
  307. }
  308. void GL::deleteBuffer(Buffer b) {
  309. if(b != 0) {
  310. glDeleteBuffers(1, &b);
  311. }
  312. }
  313. void GL::deleteVertexArray(VertexArray va) {
  314. if(va != 0) {
  315. glDeleteVertexArrays(1, &va);
  316. }
  317. }
  318. void GL::bindVertexArray(VertexArray va) {
  319. glBindVertexArray(va);
  320. }
  321. void GL::bindBuffer(Buffer b) {
  322. glBindBuffer(GL_ARRAY_BUFFER, b);
  323. }
  324. void GL::bufferData(int size, const void* data, BufferUsage usage) {
  325. static constexpr GLenum MAP[] = {0, GL_STATIC_DRAW, GL_STREAM_DRAW,
  326. GL_DYNAMIC_DRAW};
  327. static constexpr int MAP_SIZE =
  328. static_cast<int>(sizeof(MAP) / sizeof(GLenum));
  329. int index = static_cast<int>(usage);
  330. GLenum realUsage = index < 0 || index >= MAP_SIZE ? MAP[0] : MAP[index];
  331. glBufferData(GL_ARRAY_BUFFER, size, data, realUsage);
  332. }
  333. void GL::bufferSubData(int offset, int size, const void* data) {
  334. glBufferSubData(GL_ARRAY_BUFFER, offset, size, data);
  335. }
  336. void GL::drawTriangles(int offset, int vertices) {
  337. glDrawArrays(GL_TRIANGLES, offset, vertices);
  338. }
  339. void GL::drawTriangleStrip(int offset, int vertices) {
  340. glDrawArrays(GL_TRIANGLE_STRIP, offset, vertices);
  341. }
  342. void GL::drawPoints(int offset, int vertices) {
  343. glDrawArrays(GL_POINTS, offset, vertices);
  344. }