GL.cpp 12 KB

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