GL.cpp 12 KB

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