GLUtil.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. #include "GLUtil.hh"
  2. #include "File.hh"
  3. #include "FileContext.hh"
  4. #include "FileException.hh"
  5. #include "InitException.hh"
  6. #include "vla.hh"
  7. #include "Version.hh"
  8. #include <iostream>
  9. #include <vector>
  10. #include <cstdio>
  11. using std::string;
  12. using namespace openmsx;
  13. namespace gl {
  14. /*
  15. void checkGLError(const string& prefix)
  16. {
  17. GLenum error = glGetError();
  18. if (error != GL_NO_ERROR) {
  19. string err = (char*)gluErrorString(error);
  20. std::cerr << "GL error: " << prefix << ": " << err << '\n';
  21. }
  22. }
  23. */
  24. // class Texture
  25. Texture::Texture(bool interpolation, bool wrap)
  26. {
  27. allocate();
  28. setInterpolation(interpolation);
  29. setWrapMode(wrap);
  30. }
  31. void Texture::allocate()
  32. {
  33. glGenTextures(1, &textureId);
  34. }
  35. void Texture::reset()
  36. {
  37. glDeleteTextures(1, &textureId); // ok to delete 0-texture
  38. textureId = 0;
  39. }
  40. void Texture::setInterpolation(bool interpolation)
  41. {
  42. bind();
  43. int mode = interpolation ? GL_LINEAR : GL_NEAREST;
  44. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mode);
  45. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mode);
  46. }
  47. void Texture::setWrapMode(bool wrap)
  48. {
  49. bind();
  50. int mode = wrap ? GL_REPEAT : GL_CLAMP_TO_EDGE;
  51. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, mode);
  52. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, mode);
  53. }
  54. // class ColorTexture
  55. ColorTexture::ColorTexture(GLsizei width_, GLsizei height_)
  56. {
  57. resize(width_, height_);
  58. }
  59. void ColorTexture::resize(GLsizei width_, GLsizei height_)
  60. {
  61. width = width_;
  62. height = height_;
  63. bind();
  64. glTexImage2D(
  65. GL_TEXTURE_2D, // target
  66. 0, // level
  67. GL_RGBA8, // internal format
  68. width, // width
  69. height, // height
  70. 0, // border
  71. GL_BGRA, // format
  72. GL_UNSIGNED_BYTE, // type
  73. nullptr); // data
  74. }
  75. // class FrameBufferObject
  76. FrameBufferObject::FrameBufferObject(Texture& texture)
  77. {
  78. glGenFramebuffers(1, &bufferId);
  79. push();
  80. glFramebufferTexture2D(GL_FRAMEBUFFER,
  81. GL_COLOR_ATTACHMENT0,
  82. GL_TEXTURE_2D, texture.textureId, 0);
  83. bool success = glCheckFramebufferStatus(GL_FRAMEBUFFER) ==
  84. GL_FRAMEBUFFER_COMPLETE;
  85. pop();
  86. if (!success) {
  87. throw InitException(
  88. "Your OpenGL implementation support for "
  89. "framebuffer objects is too limited.");
  90. }
  91. }
  92. FrameBufferObject::~FrameBufferObject()
  93. {
  94. pop();
  95. glDeleteFramebuffers(1, &bufferId);
  96. }
  97. void FrameBufferObject::push()
  98. {
  99. glGetIntegerv(GL_FRAMEBUFFER_BINDING, &previousId);
  100. glBindFramebuffer(GL_FRAMEBUFFER, bufferId);
  101. }
  102. void FrameBufferObject::pop()
  103. {
  104. glBindFramebuffer(GL_FRAMEBUFFER, GLuint(previousId));
  105. }
  106. // class Shader
  107. Shader::Shader(GLenum type, const string& filename)
  108. {
  109. init(type, {}, filename);
  110. }
  111. Shader::Shader(GLenum type, const string& header, const string& filename)
  112. {
  113. init(type, header, filename);
  114. }
  115. void Shader::init(GLenum type, const string& header, const string& filename)
  116. {
  117. // Load shader source.
  118. string source = "#version 330\n" + header;
  119. try {
  120. File file(systemFileContext().resolve("shaders/" + filename));
  121. auto mmap = file.mmap();
  122. source.append(reinterpret_cast<const char*>(mmap.data()),
  123. mmap.size());
  124. } catch (FileException& e) {
  125. std::cerr << "Cannot find shader: " << e.getMessage() << '\n';
  126. handle = 0;
  127. return;
  128. }
  129. // Allocate shader handle.
  130. handle = glCreateShader(type);
  131. if (handle == 0) {
  132. std::cerr << "Failed to allocate shader\n";
  133. return;
  134. }
  135. // Set shader source.
  136. const char* sourcePtr = source.c_str();
  137. glShaderSource(handle, 1, &sourcePtr, nullptr);
  138. // Compile shader and print any errors and warnings.
  139. glCompileShader(handle);
  140. const bool ok = isOK();
  141. GLint infoLogLength = 0;
  142. glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &infoLogLength);
  143. // note: the null terminator is included, so empty string has length 1
  144. if (!ok || (!Version::RELEASE && infoLogLength > 1)) {
  145. VLA(GLchar, infoLog, infoLogLength);
  146. glGetShaderInfoLog(handle, infoLogLength, nullptr, infoLog);
  147. fprintf(stderr, "%s(s) compiling shader \"%s\":\n%s",
  148. ok ? "Warning" : "Error", filename.c_str(),
  149. infoLogLength > 1 ? infoLog : "(no details available)\n");
  150. }
  151. }
  152. Shader::~Shader()
  153. {
  154. glDeleteShader(handle); // ok to delete '0'
  155. }
  156. bool Shader::isOK() const
  157. {
  158. if (handle == 0) return false;
  159. GLint compileStatus = GL_FALSE;
  160. glGetShaderiv(handle, GL_COMPILE_STATUS, &compileStatus);
  161. return compileStatus == GL_TRUE;
  162. }
  163. // class VertexShader
  164. VertexShader::VertexShader(const string& filename)
  165. : Shader(GL_VERTEX_SHADER, filename)
  166. {
  167. }
  168. VertexShader::VertexShader(const string& header, const string& filename)
  169. : Shader(GL_VERTEX_SHADER, header, filename)
  170. {
  171. }
  172. // class FragmentShader
  173. FragmentShader::FragmentShader(const string& filename)
  174. : Shader(GL_FRAGMENT_SHADER, filename)
  175. {
  176. }
  177. FragmentShader::FragmentShader(const string& header, const string& filename)
  178. : Shader(GL_FRAGMENT_SHADER, header, filename)
  179. {
  180. }
  181. // class ShaderProgram
  182. void ShaderProgram::allocate()
  183. {
  184. handle = glCreateProgram();
  185. if (handle == 0) {
  186. std::cerr << "Failed to allocate program\n";
  187. }
  188. }
  189. void ShaderProgram::reset()
  190. {
  191. glDeleteProgram(handle); // ok to delete '0'
  192. handle = 0;
  193. }
  194. bool ShaderProgram::isOK() const
  195. {
  196. if (handle == 0) return false;
  197. GLint linkStatus = GL_FALSE;
  198. glGetProgramiv(handle, GL_LINK_STATUS, &linkStatus);
  199. return linkStatus == GL_TRUE;
  200. }
  201. void ShaderProgram::attach(const Shader& shader)
  202. {
  203. // Sanity check on this program.
  204. if (handle == 0) return;
  205. // Sanity check on the shader.
  206. if (!shader.isOK()) return;
  207. // Attach it.
  208. glAttachShader(handle, shader.handle);
  209. }
  210. void ShaderProgram::link()
  211. {
  212. // Sanity check on this program.
  213. if (handle == 0) return;
  214. // Link the program and print any errors and warnings.
  215. glLinkProgram(handle);
  216. const bool ok = isOK();
  217. GLint infoLogLength = 0;
  218. glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &infoLogLength);
  219. // note: the null terminator is included, so empty string has length 1
  220. if (!ok || (!Version::RELEASE && infoLogLength > 1)) {
  221. VLA(GLchar, infoLog, infoLogLength);
  222. glGetProgramInfoLog(handle, infoLogLength, nullptr, infoLog);
  223. fprintf(stderr, "%s(s) linking shader program:\n%s\n",
  224. ok ? "Warning" : "Error",
  225. infoLogLength > 1 ? infoLog : "(no details available)\n");
  226. }
  227. }
  228. void ShaderProgram::bindAttribLocation(unsigned index, const char* name)
  229. {
  230. glBindAttribLocation(handle, index, name);
  231. }
  232. GLint ShaderProgram::getUniformLocation(const char* name) const
  233. {
  234. // Sanity check on this program.
  235. if (!isOK()) return -1;
  236. return glGetUniformLocation(handle, name);
  237. }
  238. void ShaderProgram::activate() const
  239. {
  240. glUseProgram(handle);
  241. }
  242. // only useful for debugging
  243. void ShaderProgram::validate() const
  244. {
  245. glValidateProgram(handle);
  246. GLint validateStatus = GL_FALSE;
  247. glGetProgramiv(handle, GL_VALIDATE_STATUS, &validateStatus);
  248. GLint infoLogLength = 0;
  249. glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &infoLogLength);
  250. // note: the null terminator is included, so empty string has length 1
  251. VLA(GLchar, infoLog, infoLogLength);
  252. glGetProgramInfoLog(handle, infoLogLength, nullptr, infoLog);
  253. std::cout << "Validate "
  254. << ((validateStatus == GL_TRUE) ? "OK" : "FAIL")
  255. << ": " << infoLog << '\n';
  256. }
  257. // class BufferObject
  258. BufferObject::BufferObject()
  259. {
  260. glGenBuffers(1, &bufferId);
  261. }
  262. BufferObject::~BufferObject()
  263. {
  264. glDeleteBuffers(1, &bufferId); // ok to delete 0-buffer
  265. }
  266. // class VertexArray
  267. VertexArray::VertexArray()
  268. {
  269. glGenVertexArrays(1, &bufferId);
  270. }
  271. VertexArray::~VertexArray()
  272. {
  273. unbind();
  274. glDeleteVertexArrays(1, &bufferId); // ok to delete 0-buffer
  275. }
  276. } // namespace gl