SoftwareGLContext.cpp 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037
  1. /*
  2. * Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
  3. * Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "SoftwareGLContext.h"
  8. #include "GLStruct.h"
  9. #include "SoftwareRasterizer.h"
  10. #include <AK/Assertions.h>
  11. #include <AK/Debug.h>
  12. #include <AK/Format.h>
  13. #include <AK/QuickSort.h>
  14. #include <AK/TemporaryChange.h>
  15. #include <AK/Variant.h>
  16. #include <AK/Vector.h>
  17. #include <LibGfx/Bitmap.h>
  18. #include <LibGfx/Painter.h>
  19. #include <LibGfx/Vector4.h>
  20. using AK::dbgln;
  21. namespace GL {
  22. // FIXME: We should set this up when we create the context!
  23. static constexpr size_t MATRIX_STACK_LIMIT = 1024;
  24. #define APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(name, ...) \
  25. if (should_append_to_listing()) { \
  26. append_to_listing<&SoftwareGLContext::name>(__VA_ARGS__); \
  27. if (!should_execute_after_appending_to_listing()) \
  28. return; \
  29. }
  30. #define RETURN_WITH_ERROR_IF(condition, error) \
  31. if (condition) { \
  32. if (m_error == GL_NO_ERROR) \
  33. m_error = error; \
  34. return; \
  35. }
  36. #define RETURN_VALUE_WITH_ERROR_IF(condition, error, return_value) \
  37. if (condition) { \
  38. if (m_error == GL_NO_ERROR) \
  39. m_error = error; \
  40. return return_value; \
  41. }
  42. SoftwareGLContext::SoftwareGLContext(Gfx::Bitmap& frontbuffer)
  43. : m_frontbuffer(frontbuffer)
  44. , m_rasterizer(frontbuffer.size())
  45. {
  46. }
  47. void SoftwareGLContext::gl_begin(GLenum mode)
  48. {
  49. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_begin, mode);
  50. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  51. RETURN_WITH_ERROR_IF(mode < GL_TRIANGLES || mode > GL_POLYGON, GL_INVALID_ENUM);
  52. m_current_draw_mode = mode;
  53. m_in_draw_state = true; // Certain commands will now generate an error
  54. }
  55. void SoftwareGLContext::gl_clear(GLbitfield mask)
  56. {
  57. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_clear, mask);
  58. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  59. RETURN_WITH_ERROR_IF(mask & ~(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT), GL_INVALID_ENUM);
  60. if (mask & GL_COLOR_BUFFER_BIT)
  61. m_rasterizer.clear_color(m_clear_color);
  62. if (mask & GL_DEPTH_BUFFER_BIT)
  63. m_rasterizer.clear_depth(static_cast<float>(m_clear_depth));
  64. }
  65. void SoftwareGLContext::gl_clear_color(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
  66. {
  67. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_clear_color, red, green, blue, alpha);
  68. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  69. m_clear_color = { red, green, blue, alpha };
  70. }
  71. void SoftwareGLContext::gl_clear_depth(GLdouble depth)
  72. {
  73. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_clear_depth, depth);
  74. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  75. m_clear_depth = depth;
  76. }
  77. void SoftwareGLContext::gl_color(GLdouble r, GLdouble g, GLdouble b, GLdouble a)
  78. {
  79. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_color, r, g, b, a);
  80. m_current_vertex_color = { (float)r, (float)g, (float)b, (float)a };
  81. }
  82. void SoftwareGLContext::gl_end()
  83. {
  84. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_end);
  85. // At this point, the user has effectively specified that they are done with defining the geometry
  86. // of what they want to draw. We now need to do a few things (https://www.khronos.org/opengl/wiki/Rendering_Pipeline_Overview):
  87. //
  88. // 1. Transform all of the vertices in the current vertex list into eye space by mulitplying the model-view matrix
  89. // 2. Transform all of the vertices from eye space into clip space by multiplying by the projection matrix
  90. // 3. If culling is enabled, we cull the desired faces (https://learnopengl.com/Advanced-OpenGL/Face-culling)
  91. // 4. Each element of the vertex is then divided by w to bring the positions into NDC (Normalized Device Coordinates)
  92. // 5. The vertices are sorted (for the rasteriser, how are we doing this? 3Dfx did this top to bottom in terms of vertex y coordinates)
  93. // 6. The vertices are then sent off to the rasteriser and drawn to the screen
  94. float scr_width = m_frontbuffer->width();
  95. float scr_height = m_frontbuffer->height();
  96. // Make sure we had a `glBegin` before this call...
  97. RETURN_WITH_ERROR_IF(!m_in_draw_state, GL_INVALID_OPERATION);
  98. triangle_list.clear_with_capacity();
  99. processed_triangles.clear_with_capacity();
  100. // Let's construct some triangles
  101. if (m_current_draw_mode == GL_TRIANGLES) {
  102. GLTriangle triangle;
  103. for (size_t i = 0; i < vertex_list.size(); i += 3) {
  104. triangle.vertices[0] = vertex_list.at(i);
  105. triangle.vertices[1] = vertex_list.at(i + 1);
  106. triangle.vertices[2] = vertex_list.at(i + 2);
  107. triangle_list.append(triangle);
  108. }
  109. } else if (m_current_draw_mode == GL_QUADS) {
  110. // We need to construct two triangles to form the quad
  111. GLTriangle triangle;
  112. VERIFY(vertex_list.size() % 4 == 0);
  113. for (size_t i = 0; i < vertex_list.size(); i += 4) {
  114. // Triangle 1
  115. triangle.vertices[0] = vertex_list.at(i);
  116. triangle.vertices[1] = vertex_list.at(i + 1);
  117. triangle.vertices[2] = vertex_list.at(i + 2);
  118. triangle_list.append(triangle);
  119. // Triangle 2
  120. triangle.vertices[0] = vertex_list.at(i + 2);
  121. triangle.vertices[1] = vertex_list.at(i + 3);
  122. triangle.vertices[2] = vertex_list.at(i);
  123. triangle_list.append(triangle);
  124. }
  125. } else if (m_current_draw_mode == GL_TRIANGLE_FAN) {
  126. GLTriangle triangle;
  127. triangle.vertices[0] = vertex_list.at(0); // Root vertex is always the vertex defined first
  128. for (size_t i = 1; i < vertex_list.size() - 1; i++) // This is technically `n-2` triangles. We start at index 1
  129. {
  130. triangle.vertices[1] = vertex_list.at(i);
  131. triangle.vertices[2] = vertex_list.at(i + 1);
  132. triangle_list.append(triangle);
  133. }
  134. } else if (m_current_draw_mode == GL_TRIANGLE_STRIP) {
  135. GLTriangle triangle;
  136. for (size_t i = 0; i < vertex_list.size() - 2; i++) {
  137. triangle.vertices[0] = vertex_list.at(i);
  138. triangle.vertices[1] = vertex_list.at(i + 1);
  139. triangle.vertices[2] = vertex_list.at(i + 2);
  140. triangle_list.append(triangle);
  141. }
  142. } else {
  143. vertex_list.clear_with_capacity();
  144. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  145. }
  146. vertex_list.clear_with_capacity();
  147. auto mvp = m_projection_matrix * m_model_view_matrix;
  148. // Now let's transform each triangle and send that to the GPU
  149. for (size_t i = 0; i < triangle_list.size(); i++) {
  150. GLTriangle& triangle = triangle_list.at(i);
  151. // First multiply the vertex by the MODELVIEW matrix and then the PROJECTION matrix
  152. triangle.vertices[0].position = mvp * triangle.vertices[0].position;
  153. triangle.vertices[1].position = mvp * triangle.vertices[1].position;
  154. triangle.vertices[2].position = mvp * triangle.vertices[2].position;
  155. // At this point, we're in clip space
  156. // Here's where we do the clipping. This is a really crude implementation of the
  157. // https://learnopengl.com/Getting-started/Coordinate-Systems
  158. // "Note that if only a part of a primitive e.g. a triangle is outside the clipping volume OpenGL
  159. // will reconstruct the triangle as one or more triangles to fit inside the clipping range. "
  160. //
  161. // ALL VERTICES ARE DEFINED IN A CLOCKWISE ORDER
  162. // Okay, let's do some face culling first
  163. m_clipped_vertices.clear_with_capacity();
  164. m_clipped_vertices.append(triangle.vertices[0]);
  165. m_clipped_vertices.append(triangle.vertices[1]);
  166. m_clipped_vertices.append(triangle.vertices[2]);
  167. m_clipper.clip_triangle_against_frustum(m_clipped_vertices);
  168. if (m_clipped_vertices.size() < 3)
  169. continue;
  170. for (auto& vec : m_clipped_vertices) {
  171. // perspective divide
  172. float w = vec.position.w();
  173. vec.position.set_x(vec.position.x() / w);
  174. vec.position.set_y(vec.position.y() / w);
  175. vec.position.set_z(vec.position.z() / w);
  176. vec.position.set_w(1 / w);
  177. // to screen space
  178. vec.position.set_x(scr_width / 2 + vec.position.x() * scr_width / 2);
  179. vec.position.set_y(scr_height / 2 - vec.position.y() * scr_height / 2);
  180. }
  181. GLTriangle tri;
  182. tri.vertices[0] = m_clipped_vertices[0];
  183. for (size_t i = 1; i < m_clipped_vertices.size() - 1; i++) {
  184. tri.vertices[1] = m_clipped_vertices[i];
  185. tri.vertices[2] = m_clipped_vertices[i + 1];
  186. processed_triangles.append(tri);
  187. }
  188. }
  189. for (size_t i = 0; i < processed_triangles.size(); i++) {
  190. GLTriangle& triangle = processed_triangles.at(i);
  191. // Let's calculate the (signed) area of the triangle
  192. // https://cp-algorithms.com/geometry/oriented-triangle-area.html
  193. float dxAB = triangle.vertices[0].position.x() - triangle.vertices[1].position.x(); // A.x - B.x
  194. float dxBC = triangle.vertices[1].position.x() - triangle.vertices[2].position.x(); // B.X - C.x
  195. float dyAB = triangle.vertices[0].position.y() - triangle.vertices[1].position.y();
  196. float dyBC = triangle.vertices[1].position.y() - triangle.vertices[2].position.y();
  197. float area = (dxAB * dyBC) - (dxBC * dyAB);
  198. if (area == 0.0f)
  199. continue;
  200. if (m_cull_faces) {
  201. bool is_front = (m_front_face == GL_CCW ? area < 0 : area > 0);
  202. if (is_front && (m_culled_sides == GL_FRONT || m_culled_sides == GL_FRONT_AND_BACK))
  203. continue;
  204. if (!is_front && (m_culled_sides == GL_BACK || m_culled_sides == GL_FRONT_AND_BACK))
  205. continue;
  206. }
  207. if (area > 0) {
  208. swap(triangle.vertices[0], triangle.vertices[1]);
  209. }
  210. m_rasterizer.submit_triangle(triangle, m_texture_units);
  211. }
  212. m_in_draw_state = false;
  213. }
  214. void SoftwareGLContext::gl_frustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near_val, GLdouble far_val)
  215. {
  216. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_frustum, left, right, bottom, top, near_val, far_val);
  217. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  218. // Let's do some math!
  219. // FIXME: Are we losing too much precision by doing this?
  220. float a = static_cast<float>((right + left) / (right - left));
  221. float b = static_cast<float>((top + bottom) / (top - bottom));
  222. float c = static_cast<float>(-((far_val + near_val) / (far_val - near_val)));
  223. float d = static_cast<float>(-((2 * (far_val * near_val)) / (far_val - near_val)));
  224. FloatMatrix4x4 frustum {
  225. ((2 * (float)near_val) / ((float)right - (float)left)), 0, a, 0,
  226. 0, ((2 * (float)near_val) / ((float)top - (float)bottom)), b, 0,
  227. 0, 0, c, d,
  228. 0, 0, -1, 0
  229. };
  230. if (m_current_matrix_mode == GL_PROJECTION) {
  231. m_projection_matrix = m_projection_matrix * frustum;
  232. } else if (m_current_matrix_mode == GL_MODELVIEW) {
  233. dbgln_if(GL_DEBUG, "glFrustum(): frustum created with curr_matrix_mode == GL_MODELVIEW!!!");
  234. m_projection_matrix = m_model_view_matrix * frustum;
  235. }
  236. }
  237. void SoftwareGLContext::gl_ortho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near_val, GLdouble far_val)
  238. {
  239. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_ortho, left, right, bottom, top, near_val, far_val);
  240. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  241. RETURN_WITH_ERROR_IF(left == right || bottom == top || near_val == far_val, GL_INVALID_VALUE);
  242. auto rl = right - left;
  243. auto tb = top - bottom;
  244. auto fn = far_val - near_val;
  245. auto tx = -(right + left) / rl;
  246. auto ty = -(top + bottom) / tb;
  247. auto tz = -(far_val + near_val) / fn;
  248. FloatMatrix4x4 projection {
  249. static_cast<float>(2 / rl), 0, 0, static_cast<float>(tx),
  250. 0, static_cast<float>(2 / tb), 0, static_cast<float>(ty),
  251. 0, 0, static_cast<float>(-2 / fn), static_cast<float>(tz),
  252. 0, 0, 0, 1
  253. };
  254. if (m_current_matrix_mode == GL_PROJECTION) {
  255. m_projection_matrix = m_projection_matrix * projection;
  256. } else if (m_current_matrix_mode == GL_MODELVIEW) {
  257. m_projection_matrix = m_model_view_matrix * projection;
  258. }
  259. }
  260. GLenum SoftwareGLContext::gl_get_error()
  261. {
  262. if (m_in_draw_state)
  263. return GL_INVALID_OPERATION;
  264. auto last_error = m_error;
  265. m_error = GL_NO_ERROR;
  266. return last_error;
  267. }
  268. GLubyte* SoftwareGLContext::gl_get_string(GLenum name)
  269. {
  270. RETURN_VALUE_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION, nullptr);
  271. switch (name) {
  272. case GL_VENDOR:
  273. return reinterpret_cast<GLubyte*>(const_cast<char*>("The SerenityOS Developers"));
  274. case GL_RENDERER:
  275. return reinterpret_cast<GLubyte*>(const_cast<char*>("SerenityOS OpenGL"));
  276. case GL_VERSION:
  277. return reinterpret_cast<GLubyte*>(const_cast<char*>("1.5"));
  278. case GL_EXTENSIONS:
  279. return reinterpret_cast<GLubyte*>(const_cast<char*>(""));
  280. default:
  281. dbgln_if(GL_DEBUG, "glGetString(): Unknown enum name!");
  282. break;
  283. }
  284. RETURN_VALUE_WITH_ERROR_IF(true, GL_INVALID_ENUM, nullptr);
  285. }
  286. void SoftwareGLContext::gl_load_identity()
  287. {
  288. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_load_identity);
  289. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  290. if (m_current_matrix_mode == GL_PROJECTION)
  291. m_projection_matrix = FloatMatrix4x4::identity();
  292. else if (m_current_matrix_mode == GL_MODELVIEW)
  293. m_model_view_matrix = FloatMatrix4x4::identity();
  294. else
  295. VERIFY_NOT_REACHED();
  296. }
  297. void SoftwareGLContext::gl_load_matrix(const FloatMatrix4x4& matrix)
  298. {
  299. if (should_append_to_listing()) {
  300. auto ptr = store_in_listing(matrix);
  301. append_to_listing<&SoftwareGLContext::gl_load_matrix>(*ptr);
  302. if (!should_execute_after_appending_to_listing())
  303. return;
  304. }
  305. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  306. if (m_current_matrix_mode == GL_PROJECTION)
  307. m_projection_matrix = matrix;
  308. else if (m_current_matrix_mode == GL_MODELVIEW)
  309. m_model_view_matrix = matrix;
  310. else
  311. VERIFY_NOT_REACHED();
  312. }
  313. void SoftwareGLContext::gl_matrix_mode(GLenum mode)
  314. {
  315. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_matrix_mode, mode);
  316. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  317. RETURN_WITH_ERROR_IF(mode < GL_MODELVIEW || mode > GL_PROJECTION, GL_INVALID_ENUM);
  318. m_current_matrix_mode = mode;
  319. }
  320. void SoftwareGLContext::gl_push_matrix()
  321. {
  322. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_push_matrix);
  323. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  324. dbgln_if(GL_DEBUG, "glPushMatrix(): Pushing matrix to the matrix stack (matrix_mode {})", m_current_matrix_mode);
  325. switch (m_current_matrix_mode) {
  326. case GL_PROJECTION:
  327. RETURN_WITH_ERROR_IF(m_projection_matrix_stack.size() >= MATRIX_STACK_LIMIT, GL_STACK_OVERFLOW);
  328. m_projection_matrix_stack.append(m_projection_matrix);
  329. break;
  330. case GL_MODELVIEW:
  331. RETURN_WITH_ERROR_IF(m_model_view_matrix_stack.size() >= MATRIX_STACK_LIMIT, GL_STACK_OVERFLOW);
  332. m_model_view_matrix_stack.append(m_model_view_matrix);
  333. break;
  334. default:
  335. dbgln_if(GL_DEBUG, "glPushMatrix(): Attempt to push matrix with invalid matrix mode {})", m_current_matrix_mode);
  336. return;
  337. }
  338. }
  339. void SoftwareGLContext::gl_pop_matrix()
  340. {
  341. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_pop_matrix);
  342. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  343. dbgln_if(GL_DEBUG, "glPopMatrix(): Popping matrix from matrix stack (matrix_mode = {})", m_current_matrix_mode);
  344. // FIXME: Make sure stack::top() doesn't cause any nasty issues if it's empty (that could result in a lockup/hang)
  345. switch (m_current_matrix_mode) {
  346. case GL_PROJECTION:
  347. RETURN_WITH_ERROR_IF(m_projection_matrix_stack.size() == 0, GL_STACK_UNDERFLOW);
  348. m_projection_matrix = m_projection_matrix_stack.take_last();
  349. break;
  350. case GL_MODELVIEW:
  351. RETURN_WITH_ERROR_IF(m_model_view_matrix_stack.size() == 0, GL_STACK_UNDERFLOW);
  352. m_model_view_matrix = m_model_view_matrix_stack.take_last();
  353. break;
  354. default:
  355. dbgln_if(GL_DEBUG, "glPopMatrix(): Attempt to pop matrix with invalid matrix mode, {}", m_current_matrix_mode);
  356. return;
  357. }
  358. }
  359. void SoftwareGLContext::gl_rotate(GLdouble angle, GLdouble x, GLdouble y, GLdouble z)
  360. {
  361. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_rotate, angle, x, y, z);
  362. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  363. FloatVector3 axis = { (float)x, (float)y, (float)z };
  364. axis.normalize();
  365. auto rotation_mat = Gfx::rotation_matrix(axis, static_cast<float>(angle * M_PI * 2 / 360));
  366. if (m_current_matrix_mode == GL_MODELVIEW)
  367. m_model_view_matrix = m_model_view_matrix * rotation_mat;
  368. else if (m_current_matrix_mode == GL_PROJECTION)
  369. m_projection_matrix = m_projection_matrix * rotation_mat;
  370. }
  371. void SoftwareGLContext::gl_scale(GLdouble x, GLdouble y, GLdouble z)
  372. {
  373. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_scale, x, y, z);
  374. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  375. if (m_current_matrix_mode == GL_MODELVIEW) {
  376. m_model_view_matrix = m_model_view_matrix * Gfx::scale_matrix(FloatVector3 { static_cast<float>(x), static_cast<float>(y), static_cast<float>(z) });
  377. } else if (m_current_matrix_mode == GL_PROJECTION) {
  378. m_projection_matrix = m_projection_matrix * Gfx::scale_matrix(FloatVector3 { static_cast<float>(x), static_cast<float>(y), static_cast<float>(z) });
  379. }
  380. }
  381. void SoftwareGLContext::gl_translate(GLdouble x, GLdouble y, GLdouble z)
  382. {
  383. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_translate, x, y, z);
  384. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  385. if (m_current_matrix_mode == GL_MODELVIEW) {
  386. m_model_view_matrix = m_model_view_matrix * Gfx::translation_matrix(FloatVector3 { (float)x, (float)y, (float)z });
  387. } else if (m_current_matrix_mode == GL_PROJECTION) {
  388. m_projection_matrix = m_projection_matrix * Gfx::translation_matrix(FloatVector3 { (float)x, (float)y, (float)z });
  389. }
  390. }
  391. void SoftwareGLContext::gl_vertex(GLdouble x, GLdouble y, GLdouble z, GLdouble w)
  392. {
  393. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_vertex, x, y, z, w);
  394. GLVertex vertex;
  395. vertex.position = { static_cast<float>(x), static_cast<float>(y), static_cast<float>(z), static_cast<float>(w) };
  396. vertex.color = m_current_vertex_color;
  397. vertex.tex_coord = { m_current_vertex_tex_coord.x(), m_current_vertex_tex_coord.y() };
  398. vertex_list.append(vertex);
  399. }
  400. // FIXME: We need to add `r` and `q` to our GLVertex?!
  401. void SoftwareGLContext::gl_tex_coord(GLfloat s, GLfloat t, GLfloat r, GLfloat q)
  402. {
  403. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_coord, s, t, r, q);
  404. m_current_vertex_tex_coord = { s, t, r, q };
  405. }
  406. void SoftwareGLContext::gl_viewport(GLint x, GLint y, GLsizei width, GLsizei height)
  407. {
  408. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_viewport, x, y, width, height);
  409. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  410. (void)(x);
  411. (void)(y);
  412. (void)(width);
  413. (void)(height);
  414. }
  415. void SoftwareGLContext::gl_enable(GLenum capability)
  416. {
  417. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_enable, capability);
  418. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  419. auto rasterizer_options = m_rasterizer.options();
  420. bool update_rasterizer_options = false;
  421. switch (capability) {
  422. case GL_CULL_FACE:
  423. m_cull_faces = true;
  424. break;
  425. case GL_DEPTH_TEST:
  426. m_depth_test_enabled = true;
  427. rasterizer_options.enable_depth_test = true;
  428. update_rasterizer_options = true;
  429. break;
  430. case GL_BLEND:
  431. m_blend_enabled = true;
  432. rasterizer_options.enable_blending = true;
  433. update_rasterizer_options = true;
  434. break;
  435. case GL_ALPHA_TEST:
  436. m_alpha_test_enabled = true;
  437. rasterizer_options.enable_alpha_test = true;
  438. update_rasterizer_options = true;
  439. break;
  440. case GL_FOG:
  441. rasterizer_options.fog_enabled = true;
  442. update_rasterizer_options = true;
  443. break;
  444. case GL_SCISSOR_TEST:
  445. rasterizer_options.scissor_enabled = true;
  446. update_rasterizer_options = true;
  447. break;
  448. case GL_TEXTURE_1D:
  449. m_active_texture_unit->set_texture_1d_enabled(true);
  450. break;
  451. case GL_TEXTURE_2D:
  452. m_active_texture_unit->set_texture_2d_enabled(true);
  453. break;
  454. case GL_TEXTURE_3D:
  455. m_active_texture_unit->set_texture_3d_enabled(true);
  456. break;
  457. case GL_TEXTURE_CUBE_MAP:
  458. m_active_texture_unit->set_texture_cube_map_enabled(true);
  459. break;
  460. default:
  461. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  462. }
  463. if (update_rasterizer_options)
  464. m_rasterizer.set_options(rasterizer_options);
  465. }
  466. void SoftwareGLContext::gl_disable(GLenum capability)
  467. {
  468. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_disable, capability);
  469. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  470. auto rasterizer_options = m_rasterizer.options();
  471. bool update_rasterizer_options = false;
  472. switch (capability) {
  473. case GL_CULL_FACE:
  474. m_cull_faces = false;
  475. break;
  476. case GL_DEPTH_TEST:
  477. m_depth_test_enabled = false;
  478. rasterizer_options.enable_depth_test = false;
  479. update_rasterizer_options = true;
  480. break;
  481. case GL_BLEND:
  482. m_blend_enabled = false;
  483. rasterizer_options.enable_blending = false;
  484. update_rasterizer_options = true;
  485. break;
  486. case GL_ALPHA_TEST:
  487. m_alpha_test_enabled = false;
  488. rasterizer_options.enable_alpha_test = false;
  489. update_rasterizer_options = true;
  490. break;
  491. case GL_FOG:
  492. rasterizer_options.fog_enabled = false;
  493. update_rasterizer_options = true;
  494. break;
  495. case GL_SCISSOR_TEST:
  496. rasterizer_options.scissor_enabled = false;
  497. update_rasterizer_options = true;
  498. break;
  499. case GL_TEXTURE_1D:
  500. m_active_texture_unit->set_texture_1d_enabled(false);
  501. break;
  502. case GL_TEXTURE_2D:
  503. m_active_texture_unit->set_texture_2d_enabled(false);
  504. break;
  505. case GL_TEXTURE_3D:
  506. m_active_texture_unit->set_texture_3d_enabled(false);
  507. break;
  508. case GL_TEXTURE_CUBE_MAP:
  509. m_active_texture_unit->set_texture_cube_map_enabled(false);
  510. break;
  511. default:
  512. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  513. }
  514. if (update_rasterizer_options)
  515. m_rasterizer.set_options(rasterizer_options);
  516. }
  517. GLboolean SoftwareGLContext::gl_is_enabled(GLenum capability)
  518. {
  519. RETURN_VALUE_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION, 0);
  520. auto rasterizer_options = m_rasterizer.options();
  521. switch (capability) {
  522. case GL_CULL_FACE:
  523. return m_cull_faces;
  524. case GL_DEPTH_TEST:
  525. return m_depth_test_enabled;
  526. case GL_BLEND:
  527. return m_blend_enabled;
  528. case GL_ALPHA_TEST:
  529. return m_alpha_test_enabled;
  530. case GL_FOG:
  531. return rasterizer_options.fog_enabled;
  532. case GL_SCISSOR_TEST:
  533. return rasterizer_options.scissor_enabled;
  534. }
  535. RETURN_VALUE_WITH_ERROR_IF(true, GL_INVALID_ENUM, 0);
  536. }
  537. void SoftwareGLContext::gl_gen_textures(GLsizei n, GLuint* textures)
  538. {
  539. RETURN_WITH_ERROR_IF(n < 0, GL_INVALID_VALUE);
  540. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  541. m_name_allocator.allocate(n, textures);
  542. // Initialize all texture names with a nullptr
  543. for (auto i = 0; i < n; i++) {
  544. GLuint name = textures[i];
  545. m_allocated_textures.set(name, nullptr);
  546. }
  547. }
  548. void SoftwareGLContext::gl_delete_textures(GLsizei n, const GLuint* textures)
  549. {
  550. RETURN_WITH_ERROR_IF(n < 0, GL_INVALID_VALUE);
  551. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  552. m_name_allocator.free(n, textures);
  553. for (auto i = 0; i < n; i++) {
  554. GLuint name = textures[i];
  555. auto texture_object = m_allocated_textures.find(name);
  556. if (texture_object == m_allocated_textures.end() || texture_object->value.is_null())
  557. continue;
  558. // Check all texture units
  559. for (auto& texture_unit : m_texture_units) {
  560. if (texture_object->value == texture_unit.bound_texture())
  561. texture_unit.bind_texture_to_target(GL_TEXTURE_2D, nullptr);
  562. }
  563. m_allocated_textures.remove(name);
  564. }
  565. }
  566. void SoftwareGLContext::gl_tex_image_2d(GLenum target, GLint level, GLint internal_format, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid* data)
  567. {
  568. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  569. // We only support GL_TEXTURE_2D for now
  570. RETURN_WITH_ERROR_IF(target != GL_TEXTURE_2D, GL_INVALID_ENUM);
  571. // Check if there is actually a texture bound
  572. RETURN_WITH_ERROR_IF(target == GL_TEXTURE_2D && m_active_texture_unit->currently_bound_target() != GL_TEXTURE_2D, GL_INVALID_OPERATION);
  573. // Internal format can also be a number between 1 and 4. Symbolic formats were only added with EXT_texture, promoted to core in OpenGL 1.1
  574. if (internal_format == 1)
  575. internal_format = GL_ALPHA;
  576. else if (internal_format == 2)
  577. internal_format = GL_LUMINANCE_ALPHA;
  578. else if (internal_format == 3)
  579. internal_format = GL_RGB;
  580. else if (internal_format == 4)
  581. internal_format = GL_RGBA;
  582. // We only support symbolic constants for now
  583. RETURN_WITH_ERROR_IF(!(internal_format == GL_RGB || internal_format == GL_RGBA), GL_INVALID_ENUM);
  584. RETURN_WITH_ERROR_IF(type != GL_UNSIGNED_BYTE, GL_INVALID_VALUE);
  585. RETURN_WITH_ERROR_IF(level < 0 || level > Texture2D::LOG2_MAX_TEXTURE_SIZE, GL_INVALID_VALUE);
  586. RETURN_WITH_ERROR_IF(width < 0 || height < 0 || width > (2 + Texture2D::MAX_TEXTURE_SIZE) || height > (2 + Texture2D::MAX_TEXTURE_SIZE), GL_INVALID_VALUE);
  587. // Check if width and height are a power of 2
  588. RETURN_WITH_ERROR_IF((width & (width - 1)) != 0, GL_INVALID_VALUE);
  589. RETURN_WITH_ERROR_IF((height & (height - 1)) != 0, GL_INVALID_VALUE);
  590. RETURN_WITH_ERROR_IF(border < 0 || border > 1, GL_INVALID_VALUE);
  591. m_active_texture_unit->bound_texture_2d()->upload_texture_data(level, internal_format, width, height, border, format, type, data, m_unpack_row_length);
  592. }
  593. void SoftwareGLContext::gl_tex_sub_image_2d(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid* data)
  594. {
  595. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  596. // We only support GL_TEXTURE_2D for now
  597. RETURN_WITH_ERROR_IF(target != GL_TEXTURE_2D, GL_INVALID_ENUM);
  598. // Check if there is actually a texture bound
  599. RETURN_WITH_ERROR_IF(target == GL_TEXTURE_2D && m_active_texture_unit->currently_bound_target() != GL_TEXTURE_2D, GL_INVALID_OPERATION);
  600. // We only support symbolic constants for now
  601. RETURN_WITH_ERROR_IF(type != GL_UNSIGNED_BYTE, GL_INVALID_VALUE);
  602. RETURN_WITH_ERROR_IF(level < 0 || level > Texture2D::LOG2_MAX_TEXTURE_SIZE, GL_INVALID_VALUE);
  603. RETURN_WITH_ERROR_IF(width < 0 || height < 0 || width > (2 + Texture2D::MAX_TEXTURE_SIZE) || height > (2 + Texture2D::MAX_TEXTURE_SIZE), GL_INVALID_VALUE);
  604. auto texture = m_active_texture_unit->bound_texture_2d();
  605. RETURN_WITH_ERROR_IF(xoffset < 0 || yoffset < 0 || xoffset + width > texture->width_at_lod(level) || yoffset + height > texture->height_at_lod(level), GL_INVALID_VALUE);
  606. texture->replace_sub_texture_data(level, xoffset, yoffset, width, height, format, type, data, m_unpack_row_length);
  607. }
  608. void SoftwareGLContext::gl_tex_parameter(GLenum target, GLenum pname, GLfloat param)
  609. {
  610. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_parameter, target, pname, param);
  611. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  612. // FIXME: We currently only support GL_TETXURE_2D targets. 1D, 3D and CUBE should also be supported (https://docs.gl/gl2/glTexParameter)
  613. RETURN_WITH_ERROR_IF(target != GL_TEXTURE_2D, GL_INVALID_ENUM);
  614. // FIXME: implement the remaining parameters. (https://docs.gl/gl2/glTexParameter)
  615. RETURN_WITH_ERROR_IF(!(pname == GL_TEXTURE_MIN_FILTER
  616. || pname == GL_TEXTURE_MAG_FILTER
  617. || pname == GL_TEXTURE_WRAP_S
  618. || pname == GL_TEXTURE_WRAP_T),
  619. GL_INVALID_ENUM);
  620. if (target == GL_TEXTURE_2D) {
  621. auto texture2d = m_active_texture_unit->bound_texture_2d();
  622. if (texture2d.is_null())
  623. return;
  624. switch (pname) {
  625. case GL_TEXTURE_MIN_FILTER:
  626. RETURN_WITH_ERROR_IF(!(param == GL_NEAREST
  627. || param == GL_LINEAR
  628. || param == GL_NEAREST_MIPMAP_NEAREST
  629. || param == GL_LINEAR_MIPMAP_NEAREST
  630. || param == GL_NEAREST_MIPMAP_LINEAR
  631. || param == GL_LINEAR_MIPMAP_LINEAR),
  632. GL_INVALID_ENUM);
  633. texture2d->sampler().set_min_filter(param);
  634. break;
  635. case GL_TEXTURE_MAG_FILTER:
  636. RETURN_WITH_ERROR_IF(!(param == GL_NEAREST
  637. || param == GL_LINEAR),
  638. GL_INVALID_ENUM);
  639. texture2d->sampler().set_mag_filter(param);
  640. break;
  641. case GL_TEXTURE_WRAP_S:
  642. RETURN_WITH_ERROR_IF(!(param == GL_CLAMP
  643. || param == GL_CLAMP_TO_BORDER
  644. || param == GL_CLAMP_TO_EDGE
  645. || param == GL_MIRRORED_REPEAT
  646. || param == GL_REPEAT),
  647. GL_INVALID_ENUM);
  648. texture2d->sampler().set_wrap_s_mode(param);
  649. break;
  650. case GL_TEXTURE_WRAP_T:
  651. RETURN_WITH_ERROR_IF(!(param == GL_CLAMP
  652. || param == GL_CLAMP_TO_BORDER
  653. || param == GL_CLAMP_TO_EDGE
  654. || param == GL_MIRRORED_REPEAT
  655. || param == GL_REPEAT),
  656. GL_INVALID_ENUM);
  657. texture2d->sampler().set_wrap_t_mode(param);
  658. break;
  659. default:
  660. VERIFY_NOT_REACHED();
  661. }
  662. }
  663. }
  664. void SoftwareGLContext::gl_front_face(GLenum face)
  665. {
  666. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_front_face, face);
  667. RETURN_WITH_ERROR_IF(face < GL_CW || face > GL_CCW, GL_INVALID_ENUM);
  668. m_front_face = face;
  669. }
  670. void SoftwareGLContext::gl_cull_face(GLenum cull_mode)
  671. {
  672. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_cull_face, cull_mode);
  673. RETURN_WITH_ERROR_IF(cull_mode < GL_FRONT || cull_mode > GL_FRONT_AND_BACK, GL_INVALID_ENUM);
  674. m_culled_sides = cull_mode;
  675. }
  676. GLuint SoftwareGLContext::gl_gen_lists(GLsizei range)
  677. {
  678. RETURN_VALUE_WITH_ERROR_IF(range <= 0, GL_INVALID_VALUE, 0);
  679. RETURN_VALUE_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION, 0);
  680. auto initial_entry = m_listings.size();
  681. m_listings.resize(range + initial_entry);
  682. return initial_entry + 1;
  683. }
  684. void SoftwareGLContext::gl_call_list(GLuint list)
  685. {
  686. if (m_gl_call_depth > max_allowed_gl_call_depth)
  687. return;
  688. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_call_list, list);
  689. if (m_listings.size() < list)
  690. return;
  691. TemporaryChange change { m_gl_call_depth, m_gl_call_depth + 1 };
  692. auto& listing = m_listings[list - 1];
  693. for (auto& entry : listing.entries) {
  694. entry.function.visit([&](auto& function) {
  695. entry.arguments.visit([&](auto& arguments) {
  696. auto apply = [&]<typename... Args>(Args && ... args)
  697. {
  698. if constexpr (requires { (this->*function)(forward<Args>(args)...); })
  699. (this->*function)(forward<Args>(args)...);
  700. };
  701. arguments.apply_as_args(apply);
  702. });
  703. });
  704. }
  705. }
  706. void SoftwareGLContext::gl_delete_lists(GLuint list, GLsizei range)
  707. {
  708. if (m_listings.size() < list || m_listings.size() <= list + range)
  709. return;
  710. for (auto& entry : m_listings.span().slice(list - 1, range))
  711. entry.entries.clear_with_capacity();
  712. }
  713. void SoftwareGLContext::gl_end_list()
  714. {
  715. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  716. RETURN_WITH_ERROR_IF(!m_current_listing_index.has_value(), GL_INVALID_OPERATION);
  717. m_listings[m_current_listing_index->index] = move(m_current_listing_index->listing);
  718. m_current_listing_index.clear();
  719. }
  720. void SoftwareGLContext::gl_new_list(GLuint list, GLenum mode)
  721. {
  722. RETURN_WITH_ERROR_IF(list == 0, GL_INVALID_VALUE);
  723. RETURN_WITH_ERROR_IF(mode != GL_COMPILE && mode != GL_COMPILE_AND_EXECUTE, GL_INVALID_ENUM);
  724. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  725. RETURN_WITH_ERROR_IF(m_current_listing_index.has_value(), GL_INVALID_OPERATION);
  726. if (m_listings.size() < list)
  727. return;
  728. m_current_listing_index = CurrentListing { {}, static_cast<size_t>(list - 1), mode };
  729. }
  730. void SoftwareGLContext::gl_flush()
  731. {
  732. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  733. // No-op since SoftwareGLContext is completely synchronous at the moment
  734. }
  735. void SoftwareGLContext::gl_finish()
  736. {
  737. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  738. // No-op since SoftwareGLContext is completely synchronous at the moment
  739. }
  740. void SoftwareGLContext::gl_blend_func(GLenum src_factor, GLenum dst_factor)
  741. {
  742. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_blend_func, src_factor, dst_factor);
  743. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  744. // FIXME: The list of allowed enums differs between API versions
  745. // This was taken from the 2.0 spec on https://docs.gl/gl2/glBlendFunc
  746. RETURN_WITH_ERROR_IF(!(src_factor == GL_ZERO
  747. || src_factor == GL_ONE
  748. || src_factor == GL_SRC_COLOR
  749. || src_factor == GL_ONE_MINUS_SRC_COLOR
  750. || src_factor == GL_DST_COLOR
  751. || src_factor == GL_ONE_MINUS_DST_COLOR
  752. || src_factor == GL_SRC_ALPHA
  753. || src_factor == GL_ONE_MINUS_SRC_ALPHA
  754. || src_factor == GL_DST_ALPHA
  755. || src_factor == GL_ONE_MINUS_DST_ALPHA
  756. || src_factor == GL_CONSTANT_COLOR
  757. || src_factor == GL_ONE_MINUS_CONSTANT_COLOR
  758. || src_factor == GL_CONSTANT_ALPHA
  759. || src_factor == GL_ONE_MINUS_CONSTANT_ALPHA
  760. || src_factor == GL_SRC_ALPHA_SATURATE),
  761. GL_INVALID_ENUM);
  762. RETURN_WITH_ERROR_IF(!(dst_factor == GL_ZERO
  763. || dst_factor == GL_ONE
  764. || dst_factor == GL_SRC_COLOR
  765. || dst_factor == GL_ONE_MINUS_SRC_COLOR
  766. || dst_factor == GL_DST_COLOR
  767. || dst_factor == GL_ONE_MINUS_DST_COLOR
  768. || dst_factor == GL_SRC_ALPHA
  769. || dst_factor == GL_ONE_MINUS_SRC_ALPHA
  770. || dst_factor == GL_DST_ALPHA
  771. || dst_factor == GL_ONE_MINUS_DST_ALPHA
  772. || dst_factor == GL_CONSTANT_COLOR
  773. || dst_factor == GL_ONE_MINUS_CONSTANT_COLOR
  774. || dst_factor == GL_CONSTANT_ALPHA
  775. || dst_factor == GL_ONE_MINUS_CONSTANT_ALPHA),
  776. GL_INVALID_ENUM);
  777. m_blend_source_factor = src_factor;
  778. m_blend_destination_factor = dst_factor;
  779. auto options = m_rasterizer.options();
  780. options.blend_source_factor = m_blend_source_factor;
  781. options.blend_destination_factor = m_blend_destination_factor;
  782. m_rasterizer.set_options(options);
  783. }
  784. void SoftwareGLContext::gl_shade_model(GLenum mode)
  785. {
  786. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_shade_model, mode);
  787. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  788. RETURN_WITH_ERROR_IF(mode != GL_FLAT && mode != GL_SMOOTH, GL_INVALID_ENUM);
  789. auto options = m_rasterizer.options();
  790. options.shade_smooth = (mode == GL_SMOOTH);
  791. m_rasterizer.set_options(options);
  792. }
  793. void SoftwareGLContext::gl_alpha_func(GLenum func, GLclampf ref)
  794. {
  795. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_alpha_func, func, ref);
  796. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  797. RETURN_WITH_ERROR_IF(func < GL_NEVER || func > GL_ALWAYS, GL_INVALID_ENUM);
  798. m_alpha_test_func = func;
  799. m_alpha_test_ref_value = ref;
  800. auto options = m_rasterizer.options();
  801. options.alpha_test_func = m_alpha_test_func;
  802. options.alpha_test_ref_value = m_alpha_test_ref_value;
  803. m_rasterizer.set_options(options);
  804. }
  805. void SoftwareGLContext::gl_hint(GLenum target, GLenum mode)
  806. {
  807. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_hint, target, mode);
  808. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  809. RETURN_WITH_ERROR_IF(target != GL_PERSPECTIVE_CORRECTION_HINT
  810. && target != GL_POINT_SMOOTH_HINT
  811. && target != GL_LINE_SMOOTH_HINT
  812. && target != GL_POLYGON_SMOOTH_HINT
  813. && target != GL_FOG_HINT
  814. && target != GL_GENERATE_MIPMAP_HINT
  815. && target != GL_TEXTURE_COMPRESSION_HINT,
  816. GL_INVALID_ENUM);
  817. RETURN_WITH_ERROR_IF(mode != GL_DONT_CARE
  818. && mode != GL_FASTEST
  819. && mode != GL_NICEST,
  820. GL_INVALID_ENUM);
  821. // According to the spec implementors are free to ignore glHint. So we do.
  822. }
  823. void SoftwareGLContext::gl_read_buffer(GLenum mode)
  824. {
  825. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_read_buffer, mode);
  826. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  827. // FIXME: Also allow aux buffers GL_AUX0 through GL_AUX3 here
  828. // plus any aux buffer between 0 and GL_AUX_BUFFERS
  829. RETURN_WITH_ERROR_IF(mode != GL_FRONT_LEFT
  830. && mode != GL_FRONT_RIGHT
  831. && mode != GL_BACK_LEFT
  832. && mode != GL_BACK_RIGHT
  833. && mode != GL_FRONT
  834. && mode != GL_BACK
  835. && mode != GL_LEFT
  836. && mode != GL_RIGHT,
  837. GL_INVALID_ENUM);
  838. // FIXME: We do not currently have aux buffers, so make it an invalid
  839. // operation to select anything but front or back buffers. Also we do
  840. // not allow selecting the stereoscopic RIGHT buffers since we do not
  841. // have them configured.
  842. RETURN_WITH_ERROR_IF(mode != GL_FRONT_LEFT
  843. && mode != GL_FRONT
  844. && mode != GL_BACK_LEFT
  845. && mode != GL_BACK
  846. && mode != GL_FRONT
  847. && mode != GL_BACK
  848. && mode != GL_LEFT,
  849. GL_INVALID_OPERATION);
  850. m_current_read_buffer = mode;
  851. }
  852. void SoftwareGLContext::gl_draw_buffer(GLenum buffer)
  853. {
  854. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_draw_buffer, buffer);
  855. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  856. // FIXME: Also allow aux buffers GL_AUX0 through GL_AUX3 here
  857. // plus any aux buffer between 0 and GL_AUX_BUFFERS
  858. RETURN_WITH_ERROR_IF(buffer != GL_NONE
  859. && buffer != GL_FRONT_LEFT
  860. && buffer != GL_FRONT_RIGHT
  861. && buffer != GL_BACK_LEFT
  862. && buffer != GL_BACK_RIGHT
  863. && buffer != GL_FRONT
  864. && buffer != GL_BACK
  865. && buffer != GL_LEFT
  866. && buffer != GL_RIGHT,
  867. GL_INVALID_ENUM);
  868. // FIXME: We do not currently have aux buffers, so make it an invalid
  869. // operation to select anything but front or back buffers. Also we do
  870. // not allow selecting the stereoscopic RIGHT buffers since we do not
  871. // have them configured.
  872. RETURN_WITH_ERROR_IF(buffer != GL_NONE
  873. && buffer != GL_FRONT_LEFT
  874. && buffer != GL_FRONT
  875. && buffer != GL_BACK_LEFT
  876. && buffer != GL_BACK
  877. && buffer != GL_FRONT
  878. && buffer != GL_BACK
  879. && buffer != GL_LEFT,
  880. GL_INVALID_OPERATION);
  881. m_current_draw_buffer = buffer;
  882. auto rasterizer_options = m_rasterizer.options();
  883. rasterizer_options.draw_buffer = m_current_draw_buffer;
  884. m_rasterizer.set_options(rasterizer_options);
  885. }
  886. void SoftwareGLContext::gl_read_pixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid* pixels)
  887. {
  888. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  889. RETURN_WITH_ERROR_IF(width < 0 || height < 0, GL_INVALID_VALUE);
  890. RETURN_WITH_ERROR_IF(format != GL_COLOR_INDEX
  891. && format != GL_STENCIL_INDEX
  892. && format != GL_DEPTH_COMPONENT
  893. && format != GL_RED
  894. && format != GL_GREEN
  895. && format != GL_BLUE
  896. && format != GL_ALPHA
  897. && format != GL_RGB
  898. && format != GL_RGBA
  899. && format != GL_LUMINANCE
  900. && format != GL_LUMINANCE_ALPHA,
  901. GL_INVALID_ENUM);
  902. RETURN_WITH_ERROR_IF(type != GL_UNSIGNED_BYTE
  903. && type != GL_BYTE
  904. && type != GL_BITMAP
  905. && type != GL_UNSIGNED_SHORT
  906. && type != GL_SHORT
  907. && type != GL_BLUE
  908. && type != GL_UNSIGNED_INT
  909. && type != GL_INT
  910. && type != GL_FLOAT,
  911. GL_INVALID_ENUM);
  912. // FIXME: We only support RGBA buffers for now.
  913. // Once we add support for indexed color modes do the correct check here
  914. RETURN_WITH_ERROR_IF(format == GL_COLOR_INDEX, GL_INVALID_OPERATION);
  915. // FIXME: We do not have stencil buffers yet
  916. // Once we add support for stencil buffers do the correct check here
  917. RETURN_WITH_ERROR_IF(format == GL_STENCIL_INDEX, GL_INVALID_OPERATION);
  918. if (format == GL_DEPTH_COMPONENT) {
  919. // FIXME: This check needs to be a bit more sophisticated. Currently the buffers
  920. // are hardcoded. Once we add proper structures for them we need to correct this check
  921. // Error because only back buffer has a depth buffer
  922. RETURN_WITH_ERROR_IF(m_current_read_buffer == GL_FRONT
  923. || m_current_read_buffer == GL_FRONT_LEFT
  924. || m_current_read_buffer == GL_FRONT_RIGHT,
  925. GL_INVALID_OPERATION);
  926. }
  927. // Some helper functions for converting float values to integer types
  928. auto float_to_i8 = [](float f) -> GLchar {
  929. return static_cast<GLchar>((0x7f * min(max(f, 0.0f), 1.0f) - 1) / 2);
  930. };
  931. auto float_to_i16 = [](float f) -> GLshort {
  932. return static_cast<GLshort>((0x7fff * min(max(f, 0.0f), 1.0f) - 1) / 2);
  933. };
  934. auto float_to_i32 = [](float f) -> GLint {
  935. return static_cast<GLint>((0x7fffffff * min(max(f, 0.0f), 1.0f) - 1) / 2);
  936. };
  937. auto float_to_u8 = [](float f) -> GLubyte {
  938. return static_cast<GLubyte>(0xff * min(max(f, 0.0f), 1.0f));
  939. };
  940. auto float_to_u16 = [](float f) -> GLushort {
  941. return static_cast<GLushort>(0xffff * min(max(f, 0.0f), 1.0f));
  942. };
  943. auto float_to_u32 = [](float f) -> GLuint {
  944. return static_cast<GLuint>(0xffffffff * min(max(f, 0.0f), 1.0f));
  945. };
  946. if (format == GL_DEPTH_COMPONENT) {
  947. // Read from depth buffer
  948. for (GLsizei i = 0; i < height; ++i) {
  949. for (GLsizei j = 0; j < width; ++j) {
  950. float depth = m_rasterizer.get_depthbuffer_value(x + j, y + i);
  951. switch (type) {
  952. case GL_BYTE:
  953. reinterpret_cast<GLchar*>(pixels)[i * width + j] = float_to_i8(depth);
  954. break;
  955. case GL_SHORT:
  956. reinterpret_cast<GLshort*>(pixels)[i * width + j] = float_to_i16(depth);
  957. break;
  958. case GL_INT:
  959. reinterpret_cast<GLint*>(pixels)[i * width + j] = float_to_i32(depth);
  960. break;
  961. case GL_UNSIGNED_BYTE:
  962. reinterpret_cast<GLubyte*>(pixels)[i * width + j] = float_to_u8(depth);
  963. break;
  964. case GL_UNSIGNED_SHORT:
  965. reinterpret_cast<GLushort*>(pixels)[i * width + j] = float_to_u16(depth);
  966. break;
  967. case GL_UNSIGNED_INT:
  968. reinterpret_cast<GLuint*>(pixels)[i * width + j] = float_to_u32(depth);
  969. break;
  970. case GL_FLOAT:
  971. reinterpret_cast<GLfloat*>(pixels)[i * width + j] = min(max(depth, 0.0f), 1.0f);
  972. break;
  973. }
  974. }
  975. }
  976. return;
  977. }
  978. bool write_red = false;
  979. bool write_green = false;
  980. bool write_blue = false;
  981. bool write_alpha = false;
  982. size_t component_count = 0;
  983. size_t component_size = 0;
  984. size_t red_offset = 0;
  985. size_t green_offset = 0;
  986. size_t blue_offset = 0;
  987. size_t alpha_offset = 0;
  988. char* red_ptr = nullptr;
  989. char* green_ptr = nullptr;
  990. char* blue_ptr = nullptr;
  991. char* alpha_ptr = nullptr;
  992. switch (format) {
  993. case GL_RGB:
  994. write_red = true;
  995. write_green = true;
  996. write_blue = true;
  997. component_count = 3;
  998. red_offset = 2;
  999. green_offset = 1;
  1000. blue_offset = 0;
  1001. break;
  1002. case GL_RGBA:
  1003. write_red = true;
  1004. write_green = true;
  1005. write_blue = true;
  1006. write_alpha = true;
  1007. component_count = 4;
  1008. red_offset = 3;
  1009. green_offset = 2;
  1010. blue_offset = 1;
  1011. alpha_offset = 0;
  1012. break;
  1013. case GL_RED:
  1014. write_red = true;
  1015. component_count = 1;
  1016. red_offset = 0;
  1017. break;
  1018. case GL_GREEN:
  1019. write_green = true;
  1020. component_count = 1;
  1021. green_offset = 0;
  1022. break;
  1023. case GL_BLUE:
  1024. write_blue = true;
  1025. component_count = 1;
  1026. blue_offset = 0;
  1027. break;
  1028. case GL_ALPHA:
  1029. write_alpha = true;
  1030. component_count = 1;
  1031. alpha_offset = 0;
  1032. break;
  1033. }
  1034. switch (type) {
  1035. case GL_BYTE:
  1036. case GL_UNSIGNED_BYTE:
  1037. component_size = 1;
  1038. break;
  1039. case GL_SHORT:
  1040. case GL_UNSIGNED_SHORT:
  1041. component_size = 2;
  1042. break;
  1043. case GL_INT:
  1044. case GL_UNSIGNED_INT:
  1045. case GL_FLOAT:
  1046. component_size = 4;
  1047. break;
  1048. }
  1049. char* out_ptr = reinterpret_cast<char*>(pixels);
  1050. for (int i = 0; i < (int)height; ++i) {
  1051. for (int j = 0; j < (int)width; ++j) {
  1052. Gfx::RGBA32 color {};
  1053. if (m_current_read_buffer == GL_FRONT || m_current_read_buffer == GL_LEFT || m_current_read_buffer == GL_FRONT_LEFT) {
  1054. if (y + i >= m_frontbuffer->width() || x + j >= m_frontbuffer->height())
  1055. color = 0;
  1056. else
  1057. color = m_frontbuffer->scanline(y + i)[x + j];
  1058. } else {
  1059. color = m_rasterizer.get_backbuffer_pixel(x + j, y + i);
  1060. }
  1061. float red = ((color >> 24) & 0xff) / 255.0f;
  1062. float green = ((color >> 16) & 0xff) / 255.0f;
  1063. float blue = ((color >> 8) & 0xff) / 255.0f;
  1064. float alpha = (color & 0xff) / 255.0f;
  1065. // FIXME: Set up write pointers based on selected endianness (glPixelStore)
  1066. red_ptr = out_ptr + (component_size * red_offset);
  1067. green_ptr = out_ptr + (component_size * green_offset);
  1068. blue_ptr = out_ptr + (component_size * blue_offset);
  1069. alpha_ptr = out_ptr + (component_size * alpha_offset);
  1070. switch (type) {
  1071. case GL_BYTE:
  1072. if (write_red)
  1073. *reinterpret_cast<GLchar*>(red_ptr) = float_to_i8(red);
  1074. if (write_green)
  1075. *reinterpret_cast<GLchar*>(green_ptr) = float_to_i8(green);
  1076. if (write_blue)
  1077. *reinterpret_cast<GLchar*>(blue_ptr) = float_to_i8(blue);
  1078. if (write_alpha)
  1079. *reinterpret_cast<GLchar*>(alpha_ptr) = float_to_i8(alpha);
  1080. break;
  1081. case GL_UNSIGNED_BYTE:
  1082. if (write_red)
  1083. *reinterpret_cast<GLubyte*>(red_ptr) = float_to_u8(red);
  1084. if (write_green)
  1085. *reinterpret_cast<GLubyte*>(green_ptr) = float_to_u8(green);
  1086. if (write_blue)
  1087. *reinterpret_cast<GLubyte*>(blue_ptr) = float_to_u8(blue);
  1088. if (write_alpha)
  1089. *reinterpret_cast<GLubyte*>(alpha_ptr) = float_to_u8(alpha);
  1090. break;
  1091. case GL_SHORT:
  1092. if (write_red)
  1093. *reinterpret_cast<GLshort*>(red_ptr) = float_to_i16(red);
  1094. if (write_green)
  1095. *reinterpret_cast<GLshort*>(green_ptr) = float_to_i16(green);
  1096. if (write_blue)
  1097. *reinterpret_cast<GLshort*>(blue_ptr) = float_to_i16(blue);
  1098. if (write_alpha)
  1099. *reinterpret_cast<GLshort*>(alpha_ptr) = float_to_i16(alpha);
  1100. break;
  1101. case GL_UNSIGNED_SHORT:
  1102. if (write_red)
  1103. *reinterpret_cast<GLushort*>(red_ptr) = float_to_u16(red);
  1104. if (write_green)
  1105. *reinterpret_cast<GLushort*>(green_ptr) = float_to_u16(green);
  1106. if (write_blue)
  1107. *reinterpret_cast<GLushort*>(blue_ptr) = float_to_u16(blue);
  1108. if (write_alpha)
  1109. *reinterpret_cast<GLushort*>(alpha_ptr) = float_to_u16(alpha);
  1110. break;
  1111. case GL_INT:
  1112. if (write_red)
  1113. *reinterpret_cast<GLint*>(red_ptr) = float_to_i32(red);
  1114. if (write_green)
  1115. *reinterpret_cast<GLint*>(green_ptr) = float_to_i32(green);
  1116. if (write_blue)
  1117. *reinterpret_cast<GLint*>(blue_ptr) = float_to_i32(blue);
  1118. if (write_alpha)
  1119. *reinterpret_cast<GLint*>(alpha_ptr) = float_to_i32(alpha);
  1120. break;
  1121. case GL_UNSIGNED_INT:
  1122. if (write_red)
  1123. *reinterpret_cast<GLuint*>(red_ptr) = float_to_u32(red);
  1124. if (write_green)
  1125. *reinterpret_cast<GLuint*>(green_ptr) = float_to_u32(green);
  1126. if (write_blue)
  1127. *reinterpret_cast<GLuint*>(blue_ptr) = float_to_u32(blue);
  1128. if (write_alpha)
  1129. *reinterpret_cast<GLuint*>(alpha_ptr) = float_to_u32(alpha);
  1130. break;
  1131. case GL_FLOAT:
  1132. if (write_red)
  1133. *reinterpret_cast<GLfloat*>(red_ptr) = min(max(red, 0.0f), 1.0f);
  1134. if (write_green)
  1135. *reinterpret_cast<GLfloat*>(green_ptr) = min(max(green, 0.0f), 1.0f);
  1136. if (write_blue)
  1137. *reinterpret_cast<GLfloat*>(blue_ptr) = min(max(blue, 0.0f), 1.0f);
  1138. if (write_alpha)
  1139. *reinterpret_cast<GLfloat*>(alpha_ptr) = min(max(alpha, 0.0f), 1.0f);
  1140. break;
  1141. }
  1142. out_ptr += component_size * component_count;
  1143. }
  1144. }
  1145. }
  1146. void SoftwareGLContext::gl_bind_texture(GLenum target, GLuint texture)
  1147. {
  1148. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1149. // FIXME: We only support GL_TEXTURE_2D for now
  1150. RETURN_WITH_ERROR_IF(target != GL_TEXTURE_2D, GL_INVALID_ENUM);
  1151. if (texture == 0) {
  1152. switch (target) {
  1153. case GL_TEXTURE_2D:
  1154. m_active_texture_unit->bind_texture_to_target(target, nullptr);
  1155. return;
  1156. default:
  1157. VERIFY_NOT_REACHED();
  1158. return;
  1159. }
  1160. }
  1161. auto it = m_allocated_textures.find(texture);
  1162. // The texture name does not exist
  1163. RETURN_WITH_ERROR_IF(it == m_allocated_textures.end(), GL_INVALID_VALUE);
  1164. auto texture_object = it->value;
  1165. // Binding a texture to a different target than it was first bound is an invalid operation
  1166. // FIXME: We only support GL_TEXTURE_2D for now
  1167. RETURN_WITH_ERROR_IF(target == GL_TEXTURE_2D && !texture_object.is_null() && !texture_object->is_texture_2d(), GL_INVALID_OPERATION);
  1168. if (!texture_object) {
  1169. // This is the first time the texture is bound. Allocate an actual texture object
  1170. switch (target) {
  1171. case GL_TEXTURE_2D:
  1172. texture_object = adopt_ref(*new Texture2D());
  1173. break;
  1174. default:
  1175. VERIFY_NOT_REACHED();
  1176. break;
  1177. }
  1178. m_allocated_textures.set(texture, texture_object);
  1179. }
  1180. switch (target) {
  1181. case GL_TEXTURE_2D:
  1182. m_active_texture_unit->bind_texture_to_target(target, texture_object);
  1183. break;
  1184. }
  1185. }
  1186. void SoftwareGLContext::gl_active_texture(GLenum texture)
  1187. {
  1188. RETURN_WITH_ERROR_IF(texture < GL_TEXTURE0 || texture > GL_TEXTURE31, GL_INVALID_ENUM);
  1189. m_active_texture_unit = &m_texture_units.at(texture - GL_TEXTURE0);
  1190. }
  1191. void SoftwareGLContext::gl_get_floatv(GLenum pname, GLfloat* params)
  1192. {
  1193. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1194. auto flatten_and_assign_matrix = [&params](const FloatMatrix4x4& matrix) {
  1195. auto elements = matrix.elements();
  1196. for (size_t i = 0; i < 4; ++i) {
  1197. for (size_t j = 0; j < 4; ++j) {
  1198. params[i * 4 + j] = elements[i][j];
  1199. }
  1200. }
  1201. };
  1202. switch (pname) {
  1203. case GL_MODELVIEW_MATRIX:
  1204. if (m_current_matrix_mode == GL_MODELVIEW)
  1205. flatten_and_assign_matrix(m_model_view_matrix);
  1206. else {
  1207. if (m_model_view_matrix_stack.is_empty())
  1208. flatten_and_assign_matrix(FloatMatrix4x4::identity());
  1209. else
  1210. flatten_and_assign_matrix(m_model_view_matrix_stack.last());
  1211. }
  1212. break;
  1213. default:
  1214. // FIXME: Because glQuake only requires GL_MODELVIEW_MATRIX, that is the only parameter
  1215. // that we currently support. More parameters should be supported.
  1216. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1217. }
  1218. }
  1219. void SoftwareGLContext::gl_get_booleanv(GLenum pname, GLboolean* data)
  1220. {
  1221. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1222. switch (pname) {
  1223. case GL_BLEND:
  1224. *data = m_blend_enabled ? GL_TRUE : GL_FALSE;
  1225. break;
  1226. case GL_ALPHA_TEST:
  1227. *data = m_alpha_test_func ? GL_TRUE : GL_FALSE;
  1228. break;
  1229. case GL_DEPTH_TEST:
  1230. *data = m_depth_test_enabled ? GL_TRUE : GL_FALSE;
  1231. break;
  1232. case GL_CULL_FACE:
  1233. *data = m_cull_faces ? GL_TRUE : GL_FALSE;
  1234. break;
  1235. case GL_TEXTURE_1D:
  1236. *data = m_active_texture_unit->texture_1d_enabled() ? GL_TRUE : GL_FALSE;
  1237. break;
  1238. case GL_TEXTURE_2D:
  1239. *data = m_active_texture_unit->texture_2d_enabled() ? GL_TRUE : GL_FALSE;
  1240. break;
  1241. case GL_TEXTURE_3D:
  1242. *data = m_active_texture_unit->texture_3d_enabled() ? GL_TRUE : GL_FALSE;
  1243. break;
  1244. case GL_TEXTURE_CUBE_MAP:
  1245. *data = m_active_texture_unit->texture_cube_map_enabled() ? GL_TRUE : GL_FALSE;
  1246. break;
  1247. default:
  1248. // According to the Khronos docs, we always return GL_INVALID_ENUM if we encounter a non-accepted value
  1249. // for `pname`
  1250. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1251. }
  1252. }
  1253. void SoftwareGLContext::gl_get_integerv(GLenum pname, GLint* data)
  1254. {
  1255. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1256. switch (pname) {
  1257. case GL_BLEND_SRC_ALPHA:
  1258. *data = m_blend_source_factor;
  1259. break;
  1260. case GL_BLEND_DST_ALPHA:
  1261. *data = m_blend_destination_factor;
  1262. break;
  1263. case GL_MAX_TEXTURE_UNITS:
  1264. *data = m_texture_units.size();
  1265. break;
  1266. case GL_MAX_TEXTURE_SIZE:
  1267. *data = 4096;
  1268. break;
  1269. case GL_SCISSOR_BOX: {
  1270. auto scissor_box = m_rasterizer.options().scissor_box;
  1271. *(data + 0) = scissor_box.x();
  1272. *(data + 1) = scissor_box.y();
  1273. *(data + 2) = scissor_box.width();
  1274. *(data + 3) = scissor_box.height();
  1275. break;
  1276. }
  1277. default:
  1278. // According to the Khronos docs, we always return GL_INVALID_ENUM if we encounter a non-accepted value
  1279. // for `pname`
  1280. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1281. }
  1282. }
  1283. void SoftwareGLContext::gl_depth_mask(GLboolean flag)
  1284. {
  1285. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_depth_mask, flag);
  1286. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1287. auto options = m_rasterizer.options();
  1288. options.enable_depth_write = (flag != GL_FALSE);
  1289. m_rasterizer.set_options(options);
  1290. }
  1291. void SoftwareGLContext::gl_enable_client_state(GLenum cap)
  1292. {
  1293. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1294. switch (cap) {
  1295. case GL_VERTEX_ARRAY:
  1296. m_client_side_vertex_array_enabled = true;
  1297. break;
  1298. case GL_COLOR_ARRAY:
  1299. m_client_side_color_array_enabled = true;
  1300. break;
  1301. case GL_TEXTURE_COORD_ARRAY:
  1302. m_client_side_texture_coord_array_enabled = true;
  1303. break;
  1304. default:
  1305. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1306. }
  1307. }
  1308. void SoftwareGLContext::gl_disable_client_state(GLenum cap)
  1309. {
  1310. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1311. switch (cap) {
  1312. case GL_VERTEX_ARRAY:
  1313. m_client_side_vertex_array_enabled = false;
  1314. break;
  1315. case GL_COLOR_ARRAY:
  1316. m_client_side_color_array_enabled = false;
  1317. break;
  1318. case GL_TEXTURE_COORD_ARRAY:
  1319. m_client_side_texture_coord_array_enabled = false;
  1320. break;
  1321. default:
  1322. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1323. }
  1324. }
  1325. void SoftwareGLContext::gl_vertex_pointer(GLint size, GLenum type, GLsizei stride, const void* pointer)
  1326. {
  1327. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1328. RETURN_WITH_ERROR_IF(!(size == 2 || size == 3 || size == 4), GL_INVALID_VALUE);
  1329. RETURN_WITH_ERROR_IF(!(type == GL_SHORT || type == GL_INT || type == GL_FLOAT || type == GL_DOUBLE), GL_INVALID_ENUM);
  1330. RETURN_WITH_ERROR_IF(stride < 0, GL_INVALID_VALUE);
  1331. m_client_vertex_pointer.size = size;
  1332. m_client_vertex_pointer.type = type;
  1333. m_client_vertex_pointer.stride = stride;
  1334. m_client_vertex_pointer.pointer = pointer;
  1335. }
  1336. void SoftwareGLContext::gl_color_pointer(GLint size, GLenum type, GLsizei stride, const void* pointer)
  1337. {
  1338. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1339. RETURN_WITH_ERROR_IF(!(size == 3 || size == 4), GL_INVALID_VALUE);
  1340. RETURN_WITH_ERROR_IF(!(type == GL_BYTE
  1341. || type == GL_UNSIGNED_BYTE
  1342. || type == GL_SHORT
  1343. || type == GL_UNSIGNED_SHORT
  1344. || type == GL_INT
  1345. || type == GL_UNSIGNED_INT
  1346. || type == GL_FLOAT
  1347. || type == GL_DOUBLE),
  1348. GL_INVALID_ENUM);
  1349. RETURN_WITH_ERROR_IF(stride < 0, GL_INVALID_VALUE);
  1350. m_client_color_pointer.size = size;
  1351. m_client_color_pointer.type = type;
  1352. m_client_color_pointer.stride = stride;
  1353. m_client_color_pointer.pointer = pointer;
  1354. }
  1355. void SoftwareGLContext::gl_tex_coord_pointer(GLint size, GLenum type, GLsizei stride, const void* pointer)
  1356. {
  1357. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1358. RETURN_WITH_ERROR_IF(!(size == 1 || size == 2 || size == 3 || size == 4), GL_INVALID_VALUE);
  1359. RETURN_WITH_ERROR_IF(!(type == GL_SHORT || type == GL_INT || type == GL_FLOAT || type == GL_DOUBLE), GL_INVALID_ENUM);
  1360. RETURN_WITH_ERROR_IF(stride < 0, GL_INVALID_VALUE);
  1361. m_client_tex_coord_pointer.size = size;
  1362. m_client_tex_coord_pointer.type = type;
  1363. m_client_tex_coord_pointer.stride = stride;
  1364. m_client_tex_coord_pointer.pointer = pointer;
  1365. }
  1366. void SoftwareGLContext::gl_tex_env(GLenum target, GLenum pname, GLfloat param)
  1367. {
  1368. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_env, target, pname, param);
  1369. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1370. if (target == GL_TEXTURE_ENV) {
  1371. if (pname == GL_TEXTURE_ENV_MODE) {
  1372. auto param_enum = static_cast<GLenum>(param);
  1373. switch (param_enum) {
  1374. case GL_MODULATE:
  1375. case GL_REPLACE:
  1376. case GL_DECAL:
  1377. m_active_texture_unit->set_env_mode(param_enum);
  1378. break;
  1379. default:
  1380. // FIXME: We currently only support a subset of possible param values. Implement the rest!
  1381. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1382. break;
  1383. }
  1384. } else {
  1385. // FIXME: We currently only support a subset of possible pname values. Implement the rest!
  1386. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1387. }
  1388. } else {
  1389. // FIXME: We currently only support a subset of possible target values. Implement the rest!
  1390. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1391. }
  1392. }
  1393. void SoftwareGLContext::gl_draw_arrays(GLenum mode, GLint first, GLsizei count)
  1394. {
  1395. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_draw_arrays, mode, first, count);
  1396. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1397. // FIXME: Some modes are still missing (GL_POINTS, GL_LINE_STRIP, GL_LINE_LOOP, GL_LINES,GL_QUAD_STRIP)
  1398. RETURN_WITH_ERROR_IF(!(mode == GL_TRIANGLE_STRIP
  1399. || mode == GL_TRIANGLE_FAN
  1400. || mode == GL_TRIANGLES
  1401. || mode == GL_QUADS
  1402. || mode == GL_POLYGON),
  1403. GL_INVALID_ENUM);
  1404. RETURN_WITH_ERROR_IF(count < 0, GL_INVALID_VALUE);
  1405. // At least the vertex array needs to be enabled
  1406. if (!m_client_side_vertex_array_enabled)
  1407. return;
  1408. auto last = first + count;
  1409. glBegin(mode);
  1410. for (int i = first; i < last; i++) {
  1411. if (m_client_side_texture_coord_array_enabled) {
  1412. float tex_coords[4] { 0, 0, 0, 0 };
  1413. read_from_vertex_attribute_pointer(m_client_tex_coord_pointer, i, tex_coords, false);
  1414. glTexCoord4fv(tex_coords);
  1415. }
  1416. if (m_client_side_color_array_enabled) {
  1417. float color[4] { 0, 0, 0, 1 };
  1418. read_from_vertex_attribute_pointer(m_client_color_pointer, i, color, true);
  1419. glColor4fv(color);
  1420. }
  1421. float vertex[4] { 0, 0, 0, 1 };
  1422. read_from_vertex_attribute_pointer(m_client_vertex_pointer, i, vertex, false);
  1423. glVertex4fv(vertex);
  1424. }
  1425. glEnd();
  1426. }
  1427. void SoftwareGLContext::gl_draw_elements(GLenum mode, GLsizei count, GLenum type, const void* indices)
  1428. {
  1429. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_draw_elements, mode, count, type, indices);
  1430. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1431. // FIXME: Some modes are still missing (GL_POINTS, GL_LINE_STRIP, GL_LINE_LOOP, GL_LINES,GL_QUAD_STRIP)
  1432. RETURN_WITH_ERROR_IF(!(mode == GL_TRIANGLE_STRIP
  1433. || mode == GL_TRIANGLE_FAN
  1434. || mode == GL_TRIANGLES
  1435. || mode == GL_QUADS
  1436. || mode == GL_POLYGON),
  1437. GL_INVALID_ENUM);
  1438. RETURN_WITH_ERROR_IF(!(type == GL_UNSIGNED_BYTE
  1439. || type == GL_UNSIGNED_SHORT
  1440. || type == GL_UNSIGNED_INT),
  1441. GL_INVALID_ENUM);
  1442. RETURN_WITH_ERROR_IF(count < 0, GL_INVALID_VALUE);
  1443. // At least the vertex array needs to be enabled
  1444. if (!m_client_side_vertex_array_enabled)
  1445. return;
  1446. glBegin(mode);
  1447. for (int index = 0; index < count; index++) {
  1448. int i = 0;
  1449. switch (type) {
  1450. case GL_UNSIGNED_BYTE:
  1451. i = reinterpret_cast<const GLubyte*>(indices)[index];
  1452. break;
  1453. case GL_UNSIGNED_SHORT:
  1454. i = reinterpret_cast<const GLushort*>(indices)[index];
  1455. break;
  1456. case GL_UNSIGNED_INT:
  1457. i = reinterpret_cast<const GLuint*>(indices)[index];
  1458. break;
  1459. }
  1460. if (m_client_side_texture_coord_array_enabled) {
  1461. float tex_coords[4] { 0, 0, 0, 0 };
  1462. read_from_vertex_attribute_pointer(m_client_tex_coord_pointer, i, tex_coords, false);
  1463. glTexCoord4fv(tex_coords);
  1464. }
  1465. if (m_client_side_color_array_enabled) {
  1466. float color[4] { 0, 0, 0, 1 };
  1467. read_from_vertex_attribute_pointer(m_client_color_pointer, i, color, true);
  1468. glColor4fv(color);
  1469. }
  1470. float vertex[4] { 0, 0, 0, 1 };
  1471. read_from_vertex_attribute_pointer(m_client_vertex_pointer, i, vertex, false);
  1472. glVertex4fv(vertex);
  1473. }
  1474. glEnd();
  1475. }
  1476. void SoftwareGLContext::gl_depth_range(GLdouble min, GLdouble max)
  1477. {
  1478. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_depth_range, min, max);
  1479. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1480. auto options = m_rasterizer.options();
  1481. options.depth_min = clamp(min, 0.f, 1.f);
  1482. options.depth_max = clamp(max, 0.f, 1.f);
  1483. m_rasterizer.set_options(options);
  1484. }
  1485. void SoftwareGLContext::gl_depth_func(GLenum func)
  1486. {
  1487. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_depth_func, func);
  1488. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1489. RETURN_WITH_ERROR_IF(!(func == GL_NEVER
  1490. || func == GL_LESS
  1491. || func == GL_EQUAL
  1492. || func == GL_LEQUAL
  1493. || func == GL_GREATER
  1494. || func == GL_NOTEQUAL
  1495. || func == GL_GEQUAL
  1496. || func == GL_ALWAYS),
  1497. GL_INVALID_ENUM);
  1498. auto options = m_rasterizer.options();
  1499. options.depth_func = func;
  1500. m_rasterizer.set_options(options);
  1501. }
  1502. // General helper function to read arbitrary vertex attribute data into a float array
  1503. void SoftwareGLContext::read_from_vertex_attribute_pointer(VertexAttribPointer const& attrib, int index, float* elements, bool normalize)
  1504. {
  1505. auto byte_ptr = reinterpret_cast<const char*>(attrib.pointer);
  1506. size_t stride = attrib.stride;
  1507. switch (attrib.type) {
  1508. case GL_BYTE: {
  1509. if (stride == 0)
  1510. stride = sizeof(GLbyte) * attrib.size;
  1511. for (int i = 0; i < attrib.size; i++) {
  1512. elements[i] = *(reinterpret_cast<const GLbyte*>(byte_ptr + stride * index) + i);
  1513. if (normalize)
  1514. elements[i] /= 0x80;
  1515. }
  1516. break;
  1517. }
  1518. case GL_UNSIGNED_BYTE: {
  1519. if (stride == 0)
  1520. stride = sizeof(GLubyte) * attrib.size;
  1521. for (int i = 0; i < attrib.size; i++) {
  1522. elements[i] = *(reinterpret_cast<const GLubyte*>(byte_ptr + stride * index) + i);
  1523. if (normalize)
  1524. elements[i] /= 0xff;
  1525. }
  1526. break;
  1527. }
  1528. case GL_SHORT: {
  1529. if (stride == 0)
  1530. stride = sizeof(GLshort) * attrib.size;
  1531. for (int i = 0; i < attrib.size; i++) {
  1532. elements[i] = *(reinterpret_cast<const GLshort*>(byte_ptr + stride * index) + i);
  1533. if (normalize)
  1534. elements[i] /= 0x8000;
  1535. }
  1536. break;
  1537. }
  1538. case GL_UNSIGNED_SHORT: {
  1539. if (stride == 0)
  1540. stride = sizeof(GLushort) * attrib.size;
  1541. for (int i = 0; i < attrib.size; i++) {
  1542. elements[i] = *(reinterpret_cast<const GLushort*>(byte_ptr + stride * index) + i);
  1543. if (normalize)
  1544. elements[i] /= 0xffff;
  1545. }
  1546. break;
  1547. }
  1548. case GL_INT: {
  1549. if (stride == 0)
  1550. stride = sizeof(GLint) * attrib.size;
  1551. for (int i = 0; i < attrib.size; i++) {
  1552. elements[i] = *(reinterpret_cast<const GLint*>(byte_ptr + stride * index) + i);
  1553. if (normalize)
  1554. elements[i] /= 0x80000000;
  1555. }
  1556. break;
  1557. }
  1558. case GL_UNSIGNED_INT: {
  1559. if (stride == 0)
  1560. stride = sizeof(GLuint) * attrib.size;
  1561. for (int i = 0; i < attrib.size; i++) {
  1562. elements[i] = *(reinterpret_cast<const GLuint*>(byte_ptr + stride * index) + i);
  1563. if (normalize)
  1564. elements[i] /= 0xffffffff;
  1565. }
  1566. break;
  1567. }
  1568. case GL_FLOAT: {
  1569. if (stride == 0)
  1570. stride = sizeof(GLfloat) * attrib.size;
  1571. for (int i = 0; i < attrib.size; i++) {
  1572. elements[i] = *(reinterpret_cast<const GLfloat*>(byte_ptr + stride * index) + i);
  1573. }
  1574. break;
  1575. }
  1576. case GL_DOUBLE: {
  1577. if (stride == 0)
  1578. stride = sizeof(GLdouble) * attrib.size;
  1579. for (int i = 0; i < attrib.size; i++) {
  1580. elements[i] = static_cast<float>(*(reinterpret_cast<const GLdouble*>(byte_ptr + stride * index) + i));
  1581. }
  1582. break;
  1583. }
  1584. }
  1585. }
  1586. void SoftwareGLContext::gl_color_mask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha)
  1587. {
  1588. auto options = m_rasterizer.options();
  1589. auto mask = options.color_mask;
  1590. if (!red)
  1591. mask &= ~0x000000ff;
  1592. else
  1593. mask |= 0x000000ff;
  1594. if (!green)
  1595. mask &= ~0x0000ff00;
  1596. else
  1597. mask |= 0x0000ff00;
  1598. if (!blue)
  1599. mask &= ~0x00ff0000;
  1600. else
  1601. mask |= 0x00ff0000;
  1602. if (!alpha)
  1603. mask &= ~0xff000000;
  1604. else
  1605. mask |= 0xff000000;
  1606. options.color_mask = mask;
  1607. m_rasterizer.set_options(options);
  1608. }
  1609. void SoftwareGLContext::gl_polygon_mode(GLenum face, GLenum mode)
  1610. {
  1611. RETURN_WITH_ERROR_IF(!(face == GL_BACK || face == GL_FRONT || face == GL_FRONT_AND_BACK), GL_INVALID_ENUM);
  1612. RETURN_WITH_ERROR_IF(!(mode == GL_POINT || mode == GL_LINE || mode == GL_FILL), GL_INVALID_ENUM);
  1613. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1614. auto options = m_rasterizer.options();
  1615. options.polygon_mode = mode;
  1616. m_rasterizer.set_options(options);
  1617. }
  1618. void SoftwareGLContext::gl_polygon_offset(GLfloat factor, GLfloat units)
  1619. {
  1620. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_polygon_offset, factor, units);
  1621. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1622. auto rasterizer_options = m_rasterizer.options();
  1623. rasterizer_options.depth_offset_factor = factor;
  1624. rasterizer_options.depth_offset_constant = units;
  1625. m_rasterizer.set_options(rasterizer_options);
  1626. }
  1627. void SoftwareGLContext::gl_fogfv(GLenum pname, GLfloat* params)
  1628. {
  1629. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1630. auto options = m_rasterizer.options();
  1631. switch (pname) {
  1632. case GL_FOG_COLOR:
  1633. // Set rasterizer options fog color
  1634. // NOTE: We purposefully don't check for `nullptr` here (as with other calls). The spec states nothing
  1635. // about us checking for such things. If the programmer does so and hits SIGSEGV, that's on them.
  1636. options.fog_color = FloatVector4 { params[0], params[1], params[2], params[3] };
  1637. break;
  1638. default:
  1639. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1640. }
  1641. m_rasterizer.set_options(options);
  1642. }
  1643. void SoftwareGLContext::gl_fogf(GLenum pname, GLfloat param)
  1644. {
  1645. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1646. RETURN_WITH_ERROR_IF(param < 0.0f, GL_INVALID_VALUE);
  1647. auto options = m_rasterizer.options();
  1648. switch (pname) {
  1649. case GL_FOG_DENSITY:
  1650. options.fog_density = param;
  1651. break;
  1652. default:
  1653. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1654. }
  1655. m_rasterizer.set_options(options);
  1656. }
  1657. void SoftwareGLContext::gl_fogi(GLenum pname, GLint param)
  1658. {
  1659. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1660. RETURN_WITH_ERROR_IF(!(param == GL_EXP || param == GL_EXP2 || param != GL_LINEAR), GL_INVALID_ENUM);
  1661. auto options = m_rasterizer.options();
  1662. switch (pname) {
  1663. case GL_FOG_MODE:
  1664. options.fog_mode = param;
  1665. break;
  1666. default:
  1667. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1668. }
  1669. m_rasterizer.set_options(options);
  1670. }
  1671. void SoftwareGLContext::gl_pixel_store(GLenum pname, GLfloat param)
  1672. {
  1673. // FIXME: Implement missing parameters
  1674. switch (pname) {
  1675. case GL_UNPACK_ROW_LENGTH:
  1676. RETURN_WITH_ERROR_IF(param < 0, GL_INVALID_VALUE);
  1677. m_unpack_row_length = static_cast<size_t>(param);
  1678. break;
  1679. default:
  1680. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1681. break;
  1682. }
  1683. }
  1684. void SoftwareGLContext::gl_scissor(GLint x, GLint y, GLsizei width, GLsizei height)
  1685. {
  1686. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_scissor, x, y, width, height);
  1687. RETURN_WITH_ERROR_IF(width < 0 || height < 0, GL_INVALID_VALUE);
  1688. auto options = m_rasterizer.options();
  1689. options.scissor_box = { x, y, width, height };
  1690. m_rasterizer.set_options(options);
  1691. }
  1692. void SoftwareGLContext::present()
  1693. {
  1694. m_rasterizer.blit_to(*m_frontbuffer);
  1695. }
  1696. }