SoftwareGLContext.cpp 97 KB

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