SoftwareGLContext.cpp 76 KB

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