SoftwareGLContext.cpp 94 KB

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