SoftwareGLContext.cpp 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305
  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.normal = m_current_vertex_normal;
  416. vertex_list.append(vertex);
  417. }
  418. // FIXME: We need to add `r` and `q` to our GLVertex?!
  419. void SoftwareGLContext::gl_tex_coord(GLfloat s, GLfloat t, GLfloat r, GLfloat q)
  420. {
  421. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_coord, s, t, r, q);
  422. m_current_vertex_tex_coord = { s, t, r, q };
  423. }
  424. void SoftwareGLContext::gl_viewport(GLint x, GLint y, GLsizei width, GLsizei height)
  425. {
  426. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_viewport, x, y, width, height);
  427. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  428. (void)(x);
  429. (void)(y);
  430. (void)(width);
  431. (void)(height);
  432. }
  433. void SoftwareGLContext::gl_enable(GLenum capability)
  434. {
  435. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_enable, capability);
  436. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  437. auto rasterizer_options = m_rasterizer.options();
  438. bool update_rasterizer_options = false;
  439. switch (capability) {
  440. case GL_CULL_FACE:
  441. m_cull_faces = true;
  442. break;
  443. case GL_DEPTH_TEST:
  444. m_depth_test_enabled = true;
  445. rasterizer_options.enable_depth_test = true;
  446. update_rasterizer_options = true;
  447. break;
  448. case GL_BLEND:
  449. m_blend_enabled = true;
  450. rasterizer_options.enable_blending = true;
  451. update_rasterizer_options = true;
  452. break;
  453. case GL_ALPHA_TEST:
  454. m_alpha_test_enabled = true;
  455. rasterizer_options.enable_alpha_test = true;
  456. update_rasterizer_options = true;
  457. break;
  458. case GL_FOG:
  459. rasterizer_options.fog_enabled = true;
  460. update_rasterizer_options = true;
  461. break;
  462. case GL_SCISSOR_TEST:
  463. rasterizer_options.scissor_enabled = true;
  464. update_rasterizer_options = true;
  465. break;
  466. case GL_STENCIL_TEST:
  467. m_stencil_test_enabled = true;
  468. break;
  469. case GL_TEXTURE_1D:
  470. m_active_texture_unit->set_texture_1d_enabled(true);
  471. break;
  472. case GL_TEXTURE_2D:
  473. m_active_texture_unit->set_texture_2d_enabled(true);
  474. break;
  475. case GL_TEXTURE_3D:
  476. m_active_texture_unit->set_texture_3d_enabled(true);
  477. break;
  478. case GL_TEXTURE_CUBE_MAP:
  479. m_active_texture_unit->set_texture_cube_map_enabled(true);
  480. break;
  481. default:
  482. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  483. }
  484. if (update_rasterizer_options)
  485. m_rasterizer.set_options(rasterizer_options);
  486. }
  487. void SoftwareGLContext::gl_disable(GLenum capability)
  488. {
  489. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_disable, capability);
  490. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  491. auto rasterizer_options = m_rasterizer.options();
  492. bool update_rasterizer_options = false;
  493. switch (capability) {
  494. case GL_CULL_FACE:
  495. m_cull_faces = false;
  496. break;
  497. case GL_DEPTH_TEST:
  498. m_depth_test_enabled = false;
  499. rasterizer_options.enable_depth_test = false;
  500. update_rasterizer_options = true;
  501. break;
  502. case GL_BLEND:
  503. m_blend_enabled = false;
  504. rasterizer_options.enable_blending = false;
  505. update_rasterizer_options = true;
  506. break;
  507. case GL_ALPHA_TEST:
  508. m_alpha_test_enabled = false;
  509. rasterizer_options.enable_alpha_test = false;
  510. update_rasterizer_options = true;
  511. break;
  512. case GL_FOG:
  513. rasterizer_options.fog_enabled = false;
  514. update_rasterizer_options = true;
  515. break;
  516. case GL_SCISSOR_TEST:
  517. rasterizer_options.scissor_enabled = false;
  518. update_rasterizer_options = true;
  519. break;
  520. case GL_STENCIL_TEST:
  521. m_stencil_test_enabled = false;
  522. break;
  523. case GL_TEXTURE_1D:
  524. m_active_texture_unit->set_texture_1d_enabled(false);
  525. break;
  526. case GL_TEXTURE_2D:
  527. m_active_texture_unit->set_texture_2d_enabled(false);
  528. break;
  529. case GL_TEXTURE_3D:
  530. m_active_texture_unit->set_texture_3d_enabled(false);
  531. break;
  532. case GL_TEXTURE_CUBE_MAP:
  533. m_active_texture_unit->set_texture_cube_map_enabled(false);
  534. break;
  535. default:
  536. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  537. }
  538. if (update_rasterizer_options)
  539. m_rasterizer.set_options(rasterizer_options);
  540. }
  541. GLboolean SoftwareGLContext::gl_is_enabled(GLenum capability)
  542. {
  543. RETURN_VALUE_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION, 0);
  544. auto rasterizer_options = m_rasterizer.options();
  545. switch (capability) {
  546. case GL_CULL_FACE:
  547. return m_cull_faces;
  548. case GL_DEPTH_TEST:
  549. return m_depth_test_enabled;
  550. case GL_BLEND:
  551. return m_blend_enabled;
  552. case GL_ALPHA_TEST:
  553. return m_alpha_test_enabled;
  554. case GL_FOG:
  555. return rasterizer_options.fog_enabled;
  556. case GL_SCISSOR_TEST:
  557. return rasterizer_options.scissor_enabled;
  558. case GL_STENCIL_TEST:
  559. return m_stencil_test_enabled;
  560. }
  561. RETURN_VALUE_WITH_ERROR_IF(true, GL_INVALID_ENUM, 0);
  562. }
  563. void SoftwareGLContext::gl_gen_textures(GLsizei n, GLuint* textures)
  564. {
  565. RETURN_WITH_ERROR_IF(n < 0, GL_INVALID_VALUE);
  566. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  567. m_name_allocator.allocate(n, textures);
  568. // Initialize all texture names with a nullptr
  569. for (auto i = 0; i < n; i++) {
  570. GLuint name = textures[i];
  571. m_allocated_textures.set(name, nullptr);
  572. }
  573. }
  574. void SoftwareGLContext::gl_delete_textures(GLsizei n, const GLuint* textures)
  575. {
  576. RETURN_WITH_ERROR_IF(n < 0, GL_INVALID_VALUE);
  577. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  578. for (auto i = 0; i < n; i++) {
  579. GLuint name = textures[i];
  580. if (name == 0)
  581. continue;
  582. m_name_allocator.free(name);
  583. auto texture_object = m_allocated_textures.find(name);
  584. if (texture_object == m_allocated_textures.end() || texture_object->value.is_null())
  585. continue;
  586. // Check all texture units
  587. for (auto& texture_unit : m_texture_units) {
  588. if (texture_object->value == texture_unit.bound_texture())
  589. texture_unit.bind_texture_to_target(GL_TEXTURE_2D, nullptr);
  590. }
  591. m_allocated_textures.remove(name);
  592. }
  593. }
  594. 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)
  595. {
  596. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  597. // We only support GL_TEXTURE_2D for now
  598. RETURN_WITH_ERROR_IF(target != GL_TEXTURE_2D, GL_INVALID_ENUM);
  599. // Check if there is actually a texture bound
  600. RETURN_WITH_ERROR_IF(target == GL_TEXTURE_2D && m_active_texture_unit->currently_bound_target() != GL_TEXTURE_2D, GL_INVALID_OPERATION);
  601. // 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
  602. if (internal_format == 1)
  603. internal_format = GL_ALPHA;
  604. else if (internal_format == 2)
  605. internal_format = GL_LUMINANCE_ALPHA;
  606. else if (internal_format == 3)
  607. internal_format = GL_RGB;
  608. else if (internal_format == 4)
  609. internal_format = GL_RGBA;
  610. // We only support symbolic constants for now
  611. RETURN_WITH_ERROR_IF(!(internal_format == GL_RGB || internal_format == GL_RGBA), GL_INVALID_ENUM);
  612. RETURN_WITH_ERROR_IF(type != GL_UNSIGNED_BYTE, GL_INVALID_VALUE);
  613. RETURN_WITH_ERROR_IF(level < 0 || level > Texture2D::LOG2_MAX_TEXTURE_SIZE, GL_INVALID_VALUE);
  614. RETURN_WITH_ERROR_IF(width < 0 || height < 0 || width > (2 + Texture2D::MAX_TEXTURE_SIZE) || height > (2 + Texture2D::MAX_TEXTURE_SIZE), GL_INVALID_VALUE);
  615. // Check if width and height are a power of 2
  616. RETURN_WITH_ERROR_IF((width & (width - 1)) != 0, GL_INVALID_VALUE);
  617. RETURN_WITH_ERROR_IF((height & (height - 1)) != 0, GL_INVALID_VALUE);
  618. RETURN_WITH_ERROR_IF(border < 0 || border > 1, GL_INVALID_VALUE);
  619. 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);
  620. }
  621. 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)
  622. {
  623. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  624. // We only support GL_TEXTURE_2D for now
  625. RETURN_WITH_ERROR_IF(target != GL_TEXTURE_2D, GL_INVALID_ENUM);
  626. // Check if there is actually a texture bound
  627. RETURN_WITH_ERROR_IF(target == GL_TEXTURE_2D && m_active_texture_unit->currently_bound_target() != GL_TEXTURE_2D, GL_INVALID_OPERATION);
  628. // We only support symbolic constants for now
  629. RETURN_WITH_ERROR_IF(type != GL_UNSIGNED_BYTE, GL_INVALID_VALUE);
  630. RETURN_WITH_ERROR_IF(level < 0 || level > Texture2D::LOG2_MAX_TEXTURE_SIZE, GL_INVALID_VALUE);
  631. RETURN_WITH_ERROR_IF(width < 0 || height < 0 || width > (2 + Texture2D::MAX_TEXTURE_SIZE) || height > (2 + Texture2D::MAX_TEXTURE_SIZE), GL_INVALID_VALUE);
  632. auto texture = m_active_texture_unit->bound_texture_2d();
  633. 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);
  634. texture->replace_sub_texture_data(level, xoffset, yoffset, width, height, format, type, data, m_unpack_row_length, m_unpack_alignment);
  635. }
  636. void SoftwareGLContext::gl_tex_parameter(GLenum target, GLenum pname, GLfloat param)
  637. {
  638. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_parameter, target, pname, param);
  639. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  640. // FIXME: We currently only support GL_TETXURE_2D targets. 1D, 3D and CUBE should also be supported (https://docs.gl/gl2/glTexParameter)
  641. RETURN_WITH_ERROR_IF(target != GL_TEXTURE_2D, GL_INVALID_ENUM);
  642. // FIXME: implement the remaining parameters. (https://docs.gl/gl2/glTexParameter)
  643. RETURN_WITH_ERROR_IF(!(pname == GL_TEXTURE_MIN_FILTER
  644. || pname == GL_TEXTURE_MAG_FILTER
  645. || pname == GL_TEXTURE_WRAP_S
  646. || pname == GL_TEXTURE_WRAP_T),
  647. GL_INVALID_ENUM);
  648. if (target == GL_TEXTURE_2D) {
  649. auto texture2d = m_active_texture_unit->bound_texture_2d();
  650. if (texture2d.is_null())
  651. return;
  652. switch (pname) {
  653. case GL_TEXTURE_MIN_FILTER:
  654. RETURN_WITH_ERROR_IF(!(param == GL_NEAREST
  655. || param == GL_LINEAR
  656. || param == GL_NEAREST_MIPMAP_NEAREST
  657. || param == GL_LINEAR_MIPMAP_NEAREST
  658. || param == GL_NEAREST_MIPMAP_LINEAR
  659. || param == GL_LINEAR_MIPMAP_LINEAR),
  660. GL_INVALID_ENUM);
  661. texture2d->sampler().set_min_filter(param);
  662. break;
  663. case GL_TEXTURE_MAG_FILTER:
  664. RETURN_WITH_ERROR_IF(!(param == GL_NEAREST
  665. || param == GL_LINEAR),
  666. GL_INVALID_ENUM);
  667. texture2d->sampler().set_mag_filter(param);
  668. break;
  669. case GL_TEXTURE_WRAP_S:
  670. RETURN_WITH_ERROR_IF(!(param == GL_CLAMP
  671. || param == GL_CLAMP_TO_BORDER
  672. || param == GL_CLAMP_TO_EDGE
  673. || param == GL_MIRRORED_REPEAT
  674. || param == GL_REPEAT),
  675. GL_INVALID_ENUM);
  676. texture2d->sampler().set_wrap_s_mode(param);
  677. break;
  678. case GL_TEXTURE_WRAP_T:
  679. RETURN_WITH_ERROR_IF(!(param == GL_CLAMP
  680. || param == GL_CLAMP_TO_BORDER
  681. || param == GL_CLAMP_TO_EDGE
  682. || param == GL_MIRRORED_REPEAT
  683. || param == GL_REPEAT),
  684. GL_INVALID_ENUM);
  685. texture2d->sampler().set_wrap_t_mode(param);
  686. break;
  687. default:
  688. VERIFY_NOT_REACHED();
  689. }
  690. }
  691. }
  692. void SoftwareGLContext::gl_front_face(GLenum face)
  693. {
  694. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_front_face, face);
  695. RETURN_WITH_ERROR_IF(face < GL_CW || face > GL_CCW, GL_INVALID_ENUM);
  696. m_front_face = face;
  697. }
  698. void SoftwareGLContext::gl_cull_face(GLenum cull_mode)
  699. {
  700. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_cull_face, cull_mode);
  701. RETURN_WITH_ERROR_IF(cull_mode < GL_FRONT || cull_mode > GL_FRONT_AND_BACK, GL_INVALID_ENUM);
  702. m_culled_sides = cull_mode;
  703. }
  704. GLuint SoftwareGLContext::gl_gen_lists(GLsizei range)
  705. {
  706. RETURN_VALUE_WITH_ERROR_IF(range <= 0, GL_INVALID_VALUE, 0);
  707. RETURN_VALUE_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION, 0);
  708. auto initial_entry = m_listings.size();
  709. m_listings.resize(range + initial_entry);
  710. return initial_entry + 1;
  711. }
  712. void SoftwareGLContext::invoke_list(size_t list_index)
  713. {
  714. auto& listing = m_listings[list_index - 1];
  715. for (auto& entry : listing.entries) {
  716. entry.function.visit([&](auto& function) {
  717. entry.arguments.visit([&](auto& arguments) {
  718. auto apply = [&]<typename... Args>(Args && ... args)
  719. {
  720. if constexpr (requires { (this->*function)(forward<Args>(args)...); })
  721. (this->*function)(forward<Args>(args)...);
  722. };
  723. arguments.apply_as_args(apply);
  724. });
  725. });
  726. }
  727. }
  728. void SoftwareGLContext::gl_call_list(GLuint list)
  729. {
  730. if (m_gl_call_depth > max_allowed_gl_call_depth)
  731. return;
  732. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_call_list, list);
  733. if (m_listings.size() < list)
  734. return;
  735. TemporaryChange change { m_gl_call_depth, m_gl_call_depth + 1 };
  736. invoke_list(list);
  737. }
  738. void SoftwareGLContext::gl_call_lists(GLsizei n, GLenum type, void const* lists)
  739. {
  740. if (m_gl_call_depth > max_allowed_gl_call_depth)
  741. return;
  742. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_call_lists, n, type, lists);
  743. RETURN_WITH_ERROR_IF(n < 0, GL_INVALID_VALUE);
  744. RETURN_WITH_ERROR_IF(!(type == GL_BYTE
  745. || type == GL_UNSIGNED_BYTE
  746. || type == GL_SHORT
  747. || type == GL_UNSIGNED_SHORT
  748. || type == GL_INT
  749. || type == GL_UNSIGNED_INT
  750. || type == GL_FLOAT
  751. || type == GL_2_BYTES
  752. || type == GL_3_BYTES
  753. || type == GL_4_BYTES),
  754. GL_INVALID_ENUM);
  755. TemporaryChange change { m_gl_call_depth, m_gl_call_depth + 1 };
  756. auto invoke_all_lists = [&]<typename T>(T const* lists) {
  757. for (int i = 0; i < n; ++i) {
  758. auto list = static_cast<size_t>(lists[i]);
  759. invoke_list(m_list_base + list);
  760. }
  761. };
  762. switch (type) {
  763. case GL_BYTE:
  764. invoke_all_lists(static_cast<GLbyte const*>(lists));
  765. break;
  766. case GL_UNSIGNED_BYTE:
  767. invoke_all_lists(static_cast<GLubyte const*>(lists));
  768. break;
  769. case GL_SHORT:
  770. invoke_all_lists(static_cast<GLshort const*>(lists));
  771. break;
  772. case GL_UNSIGNED_SHORT:
  773. invoke_all_lists(static_cast<GLushort const*>(lists));
  774. break;
  775. case GL_INT:
  776. invoke_all_lists(static_cast<GLint const*>(lists));
  777. break;
  778. case GL_UNSIGNED_INT:
  779. invoke_all_lists(static_cast<GLuint const*>(lists));
  780. break;
  781. case GL_FLOAT:
  782. invoke_all_lists(static_cast<GLfloat const*>(lists));
  783. break;
  784. case GL_2_BYTES:
  785. case GL_3_BYTES:
  786. case GL_4_BYTES:
  787. dbgln("SoftwareGLContext FIXME: unimplemented glCallLists() with type {}", type);
  788. break;
  789. default:
  790. VERIFY_NOT_REACHED();
  791. }
  792. }
  793. void SoftwareGLContext::gl_delete_lists(GLuint list, GLsizei range)
  794. {
  795. if (m_listings.size() < list || m_listings.size() <= list + range)
  796. return;
  797. for (auto& entry : m_listings.span().slice(list - 1, range))
  798. entry.entries.clear_with_capacity();
  799. }
  800. void SoftwareGLContext::gl_list_base(GLuint base)
  801. {
  802. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_list_base, base);
  803. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  804. m_list_base = base;
  805. }
  806. void SoftwareGLContext::gl_end_list()
  807. {
  808. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  809. RETURN_WITH_ERROR_IF(!m_current_listing_index.has_value(), GL_INVALID_OPERATION);
  810. m_listings[m_current_listing_index->index] = move(m_current_listing_index->listing);
  811. m_current_listing_index.clear();
  812. }
  813. void SoftwareGLContext::gl_new_list(GLuint list, GLenum mode)
  814. {
  815. RETURN_WITH_ERROR_IF(list == 0, GL_INVALID_VALUE);
  816. RETURN_WITH_ERROR_IF(mode != GL_COMPILE && mode != GL_COMPILE_AND_EXECUTE, GL_INVALID_ENUM);
  817. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  818. RETURN_WITH_ERROR_IF(m_current_listing_index.has_value(), GL_INVALID_OPERATION);
  819. if (m_listings.size() < list)
  820. return;
  821. m_current_listing_index = CurrentListing { {}, static_cast<size_t>(list - 1), mode };
  822. }
  823. void SoftwareGLContext::gl_flush()
  824. {
  825. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  826. // No-op since SoftwareGLContext is completely synchronous at the moment
  827. }
  828. void SoftwareGLContext::gl_finish()
  829. {
  830. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  831. // No-op since SoftwareGLContext is completely synchronous at the moment
  832. }
  833. void SoftwareGLContext::gl_blend_func(GLenum src_factor, GLenum dst_factor)
  834. {
  835. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_blend_func, src_factor, dst_factor);
  836. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  837. // FIXME: The list of allowed enums differs between API versions
  838. // This was taken from the 2.0 spec on https://docs.gl/gl2/glBlendFunc
  839. RETURN_WITH_ERROR_IF(!(src_factor == GL_ZERO
  840. || src_factor == GL_ONE
  841. || src_factor == GL_SRC_COLOR
  842. || src_factor == GL_ONE_MINUS_SRC_COLOR
  843. || src_factor == GL_DST_COLOR
  844. || src_factor == GL_ONE_MINUS_DST_COLOR
  845. || src_factor == GL_SRC_ALPHA
  846. || src_factor == GL_ONE_MINUS_SRC_ALPHA
  847. || src_factor == GL_DST_ALPHA
  848. || src_factor == GL_ONE_MINUS_DST_ALPHA
  849. || src_factor == GL_CONSTANT_COLOR
  850. || src_factor == GL_ONE_MINUS_CONSTANT_COLOR
  851. || src_factor == GL_CONSTANT_ALPHA
  852. || src_factor == GL_ONE_MINUS_CONSTANT_ALPHA
  853. || src_factor == GL_SRC_ALPHA_SATURATE),
  854. GL_INVALID_ENUM);
  855. RETURN_WITH_ERROR_IF(!(dst_factor == GL_ZERO
  856. || dst_factor == GL_ONE
  857. || dst_factor == GL_SRC_COLOR
  858. || dst_factor == GL_ONE_MINUS_SRC_COLOR
  859. || dst_factor == GL_DST_COLOR
  860. || dst_factor == GL_ONE_MINUS_DST_COLOR
  861. || dst_factor == GL_SRC_ALPHA
  862. || dst_factor == GL_ONE_MINUS_SRC_ALPHA
  863. || dst_factor == GL_DST_ALPHA
  864. || dst_factor == GL_ONE_MINUS_DST_ALPHA
  865. || dst_factor == GL_CONSTANT_COLOR
  866. || dst_factor == GL_ONE_MINUS_CONSTANT_COLOR
  867. || dst_factor == GL_CONSTANT_ALPHA
  868. || dst_factor == GL_ONE_MINUS_CONSTANT_ALPHA),
  869. GL_INVALID_ENUM);
  870. m_blend_source_factor = src_factor;
  871. m_blend_destination_factor = dst_factor;
  872. auto options = m_rasterizer.options();
  873. options.blend_source_factor = m_blend_source_factor;
  874. options.blend_destination_factor = m_blend_destination_factor;
  875. m_rasterizer.set_options(options);
  876. }
  877. void SoftwareGLContext::gl_shade_model(GLenum mode)
  878. {
  879. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_shade_model, mode);
  880. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  881. RETURN_WITH_ERROR_IF(mode != GL_FLAT && mode != GL_SMOOTH, GL_INVALID_ENUM);
  882. auto options = m_rasterizer.options();
  883. options.shade_smooth = (mode == GL_SMOOTH);
  884. m_rasterizer.set_options(options);
  885. }
  886. void SoftwareGLContext::gl_alpha_func(GLenum func, GLclampf ref)
  887. {
  888. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_alpha_func, func, ref);
  889. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  890. RETURN_WITH_ERROR_IF(func < GL_NEVER || func > GL_ALWAYS, GL_INVALID_ENUM);
  891. m_alpha_test_func = func;
  892. m_alpha_test_ref_value = ref;
  893. auto options = m_rasterizer.options();
  894. options.alpha_test_func = m_alpha_test_func;
  895. options.alpha_test_ref_value = m_alpha_test_ref_value;
  896. m_rasterizer.set_options(options);
  897. }
  898. void SoftwareGLContext::gl_hint(GLenum target, GLenum mode)
  899. {
  900. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_hint, target, mode);
  901. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  902. RETURN_WITH_ERROR_IF(target != GL_PERSPECTIVE_CORRECTION_HINT
  903. && target != GL_POINT_SMOOTH_HINT
  904. && target != GL_LINE_SMOOTH_HINT
  905. && target != GL_POLYGON_SMOOTH_HINT
  906. && target != GL_FOG_HINT
  907. && target != GL_GENERATE_MIPMAP_HINT
  908. && target != GL_TEXTURE_COMPRESSION_HINT,
  909. GL_INVALID_ENUM);
  910. RETURN_WITH_ERROR_IF(mode != GL_DONT_CARE
  911. && mode != GL_FASTEST
  912. && mode != GL_NICEST,
  913. GL_INVALID_ENUM);
  914. // According to the spec implementors are free to ignore glHint. So we do.
  915. }
  916. void SoftwareGLContext::gl_read_buffer(GLenum mode)
  917. {
  918. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_read_buffer, mode);
  919. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  920. // FIXME: Also allow aux buffers GL_AUX0 through GL_AUX3 here
  921. // plus any aux buffer between 0 and GL_AUX_BUFFERS
  922. RETURN_WITH_ERROR_IF(mode != GL_FRONT_LEFT
  923. && mode != GL_FRONT_RIGHT
  924. && mode != GL_BACK_LEFT
  925. && mode != GL_BACK_RIGHT
  926. && mode != GL_FRONT
  927. && mode != GL_BACK
  928. && mode != GL_LEFT
  929. && mode != GL_RIGHT,
  930. GL_INVALID_ENUM);
  931. // FIXME: We do not currently have aux buffers, so make it an invalid
  932. // operation to select anything but front or back buffers. Also we do
  933. // not allow selecting the stereoscopic RIGHT buffers since we do not
  934. // have them configured.
  935. RETURN_WITH_ERROR_IF(mode != GL_FRONT_LEFT
  936. && mode != GL_FRONT
  937. && mode != GL_BACK_LEFT
  938. && mode != GL_BACK
  939. && mode != GL_FRONT
  940. && mode != GL_BACK
  941. && mode != GL_LEFT,
  942. GL_INVALID_OPERATION);
  943. m_current_read_buffer = mode;
  944. }
  945. void SoftwareGLContext::gl_draw_buffer(GLenum buffer)
  946. {
  947. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_draw_buffer, buffer);
  948. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  949. // FIXME: Also allow aux buffers GL_AUX0 through GL_AUX3 here
  950. // plus any aux buffer between 0 and GL_AUX_BUFFERS
  951. RETURN_WITH_ERROR_IF(buffer != GL_NONE
  952. && buffer != GL_FRONT_LEFT
  953. && buffer != GL_FRONT_RIGHT
  954. && buffer != GL_BACK_LEFT
  955. && buffer != GL_BACK_RIGHT
  956. && buffer != GL_FRONT
  957. && buffer != GL_BACK
  958. && buffer != GL_LEFT
  959. && buffer != GL_RIGHT,
  960. GL_INVALID_ENUM);
  961. // FIXME: We do not currently have aux buffers, so make it an invalid
  962. // operation to select anything but front or back buffers. Also we do
  963. // not allow selecting the stereoscopic RIGHT buffers since we do not
  964. // have them configured.
  965. RETURN_WITH_ERROR_IF(buffer != GL_NONE
  966. && buffer != GL_FRONT_LEFT
  967. && buffer != GL_FRONT
  968. && buffer != GL_BACK_LEFT
  969. && buffer != GL_BACK
  970. && buffer != GL_FRONT
  971. && buffer != GL_BACK
  972. && buffer != GL_LEFT,
  973. GL_INVALID_OPERATION);
  974. m_current_draw_buffer = buffer;
  975. auto rasterizer_options = m_rasterizer.options();
  976. rasterizer_options.draw_buffer = m_current_draw_buffer;
  977. m_rasterizer.set_options(rasterizer_options);
  978. }
  979. void SoftwareGLContext::gl_read_pixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid* pixels)
  980. {
  981. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  982. RETURN_WITH_ERROR_IF(width < 0 || height < 0, GL_INVALID_VALUE);
  983. RETURN_WITH_ERROR_IF(format != GL_COLOR_INDEX
  984. && format != GL_STENCIL_INDEX
  985. && format != GL_DEPTH_COMPONENT
  986. && format != GL_RED
  987. && format != GL_GREEN
  988. && format != GL_BLUE
  989. && format != GL_ALPHA
  990. && format != GL_RGB
  991. && format != GL_RGBA
  992. && format != GL_LUMINANCE
  993. && format != GL_LUMINANCE_ALPHA,
  994. GL_INVALID_ENUM);
  995. RETURN_WITH_ERROR_IF(type != GL_UNSIGNED_BYTE
  996. && type != GL_BYTE
  997. && type != GL_BITMAP
  998. && type != GL_UNSIGNED_SHORT
  999. && type != GL_SHORT
  1000. && type != GL_BLUE
  1001. && type != GL_UNSIGNED_INT
  1002. && type != GL_INT
  1003. && type != GL_FLOAT,
  1004. GL_INVALID_ENUM);
  1005. // FIXME: We only support RGBA buffers for now.
  1006. // Once we add support for indexed color modes do the correct check here
  1007. RETURN_WITH_ERROR_IF(format == GL_COLOR_INDEX, GL_INVALID_OPERATION);
  1008. // FIXME: We do not have stencil buffers yet
  1009. // Once we add support for stencil buffers do the correct check here
  1010. RETURN_WITH_ERROR_IF(format == GL_STENCIL_INDEX, GL_INVALID_OPERATION);
  1011. if (format == GL_DEPTH_COMPONENT) {
  1012. // FIXME: This check needs to be a bit more sophisticated. Currently the buffers
  1013. // are hardcoded. Once we add proper structures for them we need to correct this check
  1014. // Error because only back buffer has a depth buffer
  1015. RETURN_WITH_ERROR_IF(m_current_read_buffer == GL_FRONT
  1016. || m_current_read_buffer == GL_FRONT_LEFT
  1017. || m_current_read_buffer == GL_FRONT_RIGHT,
  1018. GL_INVALID_OPERATION);
  1019. }
  1020. // Some helper functions for converting float values to integer types
  1021. auto float_to_i8 = [](float f) -> GLchar {
  1022. return static_cast<GLchar>((0x7f * min(max(f, 0.0f), 1.0f) - 1) / 2);
  1023. };
  1024. auto float_to_i16 = [](float f) -> GLshort {
  1025. return static_cast<GLshort>((0x7fff * min(max(f, 0.0f), 1.0f) - 1) / 2);
  1026. };
  1027. auto float_to_i32 = [](float f) -> GLint {
  1028. return static_cast<GLint>((0x7fffffff * min(max(f, 0.0f), 1.0f) - 1) / 2);
  1029. };
  1030. auto float_to_u8 = [](float f) -> GLubyte {
  1031. return static_cast<GLubyte>(0xff * min(max(f, 0.0f), 1.0f));
  1032. };
  1033. auto float_to_u16 = [](float f) -> GLushort {
  1034. return static_cast<GLushort>(0xffff * min(max(f, 0.0f), 1.0f));
  1035. };
  1036. auto float_to_u32 = [](float f) -> GLuint {
  1037. return static_cast<GLuint>(0xffffffff * min(max(f, 0.0f), 1.0f));
  1038. };
  1039. u8 component_size = 0;
  1040. switch (type) {
  1041. case GL_BYTE:
  1042. case GL_UNSIGNED_BYTE:
  1043. component_size = 1;
  1044. break;
  1045. case GL_SHORT:
  1046. case GL_UNSIGNED_SHORT:
  1047. component_size = 2;
  1048. break;
  1049. case GL_INT:
  1050. case GL_UNSIGNED_INT:
  1051. case GL_FLOAT:
  1052. component_size = 4;
  1053. break;
  1054. }
  1055. if (format == GL_DEPTH_COMPONENT) {
  1056. auto const row_stride = (width * component_size + m_pack_alignment - 1) / m_pack_alignment * m_pack_alignment;
  1057. // Read from depth buffer
  1058. for (GLsizei i = 0; i < height; ++i) {
  1059. for (GLsizei j = 0; j < width; ++j) {
  1060. float depth = m_rasterizer.get_depthbuffer_value(x + j, y + i);
  1061. auto char_ptr = reinterpret_cast<char*>(pixels) + i * row_stride + j * component_size;
  1062. switch (type) {
  1063. case GL_BYTE:
  1064. *reinterpret_cast<GLchar*>(char_ptr) = float_to_i8(depth);
  1065. break;
  1066. case GL_SHORT:
  1067. *reinterpret_cast<GLshort*>(char_ptr) = float_to_i16(depth);
  1068. break;
  1069. case GL_INT:
  1070. *reinterpret_cast<GLint*>(char_ptr) = float_to_i32(depth);
  1071. break;
  1072. case GL_UNSIGNED_BYTE:
  1073. *reinterpret_cast<GLubyte*>(char_ptr) = float_to_u8(depth);
  1074. break;
  1075. case GL_UNSIGNED_SHORT:
  1076. *reinterpret_cast<GLushort*>(char_ptr) = float_to_u16(depth);
  1077. break;
  1078. case GL_UNSIGNED_INT:
  1079. *reinterpret_cast<GLuint*>(char_ptr) = float_to_u32(depth);
  1080. break;
  1081. case GL_FLOAT:
  1082. *reinterpret_cast<GLfloat*>(char_ptr) = min(max(depth, 0.0f), 1.0f);
  1083. break;
  1084. }
  1085. }
  1086. }
  1087. return;
  1088. }
  1089. bool write_red = false;
  1090. bool write_green = false;
  1091. bool write_blue = false;
  1092. bool write_alpha = false;
  1093. size_t component_count = 0;
  1094. size_t red_offset = 0;
  1095. size_t green_offset = 0;
  1096. size_t blue_offset = 0;
  1097. size_t alpha_offset = 0;
  1098. char* red_ptr = nullptr;
  1099. char* green_ptr = nullptr;
  1100. char* blue_ptr = nullptr;
  1101. char* alpha_ptr = nullptr;
  1102. switch (format) {
  1103. case GL_RGB:
  1104. write_red = true;
  1105. write_green = true;
  1106. write_blue = true;
  1107. component_count = 3;
  1108. red_offset = 2;
  1109. green_offset = 1;
  1110. blue_offset = 0;
  1111. break;
  1112. case GL_RGBA:
  1113. write_red = true;
  1114. write_green = true;
  1115. write_blue = true;
  1116. write_alpha = true;
  1117. component_count = 4;
  1118. red_offset = 3;
  1119. green_offset = 2;
  1120. blue_offset = 1;
  1121. alpha_offset = 0;
  1122. break;
  1123. case GL_RED:
  1124. write_red = true;
  1125. component_count = 1;
  1126. red_offset = 0;
  1127. break;
  1128. case GL_GREEN:
  1129. write_green = true;
  1130. component_count = 1;
  1131. green_offset = 0;
  1132. break;
  1133. case GL_BLUE:
  1134. write_blue = true;
  1135. component_count = 1;
  1136. blue_offset = 0;
  1137. break;
  1138. case GL_ALPHA:
  1139. write_alpha = true;
  1140. component_count = 1;
  1141. alpha_offset = 0;
  1142. break;
  1143. }
  1144. auto const pixel_bytes = component_size * component_count;
  1145. auto const row_alignment_bytes = (m_pack_alignment - ((width * pixel_bytes) % m_pack_alignment)) % m_pack_alignment;
  1146. char* out_ptr = reinterpret_cast<char*>(pixels);
  1147. for (int i = 0; i < (int)height; ++i) {
  1148. for (int j = 0; j < (int)width; ++j) {
  1149. Gfx::RGBA32 color {};
  1150. if (m_current_read_buffer == GL_FRONT || m_current_read_buffer == GL_LEFT || m_current_read_buffer == GL_FRONT_LEFT) {
  1151. if (y + i >= m_frontbuffer->width() || x + j >= m_frontbuffer->height())
  1152. color = 0;
  1153. else
  1154. color = m_frontbuffer->scanline(y + i)[x + j];
  1155. } else {
  1156. color = m_rasterizer.get_backbuffer_pixel(x + j, y + i);
  1157. }
  1158. float red = ((color >> 24) & 0xff) / 255.0f;
  1159. float green = ((color >> 16) & 0xff) / 255.0f;
  1160. float blue = ((color >> 8) & 0xff) / 255.0f;
  1161. float alpha = (color & 0xff) / 255.0f;
  1162. // FIXME: Set up write pointers based on selected endianness (glPixelStore)
  1163. red_ptr = out_ptr + (component_size * red_offset);
  1164. green_ptr = out_ptr + (component_size * green_offset);
  1165. blue_ptr = out_ptr + (component_size * blue_offset);
  1166. alpha_ptr = out_ptr + (component_size * alpha_offset);
  1167. switch (type) {
  1168. case GL_BYTE:
  1169. if (write_red)
  1170. *reinterpret_cast<GLchar*>(red_ptr) = float_to_i8(red);
  1171. if (write_green)
  1172. *reinterpret_cast<GLchar*>(green_ptr) = float_to_i8(green);
  1173. if (write_blue)
  1174. *reinterpret_cast<GLchar*>(blue_ptr) = float_to_i8(blue);
  1175. if (write_alpha)
  1176. *reinterpret_cast<GLchar*>(alpha_ptr) = float_to_i8(alpha);
  1177. break;
  1178. case GL_UNSIGNED_BYTE:
  1179. if (write_red)
  1180. *reinterpret_cast<GLubyte*>(red_ptr) = float_to_u8(red);
  1181. if (write_green)
  1182. *reinterpret_cast<GLubyte*>(green_ptr) = float_to_u8(green);
  1183. if (write_blue)
  1184. *reinterpret_cast<GLubyte*>(blue_ptr) = float_to_u8(blue);
  1185. if (write_alpha)
  1186. *reinterpret_cast<GLubyte*>(alpha_ptr) = float_to_u8(alpha);
  1187. break;
  1188. case GL_SHORT:
  1189. if (write_red)
  1190. *reinterpret_cast<GLshort*>(red_ptr) = float_to_i16(red);
  1191. if (write_green)
  1192. *reinterpret_cast<GLshort*>(green_ptr) = float_to_i16(green);
  1193. if (write_blue)
  1194. *reinterpret_cast<GLshort*>(blue_ptr) = float_to_i16(blue);
  1195. if (write_alpha)
  1196. *reinterpret_cast<GLshort*>(alpha_ptr) = float_to_i16(alpha);
  1197. break;
  1198. case GL_UNSIGNED_SHORT:
  1199. if (write_red)
  1200. *reinterpret_cast<GLushort*>(red_ptr) = float_to_u16(red);
  1201. if (write_green)
  1202. *reinterpret_cast<GLushort*>(green_ptr) = float_to_u16(green);
  1203. if (write_blue)
  1204. *reinterpret_cast<GLushort*>(blue_ptr) = float_to_u16(blue);
  1205. if (write_alpha)
  1206. *reinterpret_cast<GLushort*>(alpha_ptr) = float_to_u16(alpha);
  1207. break;
  1208. case GL_INT:
  1209. if (write_red)
  1210. *reinterpret_cast<GLint*>(red_ptr) = float_to_i32(red);
  1211. if (write_green)
  1212. *reinterpret_cast<GLint*>(green_ptr) = float_to_i32(green);
  1213. if (write_blue)
  1214. *reinterpret_cast<GLint*>(blue_ptr) = float_to_i32(blue);
  1215. if (write_alpha)
  1216. *reinterpret_cast<GLint*>(alpha_ptr) = float_to_i32(alpha);
  1217. break;
  1218. case GL_UNSIGNED_INT:
  1219. if (write_red)
  1220. *reinterpret_cast<GLuint*>(red_ptr) = float_to_u32(red);
  1221. if (write_green)
  1222. *reinterpret_cast<GLuint*>(green_ptr) = float_to_u32(green);
  1223. if (write_blue)
  1224. *reinterpret_cast<GLuint*>(blue_ptr) = float_to_u32(blue);
  1225. if (write_alpha)
  1226. *reinterpret_cast<GLuint*>(alpha_ptr) = float_to_u32(alpha);
  1227. break;
  1228. case GL_FLOAT:
  1229. if (write_red)
  1230. *reinterpret_cast<GLfloat*>(red_ptr) = min(max(red, 0.0f), 1.0f);
  1231. if (write_green)
  1232. *reinterpret_cast<GLfloat*>(green_ptr) = min(max(green, 0.0f), 1.0f);
  1233. if (write_blue)
  1234. *reinterpret_cast<GLfloat*>(blue_ptr) = min(max(blue, 0.0f), 1.0f);
  1235. if (write_alpha)
  1236. *reinterpret_cast<GLfloat*>(alpha_ptr) = min(max(alpha, 0.0f), 1.0f);
  1237. break;
  1238. }
  1239. out_ptr += pixel_bytes;
  1240. }
  1241. out_ptr += row_alignment_bytes;
  1242. }
  1243. }
  1244. void SoftwareGLContext::gl_bind_texture(GLenum target, GLuint texture)
  1245. {
  1246. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1247. // FIXME: We only support GL_TEXTURE_2D for now
  1248. RETURN_WITH_ERROR_IF(target != GL_TEXTURE_2D, GL_INVALID_ENUM);
  1249. if (texture == 0) {
  1250. switch (target) {
  1251. case GL_TEXTURE_2D:
  1252. m_active_texture_unit->bind_texture_to_target(target, nullptr);
  1253. return;
  1254. default:
  1255. VERIFY_NOT_REACHED();
  1256. return;
  1257. }
  1258. }
  1259. auto it = m_allocated_textures.find(texture);
  1260. // The texture name does not exist
  1261. RETURN_WITH_ERROR_IF(it == m_allocated_textures.end(), GL_INVALID_VALUE);
  1262. auto texture_object = it->value;
  1263. // Binding a texture to a different target than it was first bound is an invalid operation
  1264. // FIXME: We only support GL_TEXTURE_2D for now
  1265. RETURN_WITH_ERROR_IF(target == GL_TEXTURE_2D && !texture_object.is_null() && !texture_object->is_texture_2d(), GL_INVALID_OPERATION);
  1266. if (!texture_object) {
  1267. // This is the first time the texture is bound. Allocate an actual texture object
  1268. switch (target) {
  1269. case GL_TEXTURE_2D:
  1270. texture_object = adopt_ref(*new Texture2D());
  1271. break;
  1272. default:
  1273. VERIFY_NOT_REACHED();
  1274. }
  1275. m_allocated_textures.set(texture, texture_object);
  1276. }
  1277. switch (target) {
  1278. case GL_TEXTURE_2D:
  1279. m_active_texture_unit->bind_texture_to_target(target, texture_object);
  1280. break;
  1281. }
  1282. }
  1283. void SoftwareGLContext::gl_active_texture(GLenum texture)
  1284. {
  1285. RETURN_WITH_ERROR_IF(texture < GL_TEXTURE0 || texture > GL_TEXTURE31, GL_INVALID_ENUM);
  1286. m_active_texture_unit = &m_texture_units.at(texture - GL_TEXTURE0);
  1287. }
  1288. void SoftwareGLContext::gl_get_booleanv(GLenum pname, GLboolean* data)
  1289. {
  1290. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1291. switch (pname) {
  1292. case GL_BLEND:
  1293. *data = m_blend_enabled ? GL_TRUE : GL_FALSE;
  1294. break;
  1295. case GL_ALPHA_TEST:
  1296. *data = m_alpha_test_func ? GL_TRUE : GL_FALSE;
  1297. break;
  1298. case GL_DEPTH_TEST:
  1299. *data = m_depth_test_enabled ? GL_TRUE : GL_FALSE;
  1300. break;
  1301. case GL_CULL_FACE:
  1302. *data = m_cull_faces ? GL_TRUE : GL_FALSE;
  1303. break;
  1304. case GL_STENCIL_TEST:
  1305. *data = m_stencil_test_enabled ? GL_TRUE : GL_FALSE;
  1306. break;
  1307. case GL_TEXTURE_1D:
  1308. *data = m_active_texture_unit->texture_1d_enabled() ? GL_TRUE : GL_FALSE;
  1309. break;
  1310. case GL_TEXTURE_2D:
  1311. *data = m_active_texture_unit->texture_2d_enabled() ? GL_TRUE : GL_FALSE;
  1312. break;
  1313. case GL_TEXTURE_3D:
  1314. *data = m_active_texture_unit->texture_3d_enabled() ? GL_TRUE : GL_FALSE;
  1315. break;
  1316. case GL_TEXTURE_CUBE_MAP:
  1317. *data = m_active_texture_unit->texture_cube_map_enabled() ? GL_TRUE : GL_FALSE;
  1318. break;
  1319. default:
  1320. // According to the Khronos docs, we always return GL_INVALID_ENUM if we encounter a non-accepted value
  1321. // for `pname`
  1322. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1323. }
  1324. }
  1325. void SoftwareGLContext::gl_get_doublev(GLenum pname, GLdouble* params)
  1326. {
  1327. get_floating_point(pname, params);
  1328. }
  1329. void SoftwareGLContext::gl_get_floatv(GLenum pname, GLfloat* params)
  1330. {
  1331. get_floating_point(pname, params);
  1332. }
  1333. template<typename T>
  1334. void SoftwareGLContext::get_floating_point(GLenum pname, T* params)
  1335. {
  1336. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1337. auto flatten_and_assign_matrix = [&params](const FloatMatrix4x4& matrix) {
  1338. auto elements = matrix.elements();
  1339. for (size_t i = 0; i < 4; ++i) {
  1340. for (size_t j = 0; j < 4; ++j) {
  1341. params[i * 4 + j] = static_cast<T>(elements[i][j]);
  1342. }
  1343. }
  1344. };
  1345. switch (pname) {
  1346. case GL_MODELVIEW_MATRIX:
  1347. if (m_current_matrix_mode == GL_MODELVIEW)
  1348. flatten_and_assign_matrix(m_model_view_matrix);
  1349. else {
  1350. if (m_model_view_matrix_stack.is_empty())
  1351. flatten_and_assign_matrix(FloatMatrix4x4::identity());
  1352. else
  1353. flatten_and_assign_matrix(m_model_view_matrix_stack.last());
  1354. }
  1355. break;
  1356. default:
  1357. dbgln_if(GL_DEBUG, "FIXME: unimplemented floating point parameter {:#x}", pname);
  1358. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1359. }
  1360. }
  1361. void SoftwareGLContext::gl_get_integerv(GLenum pname, GLint* data)
  1362. {
  1363. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1364. switch (pname) {
  1365. case GL_BLEND_SRC_ALPHA:
  1366. *data = m_blend_source_factor;
  1367. break;
  1368. case GL_BLEND_DST_ALPHA:
  1369. *data = m_blend_destination_factor;
  1370. break;
  1371. case GL_MAX_TEXTURE_UNITS:
  1372. *data = m_texture_units.size();
  1373. break;
  1374. case GL_MAX_TEXTURE_SIZE:
  1375. *data = 4096;
  1376. break;
  1377. case GL_PACK_ALIGNMENT:
  1378. *data = m_pack_alignment;
  1379. break;
  1380. case GL_SCISSOR_BOX: {
  1381. auto scissor_box = m_rasterizer.options().scissor_box;
  1382. data[0] = scissor_box.x();
  1383. data[1] = scissor_box.y();
  1384. data[2] = scissor_box.width();
  1385. data[3] = scissor_box.height();
  1386. break;
  1387. }
  1388. case GL_UNPACK_ALIGNMENT:
  1389. *data = m_unpack_alignment;
  1390. break;
  1391. case GL_UNPACK_ROW_LENGTH:
  1392. *data = m_unpack_row_length;
  1393. break;
  1394. default:
  1395. // According to the Khronos docs, we always return GL_INVALID_ENUM if we encounter a non-accepted value
  1396. // for `pname`
  1397. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1398. }
  1399. }
  1400. void SoftwareGLContext::gl_depth_mask(GLboolean flag)
  1401. {
  1402. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_depth_mask, flag);
  1403. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1404. auto options = m_rasterizer.options();
  1405. options.enable_depth_write = (flag != GL_FALSE);
  1406. m_rasterizer.set_options(options);
  1407. }
  1408. void SoftwareGLContext::gl_enable_client_state(GLenum cap)
  1409. {
  1410. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1411. switch (cap) {
  1412. case GL_VERTEX_ARRAY:
  1413. m_client_side_vertex_array_enabled = true;
  1414. break;
  1415. case GL_COLOR_ARRAY:
  1416. m_client_side_color_array_enabled = true;
  1417. break;
  1418. case GL_TEXTURE_COORD_ARRAY:
  1419. m_client_side_texture_coord_array_enabled = true;
  1420. break;
  1421. default:
  1422. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1423. }
  1424. }
  1425. void SoftwareGLContext::gl_disable_client_state(GLenum cap)
  1426. {
  1427. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1428. switch (cap) {
  1429. case GL_VERTEX_ARRAY:
  1430. m_client_side_vertex_array_enabled = false;
  1431. break;
  1432. case GL_COLOR_ARRAY:
  1433. m_client_side_color_array_enabled = false;
  1434. break;
  1435. case GL_TEXTURE_COORD_ARRAY:
  1436. m_client_side_texture_coord_array_enabled = false;
  1437. break;
  1438. default:
  1439. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1440. }
  1441. }
  1442. void SoftwareGLContext::gl_vertex_pointer(GLint size, GLenum type, GLsizei stride, const void* pointer)
  1443. {
  1444. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1445. RETURN_WITH_ERROR_IF(!(size == 2 || size == 3 || size == 4), GL_INVALID_VALUE);
  1446. RETURN_WITH_ERROR_IF(!(type == GL_SHORT || type == GL_INT || type == GL_FLOAT || type == GL_DOUBLE), GL_INVALID_ENUM);
  1447. RETURN_WITH_ERROR_IF(stride < 0, GL_INVALID_VALUE);
  1448. m_client_vertex_pointer.size = size;
  1449. m_client_vertex_pointer.type = type;
  1450. m_client_vertex_pointer.stride = stride;
  1451. m_client_vertex_pointer.pointer = pointer;
  1452. }
  1453. void SoftwareGLContext::gl_color_pointer(GLint size, GLenum type, GLsizei stride, const void* pointer)
  1454. {
  1455. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1456. RETURN_WITH_ERROR_IF(!(size == 3 || size == 4), GL_INVALID_VALUE);
  1457. RETURN_WITH_ERROR_IF(!(type == GL_BYTE
  1458. || type == GL_UNSIGNED_BYTE
  1459. || type == GL_SHORT
  1460. || type == GL_UNSIGNED_SHORT
  1461. || type == GL_INT
  1462. || type == GL_UNSIGNED_INT
  1463. || type == GL_FLOAT
  1464. || type == GL_DOUBLE),
  1465. GL_INVALID_ENUM);
  1466. RETURN_WITH_ERROR_IF(stride < 0, GL_INVALID_VALUE);
  1467. m_client_color_pointer.size = size;
  1468. m_client_color_pointer.type = type;
  1469. m_client_color_pointer.stride = stride;
  1470. m_client_color_pointer.pointer = pointer;
  1471. }
  1472. void SoftwareGLContext::gl_tex_coord_pointer(GLint size, GLenum type, GLsizei stride, const void* pointer)
  1473. {
  1474. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1475. RETURN_WITH_ERROR_IF(!(size == 1 || size == 2 || size == 3 || size == 4), GL_INVALID_VALUE);
  1476. RETURN_WITH_ERROR_IF(!(type == GL_SHORT || type == GL_INT || type == GL_FLOAT || type == GL_DOUBLE), GL_INVALID_ENUM);
  1477. RETURN_WITH_ERROR_IF(stride < 0, GL_INVALID_VALUE);
  1478. m_client_tex_coord_pointer.size = size;
  1479. m_client_tex_coord_pointer.type = type;
  1480. m_client_tex_coord_pointer.stride = stride;
  1481. m_client_tex_coord_pointer.pointer = pointer;
  1482. }
  1483. void SoftwareGLContext::gl_tex_env(GLenum target, GLenum pname, GLfloat param)
  1484. {
  1485. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_env, target, pname, param);
  1486. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1487. if (target == GL_TEXTURE_ENV) {
  1488. if (pname == GL_TEXTURE_ENV_MODE) {
  1489. auto param_enum = static_cast<GLenum>(param);
  1490. switch (param_enum) {
  1491. case GL_MODULATE:
  1492. case GL_REPLACE:
  1493. case GL_DECAL:
  1494. m_active_texture_unit->set_env_mode(param_enum);
  1495. break;
  1496. default:
  1497. // FIXME: We currently only support a subset of possible param values. Implement the rest!
  1498. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1499. break;
  1500. }
  1501. } else {
  1502. // FIXME: We currently only support a subset of possible pname values. Implement the rest!
  1503. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1504. }
  1505. } else {
  1506. // FIXME: We currently only support a subset of possible target values. Implement the rest!
  1507. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1508. }
  1509. }
  1510. void SoftwareGLContext::gl_draw_arrays(GLenum mode, GLint first, GLsizei count)
  1511. {
  1512. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_draw_arrays, mode, first, count);
  1513. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1514. // FIXME: Some modes are still missing (GL_POINTS, GL_LINE_STRIP, GL_LINE_LOOP, GL_LINES,GL_QUAD_STRIP)
  1515. RETURN_WITH_ERROR_IF(!(mode == GL_TRIANGLE_STRIP
  1516. || mode == GL_TRIANGLE_FAN
  1517. || mode == GL_TRIANGLES
  1518. || mode == GL_QUADS
  1519. || mode == GL_POLYGON),
  1520. GL_INVALID_ENUM);
  1521. RETURN_WITH_ERROR_IF(count < 0, GL_INVALID_VALUE);
  1522. // At least the vertex array needs to be enabled
  1523. if (!m_client_side_vertex_array_enabled)
  1524. return;
  1525. auto last = first + count;
  1526. glBegin(mode);
  1527. for (int i = first; i < last; i++) {
  1528. if (m_client_side_texture_coord_array_enabled) {
  1529. float tex_coords[4] { 0, 0, 0, 0 };
  1530. read_from_vertex_attribute_pointer(m_client_tex_coord_pointer, i, tex_coords, false);
  1531. glTexCoord4fv(tex_coords);
  1532. }
  1533. if (m_client_side_color_array_enabled) {
  1534. float color[4] { 0, 0, 0, 1 };
  1535. read_from_vertex_attribute_pointer(m_client_color_pointer, i, color, true);
  1536. glColor4fv(color);
  1537. }
  1538. float vertex[4] { 0, 0, 0, 1 };
  1539. read_from_vertex_attribute_pointer(m_client_vertex_pointer, i, vertex, false);
  1540. glVertex4fv(vertex);
  1541. }
  1542. glEnd();
  1543. }
  1544. void SoftwareGLContext::gl_draw_elements(GLenum mode, GLsizei count, GLenum type, const void* indices)
  1545. {
  1546. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_draw_elements, mode, count, type, indices);
  1547. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1548. // FIXME: Some modes are still missing (GL_POINTS, GL_LINE_STRIP, GL_LINE_LOOP, GL_LINES,GL_QUAD_STRIP)
  1549. RETURN_WITH_ERROR_IF(!(mode == GL_TRIANGLE_STRIP
  1550. || mode == GL_TRIANGLE_FAN
  1551. || mode == GL_TRIANGLES
  1552. || mode == GL_QUADS
  1553. || mode == GL_POLYGON),
  1554. GL_INVALID_ENUM);
  1555. RETURN_WITH_ERROR_IF(!(type == GL_UNSIGNED_BYTE
  1556. || type == GL_UNSIGNED_SHORT
  1557. || type == GL_UNSIGNED_INT),
  1558. GL_INVALID_ENUM);
  1559. RETURN_WITH_ERROR_IF(count < 0, GL_INVALID_VALUE);
  1560. // At least the vertex array needs to be enabled
  1561. if (!m_client_side_vertex_array_enabled)
  1562. return;
  1563. glBegin(mode);
  1564. for (int index = 0; index < count; index++) {
  1565. int i = 0;
  1566. switch (type) {
  1567. case GL_UNSIGNED_BYTE:
  1568. i = reinterpret_cast<const GLubyte*>(indices)[index];
  1569. break;
  1570. case GL_UNSIGNED_SHORT:
  1571. i = reinterpret_cast<const GLushort*>(indices)[index];
  1572. break;
  1573. case GL_UNSIGNED_INT:
  1574. i = reinterpret_cast<const GLuint*>(indices)[index];
  1575. break;
  1576. }
  1577. if (m_client_side_texture_coord_array_enabled) {
  1578. float tex_coords[4] { 0, 0, 0, 0 };
  1579. read_from_vertex_attribute_pointer(m_client_tex_coord_pointer, i, tex_coords, false);
  1580. glTexCoord4fv(tex_coords);
  1581. }
  1582. if (m_client_side_color_array_enabled) {
  1583. float color[4] { 0, 0, 0, 1 };
  1584. read_from_vertex_attribute_pointer(m_client_color_pointer, i, color, true);
  1585. glColor4fv(color);
  1586. }
  1587. float vertex[4] { 0, 0, 0, 1 };
  1588. read_from_vertex_attribute_pointer(m_client_vertex_pointer, i, vertex, false);
  1589. glVertex4fv(vertex);
  1590. }
  1591. glEnd();
  1592. }
  1593. void SoftwareGLContext::gl_depth_range(GLdouble min, GLdouble max)
  1594. {
  1595. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_depth_range, min, max);
  1596. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1597. auto options = m_rasterizer.options();
  1598. options.depth_min = clamp(min, 0.f, 1.f);
  1599. options.depth_max = clamp(max, 0.f, 1.f);
  1600. m_rasterizer.set_options(options);
  1601. }
  1602. void SoftwareGLContext::gl_depth_func(GLenum func)
  1603. {
  1604. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_depth_func, func);
  1605. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1606. RETURN_WITH_ERROR_IF(!(func == GL_NEVER
  1607. || func == GL_LESS
  1608. || func == GL_EQUAL
  1609. || func == GL_LEQUAL
  1610. || func == GL_GREATER
  1611. || func == GL_NOTEQUAL
  1612. || func == GL_GEQUAL
  1613. || func == GL_ALWAYS),
  1614. GL_INVALID_ENUM);
  1615. auto options = m_rasterizer.options();
  1616. options.depth_func = func;
  1617. m_rasterizer.set_options(options);
  1618. }
  1619. // General helper function to read arbitrary vertex attribute data into a float array
  1620. void SoftwareGLContext::read_from_vertex_attribute_pointer(VertexAttribPointer const& attrib, int index, float* elements, bool normalize)
  1621. {
  1622. auto byte_ptr = reinterpret_cast<const char*>(attrib.pointer);
  1623. size_t stride = attrib.stride;
  1624. switch (attrib.type) {
  1625. case GL_BYTE: {
  1626. if (stride == 0)
  1627. stride = sizeof(GLbyte) * attrib.size;
  1628. for (int i = 0; i < attrib.size; i++) {
  1629. elements[i] = *(reinterpret_cast<const GLbyte*>(byte_ptr + stride * index) + i);
  1630. if (normalize)
  1631. elements[i] /= 0x80;
  1632. }
  1633. break;
  1634. }
  1635. case GL_UNSIGNED_BYTE: {
  1636. if (stride == 0)
  1637. stride = sizeof(GLubyte) * attrib.size;
  1638. for (int i = 0; i < attrib.size; i++) {
  1639. elements[i] = *(reinterpret_cast<const GLubyte*>(byte_ptr + stride * index) + i);
  1640. if (normalize)
  1641. elements[i] /= 0xff;
  1642. }
  1643. break;
  1644. }
  1645. case GL_SHORT: {
  1646. if (stride == 0)
  1647. stride = sizeof(GLshort) * attrib.size;
  1648. for (int i = 0; i < attrib.size; i++) {
  1649. elements[i] = *(reinterpret_cast<const GLshort*>(byte_ptr + stride * index) + i);
  1650. if (normalize)
  1651. elements[i] /= 0x8000;
  1652. }
  1653. break;
  1654. }
  1655. case GL_UNSIGNED_SHORT: {
  1656. if (stride == 0)
  1657. stride = sizeof(GLushort) * attrib.size;
  1658. for (int i = 0; i < attrib.size; i++) {
  1659. elements[i] = *(reinterpret_cast<const GLushort*>(byte_ptr + stride * index) + i);
  1660. if (normalize)
  1661. elements[i] /= 0xffff;
  1662. }
  1663. break;
  1664. }
  1665. case GL_INT: {
  1666. if (stride == 0)
  1667. stride = sizeof(GLint) * attrib.size;
  1668. for (int i = 0; i < attrib.size; i++) {
  1669. elements[i] = *(reinterpret_cast<const GLint*>(byte_ptr + stride * index) + i);
  1670. if (normalize)
  1671. elements[i] /= 0x80000000;
  1672. }
  1673. break;
  1674. }
  1675. case GL_UNSIGNED_INT: {
  1676. if (stride == 0)
  1677. stride = sizeof(GLuint) * attrib.size;
  1678. for (int i = 0; i < attrib.size; i++) {
  1679. elements[i] = *(reinterpret_cast<const GLuint*>(byte_ptr + stride * index) + i);
  1680. if (normalize)
  1681. elements[i] /= 0xffffffff;
  1682. }
  1683. break;
  1684. }
  1685. case GL_FLOAT: {
  1686. if (stride == 0)
  1687. stride = sizeof(GLfloat) * attrib.size;
  1688. for (int i = 0; i < attrib.size; i++) {
  1689. elements[i] = *(reinterpret_cast<const GLfloat*>(byte_ptr + stride * index) + i);
  1690. }
  1691. break;
  1692. }
  1693. case GL_DOUBLE: {
  1694. if (stride == 0)
  1695. stride = sizeof(GLdouble) * attrib.size;
  1696. for (int i = 0; i < attrib.size; i++) {
  1697. elements[i] = static_cast<float>(*(reinterpret_cast<const GLdouble*>(byte_ptr + stride * index) + i));
  1698. }
  1699. break;
  1700. }
  1701. }
  1702. }
  1703. void SoftwareGLContext::gl_color_mask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha)
  1704. {
  1705. auto options = m_rasterizer.options();
  1706. auto mask = options.color_mask;
  1707. if (!red)
  1708. mask &= ~0x000000ff;
  1709. else
  1710. mask |= 0x000000ff;
  1711. if (!green)
  1712. mask &= ~0x0000ff00;
  1713. else
  1714. mask |= 0x0000ff00;
  1715. if (!blue)
  1716. mask &= ~0x00ff0000;
  1717. else
  1718. mask |= 0x00ff0000;
  1719. if (!alpha)
  1720. mask &= ~0xff000000;
  1721. else
  1722. mask |= 0xff000000;
  1723. options.color_mask = mask;
  1724. m_rasterizer.set_options(options);
  1725. }
  1726. void SoftwareGLContext::gl_polygon_mode(GLenum face, GLenum mode)
  1727. {
  1728. RETURN_WITH_ERROR_IF(!(face == GL_BACK || face == GL_FRONT || face == GL_FRONT_AND_BACK), GL_INVALID_ENUM);
  1729. RETURN_WITH_ERROR_IF(!(mode == GL_POINT || mode == GL_LINE || mode == GL_FILL), GL_INVALID_ENUM);
  1730. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1731. auto options = m_rasterizer.options();
  1732. options.polygon_mode = mode;
  1733. m_rasterizer.set_options(options);
  1734. }
  1735. void SoftwareGLContext::gl_polygon_offset(GLfloat factor, GLfloat units)
  1736. {
  1737. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_polygon_offset, factor, units);
  1738. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1739. auto rasterizer_options = m_rasterizer.options();
  1740. rasterizer_options.depth_offset_factor = factor;
  1741. rasterizer_options.depth_offset_constant = units;
  1742. m_rasterizer.set_options(rasterizer_options);
  1743. }
  1744. void SoftwareGLContext::gl_fogfv(GLenum pname, GLfloat* params)
  1745. {
  1746. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1747. auto options = m_rasterizer.options();
  1748. switch (pname) {
  1749. case GL_FOG_COLOR:
  1750. // Set rasterizer options fog color
  1751. // NOTE: We purposefully don't check for `nullptr` here (as with other calls). The spec states nothing
  1752. // about us checking for such things. If the programmer does so and hits SIGSEGV, that's on them.
  1753. options.fog_color = FloatVector4 { params[0], params[1], params[2], params[3] };
  1754. break;
  1755. default:
  1756. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1757. }
  1758. m_rasterizer.set_options(options);
  1759. }
  1760. void SoftwareGLContext::gl_fogf(GLenum pname, GLfloat param)
  1761. {
  1762. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1763. RETURN_WITH_ERROR_IF(param < 0.0f, GL_INVALID_VALUE);
  1764. auto options = m_rasterizer.options();
  1765. switch (pname) {
  1766. case GL_FOG_DENSITY:
  1767. options.fog_density = param;
  1768. break;
  1769. default:
  1770. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1771. }
  1772. m_rasterizer.set_options(options);
  1773. }
  1774. void SoftwareGLContext::gl_fogi(GLenum pname, GLint param)
  1775. {
  1776. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1777. RETURN_WITH_ERROR_IF(!(param == GL_EXP || param == GL_EXP2 || param != GL_LINEAR), GL_INVALID_ENUM);
  1778. auto options = m_rasterizer.options();
  1779. switch (pname) {
  1780. case GL_FOG_MODE:
  1781. options.fog_mode = param;
  1782. break;
  1783. default:
  1784. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1785. }
  1786. m_rasterizer.set_options(options);
  1787. }
  1788. void SoftwareGLContext::gl_pixel_storei(GLenum pname, GLint param)
  1789. {
  1790. // FIXME: Implement missing parameters
  1791. switch (pname) {
  1792. case GL_PACK_ALIGNMENT:
  1793. RETURN_WITH_ERROR_IF(param != 1 && param != 2 && param != 4 && param != 8, GL_INVALID_VALUE);
  1794. m_pack_alignment = param;
  1795. break;
  1796. case GL_UNPACK_ROW_LENGTH:
  1797. RETURN_WITH_ERROR_IF(param < 0, GL_INVALID_VALUE);
  1798. m_unpack_row_length = static_cast<size_t>(param);
  1799. break;
  1800. case GL_UNPACK_ALIGNMENT:
  1801. RETURN_WITH_ERROR_IF(param != 1 && param != 2 && param != 4 && param != 8, GL_INVALID_VALUE);
  1802. m_unpack_alignment = param;
  1803. break;
  1804. default:
  1805. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1806. break;
  1807. }
  1808. }
  1809. void SoftwareGLContext::gl_scissor(GLint x, GLint y, GLsizei width, GLsizei height)
  1810. {
  1811. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_scissor, x, y, width, height);
  1812. RETURN_WITH_ERROR_IF(width < 0 || height < 0, GL_INVALID_VALUE);
  1813. auto options = m_rasterizer.options();
  1814. options.scissor_box = { x, y, width, height };
  1815. m_rasterizer.set_options(options);
  1816. }
  1817. void SoftwareGLContext::gl_stencil_func_separate(GLenum face, GLenum func, GLint ref, GLuint mask)
  1818. {
  1819. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_stencil_func_separate, face, func, ref, mask);
  1820. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1821. RETURN_WITH_ERROR_IF(!(face == GL_FRONT || face == GL_BACK || face == GL_FRONT_AND_BACK), GL_INVALID_ENUM);
  1822. RETURN_WITH_ERROR_IF(!(func == GL_NEVER
  1823. || func == GL_LESS
  1824. || func == GL_LEQUAL
  1825. || func == GL_GREATER
  1826. || func == GL_GEQUAL
  1827. || func == GL_EQUAL
  1828. || func == GL_NOTEQUAL
  1829. || func == GL_ALWAYS),
  1830. GL_INVALID_ENUM);
  1831. // FIXME: "ref is clamped to the range 02^n - 1 , where n is the number of bitplanes in the stencil buffer"
  1832. StencilFunctionOptions new_options = { func, ref, mask };
  1833. if (face == GL_FRONT || face == GL_FRONT_AND_BACK)
  1834. m_stencil_frontfacing_func = new_options;
  1835. if (face == GL_BACK || face == GL_FRONT_AND_BACK)
  1836. m_stencil_backfacing_func = new_options;
  1837. }
  1838. void SoftwareGLContext::gl_stencil_op_separate(GLenum face, GLenum sfail, GLenum dpfail, GLenum dppass)
  1839. {
  1840. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_stencil_op_separate, face, sfail, dpfail, dppass);
  1841. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1842. RETURN_WITH_ERROR_IF(!(face == GL_FRONT || face == GL_BACK || face == GL_FRONT_AND_BACK), GL_INVALID_ENUM);
  1843. RETURN_WITH_ERROR_IF(!(sfail == GL_KEEP
  1844. || sfail == GL_ZERO
  1845. || sfail == GL_REPLACE
  1846. || sfail == GL_INCR
  1847. || sfail == GL_INCR_WRAP
  1848. || sfail == GL_DECR
  1849. || sfail == GL_DECR_WRAP
  1850. || sfail == GL_INVERT),
  1851. GL_INVALID_ENUM);
  1852. RETURN_WITH_ERROR_IF(!(dpfail == GL_KEEP
  1853. || dpfail == GL_ZERO
  1854. || dpfail == GL_REPLACE
  1855. || dpfail == GL_INCR
  1856. || dpfail == GL_INCR_WRAP
  1857. || dpfail == GL_DECR
  1858. || dpfail == GL_DECR_WRAP
  1859. || dpfail == GL_INVERT),
  1860. GL_INVALID_ENUM);
  1861. RETURN_WITH_ERROR_IF(!(dppass == GL_KEEP
  1862. || dppass == GL_ZERO
  1863. || dppass == GL_REPLACE
  1864. || dppass == GL_INCR
  1865. || dppass == GL_INCR_WRAP
  1866. || dppass == GL_DECR
  1867. || dppass == GL_DECR_WRAP
  1868. || dppass == GL_INVERT),
  1869. GL_INVALID_ENUM);
  1870. StencilOperationOptions new_options = { sfail, dpfail, dppass };
  1871. if (face == GL_FRONT || face == GL_FRONT_AND_BACK)
  1872. m_stencil_frontfacing_op = new_options;
  1873. if (face == GL_BACK || face == GL_FRONT_AND_BACK)
  1874. m_stencil_backfacing_op = new_options;
  1875. }
  1876. void SoftwareGLContext::gl_normal(GLfloat nx, GLfloat ny, GLfloat nz)
  1877. {
  1878. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_normal, nx, ny, nz);
  1879. m_current_vertex_normal = { nx, ny, nz };
  1880. }
  1881. void SoftwareGLContext::gl_raster_pos(GLfloat x, GLfloat y, GLfloat z, GLfloat w)
  1882. {
  1883. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_raster_pos, x, y, z, w);
  1884. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1885. m_current_raster_position.window_coordinates = { x, y, z };
  1886. m_current_raster_position.clip_coordinate_value = w;
  1887. }
  1888. void SoftwareGLContext::gl_materialv(GLenum face, GLenum pname, GLfloat const* params)
  1889. {
  1890. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_materialv, face, pname, params);
  1891. RETURN_WITH_ERROR_IF(!(face == GL_FRONT || face == GL_BACK || face == GL_FRONT_AND_BACK), GL_INVALID_ENUM);
  1892. RETURN_WITH_ERROR_IF(!(pname == GL_AMBIENT
  1893. || pname == GL_DIFFUSE
  1894. || pname == GL_SPECULAR
  1895. || pname == GL_EMISSION
  1896. || pname == GL_SHININESS
  1897. || pname == GL_AMBIENT_AND_DIFFUSE
  1898. || pname == GL_COLOR_INDEXES),
  1899. GL_INVALID_ENUM);
  1900. GLfloat x, y, z, w;
  1901. switch (pname) {
  1902. case GL_SHININESS:
  1903. x = params[0];
  1904. y = 0.0f;
  1905. z = 0.0f;
  1906. w = 0.0f;
  1907. break;
  1908. case GL_COLOR_INDEXES:
  1909. x = params[0];
  1910. y = params[1];
  1911. z = params[2];
  1912. w = 0.0f;
  1913. break;
  1914. default:
  1915. x = params[0];
  1916. y = params[1];
  1917. z = params[2];
  1918. w = params[3];
  1919. }
  1920. // FIXME: implement this method
  1921. dbgln_if(GL_DEBUG, "SoftwareGLContext FIXME: gl_materialv({}, {}, {}, {}, {}, {})", face, pname, x, y, z, w);
  1922. }
  1923. void SoftwareGLContext::present()
  1924. {
  1925. m_rasterizer.blit_to(*m_frontbuffer);
  1926. }
  1927. }