SoftwareGLContext.cpp 139 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685
  1. /*
  2. * Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
  3. * Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
  4. * Copyright (c) 2022, Jelle Raaijmakers <jelle@gmta.nl>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/Assertions.h>
  9. #include <AK/Debug.h>
  10. #include <AK/Format.h>
  11. #include <AK/QuickSort.h>
  12. #include <AK/StringBuilder.h>
  13. #include <AK/TemporaryChange.h>
  14. #include <AK/Variant.h>
  15. #include <AK/Vector.h>
  16. #include <LibGL/SoftwareGLContext.h>
  17. #include <LibGfx/Bitmap.h>
  18. #include <LibGfx/Painter.h>
  19. #include <LibGfx/Vector4.h>
  20. #include <LibSoftGPU/Device.h>
  21. #include <LibSoftGPU/Enums.h>
  22. #include <LibSoftGPU/ImageFormat.h>
  23. namespace GL {
  24. static constexpr size_t MODELVIEW_MATRIX_STACK_LIMIT = 64;
  25. static constexpr size_t PROJECTION_MATRIX_STACK_LIMIT = 8;
  26. static constexpr size_t TEXTURE_MATRIX_STACK_LIMIT = 8;
  27. #define APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(name, ...) \
  28. if (should_append_to_listing()) { \
  29. append_to_listing<&SoftwareGLContext::name>(__VA_ARGS__); \
  30. if (!should_execute_after_appending_to_listing()) \
  31. return; \
  32. }
  33. #define APPEND_TO_CALL_LIST_WITH_ARG_AND_RETURN_IF_NEEDED(name, arg) \
  34. if (should_append_to_listing()) { \
  35. auto ptr = store_in_listing(arg); \
  36. append_to_listing<&SoftwareGLContext::name>(*ptr); \
  37. if (!should_execute_after_appending_to_listing()) \
  38. return; \
  39. }
  40. #define RETURN_WITH_ERROR_IF(condition, error) \
  41. if (condition) { \
  42. dbgln_if(GL_DEBUG, "{}(): error {:#x}", __func__, error); \
  43. if (m_error == GL_NO_ERROR) \
  44. m_error = error; \
  45. return; \
  46. }
  47. #define RETURN_VALUE_WITH_ERROR_IF(condition, error, return_value) \
  48. if (condition) { \
  49. dbgln_if(GL_DEBUG, "{}(): error {:#x}", __func__, error); \
  50. if (m_error == GL_NO_ERROR) \
  51. m_error = error; \
  52. return return_value; \
  53. }
  54. SoftwareGLContext::SoftwareGLContext(Gfx::Bitmap& frontbuffer)
  55. : m_viewport(frontbuffer.rect())
  56. , m_frontbuffer(frontbuffer)
  57. , m_rasterizer(frontbuffer.size())
  58. , m_device_info(m_rasterizer.info())
  59. {
  60. m_texture_units.resize(m_device_info.num_texture_units);
  61. m_active_texture_unit = &m_texture_units[0];
  62. // Query the number lights from the device and set set up their state
  63. // locally in the GL
  64. m_light_states.resize(m_device_info.num_lights);
  65. // Set-up light0's state, as it has a different default state
  66. // to the other lights, as per the OpenGL 1.5 spec
  67. auto& light0 = m_light_states.at(0);
  68. light0.diffuse_intensity = { 1.0f, 1.0f, 1.0f, 1.0f };
  69. light0.specular_intensity = { 1.0f, 1.0f, 1.0f, 1.0f };
  70. m_light_state_is_dirty = true;
  71. m_client_side_texture_coord_array_enabled.resize(m_device_info.num_texture_units);
  72. m_client_tex_coord_pointer.resize(m_device_info.num_texture_units);
  73. m_current_vertex_tex_coord.resize(m_device_info.num_texture_units);
  74. for (auto& tex_coord : m_current_vertex_tex_coord)
  75. tex_coord = { 0.0f, 0.0f, 0.0f, 1.0f };
  76. // Initialize the texture coordinate generation coefficients
  77. // Indices 0,1,2,3 refer to the S,T,R and Q coordinate of the respective texture
  78. // coordinate generation config.
  79. m_texture_coordinate_generation.resize(m_device_info.num_texture_units);
  80. for (auto& texture_coordinate_generation : m_texture_coordinate_generation) {
  81. texture_coordinate_generation[0].object_plane_coefficients = { 1.0f, 0.0f, 0.0f, 0.0f };
  82. texture_coordinate_generation[0].eye_plane_coefficients = { 1.0f, 0.0f, 0.0f, 0.0f };
  83. texture_coordinate_generation[1].object_plane_coefficients = { 0.0f, 1.0f, 0.0f, 0.0f };
  84. texture_coordinate_generation[1].eye_plane_coefficients = { 0.0f, 1.0f, 0.0f, 0.0f };
  85. texture_coordinate_generation[2].object_plane_coefficients = { 0.0f, 0.0f, 0.0f, 0.0f };
  86. texture_coordinate_generation[2].eye_plane_coefficients = { 0.0f, 0.0f, 0.0f, 0.0f };
  87. texture_coordinate_generation[3].object_plane_coefficients = { 0.0f, 0.0f, 0.0f, 0.0f };
  88. texture_coordinate_generation[3].eye_plane_coefficients = { 0.0f, 0.0f, 0.0f, 0.0f };
  89. }
  90. build_extension_string();
  91. }
  92. Optional<ContextParameter> SoftwareGLContext::get_context_parameter(GLenum name)
  93. {
  94. switch (name) {
  95. case GL_ALPHA_BITS:
  96. return ContextParameter { .type = GL_INT, .value = { .integer_value = sizeof(float) * 8 } };
  97. case GL_ALPHA_TEST:
  98. return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_alpha_test_enabled } };
  99. case GL_BLEND:
  100. return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_blend_enabled } };
  101. case GL_BLEND_DST_ALPHA:
  102. return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast<GLint>(m_blend_destination_factor) } };
  103. case GL_BLEND_SRC_ALPHA:
  104. return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast<GLint>(m_blend_source_factor) } };
  105. case GL_BLUE_BITS:
  106. return ContextParameter { .type = GL_INT, .value = { .integer_value = sizeof(float) * 8 } };
  107. case GL_COLOR_MATERIAL:
  108. return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_color_material_enabled } };
  109. case GL_COLOR_MATERIAL_FACE:
  110. return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast<GLint>(m_color_material_face) } };
  111. case GL_COLOR_MATERIAL_MODE:
  112. return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast<GLint>(m_color_material_mode) } };
  113. case GL_CULL_FACE:
  114. return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_cull_faces } };
  115. case GL_DEPTH_BITS:
  116. return ContextParameter { .type = GL_INT, .value = { .integer_value = sizeof(float) * 8 } };
  117. case GL_DEPTH_TEST:
  118. return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_depth_test_enabled } };
  119. case GL_DITHER:
  120. return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_dither_enabled } };
  121. case GL_DOUBLEBUFFER:
  122. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = true } };
  123. case GL_FOG: {
  124. auto fog_enabled = m_rasterizer.options().fog_enabled;
  125. return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = fog_enabled } };
  126. }
  127. case GL_GREEN_BITS:
  128. return ContextParameter { .type = GL_INT, .value = { .integer_value = sizeof(float) * 8 } };
  129. case GL_LIGHTING:
  130. return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_lighting_enabled } };
  131. case GL_MAX_LIGHTS:
  132. return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast<GLint>(m_device_info.num_lights) } };
  133. case GL_MAX_MODELVIEW_STACK_DEPTH:
  134. return ContextParameter { .type = GL_INT, .value = { .integer_value = MODELVIEW_MATRIX_STACK_LIMIT } };
  135. case GL_MAX_PROJECTION_STACK_DEPTH:
  136. return ContextParameter { .type = GL_INT, .value = { .integer_value = PROJECTION_MATRIX_STACK_LIMIT } };
  137. case GL_MAX_TEXTURE_SIZE:
  138. return ContextParameter { .type = GL_INT, .value = { .integer_value = 4096 } };
  139. case GL_MAX_TEXTURE_STACK_DEPTH:
  140. return ContextParameter { .type = GL_INT, .value = { .integer_value = TEXTURE_MATRIX_STACK_LIMIT } };
  141. case GL_MAX_TEXTURE_UNITS:
  142. return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast<GLint>(m_texture_units.size()) } };
  143. case GL_NORMALIZE:
  144. return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_normalize } };
  145. case GL_PACK_ALIGNMENT:
  146. return ContextParameter { .type = GL_INT, .value = { .integer_value = m_pack_alignment } };
  147. case GL_PACK_IMAGE_HEIGHT:
  148. return ContextParameter { .type = GL_BOOL, .value = { .integer_value = 0 } };
  149. case GL_PACK_LSB_FIRST:
  150. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = false } };
  151. case GL_PACK_ROW_LENGTH:
  152. return ContextParameter { .type = GL_INT, .value = { .integer_value = 0 } };
  153. case GL_PACK_SKIP_PIXELS:
  154. return ContextParameter { .type = GL_INT, .value = { .integer_value = 0 } };
  155. case GL_PACK_SKIP_ROWS:
  156. return ContextParameter { .type = GL_INT, .value = { .integer_value = 0 } };
  157. case GL_PACK_SWAP_BYTES:
  158. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = false } };
  159. case GL_POLYGON_OFFSET_FILL:
  160. return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_depth_offset_enabled } };
  161. case GL_RED_BITS:
  162. return ContextParameter { .type = GL_INT, .value = { .integer_value = sizeof(float) * 8 } };
  163. case GL_SCISSOR_BOX: {
  164. auto scissor_box = m_rasterizer.options().scissor_box;
  165. return ContextParameter {
  166. .type = GL_INT,
  167. .count = 4,
  168. .value = {
  169. .integer_list = {
  170. scissor_box.x(),
  171. scissor_box.y(),
  172. scissor_box.width(),
  173. scissor_box.height(),
  174. } }
  175. };
  176. } break;
  177. case GL_SCISSOR_TEST: {
  178. auto scissor_enabled = m_rasterizer.options().scissor_enabled;
  179. return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = scissor_enabled } };
  180. }
  181. case GL_STENCIL_BITS:
  182. return ContextParameter { .type = GL_INT, .value = { .integer_value = m_device_info.stencil_bits } };
  183. case GL_STENCIL_CLEAR_VALUE:
  184. return ContextParameter { .type = GL_INT, .value = { .integer_value = m_clear_stencil } };
  185. case GL_STENCIL_TEST:
  186. return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_stencil_test_enabled } };
  187. case GL_TEXTURE_1D:
  188. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_active_texture_unit->texture_1d_enabled() } };
  189. case GL_TEXTURE_2D:
  190. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_active_texture_unit->texture_2d_enabled() } };
  191. case GL_TEXTURE_3D:
  192. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_active_texture_unit->texture_3d_enabled() } };
  193. case GL_TEXTURE_CUBE_MAP:
  194. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_active_texture_unit->texture_cube_map_enabled() } };
  195. case GL_TEXTURE_GEN_Q:
  196. case GL_TEXTURE_GEN_R:
  197. case GL_TEXTURE_GEN_S:
  198. case GL_TEXTURE_GEN_T: {
  199. auto generation_enabled = texture_coordinate_generation(m_active_texture_unit_index, name).enabled;
  200. return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = generation_enabled } };
  201. }
  202. case GL_UNPACK_ALIGNMENT:
  203. return ContextParameter { .type = GL_INT, .value = { .integer_value = m_unpack_alignment } };
  204. case GL_UNPACK_IMAGE_HEIGHT:
  205. return ContextParameter { .type = GL_BOOL, .value = { .integer_value = 0 } };
  206. case GL_UNPACK_LSB_FIRST:
  207. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = false } };
  208. case GL_UNPACK_ROW_LENGTH:
  209. return ContextParameter { .type = GL_INT, .value = { .integer_value = m_unpack_row_length } };
  210. case GL_UNPACK_SKIP_PIXELS:
  211. return ContextParameter { .type = GL_INT, .value = { .integer_value = 0 } };
  212. case GL_UNPACK_SKIP_ROWS:
  213. return ContextParameter { .type = GL_INT, .value = { .integer_value = 0 } };
  214. case GL_UNPACK_SWAP_BYTES:
  215. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = false } };
  216. case GL_VIEWPORT:
  217. return ContextParameter {
  218. .type = GL_INT,
  219. .count = 4,
  220. .value = {
  221. .integer_list = {
  222. m_viewport.x(),
  223. m_viewport.y(),
  224. m_viewport.width(),
  225. m_viewport.height(),
  226. } }
  227. };
  228. default:
  229. dbgln_if(GL_DEBUG, "get_context_parameter({:#x}): unknown context parameter", name);
  230. return {};
  231. }
  232. }
  233. void SoftwareGLContext::gl_begin(GLenum mode)
  234. {
  235. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_begin, mode);
  236. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  237. RETURN_WITH_ERROR_IF(mode > GL_POLYGON, GL_INVALID_ENUM);
  238. m_current_draw_mode = mode;
  239. m_in_draw_state = true; // Certain commands will now generate an error
  240. }
  241. void SoftwareGLContext::gl_clear(GLbitfield mask)
  242. {
  243. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_clear, mask);
  244. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  245. RETURN_WITH_ERROR_IF(mask & ~(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT), GL_INVALID_ENUM);
  246. if (mask & GL_COLOR_BUFFER_BIT)
  247. m_rasterizer.clear_color(m_clear_color);
  248. if (mask & GL_DEPTH_BUFFER_BIT)
  249. m_rasterizer.clear_depth(m_clear_depth);
  250. if (mask & GL_STENCIL_BUFFER_BIT)
  251. m_rasterizer.clear_stencil(m_clear_stencil);
  252. }
  253. void SoftwareGLContext::gl_clear_color(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
  254. {
  255. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_clear_color, red, green, blue, alpha);
  256. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  257. m_clear_color = { red, green, blue, alpha };
  258. m_clear_color.clamp(0.f, 1.f);
  259. }
  260. void SoftwareGLContext::gl_clear_depth(GLdouble depth)
  261. {
  262. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_clear_depth, depth);
  263. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  264. m_clear_depth = clamp(static_cast<float>(depth), 0.f, 1.f);
  265. }
  266. void SoftwareGLContext::gl_clear_stencil(GLint s)
  267. {
  268. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_clear_stencil, s);
  269. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  270. m_clear_stencil = static_cast<u8>(s & ((1 << m_device_info.stencil_bits) - 1));
  271. }
  272. void SoftwareGLContext::gl_color(GLdouble r, GLdouble g, GLdouble b, GLdouble a)
  273. {
  274. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_color, r, g, b, a);
  275. m_current_vertex_color = {
  276. static_cast<float>(r),
  277. static_cast<float>(g),
  278. static_cast<float>(b),
  279. static_cast<float>(a),
  280. };
  281. }
  282. void SoftwareGLContext::gl_end()
  283. {
  284. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_end);
  285. // Make sure we had a `glBegin` before this call...
  286. RETURN_WITH_ERROR_IF(!m_in_draw_state, GL_INVALID_OPERATION);
  287. m_in_draw_state = false;
  288. // FIXME: Add support for the remaining primitive types.
  289. if (m_current_draw_mode != GL_TRIANGLES
  290. && m_current_draw_mode != GL_TRIANGLE_FAN
  291. && m_current_draw_mode != GL_TRIANGLE_STRIP
  292. && m_current_draw_mode != GL_QUADS
  293. && m_current_draw_mode != GL_QUAD_STRIP
  294. && m_current_draw_mode != GL_POLYGON) {
  295. m_vertex_list.clear_with_capacity();
  296. dbgln_if(GL_DEBUG, "gl_end(): draw mode {:#x} unsupported", m_current_draw_mode);
  297. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  298. }
  299. Vector<size_t, 32> enabled_texture_units;
  300. for (size_t i = 0; i < m_texture_units.size(); ++i) {
  301. if (m_texture_units[i].texture_2d_enabled())
  302. enabled_texture_units.append(i);
  303. }
  304. sync_device_config();
  305. SoftGPU::PrimitiveType primitive_type;
  306. switch (m_current_draw_mode) {
  307. case GL_TRIANGLES:
  308. primitive_type = SoftGPU::PrimitiveType::Triangles;
  309. break;
  310. case GL_TRIANGLE_STRIP:
  311. case GL_QUAD_STRIP:
  312. primitive_type = SoftGPU::PrimitiveType::TriangleStrip;
  313. break;
  314. case GL_TRIANGLE_FAN:
  315. case GL_POLYGON:
  316. primitive_type = SoftGPU::PrimitiveType::TriangleFan;
  317. break;
  318. case GL_QUADS:
  319. primitive_type = SoftGPU::PrimitiveType::Quads;
  320. break;
  321. default:
  322. VERIFY_NOT_REACHED();
  323. }
  324. // Set up normals transform by taking the upper left 3x3 elements from the model view matrix
  325. // See section 2.11.3 of the OpenGL 1.5 spec
  326. auto const& mv_elements = m_model_view_matrix.elements();
  327. auto const model_view_transposed = FloatMatrix3x3(
  328. mv_elements[0][0], mv_elements[1][0], mv_elements[2][0],
  329. mv_elements[0][1], mv_elements[1][1], mv_elements[2][1],
  330. mv_elements[0][2], mv_elements[1][2], mv_elements[2][2]);
  331. auto const& normal_transform = model_view_transposed.inverse();
  332. m_rasterizer.draw_primitives(primitive_type, m_model_view_matrix, normal_transform, m_projection_matrix, m_texture_matrix, m_vertex_list, enabled_texture_units);
  333. m_vertex_list.clear_with_capacity();
  334. }
  335. void SoftwareGLContext::gl_frustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near_val, GLdouble far_val)
  336. {
  337. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_frustum, left, right, bottom, top, near_val, far_val);
  338. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  339. RETURN_WITH_ERROR_IF(near_val < 0 || far_val < 0, GL_INVALID_VALUE);
  340. RETURN_WITH_ERROR_IF(left == right || bottom == top || near_val == far_val, GL_INVALID_VALUE);
  341. // Let's do some math!
  342. auto a = static_cast<float>((right + left) / (right - left));
  343. auto b = static_cast<float>((top + bottom) / (top - bottom));
  344. auto c = static_cast<float>(-((far_val + near_val) / (far_val - near_val)));
  345. auto d = static_cast<float>(-((2 * far_val * near_val) / (far_val - near_val)));
  346. FloatMatrix4x4 frustum {
  347. static_cast<float>(2 * near_val / (right - left)), 0, a, 0,
  348. 0, static_cast<float>(2 * near_val / (top - bottom)), b, 0,
  349. 0, 0, c, d,
  350. 0, 0, -1, 0
  351. };
  352. if (m_current_matrix_mode == GL_PROJECTION)
  353. m_projection_matrix = m_projection_matrix * frustum;
  354. else if (m_current_matrix_mode == GL_MODELVIEW)
  355. m_model_view_matrix = m_model_view_matrix * frustum;
  356. else if (m_current_matrix_mode == GL_TEXTURE)
  357. m_texture_matrix = m_texture_matrix * frustum;
  358. else
  359. VERIFY_NOT_REACHED();
  360. }
  361. void SoftwareGLContext::gl_ortho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near_val, GLdouble far_val)
  362. {
  363. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_ortho, left, right, bottom, top, near_val, far_val);
  364. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  365. RETURN_WITH_ERROR_IF(left == right || bottom == top || near_val == far_val, GL_INVALID_VALUE);
  366. auto rl = right - left;
  367. auto tb = top - bottom;
  368. auto fn = far_val - near_val;
  369. auto tx = -(right + left) / rl;
  370. auto ty = -(top + bottom) / tb;
  371. auto tz = -(far_val + near_val) / fn;
  372. FloatMatrix4x4 projection {
  373. static_cast<float>(2 / rl), 0, 0, static_cast<float>(tx),
  374. 0, static_cast<float>(2 / tb), 0, static_cast<float>(ty),
  375. 0, 0, static_cast<float>(-2 / fn), static_cast<float>(tz),
  376. 0, 0, 0, 1
  377. };
  378. if (m_current_matrix_mode == GL_PROJECTION)
  379. m_projection_matrix = m_projection_matrix * projection;
  380. else if (m_current_matrix_mode == GL_MODELVIEW)
  381. m_model_view_matrix = m_model_view_matrix * projection;
  382. else if (m_current_matrix_mode == GL_TEXTURE)
  383. m_texture_matrix = m_texture_matrix * projection;
  384. else
  385. VERIFY_NOT_REACHED();
  386. }
  387. GLenum SoftwareGLContext::gl_get_error()
  388. {
  389. if (m_in_draw_state)
  390. return GL_INVALID_OPERATION;
  391. auto last_error = m_error;
  392. m_error = GL_NO_ERROR;
  393. return last_error;
  394. }
  395. GLubyte* SoftwareGLContext::gl_get_string(GLenum name)
  396. {
  397. RETURN_VALUE_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION, nullptr);
  398. switch (name) {
  399. case GL_VENDOR:
  400. return reinterpret_cast<GLubyte*>(const_cast<char*>(m_device_info.vendor_name.characters()));
  401. case GL_RENDERER:
  402. return reinterpret_cast<GLubyte*>(const_cast<char*>(m_device_info.device_name.characters()));
  403. case GL_VERSION:
  404. return reinterpret_cast<GLubyte*>(const_cast<char*>("1.5"));
  405. case GL_EXTENSIONS:
  406. return reinterpret_cast<GLubyte*>(const_cast<char*>(m_extensions.characters()));
  407. case GL_SHADING_LANGUAGE_VERSION:
  408. return reinterpret_cast<GLubyte*>(const_cast<char*>("0.0"));
  409. default:
  410. dbgln_if(GL_DEBUG, "gl_get_string({:#x}): unknown name", name);
  411. break;
  412. }
  413. RETURN_VALUE_WITH_ERROR_IF(true, GL_INVALID_ENUM, nullptr);
  414. }
  415. void SoftwareGLContext::gl_load_identity()
  416. {
  417. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_load_identity);
  418. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  419. if (m_current_matrix_mode == GL_PROJECTION)
  420. m_projection_matrix = FloatMatrix4x4::identity();
  421. else if (m_current_matrix_mode == GL_MODELVIEW)
  422. m_model_view_matrix = FloatMatrix4x4::identity();
  423. else if (m_current_matrix_mode == GL_TEXTURE)
  424. m_texture_matrix = FloatMatrix4x4::identity();
  425. else
  426. VERIFY_NOT_REACHED();
  427. }
  428. void SoftwareGLContext::gl_load_matrix(const FloatMatrix4x4& matrix)
  429. {
  430. APPEND_TO_CALL_LIST_WITH_ARG_AND_RETURN_IF_NEEDED(gl_load_matrix, matrix);
  431. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  432. if (m_current_matrix_mode == GL_PROJECTION)
  433. m_projection_matrix = matrix;
  434. else if (m_current_matrix_mode == GL_MODELVIEW)
  435. m_model_view_matrix = matrix;
  436. else if (m_current_matrix_mode == GL_TEXTURE)
  437. m_texture_matrix = matrix;
  438. else
  439. VERIFY_NOT_REACHED();
  440. }
  441. void SoftwareGLContext::gl_matrix_mode(GLenum mode)
  442. {
  443. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_matrix_mode, mode);
  444. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  445. RETURN_WITH_ERROR_IF(mode < GL_MODELVIEW || mode > GL_TEXTURE, GL_INVALID_ENUM);
  446. m_current_matrix_mode = mode;
  447. }
  448. void SoftwareGLContext::gl_push_matrix()
  449. {
  450. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_push_matrix);
  451. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  452. switch (m_current_matrix_mode) {
  453. case GL_PROJECTION:
  454. RETURN_WITH_ERROR_IF(m_projection_matrix_stack.size() >= PROJECTION_MATRIX_STACK_LIMIT, GL_STACK_OVERFLOW);
  455. m_projection_matrix_stack.append(m_projection_matrix);
  456. break;
  457. case GL_MODELVIEW:
  458. RETURN_WITH_ERROR_IF(m_model_view_matrix_stack.size() >= MODELVIEW_MATRIX_STACK_LIMIT, GL_STACK_OVERFLOW);
  459. m_model_view_matrix_stack.append(m_model_view_matrix);
  460. break;
  461. case GL_TEXTURE:
  462. RETURN_WITH_ERROR_IF(m_texture_matrix_stack.size() >= TEXTURE_MATRIX_STACK_LIMIT, GL_STACK_OVERFLOW);
  463. m_texture_matrix_stack.append(m_texture_matrix);
  464. break;
  465. default:
  466. VERIFY_NOT_REACHED();
  467. }
  468. }
  469. void SoftwareGLContext::gl_pop_matrix()
  470. {
  471. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_pop_matrix);
  472. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  473. switch (m_current_matrix_mode) {
  474. case GL_PROJECTION:
  475. RETURN_WITH_ERROR_IF(m_projection_matrix_stack.size() == 0, GL_STACK_UNDERFLOW);
  476. m_projection_matrix = m_projection_matrix_stack.take_last();
  477. break;
  478. case GL_MODELVIEW:
  479. RETURN_WITH_ERROR_IF(m_model_view_matrix_stack.size() == 0, GL_STACK_UNDERFLOW);
  480. m_model_view_matrix = m_model_view_matrix_stack.take_last();
  481. break;
  482. case GL_TEXTURE:
  483. RETURN_WITH_ERROR_IF(m_texture_matrix_stack.size() == 0, GL_STACK_UNDERFLOW);
  484. m_texture_matrix = m_texture_matrix_stack.take_last();
  485. break;
  486. default:
  487. VERIFY_NOT_REACHED();
  488. }
  489. }
  490. void SoftwareGLContext::gl_mult_matrix(FloatMatrix4x4 const& matrix)
  491. {
  492. APPEND_TO_CALL_LIST_WITH_ARG_AND_RETURN_IF_NEEDED(gl_mult_matrix, matrix);
  493. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  494. if (m_current_matrix_mode == GL_MODELVIEW)
  495. m_model_view_matrix = m_model_view_matrix * matrix;
  496. else if (m_current_matrix_mode == GL_PROJECTION)
  497. m_projection_matrix = m_projection_matrix * matrix;
  498. else if (m_current_matrix_mode == GL_TEXTURE)
  499. m_texture_matrix = m_texture_matrix * matrix;
  500. else
  501. VERIFY_NOT_REACHED();
  502. }
  503. void SoftwareGLContext::gl_rotate(GLdouble angle, GLdouble x, GLdouble y, GLdouble z)
  504. {
  505. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_rotate, angle, x, y, z);
  506. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  507. FloatVector3 axis = { (float)x, (float)y, (float)z };
  508. axis.normalize();
  509. auto rotation_mat = Gfx::rotation_matrix(axis, static_cast<float>(angle * M_PI * 2 / 360));
  510. if (m_current_matrix_mode == GL_MODELVIEW)
  511. m_model_view_matrix = m_model_view_matrix * rotation_mat;
  512. else if (m_current_matrix_mode == GL_PROJECTION)
  513. m_projection_matrix = m_projection_matrix * rotation_mat;
  514. else if (m_current_matrix_mode == GL_TEXTURE)
  515. m_texture_matrix = m_texture_matrix * rotation_mat;
  516. else
  517. VERIFY_NOT_REACHED();
  518. }
  519. void SoftwareGLContext::gl_scale(GLdouble x, GLdouble y, GLdouble z)
  520. {
  521. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_scale, x, y, z);
  522. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  523. auto scale_matrix = Gfx::scale_matrix(FloatVector3 { static_cast<float>(x), static_cast<float>(y), static_cast<float>(z) });
  524. if (m_current_matrix_mode == GL_MODELVIEW)
  525. m_model_view_matrix = m_model_view_matrix * scale_matrix;
  526. else if (m_current_matrix_mode == GL_PROJECTION)
  527. m_projection_matrix = m_projection_matrix * scale_matrix;
  528. else if (m_current_matrix_mode == GL_TEXTURE)
  529. m_texture_matrix = m_texture_matrix * scale_matrix;
  530. else
  531. VERIFY_NOT_REACHED();
  532. }
  533. void SoftwareGLContext::gl_translate(GLdouble x, GLdouble y, GLdouble z)
  534. {
  535. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_translate, x, y, z);
  536. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  537. auto translation_matrix = Gfx::translation_matrix(FloatVector3 { static_cast<float>(x), static_cast<float>(y), static_cast<float>(z) });
  538. if (m_current_matrix_mode == GL_MODELVIEW)
  539. m_model_view_matrix = m_model_view_matrix * translation_matrix;
  540. else if (m_current_matrix_mode == GL_PROJECTION)
  541. m_projection_matrix = m_projection_matrix * translation_matrix;
  542. else if (m_current_matrix_mode == GL_TEXTURE)
  543. m_texture_matrix = m_texture_matrix * translation_matrix;
  544. else
  545. VERIFY_NOT_REACHED();
  546. }
  547. void SoftwareGLContext::gl_vertex(GLdouble x, GLdouble y, GLdouble z, GLdouble w)
  548. {
  549. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_vertex, x, y, z, w);
  550. SoftGPU::Vertex vertex;
  551. vertex.position = { static_cast<float>(x), static_cast<float>(y), static_cast<float>(z), static_cast<float>(w) };
  552. vertex.color = m_current_vertex_color;
  553. for (size_t i = 0; i < m_device_info.num_texture_units; ++i)
  554. vertex.tex_coords[i] = m_current_vertex_tex_coord[i];
  555. vertex.normal = m_current_vertex_normal;
  556. m_vertex_list.append(vertex);
  557. }
  558. void SoftwareGLContext::gl_tex_coord(GLfloat s, GLfloat t, GLfloat r, GLfloat q)
  559. {
  560. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_coord, s, t, r, q);
  561. m_current_vertex_tex_coord[0] = { s, t, r, q };
  562. }
  563. void SoftwareGLContext::gl_multi_tex_coord(GLenum target, GLfloat s, GLfloat t, GLfloat r, GLfloat q)
  564. {
  565. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_multi_tex_coord, target, s, t, r, q);
  566. RETURN_WITH_ERROR_IF(target < GL_TEXTURE0 || target >= GL_TEXTURE0 + m_device_info.num_texture_units, GL_INVALID_ENUM);
  567. m_current_vertex_tex_coord[target - GL_TEXTURE0] = { s, t, r, q };
  568. }
  569. void SoftwareGLContext::gl_viewport(GLint x, GLint y, GLsizei width, GLsizei height)
  570. {
  571. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_viewport, x, y, width, height);
  572. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  573. RETURN_WITH_ERROR_IF(width < 0 || height < 0, GL_INVALID_VALUE);
  574. m_viewport = { x, y, width, height };
  575. auto rasterizer_options = m_rasterizer.options();
  576. rasterizer_options.viewport = m_viewport;
  577. m_rasterizer.set_options(rasterizer_options);
  578. }
  579. void SoftwareGLContext::gl_enable(GLenum capability)
  580. {
  581. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_enable, capability);
  582. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  583. auto rasterizer_options = m_rasterizer.options();
  584. bool update_rasterizer_options = false;
  585. switch (capability) {
  586. case GL_COLOR_MATERIAL:
  587. m_color_material_enabled = true;
  588. break;
  589. case GL_CULL_FACE:
  590. m_cull_faces = true;
  591. rasterizer_options.enable_culling = true;
  592. update_rasterizer_options = true;
  593. break;
  594. case GL_DEPTH_TEST:
  595. m_depth_test_enabled = true;
  596. rasterizer_options.enable_depth_test = true;
  597. update_rasterizer_options = true;
  598. break;
  599. case GL_BLEND:
  600. m_blend_enabled = true;
  601. rasterizer_options.enable_blending = true;
  602. update_rasterizer_options = true;
  603. break;
  604. case GL_ALPHA_TEST:
  605. m_alpha_test_enabled = true;
  606. rasterizer_options.enable_alpha_test = true;
  607. update_rasterizer_options = true;
  608. break;
  609. case GL_DITHER:
  610. m_dither_enabled = true;
  611. break;
  612. case GL_FOG:
  613. rasterizer_options.fog_enabled = true;
  614. update_rasterizer_options = true;
  615. break;
  616. case GL_LIGHTING:
  617. m_lighting_enabled = true;
  618. rasterizer_options.lighting_enabled = true;
  619. update_rasterizer_options = true;
  620. break;
  621. case GL_NORMALIZE:
  622. m_normalize = true;
  623. rasterizer_options.normalization_enabled = true;
  624. update_rasterizer_options = true;
  625. break;
  626. case GL_POLYGON_OFFSET_FILL:
  627. m_depth_offset_enabled = true;
  628. rasterizer_options.depth_offset_enabled = true;
  629. update_rasterizer_options = true;
  630. break;
  631. case GL_SCISSOR_TEST:
  632. rasterizer_options.scissor_enabled = true;
  633. update_rasterizer_options = true;
  634. break;
  635. case GL_STENCIL_TEST:
  636. m_stencil_test_enabled = true;
  637. rasterizer_options.enable_stencil_test = true;
  638. update_rasterizer_options = true;
  639. break;
  640. case GL_TEXTURE_1D:
  641. m_active_texture_unit->set_texture_1d_enabled(true);
  642. m_sampler_config_is_dirty = true;
  643. break;
  644. case GL_TEXTURE_2D:
  645. m_active_texture_unit->set_texture_2d_enabled(true);
  646. m_sampler_config_is_dirty = true;
  647. break;
  648. case GL_TEXTURE_3D:
  649. m_active_texture_unit->set_texture_3d_enabled(true);
  650. m_sampler_config_is_dirty = true;
  651. break;
  652. case GL_TEXTURE_CUBE_MAP:
  653. m_active_texture_unit->set_texture_cube_map_enabled(true);
  654. m_sampler_config_is_dirty = true;
  655. break;
  656. case GL_LIGHT0:
  657. case GL_LIGHT1:
  658. case GL_LIGHT2:
  659. case GL_LIGHT3:
  660. case GL_LIGHT4:
  661. case GL_LIGHT5:
  662. case GL_LIGHT6:
  663. case GL_LIGHT7:
  664. m_light_states.at(capability - GL_LIGHT0).is_enabled = true;
  665. m_light_state_is_dirty = true;
  666. break;
  667. case GL_TEXTURE_GEN_Q:
  668. case GL_TEXTURE_GEN_R:
  669. case GL_TEXTURE_GEN_S:
  670. case GL_TEXTURE_GEN_T:
  671. texture_coordinate_generation(m_active_texture_unit_index, capability).enabled = true;
  672. m_texcoord_generation_dirty = true;
  673. break;
  674. default:
  675. dbgln_if(GL_DEBUG, "gl_enable({:#x}): unknown parameter", capability);
  676. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  677. }
  678. if (update_rasterizer_options)
  679. m_rasterizer.set_options(rasterizer_options);
  680. }
  681. void SoftwareGLContext::gl_disable(GLenum capability)
  682. {
  683. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_disable, capability);
  684. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  685. auto rasterizer_options = m_rasterizer.options();
  686. bool update_rasterizer_options = false;
  687. switch (capability) {
  688. case GL_COLOR_MATERIAL:
  689. m_color_material_enabled = false;
  690. break;
  691. case GL_CULL_FACE:
  692. m_cull_faces = false;
  693. rasterizer_options.enable_culling = false;
  694. update_rasterizer_options = true;
  695. break;
  696. case GL_DEPTH_TEST:
  697. m_depth_test_enabled = false;
  698. rasterizer_options.enable_depth_test = false;
  699. update_rasterizer_options = true;
  700. break;
  701. case GL_BLEND:
  702. m_blend_enabled = false;
  703. rasterizer_options.enable_blending = false;
  704. update_rasterizer_options = true;
  705. break;
  706. case GL_ALPHA_TEST:
  707. m_alpha_test_enabled = false;
  708. rasterizer_options.enable_alpha_test = false;
  709. update_rasterizer_options = true;
  710. break;
  711. case GL_DITHER:
  712. m_dither_enabled = false;
  713. break;
  714. case GL_FOG:
  715. rasterizer_options.fog_enabled = false;
  716. update_rasterizer_options = true;
  717. break;
  718. case GL_LIGHTING:
  719. m_lighting_enabled = false;
  720. rasterizer_options.lighting_enabled = false;
  721. update_rasterizer_options = true;
  722. break;
  723. case GL_LIGHT0:
  724. case GL_LIGHT1:
  725. case GL_LIGHT2:
  726. case GL_LIGHT3:
  727. case GL_LIGHT4:
  728. case GL_LIGHT5:
  729. case GL_LIGHT6:
  730. case GL_LIGHT7:
  731. m_light_states.at(capability - GL_LIGHT0).is_enabled = false;
  732. m_light_state_is_dirty = true;
  733. break;
  734. case GL_NORMALIZE:
  735. m_normalize = false;
  736. rasterizer_options.normalization_enabled = false;
  737. update_rasterizer_options = true;
  738. break;
  739. case GL_POLYGON_OFFSET_FILL:
  740. m_depth_offset_enabled = false;
  741. rasterizer_options.depth_offset_enabled = false;
  742. update_rasterizer_options = true;
  743. break;
  744. case GL_SCISSOR_TEST:
  745. rasterizer_options.scissor_enabled = false;
  746. update_rasterizer_options = true;
  747. break;
  748. case GL_STENCIL_TEST:
  749. m_stencil_test_enabled = false;
  750. rasterizer_options.enable_stencil_test = false;
  751. update_rasterizer_options = true;
  752. break;
  753. case GL_TEXTURE_1D:
  754. m_active_texture_unit->set_texture_1d_enabled(false);
  755. m_sampler_config_is_dirty = true;
  756. break;
  757. case GL_TEXTURE_2D:
  758. m_active_texture_unit->set_texture_2d_enabled(false);
  759. m_sampler_config_is_dirty = true;
  760. break;
  761. case GL_TEXTURE_3D:
  762. m_active_texture_unit->set_texture_3d_enabled(false);
  763. m_sampler_config_is_dirty = true;
  764. break;
  765. case GL_TEXTURE_CUBE_MAP:
  766. m_active_texture_unit->set_texture_cube_map_enabled(false);
  767. m_sampler_config_is_dirty = true;
  768. break;
  769. case GL_TEXTURE_GEN_Q:
  770. case GL_TEXTURE_GEN_R:
  771. case GL_TEXTURE_GEN_S:
  772. case GL_TEXTURE_GEN_T:
  773. texture_coordinate_generation(m_active_texture_unit_index, capability).enabled = false;
  774. m_texcoord_generation_dirty = true;
  775. break;
  776. default:
  777. dbgln_if(GL_DEBUG, "gl_disable({:#x}): unknown parameter", capability);
  778. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  779. }
  780. if (update_rasterizer_options)
  781. m_rasterizer.set_options(rasterizer_options);
  782. }
  783. GLboolean SoftwareGLContext::gl_is_enabled(GLenum capability)
  784. {
  785. RETURN_VALUE_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION, 0);
  786. auto optional_parameter = get_context_parameter(capability);
  787. RETURN_VALUE_WITH_ERROR_IF(!optional_parameter.has_value(), GL_INVALID_ENUM, 0);
  788. auto parameter = optional_parameter.release_value();
  789. RETURN_VALUE_WITH_ERROR_IF(!parameter.is_capability, GL_INVALID_ENUM, 0);
  790. return parameter.value.boolean_value;
  791. }
  792. void SoftwareGLContext::gl_gen_textures(GLsizei n, GLuint* textures)
  793. {
  794. RETURN_WITH_ERROR_IF(n < 0, GL_INVALID_VALUE);
  795. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  796. m_name_allocator.allocate(n, textures);
  797. // Initialize all texture names with a nullptr
  798. for (auto i = 0; i < n; i++) {
  799. GLuint name = textures[i];
  800. m_allocated_textures.set(name, nullptr);
  801. }
  802. }
  803. void SoftwareGLContext::gl_delete_textures(GLsizei n, const GLuint* textures)
  804. {
  805. RETURN_WITH_ERROR_IF(n < 0, GL_INVALID_VALUE);
  806. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  807. for (auto i = 0; i < n; i++) {
  808. GLuint name = textures[i];
  809. if (name == 0)
  810. continue;
  811. m_name_allocator.free(name);
  812. auto texture_object = m_allocated_textures.find(name);
  813. if (texture_object == m_allocated_textures.end() || texture_object->value.is_null())
  814. continue;
  815. // Check all texture units
  816. for (auto& texture_unit : m_texture_units) {
  817. if (texture_object->value == texture_unit.bound_texture())
  818. texture_unit.bind_texture_to_target(GL_TEXTURE_2D, nullptr);
  819. }
  820. m_allocated_textures.remove(name);
  821. }
  822. }
  823. 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)
  824. {
  825. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  826. // We only support GL_TEXTURE_2D for now
  827. RETURN_WITH_ERROR_IF(target != GL_TEXTURE_2D, GL_INVALID_ENUM);
  828. // Check if there is actually a texture bound
  829. RETURN_WITH_ERROR_IF(target == GL_TEXTURE_2D && m_active_texture_unit->currently_bound_target() != GL_TEXTURE_2D, GL_INVALID_OPERATION);
  830. // 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
  831. if (internal_format == 1)
  832. internal_format = GL_ALPHA;
  833. else if (internal_format == 2)
  834. internal_format = GL_LUMINANCE_ALPHA;
  835. else if (internal_format == 3)
  836. internal_format = GL_RGB;
  837. else if (internal_format == 4)
  838. internal_format = GL_RGBA;
  839. // We only support symbolic constants for now
  840. RETURN_WITH_ERROR_IF(!(internal_format == GL_RGB || internal_format == GL_RGBA || internal_format == GL_LUMINANCE8 || internal_format == GL_LUMINANCE8_ALPHA8), GL_INVALID_ENUM);
  841. RETURN_WITH_ERROR_IF(!(type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_SHORT_5_6_5), GL_INVALID_VALUE);
  842. RETURN_WITH_ERROR_IF(level < 0 || level > Texture2D::LOG2_MAX_TEXTURE_SIZE, GL_INVALID_VALUE);
  843. RETURN_WITH_ERROR_IF(width < 0 || height < 0 || width > (2 + Texture2D::MAX_TEXTURE_SIZE) || height > (2 + Texture2D::MAX_TEXTURE_SIZE), GL_INVALID_VALUE);
  844. // Check if width and height are a power of 2
  845. if (!m_device_info.supports_npot_textures) {
  846. RETURN_WITH_ERROR_IF(!is_power_of_two(width), GL_INVALID_VALUE);
  847. RETURN_WITH_ERROR_IF(!is_power_of_two(height), GL_INVALID_VALUE);
  848. }
  849. RETURN_WITH_ERROR_IF(border != 0, GL_INVALID_VALUE);
  850. if (level == 0) {
  851. // FIXME: OpenGL has the concept of texture and mipmap completeness. A texture has to fulfill certain criteria to be considered complete.
  852. // Trying to render while an incomplete texture is bound will result in an error.
  853. // Here we simply create a complete device image when mipmap level 0 is attached to the texture object. This has the unfortunate side effect
  854. // that constructing GL textures in any but the default mipmap order, going from level 0 upwards will cause mip levels to stay uninitialized.
  855. // To be spec compliant we should create the device image once the texture has become complete and is used for rendering the first time.
  856. // 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.
  857. m_active_texture_unit->bound_texture_2d()->set_device_image(m_rasterizer.create_image(SoftGPU::ImageFormat::BGRA8888, width, height, 1, 999, 1));
  858. m_sampler_config_is_dirty = true;
  859. }
  860. m_active_texture_unit->bound_texture_2d()->upload_texture_data(level, internal_format, width, height, format, type, data, m_unpack_row_length, m_unpack_alignment);
  861. }
  862. 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)
  863. {
  864. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  865. // We only support GL_TEXTURE_2D for now
  866. RETURN_WITH_ERROR_IF(target != GL_TEXTURE_2D, GL_INVALID_ENUM);
  867. // Check if there is actually a texture bound
  868. RETURN_WITH_ERROR_IF(target == GL_TEXTURE_2D && m_active_texture_unit->currently_bound_target() != GL_TEXTURE_2D, GL_INVALID_OPERATION);
  869. // We only support symbolic constants for now
  870. RETURN_WITH_ERROR_IF(!(format == GL_RGBA || format == GL_RGB), GL_INVALID_VALUE);
  871. RETURN_WITH_ERROR_IF(!(type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_SHORT_5_6_5), GL_INVALID_VALUE);
  872. RETURN_WITH_ERROR_IF(level < 0 || level > Texture2D::LOG2_MAX_TEXTURE_SIZE, GL_INVALID_VALUE);
  873. RETURN_WITH_ERROR_IF(width < 0 || height < 0 || width > (2 + Texture2D::MAX_TEXTURE_SIZE) || height > (2 + Texture2D::MAX_TEXTURE_SIZE), GL_INVALID_VALUE);
  874. auto texture = m_active_texture_unit->bound_texture_2d();
  875. 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);
  876. texture->replace_sub_texture_data(level, xoffset, yoffset, width, height, format, type, data, m_unpack_row_length, m_unpack_alignment);
  877. }
  878. void SoftwareGLContext::gl_tex_parameter(GLenum target, GLenum pname, GLfloat param)
  879. {
  880. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_parameter, target, pname, param);
  881. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  882. // FIXME: We currently only support GL_TETXURE_2D targets. 1D, 3D and CUBE should also be supported (https://docs.gl/gl2/glTexParameter)
  883. RETURN_WITH_ERROR_IF(target != GL_TEXTURE_2D, GL_INVALID_ENUM);
  884. // FIXME: implement the remaining parameters. (https://docs.gl/gl2/glTexParameter)
  885. RETURN_WITH_ERROR_IF(!(pname == GL_TEXTURE_MIN_FILTER
  886. || pname == GL_TEXTURE_MAG_FILTER
  887. || pname == GL_TEXTURE_WRAP_S
  888. || pname == GL_TEXTURE_WRAP_T),
  889. GL_INVALID_ENUM);
  890. if (target == GL_TEXTURE_2D) {
  891. auto texture2d = m_active_texture_unit->bound_texture_2d();
  892. if (texture2d.is_null())
  893. return;
  894. switch (pname) {
  895. case GL_TEXTURE_MIN_FILTER:
  896. RETURN_WITH_ERROR_IF(!(param == GL_NEAREST
  897. || param == GL_LINEAR
  898. || param == GL_NEAREST_MIPMAP_NEAREST
  899. || param == GL_LINEAR_MIPMAP_NEAREST
  900. || param == GL_NEAREST_MIPMAP_LINEAR
  901. || param == GL_LINEAR_MIPMAP_LINEAR),
  902. GL_INVALID_ENUM);
  903. texture2d->sampler().set_min_filter(param);
  904. break;
  905. case GL_TEXTURE_MAG_FILTER:
  906. RETURN_WITH_ERROR_IF(!(param == GL_NEAREST
  907. || param == GL_LINEAR),
  908. GL_INVALID_ENUM);
  909. texture2d->sampler().set_mag_filter(param);
  910. break;
  911. case GL_TEXTURE_WRAP_S:
  912. RETURN_WITH_ERROR_IF(!(param == GL_CLAMP
  913. || param == GL_CLAMP_TO_BORDER
  914. || param == GL_CLAMP_TO_EDGE
  915. || param == GL_MIRRORED_REPEAT
  916. || param == GL_REPEAT),
  917. GL_INVALID_ENUM);
  918. texture2d->sampler().set_wrap_s_mode(param);
  919. break;
  920. case GL_TEXTURE_WRAP_T:
  921. RETURN_WITH_ERROR_IF(!(param == GL_CLAMP
  922. || param == GL_CLAMP_TO_BORDER
  923. || param == GL_CLAMP_TO_EDGE
  924. || param == GL_MIRRORED_REPEAT
  925. || param == GL_REPEAT),
  926. GL_INVALID_ENUM);
  927. texture2d->sampler().set_wrap_t_mode(param);
  928. break;
  929. default:
  930. VERIFY_NOT_REACHED();
  931. }
  932. }
  933. m_sampler_config_is_dirty = true;
  934. }
  935. void SoftwareGLContext::gl_front_face(GLenum face)
  936. {
  937. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_front_face, face);
  938. RETURN_WITH_ERROR_IF(face < GL_CW || face > GL_CCW, GL_INVALID_ENUM);
  939. m_front_face = face;
  940. auto rasterizer_options = m_rasterizer.options();
  941. rasterizer_options.front_face = (face == GL_CW) ? SoftGPU::WindingOrder::Clockwise : SoftGPU::WindingOrder::CounterClockwise;
  942. m_rasterizer.set_options(rasterizer_options);
  943. }
  944. void SoftwareGLContext::gl_cull_face(GLenum cull_mode)
  945. {
  946. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_cull_face, cull_mode);
  947. RETURN_WITH_ERROR_IF(cull_mode < GL_FRONT || cull_mode > GL_FRONT_AND_BACK, GL_INVALID_ENUM);
  948. m_culled_sides = cull_mode;
  949. auto rasterizer_options = m_rasterizer.options();
  950. rasterizer_options.cull_back = cull_mode == GL_BACK || cull_mode == GL_FRONT_AND_BACK;
  951. rasterizer_options.cull_front = cull_mode == GL_FRONT || cull_mode == GL_FRONT_AND_BACK;
  952. m_rasterizer.set_options(rasterizer_options);
  953. }
  954. GLuint SoftwareGLContext::gl_gen_lists(GLsizei range)
  955. {
  956. RETURN_VALUE_WITH_ERROR_IF(range <= 0, GL_INVALID_VALUE, 0);
  957. RETURN_VALUE_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION, 0);
  958. auto initial_entry = m_listings.size();
  959. m_listings.resize(range + initial_entry);
  960. return initial_entry + 1;
  961. }
  962. void SoftwareGLContext::invoke_list(size_t list_index)
  963. {
  964. auto& listing = m_listings[list_index - 1];
  965. for (auto& entry : listing.entries) {
  966. entry.function.visit([&](auto& function) {
  967. entry.arguments.visit([&](auto& arguments) {
  968. auto apply = [&]<typename... Args>(Args && ... args)
  969. {
  970. if constexpr (requires { (this->*function)(forward<Args>(args)...); })
  971. (this->*function)(forward<Args>(args)...);
  972. };
  973. arguments.apply_as_args(apply);
  974. });
  975. });
  976. }
  977. }
  978. void SoftwareGLContext::gl_call_list(GLuint list)
  979. {
  980. if (m_gl_call_depth > max_allowed_gl_call_depth)
  981. return;
  982. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_call_list, list);
  983. if (m_listings.size() < list)
  984. return;
  985. TemporaryChange change { m_gl_call_depth, m_gl_call_depth + 1 };
  986. invoke_list(list);
  987. }
  988. void SoftwareGLContext::gl_call_lists(GLsizei n, GLenum type, void const* lists)
  989. {
  990. if (m_gl_call_depth > max_allowed_gl_call_depth)
  991. return;
  992. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_call_lists, n, type, lists);
  993. RETURN_WITH_ERROR_IF(n < 0, GL_INVALID_VALUE);
  994. RETURN_WITH_ERROR_IF(!(type == GL_BYTE
  995. || type == GL_UNSIGNED_BYTE
  996. || type == GL_SHORT
  997. || type == GL_UNSIGNED_SHORT
  998. || type == GL_INT
  999. || type == GL_UNSIGNED_INT
  1000. || type == GL_FLOAT
  1001. || type == GL_2_BYTES
  1002. || type == GL_3_BYTES
  1003. || type == GL_4_BYTES),
  1004. GL_INVALID_ENUM);
  1005. TemporaryChange change { m_gl_call_depth, m_gl_call_depth + 1 };
  1006. auto invoke_all_lists = [&]<typename T>(T const* lists) {
  1007. for (int i = 0; i < n; ++i) {
  1008. auto list = static_cast<size_t>(lists[i]);
  1009. invoke_list(m_list_base + list);
  1010. }
  1011. };
  1012. switch (type) {
  1013. case GL_BYTE:
  1014. invoke_all_lists(static_cast<GLbyte const*>(lists));
  1015. break;
  1016. case GL_UNSIGNED_BYTE:
  1017. invoke_all_lists(static_cast<GLubyte const*>(lists));
  1018. break;
  1019. case GL_SHORT:
  1020. invoke_all_lists(static_cast<GLshort const*>(lists));
  1021. break;
  1022. case GL_UNSIGNED_SHORT:
  1023. invoke_all_lists(static_cast<GLushort const*>(lists));
  1024. break;
  1025. case GL_INT:
  1026. invoke_all_lists(static_cast<GLint const*>(lists));
  1027. break;
  1028. case GL_UNSIGNED_INT:
  1029. invoke_all_lists(static_cast<GLuint const*>(lists));
  1030. break;
  1031. case GL_FLOAT:
  1032. invoke_all_lists(static_cast<GLfloat const*>(lists));
  1033. break;
  1034. case GL_2_BYTES:
  1035. case GL_3_BYTES:
  1036. case GL_4_BYTES:
  1037. dbgln("SoftwareGLContext FIXME: unimplemented glCallLists() with type {}", type);
  1038. break;
  1039. default:
  1040. VERIFY_NOT_REACHED();
  1041. }
  1042. }
  1043. void SoftwareGLContext::gl_delete_lists(GLuint list, GLsizei range)
  1044. {
  1045. if (m_listings.size() < list || m_listings.size() <= list + range)
  1046. return;
  1047. for (auto& entry : m_listings.span().slice(list - 1, range))
  1048. entry.entries.clear_with_capacity();
  1049. }
  1050. void SoftwareGLContext::gl_list_base(GLuint base)
  1051. {
  1052. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_list_base, base);
  1053. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1054. m_list_base = base;
  1055. }
  1056. void SoftwareGLContext::gl_end_list()
  1057. {
  1058. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1059. RETURN_WITH_ERROR_IF(!m_current_listing_index.has_value(), GL_INVALID_OPERATION);
  1060. m_listings[m_current_listing_index->index] = move(m_current_listing_index->listing);
  1061. m_current_listing_index.clear();
  1062. }
  1063. void SoftwareGLContext::gl_new_list(GLuint list, GLenum mode)
  1064. {
  1065. RETURN_WITH_ERROR_IF(list == 0, GL_INVALID_VALUE);
  1066. RETURN_WITH_ERROR_IF(mode != GL_COMPILE && mode != GL_COMPILE_AND_EXECUTE, GL_INVALID_ENUM);
  1067. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1068. RETURN_WITH_ERROR_IF(m_current_listing_index.has_value(), GL_INVALID_OPERATION);
  1069. if (m_listings.size() < list)
  1070. return;
  1071. m_current_listing_index = CurrentListing { {}, static_cast<size_t>(list - 1), mode };
  1072. }
  1073. GLboolean SoftwareGLContext::gl_is_list(GLuint list)
  1074. {
  1075. RETURN_VALUE_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION, GL_FALSE);
  1076. return list < m_listings.size() ? GL_TRUE : GL_FALSE;
  1077. }
  1078. void SoftwareGLContext::gl_flush()
  1079. {
  1080. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1081. // No-op since SoftwareGLContext is completely synchronous at the moment
  1082. }
  1083. void SoftwareGLContext::gl_finish()
  1084. {
  1085. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1086. // No-op since SoftwareGLContext is completely synchronous at the moment
  1087. }
  1088. void SoftwareGLContext::gl_blend_func(GLenum src_factor, GLenum dst_factor)
  1089. {
  1090. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_blend_func, src_factor, dst_factor);
  1091. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1092. // FIXME: The list of allowed enums differs between API versions
  1093. // This was taken from the 2.0 spec on https://docs.gl/gl2/glBlendFunc
  1094. RETURN_WITH_ERROR_IF(!(src_factor == GL_ZERO
  1095. || src_factor == GL_ONE
  1096. || src_factor == GL_SRC_COLOR
  1097. || src_factor == GL_ONE_MINUS_SRC_COLOR
  1098. || src_factor == GL_DST_COLOR
  1099. || src_factor == GL_ONE_MINUS_DST_COLOR
  1100. || src_factor == GL_SRC_ALPHA
  1101. || src_factor == GL_ONE_MINUS_SRC_ALPHA
  1102. || src_factor == GL_DST_ALPHA
  1103. || src_factor == GL_ONE_MINUS_DST_ALPHA
  1104. || src_factor == GL_CONSTANT_COLOR
  1105. || src_factor == GL_ONE_MINUS_CONSTANT_COLOR
  1106. || src_factor == GL_CONSTANT_ALPHA
  1107. || src_factor == GL_ONE_MINUS_CONSTANT_ALPHA
  1108. || src_factor == GL_SRC_ALPHA_SATURATE),
  1109. GL_INVALID_ENUM);
  1110. RETURN_WITH_ERROR_IF(!(dst_factor == GL_ZERO
  1111. || dst_factor == GL_ONE
  1112. || dst_factor == GL_SRC_COLOR
  1113. || dst_factor == GL_ONE_MINUS_SRC_COLOR
  1114. || dst_factor == GL_DST_COLOR
  1115. || dst_factor == GL_ONE_MINUS_DST_COLOR
  1116. || dst_factor == GL_SRC_ALPHA
  1117. || dst_factor == GL_ONE_MINUS_SRC_ALPHA
  1118. || dst_factor == GL_DST_ALPHA
  1119. || dst_factor == GL_ONE_MINUS_DST_ALPHA
  1120. || dst_factor == GL_CONSTANT_COLOR
  1121. || dst_factor == GL_ONE_MINUS_CONSTANT_COLOR
  1122. || dst_factor == GL_CONSTANT_ALPHA
  1123. || dst_factor == GL_ONE_MINUS_CONSTANT_ALPHA),
  1124. GL_INVALID_ENUM);
  1125. m_blend_source_factor = src_factor;
  1126. m_blend_destination_factor = dst_factor;
  1127. auto map_gl_blend_factor_to_device = [](GLenum factor) constexpr
  1128. {
  1129. switch (factor) {
  1130. case GL_ZERO:
  1131. return SoftGPU::BlendFactor::Zero;
  1132. case GL_ONE:
  1133. return SoftGPU::BlendFactor::One;
  1134. case GL_SRC_ALPHA:
  1135. return SoftGPU::BlendFactor::SrcAlpha;
  1136. case GL_ONE_MINUS_SRC_ALPHA:
  1137. return SoftGPU::BlendFactor::OneMinusSrcAlpha;
  1138. case GL_SRC_COLOR:
  1139. return SoftGPU::BlendFactor::SrcColor;
  1140. case GL_ONE_MINUS_SRC_COLOR:
  1141. return SoftGPU::BlendFactor::OneMinusSrcColor;
  1142. case GL_DST_ALPHA:
  1143. return SoftGPU::BlendFactor::DstAlpha;
  1144. case GL_ONE_MINUS_DST_ALPHA:
  1145. return SoftGPU::BlendFactor::OneMinusDstAlpha;
  1146. case GL_DST_COLOR:
  1147. return SoftGPU::BlendFactor::DstColor;
  1148. case GL_ONE_MINUS_DST_COLOR:
  1149. return SoftGPU::BlendFactor::OneMinusDstColor;
  1150. case GL_SRC_ALPHA_SATURATE:
  1151. return SoftGPU::BlendFactor::SrcAlphaSaturate;
  1152. default:
  1153. VERIFY_NOT_REACHED();
  1154. }
  1155. };
  1156. auto options = m_rasterizer.options();
  1157. options.blend_source_factor = map_gl_blend_factor_to_device(m_blend_source_factor);
  1158. options.blend_destination_factor = map_gl_blend_factor_to_device(m_blend_destination_factor);
  1159. m_rasterizer.set_options(options);
  1160. }
  1161. void SoftwareGLContext::gl_shade_model(GLenum mode)
  1162. {
  1163. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_shade_model, mode);
  1164. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1165. RETURN_WITH_ERROR_IF(mode != GL_FLAT && mode != GL_SMOOTH, GL_INVALID_ENUM);
  1166. auto options = m_rasterizer.options();
  1167. options.shade_smooth = (mode == GL_SMOOTH);
  1168. m_rasterizer.set_options(options);
  1169. }
  1170. void SoftwareGLContext::gl_alpha_func(GLenum func, GLclampf ref)
  1171. {
  1172. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_alpha_func, func, ref);
  1173. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1174. RETURN_WITH_ERROR_IF(func < GL_NEVER || func > GL_ALWAYS, GL_INVALID_ENUM);
  1175. m_alpha_test_func = func;
  1176. m_alpha_test_ref_value = ref;
  1177. auto options = m_rasterizer.options();
  1178. switch (func) {
  1179. case GL_NEVER:
  1180. options.alpha_test_func = SoftGPU::AlphaTestFunction::Never;
  1181. break;
  1182. case GL_ALWAYS:
  1183. options.alpha_test_func = SoftGPU::AlphaTestFunction::Always;
  1184. break;
  1185. case GL_LESS:
  1186. options.alpha_test_func = SoftGPU::AlphaTestFunction::Less;
  1187. break;
  1188. case GL_LEQUAL:
  1189. options.alpha_test_func = SoftGPU::AlphaTestFunction::LessOrEqual;
  1190. break;
  1191. case GL_EQUAL:
  1192. options.alpha_test_func = SoftGPU::AlphaTestFunction::Equal;
  1193. break;
  1194. case GL_NOTEQUAL:
  1195. options.alpha_test_func = SoftGPU::AlphaTestFunction::NotEqual;
  1196. break;
  1197. case GL_GEQUAL:
  1198. options.alpha_test_func = SoftGPU::AlphaTestFunction::GreaterOrEqual;
  1199. break;
  1200. case GL_GREATER:
  1201. options.alpha_test_func = SoftGPU::AlphaTestFunction::Greater;
  1202. break;
  1203. default:
  1204. VERIFY_NOT_REACHED();
  1205. }
  1206. options.alpha_test_ref_value = m_alpha_test_ref_value;
  1207. m_rasterizer.set_options(options);
  1208. }
  1209. void SoftwareGLContext::gl_hint(GLenum target, GLenum mode)
  1210. {
  1211. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_hint, target, mode);
  1212. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1213. RETURN_WITH_ERROR_IF(target != GL_PERSPECTIVE_CORRECTION_HINT
  1214. && target != GL_POINT_SMOOTH_HINT
  1215. && target != GL_LINE_SMOOTH_HINT
  1216. && target != GL_POLYGON_SMOOTH_HINT
  1217. && target != GL_FOG_HINT
  1218. && target != GL_GENERATE_MIPMAP_HINT
  1219. && target != GL_TEXTURE_COMPRESSION_HINT,
  1220. GL_INVALID_ENUM);
  1221. RETURN_WITH_ERROR_IF(mode != GL_DONT_CARE
  1222. && mode != GL_FASTEST
  1223. && mode != GL_NICEST,
  1224. GL_INVALID_ENUM);
  1225. // According to the spec implementors are free to ignore glHint. So we do.
  1226. }
  1227. void SoftwareGLContext::gl_read_buffer(GLenum mode)
  1228. {
  1229. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_read_buffer, mode);
  1230. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1231. // FIXME: Also allow aux buffers GL_AUX0 through GL_AUX3 here
  1232. // plus any aux buffer between 0 and GL_AUX_BUFFERS
  1233. RETURN_WITH_ERROR_IF(mode != GL_FRONT_LEFT
  1234. && mode != GL_FRONT_RIGHT
  1235. && mode != GL_BACK_LEFT
  1236. && mode != GL_BACK_RIGHT
  1237. && mode != GL_FRONT
  1238. && mode != GL_BACK
  1239. && mode != GL_LEFT
  1240. && mode != GL_RIGHT,
  1241. GL_INVALID_ENUM);
  1242. // FIXME: We do not currently have aux buffers, so make it an invalid
  1243. // operation to select anything but front or back buffers. Also we do
  1244. // not allow selecting the stereoscopic RIGHT buffers since we do not
  1245. // have them configured.
  1246. RETURN_WITH_ERROR_IF(mode != GL_FRONT_LEFT
  1247. && mode != GL_FRONT
  1248. && mode != GL_BACK_LEFT
  1249. && mode != GL_BACK
  1250. && mode != GL_FRONT
  1251. && mode != GL_BACK
  1252. && mode != GL_LEFT,
  1253. GL_INVALID_OPERATION);
  1254. m_current_read_buffer = mode;
  1255. }
  1256. void SoftwareGLContext::gl_draw_buffer(GLenum buffer)
  1257. {
  1258. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_draw_buffer, buffer);
  1259. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1260. // FIXME: Also allow aux buffers GL_AUX0 through GL_AUX3 here
  1261. // plus any aux buffer between 0 and GL_AUX_BUFFERS
  1262. RETURN_WITH_ERROR_IF(buffer != GL_NONE
  1263. && buffer != GL_FRONT_LEFT
  1264. && buffer != GL_FRONT_RIGHT
  1265. && buffer != GL_BACK_LEFT
  1266. && buffer != GL_BACK_RIGHT
  1267. && buffer != GL_FRONT
  1268. && buffer != GL_BACK
  1269. && buffer != GL_LEFT
  1270. && buffer != GL_RIGHT,
  1271. GL_INVALID_ENUM);
  1272. // FIXME: We do not currently have aux buffers, so make it an invalid
  1273. // operation to select anything but front or back buffers. Also we do
  1274. // not allow selecting the stereoscopic RIGHT buffers since we do not
  1275. // have them configured.
  1276. RETURN_WITH_ERROR_IF(buffer != GL_NONE
  1277. && buffer != GL_FRONT_LEFT
  1278. && buffer != GL_FRONT
  1279. && buffer != GL_BACK_LEFT
  1280. && buffer != GL_BACK
  1281. && buffer != GL_FRONT
  1282. && buffer != GL_BACK
  1283. && buffer != GL_LEFT,
  1284. GL_INVALID_OPERATION);
  1285. m_current_draw_buffer = buffer;
  1286. auto rasterizer_options = m_rasterizer.options();
  1287. // FIXME: We only have a single draw buffer in SoftGPU at the moment,
  1288. // so we simply disable color writes if GL_NONE is selected
  1289. rasterizer_options.enable_color_write = m_current_draw_buffer != GL_NONE;
  1290. m_rasterizer.set_options(rasterizer_options);
  1291. }
  1292. void SoftwareGLContext::gl_read_pixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid* pixels)
  1293. {
  1294. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1295. RETURN_WITH_ERROR_IF(width < 0 || height < 0, GL_INVALID_VALUE);
  1296. RETURN_WITH_ERROR_IF(format != GL_COLOR_INDEX
  1297. && format != GL_STENCIL_INDEX
  1298. && format != GL_DEPTH_COMPONENT
  1299. && format != GL_RED
  1300. && format != GL_GREEN
  1301. && format != GL_BLUE
  1302. && format != GL_ALPHA
  1303. && format != GL_RGB
  1304. && format != GL_RGBA
  1305. && format != GL_LUMINANCE
  1306. && format != GL_LUMINANCE_ALPHA,
  1307. GL_INVALID_ENUM);
  1308. RETURN_WITH_ERROR_IF(type != GL_UNSIGNED_BYTE
  1309. && type != GL_BYTE
  1310. && type != GL_BITMAP
  1311. && type != GL_UNSIGNED_SHORT
  1312. && type != GL_SHORT
  1313. && type != GL_BLUE
  1314. && type != GL_UNSIGNED_INT
  1315. && type != GL_INT
  1316. && type != GL_FLOAT,
  1317. GL_INVALID_ENUM);
  1318. // FIXME: We only support RGBA buffers for now.
  1319. // Once we add support for indexed color modes do the correct check here
  1320. RETURN_WITH_ERROR_IF(format == GL_COLOR_INDEX, GL_INVALID_OPERATION);
  1321. // FIXME: We do not have stencil buffers yet
  1322. // Once we add support for stencil buffers do the correct check here
  1323. RETURN_WITH_ERROR_IF(format == GL_STENCIL_INDEX, GL_INVALID_OPERATION);
  1324. if (format == GL_DEPTH_COMPONENT) {
  1325. // FIXME: This check needs to be a bit more sophisticated. Currently the buffers
  1326. // are hardcoded. Once we add proper structures for them we need to correct this check
  1327. // Error because only back buffer has a depth buffer
  1328. RETURN_WITH_ERROR_IF(m_current_read_buffer == GL_FRONT
  1329. || m_current_read_buffer == GL_FRONT_LEFT
  1330. || m_current_read_buffer == GL_FRONT_RIGHT,
  1331. GL_INVALID_OPERATION);
  1332. }
  1333. // Some helper functions for converting float values to integer types
  1334. auto float_to_i8 = [](float f) -> GLchar {
  1335. return static_cast<GLchar>((0x7f * min(max(f, 0.0f), 1.0f) - 1) / 2);
  1336. };
  1337. auto float_to_i16 = [](float f) -> GLshort {
  1338. return static_cast<GLshort>((0x7fff * min(max(f, 0.0f), 1.0f) - 1) / 2);
  1339. };
  1340. auto float_to_i32 = [](float f) -> GLint {
  1341. return static_cast<GLint>((0x7fffffff * min(max(f, 0.0f), 1.0f) - 1) / 2);
  1342. };
  1343. auto float_to_u8 = [](float f) -> GLubyte {
  1344. return static_cast<GLubyte>(0xff * min(max(f, 0.0f), 1.0f));
  1345. };
  1346. auto float_to_u16 = [](float f) -> GLushort {
  1347. return static_cast<GLushort>(0xffff * min(max(f, 0.0f), 1.0f));
  1348. };
  1349. auto float_to_u32 = [](float f) -> GLuint {
  1350. return static_cast<GLuint>(0xffffffff * min(max(f, 0.0f), 1.0f));
  1351. };
  1352. u8 component_size = 0;
  1353. switch (type) {
  1354. case GL_BYTE:
  1355. case GL_UNSIGNED_BYTE:
  1356. component_size = 1;
  1357. break;
  1358. case GL_SHORT:
  1359. case GL_UNSIGNED_SHORT:
  1360. component_size = 2;
  1361. break;
  1362. case GL_INT:
  1363. case GL_UNSIGNED_INT:
  1364. case GL_FLOAT:
  1365. component_size = 4;
  1366. break;
  1367. }
  1368. if (format == GL_DEPTH_COMPONENT) {
  1369. auto const row_stride = (width * component_size + m_pack_alignment - 1) / m_pack_alignment * m_pack_alignment;
  1370. // Read from depth buffer
  1371. for (GLsizei i = 0; i < height; ++i) {
  1372. for (GLsizei j = 0; j < width; ++j) {
  1373. float depth = m_rasterizer.get_depthbuffer_value(x + j, y + i);
  1374. auto char_ptr = reinterpret_cast<char*>(pixels) + i * row_stride + j * component_size;
  1375. switch (type) {
  1376. case GL_BYTE:
  1377. *reinterpret_cast<GLchar*>(char_ptr) = float_to_i8(depth);
  1378. break;
  1379. case GL_SHORT:
  1380. *reinterpret_cast<GLshort*>(char_ptr) = float_to_i16(depth);
  1381. break;
  1382. case GL_INT:
  1383. *reinterpret_cast<GLint*>(char_ptr) = float_to_i32(depth);
  1384. break;
  1385. case GL_UNSIGNED_BYTE:
  1386. *reinterpret_cast<GLubyte*>(char_ptr) = float_to_u8(depth);
  1387. break;
  1388. case GL_UNSIGNED_SHORT:
  1389. *reinterpret_cast<GLushort*>(char_ptr) = float_to_u16(depth);
  1390. break;
  1391. case GL_UNSIGNED_INT:
  1392. *reinterpret_cast<GLuint*>(char_ptr) = float_to_u32(depth);
  1393. break;
  1394. case GL_FLOAT:
  1395. *reinterpret_cast<GLfloat*>(char_ptr) = min(max(depth, 0.0f), 1.0f);
  1396. break;
  1397. }
  1398. }
  1399. }
  1400. return;
  1401. }
  1402. bool write_red = false;
  1403. bool write_green = false;
  1404. bool write_blue = false;
  1405. bool write_alpha = false;
  1406. size_t component_count = 0;
  1407. size_t red_offset = 0;
  1408. size_t green_offset = 0;
  1409. size_t blue_offset = 0;
  1410. size_t alpha_offset = 0;
  1411. char* red_ptr = nullptr;
  1412. char* green_ptr = nullptr;
  1413. char* blue_ptr = nullptr;
  1414. char* alpha_ptr = nullptr;
  1415. switch (format) {
  1416. case GL_RGB:
  1417. write_red = true;
  1418. write_green = true;
  1419. write_blue = true;
  1420. component_count = 3;
  1421. red_offset = 2;
  1422. green_offset = 1;
  1423. blue_offset = 0;
  1424. break;
  1425. case GL_RGBA:
  1426. write_red = true;
  1427. write_green = true;
  1428. write_blue = true;
  1429. write_alpha = true;
  1430. component_count = 4;
  1431. red_offset = 3;
  1432. green_offset = 2;
  1433. blue_offset = 1;
  1434. alpha_offset = 0;
  1435. break;
  1436. case GL_RED:
  1437. write_red = true;
  1438. component_count = 1;
  1439. red_offset = 0;
  1440. break;
  1441. case GL_GREEN:
  1442. write_green = true;
  1443. component_count = 1;
  1444. green_offset = 0;
  1445. break;
  1446. case GL_BLUE:
  1447. write_blue = true;
  1448. component_count = 1;
  1449. blue_offset = 0;
  1450. break;
  1451. case GL_ALPHA:
  1452. write_alpha = true;
  1453. component_count = 1;
  1454. alpha_offset = 0;
  1455. break;
  1456. }
  1457. auto const pixel_bytes = component_size * component_count;
  1458. auto const row_alignment_bytes = (m_pack_alignment - ((width * pixel_bytes) % m_pack_alignment)) % m_pack_alignment;
  1459. char* out_ptr = reinterpret_cast<char*>(pixels);
  1460. for (int i = 0; i < (int)height; ++i) {
  1461. for (int j = 0; j < (int)width; ++j) {
  1462. Gfx::ARGB32 color {};
  1463. if (m_current_read_buffer == GL_FRONT || m_current_read_buffer == GL_LEFT || m_current_read_buffer == GL_FRONT_LEFT) {
  1464. if (y + i >= m_frontbuffer->width() || x + j >= m_frontbuffer->height())
  1465. color = 0;
  1466. else
  1467. color = m_frontbuffer->scanline(y + i)[x + j];
  1468. } else {
  1469. color = m_rasterizer.get_color_buffer_pixel(x + j, y + i);
  1470. }
  1471. float red = ((color >> 24) & 0xff) / 255.0f;
  1472. float green = ((color >> 16) & 0xff) / 255.0f;
  1473. float blue = ((color >> 8) & 0xff) / 255.0f;
  1474. float alpha = (color & 0xff) / 255.0f;
  1475. // FIXME: Set up write pointers based on selected endianness (glPixelStore)
  1476. red_ptr = out_ptr + (component_size * red_offset);
  1477. green_ptr = out_ptr + (component_size * green_offset);
  1478. blue_ptr = out_ptr + (component_size * blue_offset);
  1479. alpha_ptr = out_ptr + (component_size * alpha_offset);
  1480. switch (type) {
  1481. case GL_BYTE:
  1482. if (write_red)
  1483. *reinterpret_cast<GLchar*>(red_ptr) = float_to_i8(red);
  1484. if (write_green)
  1485. *reinterpret_cast<GLchar*>(green_ptr) = float_to_i8(green);
  1486. if (write_blue)
  1487. *reinterpret_cast<GLchar*>(blue_ptr) = float_to_i8(blue);
  1488. if (write_alpha)
  1489. *reinterpret_cast<GLchar*>(alpha_ptr) = float_to_i8(alpha);
  1490. break;
  1491. case GL_UNSIGNED_BYTE:
  1492. if (write_red)
  1493. *reinterpret_cast<GLubyte*>(red_ptr) = float_to_u8(red);
  1494. if (write_green)
  1495. *reinterpret_cast<GLubyte*>(green_ptr) = float_to_u8(green);
  1496. if (write_blue)
  1497. *reinterpret_cast<GLubyte*>(blue_ptr) = float_to_u8(blue);
  1498. if (write_alpha)
  1499. *reinterpret_cast<GLubyte*>(alpha_ptr) = float_to_u8(alpha);
  1500. break;
  1501. case GL_SHORT:
  1502. if (write_red)
  1503. *reinterpret_cast<GLshort*>(red_ptr) = float_to_i16(red);
  1504. if (write_green)
  1505. *reinterpret_cast<GLshort*>(green_ptr) = float_to_i16(green);
  1506. if (write_blue)
  1507. *reinterpret_cast<GLshort*>(blue_ptr) = float_to_i16(blue);
  1508. if (write_alpha)
  1509. *reinterpret_cast<GLshort*>(alpha_ptr) = float_to_i16(alpha);
  1510. break;
  1511. case GL_UNSIGNED_SHORT:
  1512. if (write_red)
  1513. *reinterpret_cast<GLushort*>(red_ptr) = float_to_u16(red);
  1514. if (write_green)
  1515. *reinterpret_cast<GLushort*>(green_ptr) = float_to_u16(green);
  1516. if (write_blue)
  1517. *reinterpret_cast<GLushort*>(blue_ptr) = float_to_u16(blue);
  1518. if (write_alpha)
  1519. *reinterpret_cast<GLushort*>(alpha_ptr) = float_to_u16(alpha);
  1520. break;
  1521. case GL_INT:
  1522. if (write_red)
  1523. *reinterpret_cast<GLint*>(red_ptr) = float_to_i32(red);
  1524. if (write_green)
  1525. *reinterpret_cast<GLint*>(green_ptr) = float_to_i32(green);
  1526. if (write_blue)
  1527. *reinterpret_cast<GLint*>(blue_ptr) = float_to_i32(blue);
  1528. if (write_alpha)
  1529. *reinterpret_cast<GLint*>(alpha_ptr) = float_to_i32(alpha);
  1530. break;
  1531. case GL_UNSIGNED_INT:
  1532. if (write_red)
  1533. *reinterpret_cast<GLuint*>(red_ptr) = float_to_u32(red);
  1534. if (write_green)
  1535. *reinterpret_cast<GLuint*>(green_ptr) = float_to_u32(green);
  1536. if (write_blue)
  1537. *reinterpret_cast<GLuint*>(blue_ptr) = float_to_u32(blue);
  1538. if (write_alpha)
  1539. *reinterpret_cast<GLuint*>(alpha_ptr) = float_to_u32(alpha);
  1540. break;
  1541. case GL_FLOAT:
  1542. if (write_red)
  1543. *reinterpret_cast<GLfloat*>(red_ptr) = min(max(red, 0.0f), 1.0f);
  1544. if (write_green)
  1545. *reinterpret_cast<GLfloat*>(green_ptr) = min(max(green, 0.0f), 1.0f);
  1546. if (write_blue)
  1547. *reinterpret_cast<GLfloat*>(blue_ptr) = min(max(blue, 0.0f), 1.0f);
  1548. if (write_alpha)
  1549. *reinterpret_cast<GLfloat*>(alpha_ptr) = min(max(alpha, 0.0f), 1.0f);
  1550. break;
  1551. }
  1552. out_ptr += pixel_bytes;
  1553. }
  1554. out_ptr += row_alignment_bytes;
  1555. }
  1556. }
  1557. void SoftwareGLContext::gl_bind_texture(GLenum target, GLuint texture)
  1558. {
  1559. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1560. // FIXME: We only support GL_TEXTURE_2D for now
  1561. RETURN_WITH_ERROR_IF(target != GL_TEXTURE_2D, GL_INVALID_ENUM);
  1562. if (texture == 0) {
  1563. switch (target) {
  1564. case GL_TEXTURE_2D:
  1565. m_active_texture_unit->bind_texture_to_target(target, nullptr);
  1566. m_sampler_config_is_dirty = true;
  1567. return;
  1568. default:
  1569. VERIFY_NOT_REACHED();
  1570. return;
  1571. }
  1572. }
  1573. auto it = m_allocated_textures.find(texture);
  1574. RefPtr<Texture> texture_object;
  1575. // OpenGL 1.x supports binding texture names that were not previously generated by glGenTextures.
  1576. // If there is not an allocated texture, meaning it was not previously generated by glGenTextures,
  1577. // we can keep texture_object null to both allocate and bind the texture with the passed in texture name.
  1578. // FIXME: Later OpenGL versions such as 4.x enforce that texture names being bound were previously generated
  1579. // by glGenTextures.
  1580. if (it != m_allocated_textures.end())
  1581. texture_object = it->value;
  1582. // Binding a texture to a different target than it was first bound is an invalid operation
  1583. // FIXME: We only support GL_TEXTURE_2D for now
  1584. RETURN_WITH_ERROR_IF(target == GL_TEXTURE_2D && !texture_object.is_null() && !texture_object->is_texture_2d(), GL_INVALID_OPERATION);
  1585. if (!texture_object) {
  1586. // This is the first time the texture is bound. Allocate an actual texture object
  1587. switch (target) {
  1588. case GL_TEXTURE_2D:
  1589. texture_object = adopt_ref(*new Texture2D());
  1590. break;
  1591. default:
  1592. VERIFY_NOT_REACHED();
  1593. }
  1594. m_allocated_textures.set(texture, texture_object);
  1595. }
  1596. switch (target) {
  1597. case GL_TEXTURE_2D:
  1598. m_active_texture_unit->bind_texture_to_target(target, texture_object);
  1599. break;
  1600. }
  1601. m_sampler_config_is_dirty = true;
  1602. }
  1603. GLboolean SoftwareGLContext::gl_is_texture(GLuint texture)
  1604. {
  1605. RETURN_VALUE_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION, GL_FALSE);
  1606. if (texture == 0)
  1607. return GL_FALSE;
  1608. auto it = m_allocated_textures.find(texture);
  1609. if (it == m_allocated_textures.end())
  1610. return GL_FALSE;
  1611. return it->value.is_null() ? GL_FALSE : GL_TRUE;
  1612. }
  1613. void SoftwareGLContext::gl_active_texture(GLenum texture)
  1614. {
  1615. RETURN_WITH_ERROR_IF(texture < GL_TEXTURE0 || texture >= GL_TEXTURE0 + m_device_info.num_texture_units, GL_INVALID_ENUM);
  1616. m_active_texture_unit_index = texture - GL_TEXTURE0;
  1617. m_active_texture_unit = &m_texture_units.at(m_active_texture_unit_index);
  1618. }
  1619. void SoftwareGLContext::gl_get_booleanv(GLenum pname, GLboolean* data)
  1620. {
  1621. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1622. auto optional_parameter = get_context_parameter(pname);
  1623. RETURN_WITH_ERROR_IF(!optional_parameter.has_value(), GL_INVALID_ENUM);
  1624. auto parameter = optional_parameter.release_value();
  1625. switch (parameter.type) {
  1626. case GL_BOOL:
  1627. *data = parameter.value.boolean_value ? GL_TRUE : GL_FALSE;
  1628. break;
  1629. case GL_DOUBLE:
  1630. *data = (parameter.value.double_value == 0.0) ? GL_FALSE : GL_TRUE;
  1631. break;
  1632. case GL_INT:
  1633. *data = (parameter.value.integer_value == 0) ? GL_FALSE : GL_TRUE;
  1634. break;
  1635. default:
  1636. VERIFY_NOT_REACHED();
  1637. }
  1638. }
  1639. void SoftwareGLContext::gl_get_doublev(GLenum pname, GLdouble* params)
  1640. {
  1641. get_floating_point(pname, params);
  1642. }
  1643. void SoftwareGLContext::gl_get_floatv(GLenum pname, GLfloat* params)
  1644. {
  1645. get_floating_point(pname, params);
  1646. }
  1647. template<typename T>
  1648. void SoftwareGLContext::get_floating_point(GLenum pname, T* params)
  1649. {
  1650. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1651. // Handle matrix retrieval first
  1652. auto flatten_and_assign_matrix = [&params](FloatMatrix4x4 const& matrix) {
  1653. auto elements = matrix.elements();
  1654. for (size_t i = 0; i < 4; ++i) {
  1655. for (size_t j = 0; j < 4; ++j) {
  1656. // Return transposed matrix since OpenGL defines them as column-major
  1657. params[i * 4 + j] = static_cast<T>(elements[j][i]);
  1658. }
  1659. }
  1660. };
  1661. switch (pname) {
  1662. case GL_MODELVIEW_MATRIX:
  1663. flatten_and_assign_matrix(m_model_view_matrix);
  1664. return;
  1665. case GL_PROJECTION_MATRIX:
  1666. flatten_and_assign_matrix(m_projection_matrix);
  1667. return;
  1668. }
  1669. // Regular parameters
  1670. auto optional_parameter = get_context_parameter(pname);
  1671. RETURN_WITH_ERROR_IF(!optional_parameter.has_value(), GL_INVALID_ENUM);
  1672. auto parameter = optional_parameter.release_value();
  1673. switch (parameter.type) {
  1674. case GL_BOOL:
  1675. *params = parameter.value.boolean_value ? GL_TRUE : GL_FALSE;
  1676. break;
  1677. case GL_DOUBLE:
  1678. for (size_t i = 0; i < parameter.count; ++i)
  1679. params[i] = parameter.value.double_list[i];
  1680. break;
  1681. case GL_INT:
  1682. for (size_t i = 0; i < parameter.count; ++i)
  1683. params[i] = parameter.value.integer_list[i];
  1684. break;
  1685. default:
  1686. VERIFY_NOT_REACHED();
  1687. }
  1688. }
  1689. void SoftwareGLContext::gl_get_integerv(GLenum pname, GLint* data)
  1690. {
  1691. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1692. auto optional_parameter = get_context_parameter(pname);
  1693. RETURN_WITH_ERROR_IF(!optional_parameter.has_value(), GL_INVALID_ENUM);
  1694. auto parameter = optional_parameter.release_value();
  1695. switch (parameter.type) {
  1696. case GL_BOOL:
  1697. *data = parameter.value.boolean_value ? GL_TRUE : GL_FALSE;
  1698. break;
  1699. case GL_DOUBLE: {
  1700. double const int_range = static_cast<double>(NumericLimits<GLint>::max()) - NumericLimits<GLint>::min();
  1701. for (size_t i = 0; i < parameter.count; ++i) {
  1702. double const result_factor = (clamp(parameter.value.double_list[i], -1.0, 1.0) + 1.0) / 2.0;
  1703. data[i] = static_cast<GLint>(NumericLimits<GLint>::min() + result_factor * int_range);
  1704. }
  1705. break;
  1706. }
  1707. case GL_INT:
  1708. for (size_t i = 0; i < parameter.count; ++i)
  1709. data[i] = parameter.value.integer_list[i];
  1710. break;
  1711. default:
  1712. VERIFY_NOT_REACHED();
  1713. }
  1714. }
  1715. void SoftwareGLContext::gl_depth_mask(GLboolean flag)
  1716. {
  1717. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_depth_mask, flag);
  1718. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1719. auto options = m_rasterizer.options();
  1720. options.enable_depth_write = (flag != GL_FALSE);
  1721. m_rasterizer.set_options(options);
  1722. }
  1723. void SoftwareGLContext::gl_enable_client_state(GLenum cap)
  1724. {
  1725. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1726. switch (cap) {
  1727. case GL_VERTEX_ARRAY:
  1728. m_client_side_vertex_array_enabled = true;
  1729. break;
  1730. case GL_COLOR_ARRAY:
  1731. m_client_side_color_array_enabled = true;
  1732. break;
  1733. case GL_TEXTURE_COORD_ARRAY:
  1734. m_client_side_texture_coord_array_enabled[m_client_active_texture] = true;
  1735. break;
  1736. default:
  1737. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1738. }
  1739. }
  1740. void SoftwareGLContext::gl_disable_client_state(GLenum cap)
  1741. {
  1742. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1743. switch (cap) {
  1744. case GL_VERTEX_ARRAY:
  1745. m_client_side_vertex_array_enabled = false;
  1746. break;
  1747. case GL_COLOR_ARRAY:
  1748. m_client_side_color_array_enabled = false;
  1749. break;
  1750. case GL_TEXTURE_COORD_ARRAY:
  1751. m_client_side_texture_coord_array_enabled[m_client_active_texture] = false;
  1752. break;
  1753. default:
  1754. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1755. }
  1756. }
  1757. void SoftwareGLContext::gl_client_active_texture(GLenum target)
  1758. {
  1759. RETURN_WITH_ERROR_IF(target < GL_TEXTURE0 || target >= GL_TEXTURE0 + m_device_info.num_texture_units, GL_INVALID_ENUM);
  1760. m_client_active_texture = target - GL_TEXTURE0;
  1761. }
  1762. void SoftwareGLContext::gl_vertex_pointer(GLint size, GLenum type, GLsizei stride, const void* pointer)
  1763. {
  1764. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1765. RETURN_WITH_ERROR_IF(!(size == 2 || size == 3 || size == 4), GL_INVALID_VALUE);
  1766. RETURN_WITH_ERROR_IF(!(type == GL_SHORT || type == GL_INT || type == GL_FLOAT || type == GL_DOUBLE), GL_INVALID_ENUM);
  1767. RETURN_WITH_ERROR_IF(stride < 0, GL_INVALID_VALUE);
  1768. m_client_vertex_pointer.size = size;
  1769. m_client_vertex_pointer.type = type;
  1770. m_client_vertex_pointer.stride = stride;
  1771. m_client_vertex_pointer.pointer = pointer;
  1772. }
  1773. void SoftwareGLContext::gl_color_pointer(GLint size, GLenum type, GLsizei stride, const void* pointer)
  1774. {
  1775. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1776. RETURN_WITH_ERROR_IF(!(size == 3 || size == 4), GL_INVALID_VALUE);
  1777. RETURN_WITH_ERROR_IF(!(type == GL_BYTE
  1778. || type == GL_UNSIGNED_BYTE
  1779. || type == GL_SHORT
  1780. || type == GL_UNSIGNED_SHORT
  1781. || type == GL_INT
  1782. || type == GL_UNSIGNED_INT
  1783. || type == GL_FLOAT
  1784. || type == GL_DOUBLE),
  1785. GL_INVALID_ENUM);
  1786. RETURN_WITH_ERROR_IF(stride < 0, GL_INVALID_VALUE);
  1787. m_client_color_pointer.size = size;
  1788. m_client_color_pointer.type = type;
  1789. m_client_color_pointer.stride = stride;
  1790. m_client_color_pointer.pointer = pointer;
  1791. }
  1792. void SoftwareGLContext::gl_tex_coord_pointer(GLint size, GLenum type, GLsizei stride, const void* pointer)
  1793. {
  1794. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1795. RETURN_WITH_ERROR_IF(!(size == 1 || size == 2 || size == 3 || size == 4), GL_INVALID_VALUE);
  1796. RETURN_WITH_ERROR_IF(!(type == GL_SHORT || type == GL_INT || type == GL_FLOAT || type == GL_DOUBLE), GL_INVALID_ENUM);
  1797. RETURN_WITH_ERROR_IF(stride < 0, GL_INVALID_VALUE);
  1798. auto& tex_coord_pointer = m_client_tex_coord_pointer[m_client_active_texture];
  1799. tex_coord_pointer.size = size;
  1800. tex_coord_pointer.type = type;
  1801. tex_coord_pointer.stride = stride;
  1802. tex_coord_pointer.pointer = pointer;
  1803. }
  1804. void SoftwareGLContext::gl_tex_env(GLenum target, GLenum pname, GLfloat param)
  1805. {
  1806. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_env, target, pname, param);
  1807. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1808. // FIXME: We currently only support a subset of possible target values. Implement the rest!
  1809. RETURN_WITH_ERROR_IF(target != GL_TEXTURE_ENV, GL_INVALID_ENUM);
  1810. // FIXME: We currently only support a subset of possible pname values. Implement the rest!
  1811. RETURN_WITH_ERROR_IF(pname != GL_TEXTURE_ENV_MODE, GL_INVALID_ENUM);
  1812. auto param_enum = static_cast<GLenum>(param);
  1813. switch (param_enum) {
  1814. case GL_MODULATE:
  1815. case GL_REPLACE:
  1816. case GL_DECAL:
  1817. m_active_texture_unit->set_env_mode(param_enum);
  1818. break;
  1819. default:
  1820. // FIXME: We currently only support a subset of possible param values. Implement the rest!
  1821. dbgln_if(GL_DEBUG, "gl_tex_env({:#x}, {:#x}, {}): param unimplemented", target, pname, param);
  1822. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1823. }
  1824. }
  1825. void SoftwareGLContext::gl_draw_arrays(GLenum mode, GLint first, GLsizei count)
  1826. {
  1827. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_draw_arrays, mode, first, count);
  1828. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1829. // FIXME: Some modes are still missing (GL_POINTS, GL_LINE_STRIP, GL_LINE_LOOP, GL_LINES)
  1830. RETURN_WITH_ERROR_IF(!(mode == GL_TRIANGLE_STRIP
  1831. || mode == GL_TRIANGLE_FAN
  1832. || mode == GL_TRIANGLES
  1833. || mode == GL_QUADS
  1834. || mode == GL_QUAD_STRIP
  1835. || mode == GL_POLYGON),
  1836. GL_INVALID_ENUM);
  1837. RETURN_WITH_ERROR_IF(count < 0, GL_INVALID_VALUE);
  1838. // At least the vertex array needs to be enabled
  1839. if (!m_client_side_vertex_array_enabled)
  1840. return;
  1841. auto last = first + count;
  1842. gl_begin(mode);
  1843. for (int i = first; i < last; i++) {
  1844. for (size_t t = 0; t < m_client_tex_coord_pointer.size(); ++t) {
  1845. if (m_client_side_texture_coord_array_enabled[t]) {
  1846. float tex_coords[4] { 0, 0, 0, 0 };
  1847. read_from_vertex_attribute_pointer(m_client_tex_coord_pointer[t], i, tex_coords, false);
  1848. gl_multi_tex_coord(GL_TEXTURE0 + t, tex_coords[0], tex_coords[1], tex_coords[2], tex_coords[3]);
  1849. }
  1850. }
  1851. if (m_client_side_color_array_enabled) {
  1852. float color[4] { 0, 0, 0, 1 };
  1853. read_from_vertex_attribute_pointer(m_client_color_pointer, i, color, true);
  1854. gl_color(color[0], color[1], color[2], color[3]);
  1855. }
  1856. float vertex[4] { 0, 0, 0, 1 };
  1857. read_from_vertex_attribute_pointer(m_client_vertex_pointer, i, vertex, false);
  1858. gl_vertex(vertex[0], vertex[1], vertex[2], vertex[3]);
  1859. }
  1860. gl_end();
  1861. }
  1862. void SoftwareGLContext::gl_draw_elements(GLenum mode, GLsizei count, GLenum type, const void* indices)
  1863. {
  1864. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_draw_elements, mode, count, type, indices);
  1865. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1866. // FIXME: Some modes are still missing (GL_POINTS, GL_LINE_STRIP, GL_LINE_LOOP, GL_LINES)
  1867. RETURN_WITH_ERROR_IF(!(mode == GL_TRIANGLE_STRIP
  1868. || mode == GL_TRIANGLE_FAN
  1869. || mode == GL_TRIANGLES
  1870. || mode == GL_QUADS
  1871. || mode == GL_QUAD_STRIP
  1872. || mode == GL_POLYGON),
  1873. GL_INVALID_ENUM);
  1874. RETURN_WITH_ERROR_IF(!(type == GL_UNSIGNED_BYTE
  1875. || type == GL_UNSIGNED_SHORT
  1876. || type == GL_UNSIGNED_INT),
  1877. GL_INVALID_ENUM);
  1878. RETURN_WITH_ERROR_IF(count < 0, GL_INVALID_VALUE);
  1879. // At least the vertex array needs to be enabled
  1880. if (!m_client_side_vertex_array_enabled)
  1881. return;
  1882. gl_begin(mode);
  1883. for (int index = 0; index < count; index++) {
  1884. int i = 0;
  1885. switch (type) {
  1886. case GL_UNSIGNED_BYTE:
  1887. i = reinterpret_cast<const GLubyte*>(indices)[index];
  1888. break;
  1889. case GL_UNSIGNED_SHORT:
  1890. i = reinterpret_cast<const GLushort*>(indices)[index];
  1891. break;
  1892. case GL_UNSIGNED_INT:
  1893. i = reinterpret_cast<const GLuint*>(indices)[index];
  1894. break;
  1895. }
  1896. for (size_t t = 0; t < m_client_tex_coord_pointer.size(); ++t) {
  1897. if (m_client_side_texture_coord_array_enabled[t]) {
  1898. float tex_coords[4] { 0, 0, 0, 0 };
  1899. read_from_vertex_attribute_pointer(m_client_tex_coord_pointer[t], i, tex_coords, false);
  1900. gl_multi_tex_coord(GL_TEXTURE0 + t, tex_coords[0], tex_coords[1], tex_coords[2], tex_coords[3]);
  1901. }
  1902. }
  1903. if (m_client_side_color_array_enabled) {
  1904. float color[4] { 0, 0, 0, 1 };
  1905. read_from_vertex_attribute_pointer(m_client_color_pointer, i, color, true);
  1906. gl_color(color[0], color[1], color[2], color[3]);
  1907. }
  1908. float vertex[4] { 0, 0, 0, 1 };
  1909. read_from_vertex_attribute_pointer(m_client_vertex_pointer, i, vertex, false);
  1910. gl_vertex(vertex[0], vertex[1], vertex[2], vertex[3]);
  1911. }
  1912. gl_end();
  1913. }
  1914. void SoftwareGLContext::gl_draw_pixels(GLsizei width, GLsizei height, GLenum format, GLenum type, const void* data)
  1915. {
  1916. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_draw_pixels, width, height, format, type, data);
  1917. RETURN_WITH_ERROR_IF(format < GL_COLOR_INDEX || format > GL_BGRA, GL_INVALID_ENUM);
  1918. RETURN_WITH_ERROR_IF((type < GL_BYTE || type > GL_FLOAT)
  1919. && (type < GL_UNSIGNED_BYTE_3_3_2 || type > GL_UNSIGNED_INT_10_10_10_2)
  1920. && (type < GL_UNSIGNED_BYTE_2_3_3_REV || type > GL_UNSIGNED_INT_2_10_10_10_REV),
  1921. GL_INVALID_ENUM);
  1922. RETURN_WITH_ERROR_IF(type == GL_BITMAP && !(format == GL_COLOR_INDEX || format == GL_STENCIL_INDEX), GL_INVALID_ENUM);
  1923. RETURN_WITH_ERROR_IF(width < 0 || height < 0, GL_INVALID_VALUE);
  1924. // FIXME: GL_INVALID_OPERATION is generated if format is GL_STENCIL_INDEX and there is no stencil buffer
  1925. // FIXME: GL_INVALID_OPERATION is generated if format is GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA, GL_RGB, GL_RGBA,
  1926. // GL_BGR, GL_BGRA, GL_LUMINANCE, or GL_LUMINANCE_ALPHA, and the GL is in color index mode
  1927. RETURN_WITH_ERROR_IF(format != GL_RGB
  1928. && (type == GL_UNSIGNED_BYTE_3_3_2
  1929. || type == GL_UNSIGNED_BYTE_2_3_3_REV
  1930. || type == GL_UNSIGNED_SHORT_5_6_5
  1931. || type == GL_UNSIGNED_SHORT_5_6_5_REV),
  1932. GL_INVALID_OPERATION);
  1933. RETURN_WITH_ERROR_IF(!(format == GL_RGBA || format == GL_BGRA)
  1934. && (type == GL_UNSIGNED_SHORT_4_4_4_4
  1935. || type == GL_UNSIGNED_SHORT_4_4_4_4_REV
  1936. || type == GL_UNSIGNED_SHORT_5_5_5_1
  1937. || type == GL_UNSIGNED_SHORT_1_5_5_5_REV
  1938. || type == GL_UNSIGNED_INT_8_8_8_8
  1939. || type == GL_UNSIGNED_INT_8_8_8_8_REV
  1940. || type == GL_UNSIGNED_INT_10_10_10_2
  1941. || type == GL_UNSIGNED_INT_2_10_10_10_REV),
  1942. GL_INVALID_OPERATION);
  1943. // FIXME: GL_INVALID_OPERATION is generated if a non-zero buffer object name is bound to the GL_PIXEL_UNPACK_BUFFER
  1944. // target and the buffer object's data store is currently mapped.
  1945. // FIXME: GL_INVALID_OPERATION is generated if a non-zero buffer object name is bound to the GL_PIXEL_UNPACK_BUFFER
  1946. // target and the data would be unpacked from the buffer object such that the memory reads required would
  1947. // exceed the data store size.
  1948. // FIXME: GL_INVALID_OPERATION is generated if a non-zero buffer object name is bound to the GL_PIXEL_UNPACK_BUFFER
  1949. // target and data is not evenly divisible into the number of bytes needed to store in memory a datum
  1950. // indicated by type.
  1951. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1952. // FIXME: we only support RGBA + UNSIGNED_BYTE and DEPTH_COMPONENT + UNSIGNED_SHORT, implement all combinations!
  1953. if (!((format == GL_RGBA && type == GL_UNSIGNED_BYTE) || (format == GL_DEPTH_COMPONENT && type == GL_UNSIGNED_SHORT))) {
  1954. dbgln_if(GL_DEBUG, "gl_draw_pixels(): support for format {:#x} and/or type {:#x} not implemented", format, type);
  1955. return;
  1956. }
  1957. // FIXME: implement support for pixel parameters such as GL_UNPACK_ALIGNMENT
  1958. if (format == GL_RGBA) {
  1959. auto bitmap_or_error = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, { width, height });
  1960. RETURN_WITH_ERROR_IF(bitmap_or_error.is_error(), GL_OUT_OF_MEMORY);
  1961. auto bitmap = bitmap_or_error.release_value();
  1962. auto pixel_data = static_cast<u32 const*>(data);
  1963. for (int y = 0; y < height; ++y)
  1964. for (int x = 0; x < width; ++x)
  1965. bitmap->set_pixel(x, y, Color::from_argb(*(pixel_data++)));
  1966. m_rasterizer.blit_to_color_buffer_at_raster_position(bitmap);
  1967. } else if (format == GL_DEPTH_COMPONENT) {
  1968. Vector<float> depth_values;
  1969. depth_values.ensure_capacity(width * height);
  1970. auto depth_data = static_cast<u16 const*>(data);
  1971. for (int y = 0; y < height; ++y) {
  1972. for (int x = 0; x < width; ++x) {
  1973. auto u16_value = *(depth_data++);
  1974. auto float_value = static_cast<float>(u16_value) / NumericLimits<u16>::max();
  1975. depth_values.append(float_value);
  1976. }
  1977. }
  1978. m_rasterizer.blit_to_depth_buffer_at_raster_position(depth_values, width, height);
  1979. } else {
  1980. VERIFY_NOT_REACHED();
  1981. }
  1982. }
  1983. void SoftwareGLContext::gl_depth_range(GLdouble min, GLdouble max)
  1984. {
  1985. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_depth_range, min, max);
  1986. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1987. auto options = m_rasterizer.options();
  1988. options.depth_min = clamp<float>(min, 0.f, 1.f);
  1989. options.depth_max = clamp<float>(max, 0.f, 1.f);
  1990. m_rasterizer.set_options(options);
  1991. }
  1992. void SoftwareGLContext::gl_depth_func(GLenum func)
  1993. {
  1994. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_depth_func, func);
  1995. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1996. RETURN_WITH_ERROR_IF(!(func == GL_NEVER
  1997. || func == GL_LESS
  1998. || func == GL_EQUAL
  1999. || func == GL_LEQUAL
  2000. || func == GL_GREATER
  2001. || func == GL_NOTEQUAL
  2002. || func == GL_GEQUAL
  2003. || func == GL_ALWAYS),
  2004. GL_INVALID_ENUM);
  2005. auto options = m_rasterizer.options();
  2006. switch (func) {
  2007. case GL_NEVER:
  2008. options.depth_func = SoftGPU::DepthTestFunction::Never;
  2009. break;
  2010. case GL_ALWAYS:
  2011. options.depth_func = SoftGPU::DepthTestFunction::Always;
  2012. break;
  2013. case GL_LESS:
  2014. options.depth_func = SoftGPU::DepthTestFunction::Less;
  2015. break;
  2016. case GL_LEQUAL:
  2017. options.depth_func = SoftGPU::DepthTestFunction::LessOrEqual;
  2018. break;
  2019. case GL_EQUAL:
  2020. options.depth_func = SoftGPU::DepthTestFunction::Equal;
  2021. break;
  2022. case GL_NOTEQUAL:
  2023. options.depth_func = SoftGPU::DepthTestFunction::NotEqual;
  2024. break;
  2025. case GL_GEQUAL:
  2026. options.depth_func = SoftGPU::DepthTestFunction::GreaterOrEqual;
  2027. break;
  2028. case GL_GREATER:
  2029. options.depth_func = SoftGPU::DepthTestFunction::Greater;
  2030. break;
  2031. default:
  2032. VERIFY_NOT_REACHED();
  2033. }
  2034. m_rasterizer.set_options(options);
  2035. }
  2036. // General helper function to read arbitrary vertex attribute data into a float array
  2037. void SoftwareGLContext::read_from_vertex_attribute_pointer(VertexAttribPointer const& attrib, int index, float* elements, bool normalize)
  2038. {
  2039. auto byte_ptr = reinterpret_cast<const char*>(attrib.pointer);
  2040. size_t stride = attrib.stride;
  2041. switch (attrib.type) {
  2042. case GL_BYTE: {
  2043. if (stride == 0)
  2044. stride = sizeof(GLbyte) * attrib.size;
  2045. for (int i = 0; i < attrib.size; i++) {
  2046. elements[i] = *(reinterpret_cast<const GLbyte*>(byte_ptr + stride * index) + i);
  2047. if (normalize)
  2048. elements[i] /= 0x80;
  2049. }
  2050. break;
  2051. }
  2052. case GL_UNSIGNED_BYTE: {
  2053. if (stride == 0)
  2054. stride = sizeof(GLubyte) * attrib.size;
  2055. for (int i = 0; i < attrib.size; i++) {
  2056. elements[i] = *(reinterpret_cast<const GLubyte*>(byte_ptr + stride * index) + i);
  2057. if (normalize)
  2058. elements[i] /= 0xff;
  2059. }
  2060. break;
  2061. }
  2062. case GL_SHORT: {
  2063. if (stride == 0)
  2064. stride = sizeof(GLshort) * attrib.size;
  2065. for (int i = 0; i < attrib.size; i++) {
  2066. elements[i] = *(reinterpret_cast<const GLshort*>(byte_ptr + stride * index) + i);
  2067. if (normalize)
  2068. elements[i] /= 0x8000;
  2069. }
  2070. break;
  2071. }
  2072. case GL_UNSIGNED_SHORT: {
  2073. if (stride == 0)
  2074. stride = sizeof(GLushort) * attrib.size;
  2075. for (int i = 0; i < attrib.size; i++) {
  2076. elements[i] = *(reinterpret_cast<const GLushort*>(byte_ptr + stride * index) + i);
  2077. if (normalize)
  2078. elements[i] /= 0xffff;
  2079. }
  2080. break;
  2081. }
  2082. case GL_INT: {
  2083. if (stride == 0)
  2084. stride = sizeof(GLint) * attrib.size;
  2085. for (int i = 0; i < attrib.size; i++) {
  2086. elements[i] = *(reinterpret_cast<const GLint*>(byte_ptr + stride * index) + i);
  2087. if (normalize)
  2088. elements[i] /= 0x80000000;
  2089. }
  2090. break;
  2091. }
  2092. case GL_UNSIGNED_INT: {
  2093. if (stride == 0)
  2094. stride = sizeof(GLuint) * attrib.size;
  2095. for (int i = 0; i < attrib.size; i++) {
  2096. elements[i] = *(reinterpret_cast<const GLuint*>(byte_ptr + stride * index) + i);
  2097. if (normalize)
  2098. elements[i] /= 0xffffffff;
  2099. }
  2100. break;
  2101. }
  2102. case GL_FLOAT: {
  2103. if (stride == 0)
  2104. stride = sizeof(GLfloat) * attrib.size;
  2105. for (int i = 0; i < attrib.size; i++) {
  2106. elements[i] = *(reinterpret_cast<const GLfloat*>(byte_ptr + stride * index) + i);
  2107. }
  2108. break;
  2109. }
  2110. case GL_DOUBLE: {
  2111. if (stride == 0)
  2112. stride = sizeof(GLdouble) * attrib.size;
  2113. for (int i = 0; i < attrib.size; i++) {
  2114. elements[i] = static_cast<float>(*(reinterpret_cast<const GLdouble*>(byte_ptr + stride * index) + i));
  2115. }
  2116. break;
  2117. }
  2118. }
  2119. }
  2120. void SoftwareGLContext::gl_color_mask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha)
  2121. {
  2122. auto options = m_rasterizer.options();
  2123. auto mask = options.color_mask;
  2124. if (!red)
  2125. mask &= ~0x000000ff;
  2126. else
  2127. mask |= 0x000000ff;
  2128. if (!green)
  2129. mask &= ~0x0000ff00;
  2130. else
  2131. mask |= 0x0000ff00;
  2132. if (!blue)
  2133. mask &= ~0x00ff0000;
  2134. else
  2135. mask |= 0x00ff0000;
  2136. if (!alpha)
  2137. mask &= ~0xff000000;
  2138. else
  2139. mask |= 0xff000000;
  2140. options.color_mask = mask;
  2141. m_rasterizer.set_options(options);
  2142. }
  2143. void SoftwareGLContext::gl_polygon_mode(GLenum face, GLenum mode)
  2144. {
  2145. RETURN_WITH_ERROR_IF(!(face == GL_BACK || face == GL_FRONT || face == GL_FRONT_AND_BACK), GL_INVALID_ENUM);
  2146. RETURN_WITH_ERROR_IF(!(mode == GL_POINT || mode == GL_LINE || mode == GL_FILL), GL_INVALID_ENUM);
  2147. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2148. auto options = m_rasterizer.options();
  2149. // FIXME: This must support different polygon modes for front- and backside
  2150. if (face == GL_BACK) {
  2151. dbgln_if(GL_DEBUG, "gl_polygon_mode(GL_BACK, {:#x}): unimplemented", mode);
  2152. return;
  2153. }
  2154. auto map_mode = [](GLenum mode) -> SoftGPU::PolygonMode {
  2155. switch (mode) {
  2156. case GL_FILL:
  2157. return SoftGPU::PolygonMode::Fill;
  2158. case GL_LINE:
  2159. return SoftGPU::PolygonMode::Line;
  2160. case GL_POINT:
  2161. return SoftGPU::PolygonMode::Point;
  2162. default:
  2163. VERIFY_NOT_REACHED();
  2164. }
  2165. };
  2166. options.polygon_mode = map_mode(mode);
  2167. m_rasterizer.set_options(options);
  2168. }
  2169. void SoftwareGLContext::gl_polygon_offset(GLfloat factor, GLfloat units)
  2170. {
  2171. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_polygon_offset, factor, units);
  2172. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2173. auto rasterizer_options = m_rasterizer.options();
  2174. rasterizer_options.depth_offset_factor = factor;
  2175. rasterizer_options.depth_offset_constant = units;
  2176. m_rasterizer.set_options(rasterizer_options);
  2177. }
  2178. void SoftwareGLContext::gl_fogfv(GLenum pname, GLfloat* params)
  2179. {
  2180. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_fogfv, pname, params);
  2181. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2182. auto options = m_rasterizer.options();
  2183. switch (pname) {
  2184. case GL_FOG_COLOR:
  2185. options.fog_color = { params[0], params[1], params[2], params[3] };
  2186. break;
  2187. default:
  2188. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  2189. }
  2190. m_rasterizer.set_options(options);
  2191. }
  2192. void SoftwareGLContext::gl_fogf(GLenum pname, GLfloat param)
  2193. {
  2194. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_fogf, pname, param);
  2195. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2196. RETURN_WITH_ERROR_IF(param < 0.0f, GL_INVALID_VALUE);
  2197. auto options = m_rasterizer.options();
  2198. switch (pname) {
  2199. case GL_FOG_DENSITY:
  2200. options.fog_density = param;
  2201. break;
  2202. case GL_FOG_END:
  2203. options.fog_end = param;
  2204. break;
  2205. case GL_FOG_START:
  2206. options.fog_start = param;
  2207. break;
  2208. default:
  2209. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  2210. }
  2211. m_rasterizer.set_options(options);
  2212. }
  2213. void SoftwareGLContext::gl_fogi(GLenum pname, GLint param)
  2214. {
  2215. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_fogi, pname, param);
  2216. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2217. RETURN_WITH_ERROR_IF(param != GL_LINEAR && param != GL_EXP && param != GL_EXP2, GL_INVALID_ENUM);
  2218. auto options = m_rasterizer.options();
  2219. switch (pname) {
  2220. case GL_FOG_MODE:
  2221. switch (param) {
  2222. case GL_LINEAR:
  2223. options.fog_mode = SoftGPU::FogMode::Linear;
  2224. break;
  2225. case GL_EXP:
  2226. options.fog_mode = SoftGPU::FogMode::Exp;
  2227. break;
  2228. case GL_EXP2:
  2229. options.fog_mode = SoftGPU::FogMode::Exp2;
  2230. break;
  2231. }
  2232. break;
  2233. default:
  2234. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  2235. }
  2236. m_rasterizer.set_options(options);
  2237. }
  2238. void SoftwareGLContext::gl_pixel_storei(GLenum pname, GLint param)
  2239. {
  2240. // FIXME: Implement missing parameters
  2241. switch (pname) {
  2242. case GL_PACK_ALIGNMENT:
  2243. RETURN_WITH_ERROR_IF(param != 1 && param != 2 && param != 4 && param != 8, GL_INVALID_VALUE);
  2244. m_pack_alignment = param;
  2245. break;
  2246. case GL_UNPACK_ROW_LENGTH:
  2247. RETURN_WITH_ERROR_IF(param < 0, GL_INVALID_VALUE);
  2248. m_unpack_row_length = static_cast<size_t>(param);
  2249. break;
  2250. case GL_UNPACK_ALIGNMENT:
  2251. RETURN_WITH_ERROR_IF(param != 1 && param != 2 && param != 4 && param != 8, GL_INVALID_VALUE);
  2252. m_unpack_alignment = param;
  2253. break;
  2254. default:
  2255. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  2256. break;
  2257. }
  2258. }
  2259. void SoftwareGLContext::gl_scissor(GLint x, GLint y, GLsizei width, GLsizei height)
  2260. {
  2261. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_scissor, x, y, width, height);
  2262. RETURN_WITH_ERROR_IF(width < 0 || height < 0, GL_INVALID_VALUE);
  2263. auto options = m_rasterizer.options();
  2264. options.scissor_box = { x, y, width, height };
  2265. m_rasterizer.set_options(options);
  2266. }
  2267. void SoftwareGLContext::gl_stencil_func_separate(GLenum face, GLenum func, GLint ref, GLuint mask)
  2268. {
  2269. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_stencil_func_separate, face, func, ref, mask);
  2270. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2271. RETURN_WITH_ERROR_IF(!(face == GL_FRONT || face == GL_BACK || face == GL_FRONT_AND_BACK), GL_INVALID_ENUM);
  2272. RETURN_WITH_ERROR_IF(!(func == GL_NEVER
  2273. || func == GL_LESS
  2274. || func == GL_LEQUAL
  2275. || func == GL_GREATER
  2276. || func == GL_GEQUAL
  2277. || func == GL_EQUAL
  2278. || func == GL_NOTEQUAL
  2279. || func == GL_ALWAYS),
  2280. GL_INVALID_ENUM);
  2281. ref = clamp(ref, 0, (1 << m_device_info.stencil_bits) - 1);
  2282. StencilFunctionOptions new_options = { func, ref, mask };
  2283. if (face == GL_FRONT || face == GL_FRONT_AND_BACK)
  2284. m_stencil_function[Face::Front] = new_options;
  2285. if (face == GL_BACK || face == GL_FRONT_AND_BACK)
  2286. m_stencil_function[Face::Back] = new_options;
  2287. m_stencil_configuration_dirty = true;
  2288. }
  2289. void SoftwareGLContext::gl_stencil_mask_separate(GLenum face, GLuint mask)
  2290. {
  2291. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_stencil_mask_separate, face, mask);
  2292. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2293. if (face == GL_FRONT || face == GL_FRONT_AND_BACK)
  2294. m_stencil_operation[Face::Front].write_mask = mask;
  2295. if (face == GL_BACK || face == GL_FRONT_AND_BACK)
  2296. m_stencil_operation[Face::Back].write_mask = mask;
  2297. m_stencil_configuration_dirty = true;
  2298. }
  2299. void SoftwareGLContext::gl_stencil_op_separate(GLenum face, GLenum sfail, GLenum dpfail, GLenum dppass)
  2300. {
  2301. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_stencil_op_separate, face, sfail, dpfail, dppass);
  2302. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2303. RETURN_WITH_ERROR_IF(!(face == GL_FRONT || face == GL_BACK || face == GL_FRONT_AND_BACK), GL_INVALID_ENUM);
  2304. auto is_valid_op = [](GLenum op) -> bool {
  2305. return op == GL_KEEP || op == GL_ZERO || op == GL_REPLACE || op == GL_INCR || op == GL_INCR_WRAP
  2306. || op == GL_DECR || op == GL_DECR_WRAP || op == GL_INVERT;
  2307. };
  2308. RETURN_WITH_ERROR_IF(!is_valid_op(sfail), GL_INVALID_ENUM);
  2309. RETURN_WITH_ERROR_IF(!is_valid_op(dpfail), GL_INVALID_ENUM);
  2310. RETURN_WITH_ERROR_IF(!is_valid_op(dppass), GL_INVALID_ENUM);
  2311. auto update_stencil_operation = [&](Face face, GLenum sfail, GLenum dpfail, GLenum dppass) {
  2312. auto& stencil_operation = m_stencil_operation[face];
  2313. stencil_operation.op_fail = sfail;
  2314. stencil_operation.op_depth_fail = dpfail;
  2315. stencil_operation.op_pass = dppass;
  2316. };
  2317. if (face == GL_FRONT || face == GL_FRONT_AND_BACK)
  2318. update_stencil_operation(Face::Front, sfail, dpfail, dppass);
  2319. if (face == GL_BACK || face == GL_FRONT_AND_BACK)
  2320. update_stencil_operation(Face::Back, sfail, dpfail, dppass);
  2321. m_stencil_configuration_dirty = true;
  2322. }
  2323. void SoftwareGLContext::gl_normal(GLfloat nx, GLfloat ny, GLfloat nz)
  2324. {
  2325. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_normal, nx, ny, nz);
  2326. m_current_vertex_normal = { nx, ny, nz };
  2327. }
  2328. void SoftwareGLContext::gl_normal_pointer(GLenum type, GLsizei stride, void const* pointer)
  2329. {
  2330. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2331. RETURN_WITH_ERROR_IF(type != GL_BYTE
  2332. && type != GL_SHORT
  2333. && type != GL_INT
  2334. && type != GL_FLOAT
  2335. && type != GL_DOUBLE,
  2336. GL_INVALID_ENUM);
  2337. RETURN_WITH_ERROR_IF(stride < 0, GL_INVALID_VALUE);
  2338. dbgln_if(GL_DEBUG, "gl_normal_pointer({:#x}, {}, {:p}): unimplemented", type, stride, pointer);
  2339. }
  2340. void SoftwareGLContext::gl_raster_pos(GLfloat x, GLfloat y, GLfloat z, GLfloat w)
  2341. {
  2342. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_raster_pos, x, y, z, w);
  2343. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2344. m_rasterizer.set_raster_position({ x, y, z, w }, m_model_view_matrix, m_projection_matrix);
  2345. }
  2346. void SoftwareGLContext::gl_line_width(GLfloat width)
  2347. {
  2348. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_line_width, width);
  2349. RETURN_WITH_ERROR_IF(width <= 0, GL_INVALID_VALUE);
  2350. m_line_width = width;
  2351. }
  2352. void SoftwareGLContext::gl_push_attrib(GLbitfield mask)
  2353. {
  2354. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_push_attrib, mask);
  2355. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2356. // FIXME: implement
  2357. dbgln_if(GL_DEBUG, "SoftwareGLContext FIXME: implement gl_push_attrib({})", mask);
  2358. }
  2359. void SoftwareGLContext::gl_pop_attrib()
  2360. {
  2361. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_pop_attrib);
  2362. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2363. // FIXME: implement
  2364. dbgln_if(GL_DEBUG, "SoftwareGLContext FIXME: implement gl_pop_attrib()");
  2365. }
  2366. void SoftwareGLContext::gl_light_model(GLenum pname, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
  2367. {
  2368. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_light_model, pname, x, y, z, w);
  2369. RETURN_WITH_ERROR_IF(!(pname == GL_LIGHT_MODEL_AMBIENT
  2370. || pname == GL_LIGHT_MODEL_TWO_SIDE),
  2371. GL_INVALID_ENUM);
  2372. auto lighting_params = m_rasterizer.light_model();
  2373. bool update_lighting_model = false;
  2374. switch (pname) {
  2375. case GL_LIGHT_MODEL_AMBIENT:
  2376. lighting_params.scene_ambient_color = { x, y, z, w };
  2377. update_lighting_model = true;
  2378. break;
  2379. case GL_LIGHT_MODEL_TWO_SIDE:
  2380. VERIFY(y == 0.0f && z == 0.0f && w == 0.0f);
  2381. lighting_params.two_sided_lighting = x;
  2382. update_lighting_model = true;
  2383. break;
  2384. case GL_LIGHT_MODEL_LOCAL_VIEWER:
  2385. // 0 means the viewer is at infinity
  2386. // 1 means they're in local (eye) space
  2387. lighting_params.viewer_at_infinity = (x != 1.0f);
  2388. update_lighting_model = true;
  2389. break;
  2390. default:
  2391. VERIFY_NOT_REACHED();
  2392. }
  2393. if (update_lighting_model)
  2394. m_rasterizer.set_light_model_params(lighting_params);
  2395. }
  2396. void SoftwareGLContext::gl_bitmap(GLsizei width, GLsizei height, GLfloat xorig, GLfloat yorig, GLfloat xmove, GLfloat ymove, GLubyte const* bitmap)
  2397. {
  2398. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_bitmap, width, height, xorig, yorig, xmove, ymove, bitmap);
  2399. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2400. if (bitmap != nullptr) {
  2401. // FIXME: implement
  2402. dbgln_if(GL_DEBUG, "gl_bitmap({}, {}, {}, {}, {}, {}, {}): unimplemented", width, height, xorig, yorig, xmove, ymove, bitmap);
  2403. }
  2404. auto raster_position = m_rasterizer.raster_position();
  2405. raster_position.window_coordinates += { xmove, ymove, 0.f, 0.f };
  2406. m_rasterizer.set_raster_position(raster_position);
  2407. }
  2408. void SoftwareGLContext::gl_copy_tex_image_2d(GLenum target, GLint level, GLenum internalformat, GLint x, GLint y, GLsizei width, GLsizei height, GLint border)
  2409. {
  2410. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_copy_tex_image_2d, target, level, internalformat, x, y, width, height, border);
  2411. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2412. // FIXME: implement
  2413. dbgln_if(GL_DEBUG, "SoftwareGLContext FIXME: implement gl_copy_tex_image_2d({:#x}, {}, {:#x}, {}, {}, {}, {}, {})",
  2414. target, level, internalformat, x, y, width, height, border);
  2415. }
  2416. void SoftwareGLContext::gl_get_tex_parameter_integerv(GLenum target, GLint level, GLenum pname, GLint* params)
  2417. {
  2418. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2419. // FIXME: support targets other than GL_TEXTURE_2D
  2420. RETURN_WITH_ERROR_IF(target != GL_TEXTURE_2D, GL_INVALID_ENUM);
  2421. // FIXME: support other parameter names
  2422. RETURN_WITH_ERROR_IF(pname < GL_TEXTURE_WIDTH || pname > GL_TEXTURE_HEIGHT, GL_INVALID_ENUM);
  2423. RETURN_WITH_ERROR_IF(level < 0 || level > Texture2D::LOG2_MAX_TEXTURE_SIZE, GL_INVALID_VALUE);
  2424. // FIXME: GL_INVALID_VALUE is generated if target is GL_TEXTURE_BUFFER and level is not zero
  2425. // FIXME: GL_INVALID_OPERATION is generated if GL_TEXTURE_COMPRESSED_IMAGE_SIZE is queried on texture images with an uncompressed internal format or on proxy targets
  2426. switch (pname) {
  2427. case GL_TEXTURE_HEIGHT:
  2428. *params = m_active_texture_unit->bound_texture_2d()->height_at_lod(level);
  2429. break;
  2430. case GL_TEXTURE_WIDTH:
  2431. *params = m_active_texture_unit->bound_texture_2d()->width_at_lod(level);
  2432. break;
  2433. }
  2434. }
  2435. void SoftwareGLContext::gl_rect(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2)
  2436. {
  2437. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_rect, x1, y1, x2, y2);
  2438. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2439. gl_begin(GL_POLYGON);
  2440. gl_vertex(x1, y1, 0.0, 0.0);
  2441. gl_vertex(x2, y1, 0.0, 0.0);
  2442. gl_vertex(x2, y2, 0.0, 0.0);
  2443. gl_vertex(x1, y2, 0.0, 0.0);
  2444. gl_end();
  2445. }
  2446. void SoftwareGLContext::gl_tex_gen(GLenum coord, GLenum pname, GLint param)
  2447. {
  2448. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_gen, coord, pname, param);
  2449. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2450. RETURN_WITH_ERROR_IF(coord < GL_S || coord > GL_Q, GL_INVALID_ENUM);
  2451. RETURN_WITH_ERROR_IF(pname != GL_TEXTURE_GEN_MODE, GL_INVALID_ENUM);
  2452. RETURN_WITH_ERROR_IF(param != GL_EYE_LINEAR
  2453. && param != GL_OBJECT_LINEAR
  2454. && param != GL_SPHERE_MAP
  2455. && param != GL_NORMAL_MAP
  2456. && param != GL_REFLECTION_MAP,
  2457. GL_INVALID_ENUM);
  2458. RETURN_WITH_ERROR_IF((coord == GL_R || coord == GL_Q) && param == GL_SPHERE_MAP, GL_INVALID_ENUM);
  2459. RETURN_WITH_ERROR_IF(coord == GL_Q && (param == GL_REFLECTION_MAP || param == GL_NORMAL_MAP), GL_INVALID_ENUM);
  2460. GLenum const capability = GL_TEXTURE_GEN_S + (coord - GL_S);
  2461. texture_coordinate_generation(m_active_texture_unit_index, capability).generation_mode = param;
  2462. m_texcoord_generation_dirty = true;
  2463. }
  2464. void SoftwareGLContext::gl_tex_gen_floatv(GLenum coord, GLenum pname, GLfloat const* params)
  2465. {
  2466. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_gen_floatv, coord, pname, params);
  2467. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2468. RETURN_WITH_ERROR_IF(coord < GL_S || coord > GL_Q, GL_INVALID_ENUM);
  2469. RETURN_WITH_ERROR_IF(pname != GL_TEXTURE_GEN_MODE
  2470. && pname != GL_OBJECT_PLANE
  2471. && pname != GL_EYE_PLANE,
  2472. GL_INVALID_ENUM);
  2473. GLenum const capability = GL_TEXTURE_GEN_S + (coord - GL_S);
  2474. switch (pname) {
  2475. case GL_TEXTURE_GEN_MODE: {
  2476. auto param = static_cast<GLenum>(params[0]);
  2477. RETURN_WITH_ERROR_IF(param != GL_EYE_LINEAR
  2478. && param != GL_OBJECT_LINEAR
  2479. && param != GL_SPHERE_MAP
  2480. && param != GL_NORMAL_MAP
  2481. && param != GL_REFLECTION_MAP,
  2482. GL_INVALID_ENUM);
  2483. RETURN_WITH_ERROR_IF((coord == GL_R || coord == GL_Q) && param == GL_SPHERE_MAP, GL_INVALID_ENUM);
  2484. RETURN_WITH_ERROR_IF(coord == GL_Q && (param == GL_REFLECTION_MAP || param == GL_NORMAL_MAP), GL_INVALID_ENUM);
  2485. texture_coordinate_generation(m_active_texture_unit_index, capability).generation_mode = param;
  2486. break;
  2487. }
  2488. case GL_OBJECT_PLANE:
  2489. texture_coordinate_generation(m_active_texture_unit_index, capability).object_plane_coefficients = { params[0], params[1], params[2], params[3] };
  2490. break;
  2491. case GL_EYE_PLANE: {
  2492. auto const& inverse_model_view = m_model_view_matrix.inverse();
  2493. auto input_coefficients = FloatVector4 { params[0], params[1], params[2], params[3] };
  2494. // Note: we are allowed to store transformed coefficients here, according to the documentation on
  2495. // `glGetTexGen`:
  2496. //
  2497. // "The returned values are those maintained in eye coordinates. They are not equal to the values
  2498. // specified using glTexGen, unless the modelview matrix was identity when glTexGen was called."
  2499. texture_coordinate_generation(m_active_texture_unit_index, capability).eye_plane_coefficients = inverse_model_view * input_coefficients;
  2500. break;
  2501. }
  2502. default:
  2503. VERIFY_NOT_REACHED();
  2504. }
  2505. m_texcoord_generation_dirty = true;
  2506. }
  2507. void SoftwareGLContext::present()
  2508. {
  2509. m_rasterizer.blit_color_buffer_to(*m_frontbuffer);
  2510. }
  2511. void SoftwareGLContext::sync_device_config()
  2512. {
  2513. sync_device_sampler_config();
  2514. sync_device_texcoord_config();
  2515. sync_light_state();
  2516. sync_stencil_configuration();
  2517. }
  2518. void SoftwareGLContext::sync_device_sampler_config()
  2519. {
  2520. if (!m_sampler_config_is_dirty)
  2521. return;
  2522. m_sampler_config_is_dirty = false;
  2523. for (unsigned i = 0; i < m_texture_units.size(); ++i) {
  2524. SoftGPU::SamplerConfig config;
  2525. if (!m_texture_units[i].texture_2d_enabled())
  2526. continue;
  2527. auto texture = m_texture_units[i].bound_texture_2d();
  2528. config.bound_image = texture.is_null() ? nullptr : texture->device_image();
  2529. auto const& sampler = texture->sampler();
  2530. switch (sampler.min_filter()) {
  2531. case GL_NEAREST:
  2532. config.texture_min_filter = SoftGPU::TextureFilter::Nearest;
  2533. config.mipmap_filter = SoftGPU::MipMapFilter::None;
  2534. break;
  2535. case GL_LINEAR:
  2536. config.texture_min_filter = SoftGPU::TextureFilter::Linear;
  2537. config.mipmap_filter = SoftGPU::MipMapFilter::None;
  2538. break;
  2539. case GL_NEAREST_MIPMAP_NEAREST:
  2540. config.texture_min_filter = SoftGPU::TextureFilter::Nearest;
  2541. config.mipmap_filter = SoftGPU::MipMapFilter::Nearest;
  2542. break;
  2543. case GL_LINEAR_MIPMAP_NEAREST:
  2544. config.texture_min_filter = SoftGPU::TextureFilter::Linear;
  2545. config.mipmap_filter = SoftGPU::MipMapFilter::Nearest;
  2546. break;
  2547. case GL_NEAREST_MIPMAP_LINEAR:
  2548. config.texture_min_filter = SoftGPU::TextureFilter::Nearest;
  2549. config.mipmap_filter = SoftGPU::MipMapFilter::Linear;
  2550. break;
  2551. case GL_LINEAR_MIPMAP_LINEAR:
  2552. config.texture_min_filter = SoftGPU::TextureFilter::Linear;
  2553. config.mipmap_filter = SoftGPU::MipMapFilter::Linear;
  2554. break;
  2555. default:
  2556. VERIFY_NOT_REACHED();
  2557. }
  2558. switch (sampler.mag_filter()) {
  2559. case GL_NEAREST:
  2560. config.texture_mag_filter = SoftGPU::TextureFilter::Nearest;
  2561. break;
  2562. case GL_LINEAR:
  2563. config.texture_mag_filter = SoftGPU::TextureFilter::Linear;
  2564. break;
  2565. default:
  2566. VERIFY_NOT_REACHED();
  2567. }
  2568. switch (sampler.wrap_s_mode()) {
  2569. case GL_CLAMP:
  2570. config.texture_wrap_u = SoftGPU::TextureWrapMode::Clamp;
  2571. break;
  2572. case GL_CLAMP_TO_BORDER:
  2573. config.texture_wrap_u = SoftGPU::TextureWrapMode::ClampToBorder;
  2574. break;
  2575. case GL_CLAMP_TO_EDGE:
  2576. config.texture_wrap_u = SoftGPU::TextureWrapMode::ClampToEdge;
  2577. break;
  2578. case GL_REPEAT:
  2579. config.texture_wrap_u = SoftGPU::TextureWrapMode::Repeat;
  2580. break;
  2581. case GL_MIRRORED_REPEAT:
  2582. config.texture_wrap_u = SoftGPU::TextureWrapMode::MirroredRepeat;
  2583. break;
  2584. default:
  2585. VERIFY_NOT_REACHED();
  2586. }
  2587. switch (sampler.wrap_t_mode()) {
  2588. case GL_CLAMP:
  2589. config.texture_wrap_v = SoftGPU::TextureWrapMode::Clamp;
  2590. break;
  2591. case GL_CLAMP_TO_BORDER:
  2592. config.texture_wrap_v = SoftGPU::TextureWrapMode::ClampToBorder;
  2593. break;
  2594. case GL_CLAMP_TO_EDGE:
  2595. config.texture_wrap_v = SoftGPU::TextureWrapMode::ClampToEdge;
  2596. break;
  2597. case GL_REPEAT:
  2598. config.texture_wrap_v = SoftGPU::TextureWrapMode::Repeat;
  2599. break;
  2600. case GL_MIRRORED_REPEAT:
  2601. config.texture_wrap_v = SoftGPU::TextureWrapMode::MirroredRepeat;
  2602. break;
  2603. default:
  2604. VERIFY_NOT_REACHED();
  2605. }
  2606. switch (m_texture_units[i].env_mode()) {
  2607. case GL_MODULATE:
  2608. config.fixed_function_texture_env_mode = SoftGPU::TextureEnvMode::Modulate;
  2609. break;
  2610. case GL_REPLACE:
  2611. config.fixed_function_texture_env_mode = SoftGPU::TextureEnvMode::Replace;
  2612. break;
  2613. case GL_DECAL:
  2614. config.fixed_function_texture_env_mode = SoftGPU::TextureEnvMode::Decal;
  2615. break;
  2616. default:
  2617. VERIFY_NOT_REACHED();
  2618. }
  2619. m_rasterizer.set_sampler_config(i, config);
  2620. }
  2621. }
  2622. void SoftwareGLContext::sync_light_state()
  2623. {
  2624. if (!m_light_state_is_dirty)
  2625. return;
  2626. m_light_state_is_dirty = false;
  2627. auto options = m_rasterizer.options();
  2628. options.color_material_enabled = m_color_material_enabled;
  2629. switch (m_color_material_face) {
  2630. case GL_BACK:
  2631. options.color_material_face = SoftGPU::ColorMaterialFace::Back;
  2632. break;
  2633. case GL_FRONT:
  2634. options.color_material_face = SoftGPU::ColorMaterialFace::Front;
  2635. break;
  2636. case GL_FRONT_AND_BACK:
  2637. options.color_material_face = SoftGPU::ColorMaterialFace::FrontAndBack;
  2638. break;
  2639. default:
  2640. VERIFY_NOT_REACHED();
  2641. }
  2642. switch (m_color_material_mode) {
  2643. case GL_AMBIENT:
  2644. options.color_material_mode = SoftGPU::ColorMaterialMode::Ambient;
  2645. break;
  2646. case GL_AMBIENT_AND_DIFFUSE:
  2647. options.color_material_mode = SoftGPU::ColorMaterialMode::Ambient;
  2648. options.color_material_mode = SoftGPU::ColorMaterialMode::Diffuse;
  2649. break;
  2650. case GL_DIFFUSE:
  2651. options.color_material_mode = SoftGPU::ColorMaterialMode::Diffuse;
  2652. break;
  2653. case GL_EMISSION:
  2654. options.color_material_mode = SoftGPU::ColorMaterialMode::Emissive;
  2655. break;
  2656. case GL_SPECULAR:
  2657. options.color_material_mode = SoftGPU::ColorMaterialMode::Specular;
  2658. break;
  2659. default:
  2660. VERIFY_NOT_REACHED();
  2661. }
  2662. m_rasterizer.set_options(options);
  2663. for (auto light_id = 0u; light_id < SoftGPU::NUM_LIGHTS; light_id++) {
  2664. auto const& current_light_state = m_light_states.at(light_id);
  2665. m_rasterizer.set_light_state(light_id, current_light_state);
  2666. }
  2667. m_rasterizer.set_material_state(SoftGPU::Face::Front, m_material_states[Face::Front]);
  2668. m_rasterizer.set_material_state(SoftGPU::Face::Back, m_material_states[Face::Back]);
  2669. }
  2670. void SoftwareGLContext::sync_device_texcoord_config()
  2671. {
  2672. if (!m_texcoord_generation_dirty)
  2673. return;
  2674. m_texcoord_generation_dirty = false;
  2675. auto options = m_rasterizer.options();
  2676. for (size_t i = 0; i < m_device_info.num_texture_units; ++i) {
  2677. u8 enabled_coordinates = SoftGPU::TexCoordGenerationCoordinate::None;
  2678. for (GLenum capability = GL_TEXTURE_GEN_S; capability <= GL_TEXTURE_GEN_Q; ++capability) {
  2679. auto const context_coordinate_config = texture_coordinate_generation(i, capability);
  2680. if (!context_coordinate_config.enabled)
  2681. continue;
  2682. SoftGPU::TexCoordGenerationConfig* texcoord_generation_config;
  2683. switch (capability) {
  2684. case GL_TEXTURE_GEN_S:
  2685. enabled_coordinates |= SoftGPU::TexCoordGenerationCoordinate::S;
  2686. texcoord_generation_config = &options.texcoord_generation_config[i][0];
  2687. break;
  2688. case GL_TEXTURE_GEN_T:
  2689. enabled_coordinates |= SoftGPU::TexCoordGenerationCoordinate::T;
  2690. texcoord_generation_config = &options.texcoord_generation_config[i][1];
  2691. break;
  2692. case GL_TEXTURE_GEN_R:
  2693. enabled_coordinates |= SoftGPU::TexCoordGenerationCoordinate::R;
  2694. texcoord_generation_config = &options.texcoord_generation_config[i][2];
  2695. break;
  2696. case GL_TEXTURE_GEN_Q:
  2697. enabled_coordinates |= SoftGPU::TexCoordGenerationCoordinate::Q;
  2698. texcoord_generation_config = &options.texcoord_generation_config[i][3];
  2699. break;
  2700. default:
  2701. VERIFY_NOT_REACHED();
  2702. }
  2703. switch (context_coordinate_config.generation_mode) {
  2704. case GL_OBJECT_LINEAR:
  2705. texcoord_generation_config->mode = SoftGPU::TexCoordGenerationMode::ObjectLinear;
  2706. texcoord_generation_config->coefficients = context_coordinate_config.object_plane_coefficients;
  2707. break;
  2708. case GL_EYE_LINEAR:
  2709. texcoord_generation_config->mode = SoftGPU::TexCoordGenerationMode::EyeLinear;
  2710. texcoord_generation_config->coefficients = context_coordinate_config.eye_plane_coefficients;
  2711. break;
  2712. case GL_SPHERE_MAP:
  2713. texcoord_generation_config->mode = SoftGPU::TexCoordGenerationMode::SphereMap;
  2714. break;
  2715. case GL_REFLECTION_MAP:
  2716. texcoord_generation_config->mode = SoftGPU::TexCoordGenerationMode::ReflectionMap;
  2717. break;
  2718. case GL_NORMAL_MAP:
  2719. texcoord_generation_config->mode = SoftGPU::TexCoordGenerationMode::NormalMap;
  2720. break;
  2721. }
  2722. }
  2723. options.texcoord_generation_enabled_coordinates[i] = enabled_coordinates;
  2724. }
  2725. m_rasterizer.set_options(options);
  2726. }
  2727. void SoftwareGLContext::sync_stencil_configuration()
  2728. {
  2729. if (!m_stencil_configuration_dirty)
  2730. return;
  2731. m_stencil_configuration_dirty = false;
  2732. auto set_device_stencil = [&](SoftGPU::Face face, StencilFunctionOptions func, StencilOperationOptions op) {
  2733. SoftGPU::StencilConfiguration device_configuration;
  2734. // Stencil test function
  2735. auto map_func = [](GLenum func) -> SoftGPU::StencilTestFunction {
  2736. switch (func) {
  2737. case GL_ALWAYS:
  2738. return SoftGPU::StencilTestFunction::Always;
  2739. case GL_EQUAL:
  2740. return SoftGPU::StencilTestFunction::Equal;
  2741. case GL_GEQUAL:
  2742. return SoftGPU::StencilTestFunction::GreaterOrEqual;
  2743. case GL_GREATER:
  2744. return SoftGPU::StencilTestFunction::Greater;
  2745. case GL_LESS:
  2746. return SoftGPU::StencilTestFunction::Less;
  2747. case GL_LEQUAL:
  2748. return SoftGPU::StencilTestFunction::LessOrEqual;
  2749. case GL_NEVER:
  2750. return SoftGPU::StencilTestFunction::Never;
  2751. case GL_NOTEQUAL:
  2752. return SoftGPU::StencilTestFunction::NotEqual;
  2753. }
  2754. VERIFY_NOT_REACHED();
  2755. };
  2756. device_configuration.test_function = map_func(func.func);
  2757. device_configuration.reference_value = func.reference_value;
  2758. device_configuration.test_mask = func.mask;
  2759. // Stencil operation
  2760. auto map_operation = [](GLenum operation) -> SoftGPU::StencilOperation {
  2761. switch (operation) {
  2762. case GL_DECR:
  2763. return SoftGPU::StencilOperation::Decrement;
  2764. case GL_DECR_WRAP:
  2765. return SoftGPU::StencilOperation::DecrementWrap;
  2766. case GL_INCR:
  2767. return SoftGPU::StencilOperation::Increment;
  2768. case GL_INCR_WRAP:
  2769. return SoftGPU::StencilOperation::IncrementWrap;
  2770. case GL_INVERT:
  2771. return SoftGPU::StencilOperation::Invert;
  2772. case GL_KEEP:
  2773. return SoftGPU::StencilOperation::Keep;
  2774. case GL_REPLACE:
  2775. return SoftGPU::StencilOperation::Replace;
  2776. case GL_ZERO:
  2777. return SoftGPU::StencilOperation::Zero;
  2778. }
  2779. VERIFY_NOT_REACHED();
  2780. };
  2781. device_configuration.on_stencil_test_fail = map_operation(op.op_fail);
  2782. device_configuration.on_depth_test_fail = map_operation(op.op_depth_fail);
  2783. device_configuration.on_pass = map_operation(op.op_pass);
  2784. device_configuration.write_mask = op.write_mask;
  2785. m_rasterizer.set_stencil_configuration(face, device_configuration);
  2786. };
  2787. set_device_stencil(SoftGPU::Face::Front, m_stencil_function[Face::Front], m_stencil_operation[Face::Front]);
  2788. set_device_stencil(SoftGPU::Face::Back, m_stencil_function[Face::Back], m_stencil_operation[Face::Back]);
  2789. }
  2790. void SoftwareGLContext::build_extension_string()
  2791. {
  2792. Vector<StringView> extensions;
  2793. // FIXME: npot texture support became a required core feature starting with OpenGL 2.0 (https://www.khronos.org/opengl/wiki/NPOT_Texture)
  2794. // Ideally we would verify if the selected device adheres to the requested OpenGL context version before context creation
  2795. // and refuse to create a context if it doesn't.
  2796. if (m_device_info.supports_npot_textures)
  2797. extensions.append("GL_ARB_texture_non_power_of_two");
  2798. if (m_device_info.num_texture_units > 1)
  2799. extensions.append("GL_ARB_multitexture");
  2800. m_extensions = String::join(" ", extensions);
  2801. }
  2802. void SoftwareGLContext::gl_lightf(GLenum light, GLenum pname, GLfloat param)
  2803. {
  2804. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_lightf, light, pname, param);
  2805. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2806. RETURN_WITH_ERROR_IF(light < GL_LIGHT0 || light >= (GL_LIGHT0 + m_device_info.num_lights), GL_INVALID_ENUM);
  2807. RETURN_WITH_ERROR_IF(!(pname == GL_CONSTANT_ATTENUATION || pname == GL_LINEAR_ATTENUATION || pname == GL_QUADRATIC_ATTENUATION || pname != GL_SPOT_EXPONENT || pname != GL_SPOT_CUTOFF), GL_INVALID_ENUM);
  2808. auto& light_state = m_light_states.at(light - GL_LIGHT0);
  2809. switch (pname) {
  2810. case GL_CONSTANT_ATTENUATION:
  2811. light_state.constant_attenuation = param;
  2812. break;
  2813. case GL_LINEAR_ATTENUATION:
  2814. light_state.linear_attenuation = param;
  2815. break;
  2816. case GL_QUADRATIC_ATTENUATION:
  2817. light_state.quadratic_attenuation = param;
  2818. break;
  2819. case GL_SPOT_EXPONENT:
  2820. light_state.spotlight_exponent = param;
  2821. break;
  2822. case GL_SPOT_CUTOFF:
  2823. light_state.spotlight_cutoff_angle = param;
  2824. break;
  2825. default:
  2826. VERIFY_NOT_REACHED();
  2827. }
  2828. m_light_state_is_dirty = true;
  2829. }
  2830. void SoftwareGLContext::gl_lightfv(GLenum light, GLenum pname, GLfloat const* params)
  2831. {
  2832. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_lightfv, light, pname, params);
  2833. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2834. RETURN_WITH_ERROR_IF(light < GL_LIGHT0 || light >= (GL_LIGHT0 + m_device_info.num_lights), GL_INVALID_ENUM);
  2835. RETURN_WITH_ERROR_IF(!(pname == GL_AMBIENT || pname == GL_DIFFUSE || pname == GL_SPECULAR || pname == GL_POSITION || pname == GL_CONSTANT_ATTENUATION || pname == GL_LINEAR_ATTENUATION || pname == GL_QUADRATIC_ATTENUATION || pname == GL_SPOT_CUTOFF || pname == GL_SPOT_EXPONENT || pname == GL_SPOT_DIRECTION), GL_INVALID_ENUM);
  2836. auto& light_state = m_light_states.at(light - GL_LIGHT0);
  2837. switch (pname) {
  2838. case GL_AMBIENT:
  2839. light_state.ambient_intensity = { params[0], params[1], params[2], params[3] };
  2840. break;
  2841. case GL_DIFFUSE:
  2842. light_state.diffuse_intensity = { params[0], params[1], params[2], params[3] };
  2843. break;
  2844. case GL_SPECULAR:
  2845. light_state.specular_intensity = { params[0], params[1], params[2], params[3] };
  2846. break;
  2847. case GL_POSITION:
  2848. light_state.position = { params[0], params[1], params[2], params[3] };
  2849. light_state.position = m_model_view_matrix * light_state.position;
  2850. break;
  2851. case GL_CONSTANT_ATTENUATION:
  2852. light_state.constant_attenuation = *params;
  2853. break;
  2854. case GL_LINEAR_ATTENUATION:
  2855. light_state.linear_attenuation = *params;
  2856. break;
  2857. case GL_QUADRATIC_ATTENUATION:
  2858. light_state.quadratic_attenuation = *params;
  2859. break;
  2860. case GL_SPOT_EXPONENT:
  2861. light_state.spotlight_exponent = *params;
  2862. break;
  2863. case GL_SPOT_CUTOFF:
  2864. light_state.spotlight_cutoff_angle = *params;
  2865. break;
  2866. case GL_SPOT_DIRECTION: {
  2867. FloatVector4 direction_vector = { params[0], params[1], params[2], 0.0f };
  2868. direction_vector = m_model_view_matrix * direction_vector;
  2869. light_state.spotlight_direction = { direction_vector.x(), direction_vector.y(), direction_vector.z() };
  2870. break;
  2871. }
  2872. default:
  2873. VERIFY_NOT_REACHED();
  2874. }
  2875. m_light_state_is_dirty = true;
  2876. }
  2877. void SoftwareGLContext::gl_lightiv(GLenum light, GLenum pname, GLint const* params)
  2878. {
  2879. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_lightiv, light, pname, params);
  2880. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2881. RETURN_WITH_ERROR_IF(light < GL_LIGHT0 || light >= (GL_LIGHT0 + m_device_info.num_lights), GL_INVALID_ENUM);
  2882. RETURN_WITH_ERROR_IF(!(pname == GL_AMBIENT || pname == GL_DIFFUSE || pname == GL_SPECULAR || pname == GL_POSITION || pname == GL_CONSTANT_ATTENUATION || pname == GL_LINEAR_ATTENUATION || pname == GL_QUADRATIC_ATTENUATION || pname == GL_SPOT_CUTOFF || pname == GL_SPOT_EXPONENT || pname == GL_SPOT_DIRECTION), GL_INVALID_ENUM);
  2883. auto& light_state = m_light_states[light - GL_LIGHT0];
  2884. auto const to_float_vector = [](GLfloat x, GLfloat y, GLfloat z, GLfloat w) {
  2885. return FloatVector4(x, y, z, w);
  2886. };
  2887. switch (pname) {
  2888. case GL_AMBIENT:
  2889. light_state.ambient_intensity = to_float_vector(params[0], params[1], params[2], params[3]);
  2890. break;
  2891. case GL_DIFFUSE:
  2892. light_state.diffuse_intensity = to_float_vector(params[0], params[1], params[2], params[3]);
  2893. break;
  2894. case GL_SPECULAR:
  2895. light_state.specular_intensity = to_float_vector(params[0], params[1], params[2], params[3]);
  2896. break;
  2897. case GL_POSITION:
  2898. light_state.position = to_float_vector(params[0], params[1], params[2], params[3]);
  2899. light_state.position = m_model_view_matrix * light_state.position;
  2900. break;
  2901. case GL_CONSTANT_ATTENUATION:
  2902. light_state.constant_attenuation = static_cast<float>(params[0]);
  2903. break;
  2904. case GL_LINEAR_ATTENUATION:
  2905. light_state.linear_attenuation = static_cast<float>(params[0]);
  2906. break;
  2907. case GL_QUADRATIC_ATTENUATION:
  2908. light_state.quadratic_attenuation = static_cast<float>(params[0]);
  2909. break;
  2910. case GL_SPOT_EXPONENT:
  2911. light_state.spotlight_exponent = static_cast<float>(params[0]);
  2912. break;
  2913. case GL_SPOT_CUTOFF:
  2914. light_state.spotlight_cutoff_angle = static_cast<float>(params[0]);
  2915. break;
  2916. case GL_SPOT_DIRECTION: {
  2917. FloatVector4 direction_vector = to_float_vector(params[0], params[1], params[2], 0.0f);
  2918. direction_vector = m_model_view_matrix * direction_vector;
  2919. light_state.spotlight_direction = direction_vector.xyz();
  2920. break;
  2921. }
  2922. default:
  2923. VERIFY_NOT_REACHED();
  2924. }
  2925. m_light_state_is_dirty = true;
  2926. }
  2927. void SoftwareGLContext::gl_materialf(GLenum face, GLenum pname, GLfloat param)
  2928. {
  2929. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_materialf, face, pname, param);
  2930. RETURN_WITH_ERROR_IF(!(face == GL_FRONT || face == GL_BACK || face == GL_FRONT_AND_BACK), GL_INVALID_ENUM);
  2931. RETURN_WITH_ERROR_IF(pname != GL_SHININESS, GL_INVALID_ENUM);
  2932. RETURN_WITH_ERROR_IF(param > 128.0f, GL_INVALID_VALUE);
  2933. switch (face) {
  2934. case GL_FRONT:
  2935. m_material_states[Face::Front].shininess = param;
  2936. break;
  2937. case GL_BACK:
  2938. m_material_states[Face::Back].shininess = param;
  2939. break;
  2940. case GL_FRONT_AND_BACK:
  2941. m_material_states[Face::Front].shininess = param;
  2942. m_material_states[Face::Back].shininess = param;
  2943. break;
  2944. default:
  2945. VERIFY_NOT_REACHED();
  2946. }
  2947. m_light_state_is_dirty = true;
  2948. }
  2949. void SoftwareGLContext::gl_materialfv(GLenum face, GLenum pname, GLfloat const* params)
  2950. {
  2951. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_materialfv, face, pname, params);
  2952. RETURN_WITH_ERROR_IF(!(face == GL_FRONT || face == GL_BACK || face == GL_FRONT_AND_BACK), GL_INVALID_ENUM);
  2953. RETURN_WITH_ERROR_IF(!(pname == GL_AMBIENT || pname == GL_DIFFUSE || pname == GL_SPECULAR || pname == GL_EMISSION || pname == GL_SHININESS || pname == GL_AMBIENT_AND_DIFFUSE), GL_INVALID_ENUM);
  2954. RETURN_WITH_ERROR_IF((pname == GL_SHININESS && *params > 128.0f), GL_INVALID_VALUE);
  2955. auto update_material = [](SoftGPU::Material& material, GLenum pname, GLfloat const* params) {
  2956. switch (pname) {
  2957. case GL_AMBIENT:
  2958. material.ambient = { params[0], params[1], params[2], params[3] };
  2959. break;
  2960. case GL_DIFFUSE:
  2961. material.diffuse = { params[0], params[1], params[2], params[3] };
  2962. break;
  2963. case GL_SPECULAR:
  2964. material.specular = { params[0], params[1], params[2], params[3] };
  2965. break;
  2966. case GL_EMISSION:
  2967. material.emissive = { params[0], params[1], params[2], params[3] };
  2968. break;
  2969. case GL_SHININESS:
  2970. material.shininess = *params;
  2971. break;
  2972. case GL_AMBIENT_AND_DIFFUSE:
  2973. material.ambient = { params[0], params[1], params[2], params[3] };
  2974. material.diffuse = { params[0], params[1], params[2], params[3] };
  2975. break;
  2976. }
  2977. };
  2978. switch (face) {
  2979. case GL_FRONT:
  2980. update_material(m_material_states[Face::Front], pname, params);
  2981. break;
  2982. case GL_BACK:
  2983. update_material(m_material_states[Face::Back], pname, params);
  2984. break;
  2985. case GL_FRONT_AND_BACK:
  2986. update_material(m_material_states[Face::Front], pname, params);
  2987. update_material(m_material_states[Face::Back], pname, params);
  2988. break;
  2989. }
  2990. m_light_state_is_dirty = true;
  2991. }
  2992. void SoftwareGLContext::gl_materialiv(GLenum face, GLenum pname, GLint const* params)
  2993. {
  2994. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_materialiv, face, pname, params);
  2995. RETURN_WITH_ERROR_IF(!(face == GL_FRONT || face == GL_BACK || face == GL_FRONT_AND_BACK), GL_INVALID_ENUM);
  2996. RETURN_WITH_ERROR_IF(!(pname == GL_AMBIENT || pname == GL_DIFFUSE || pname == GL_SPECULAR || pname == GL_EMISSION || pname == GL_SHININESS || pname == GL_AMBIENT_AND_DIFFUSE), GL_INVALID_ENUM);
  2997. RETURN_WITH_ERROR_IF((pname == GL_SHININESS && *params > 128), GL_INVALID_VALUE);
  2998. auto update_material = [](SoftGPU::Material& material, GLenum pname, GLint const* params) {
  2999. switch (pname) {
  3000. case GL_AMBIENT:
  3001. material.ambient = { static_cast<float>(params[0]), static_cast<float>(params[1]), static_cast<float>(params[2]), static_cast<float>(params[3]) };
  3002. break;
  3003. case GL_DIFFUSE:
  3004. material.diffuse = { static_cast<float>(params[0]), static_cast<float>(params[1]), static_cast<float>(params[2]), static_cast<float>(params[3]) };
  3005. break;
  3006. case GL_SPECULAR:
  3007. material.specular = { static_cast<float>(params[0]), static_cast<float>(params[1]), static_cast<float>(params[2]), static_cast<float>(params[3]) };
  3008. break;
  3009. case GL_EMISSION:
  3010. material.emissive = { static_cast<float>(params[0]), static_cast<float>(params[1]), static_cast<float>(params[2]), static_cast<float>(params[3]) };
  3011. break;
  3012. case GL_SHININESS:
  3013. material.shininess = static_cast<float>(params[0]);
  3014. break;
  3015. case GL_AMBIENT_AND_DIFFUSE:
  3016. material.ambient = { static_cast<float>(params[0]), static_cast<float>(params[1]), static_cast<float>(params[2]), static_cast<float>(params[3]) };
  3017. material.diffuse = { static_cast<float>(params[0]), static_cast<float>(params[1]), static_cast<float>(params[2]), static_cast<float>(params[3]) };
  3018. break;
  3019. }
  3020. };
  3021. switch (face) {
  3022. case GL_FRONT:
  3023. update_material(m_material_states[Face::Front], pname, params);
  3024. break;
  3025. case GL_BACK:
  3026. update_material(m_material_states[Face::Back], pname, params);
  3027. break;
  3028. case GL_FRONT_AND_BACK:
  3029. update_material(m_material_states[Face::Front], pname, params);
  3030. update_material(m_material_states[Face::Back], pname, params);
  3031. break;
  3032. }
  3033. m_light_state_is_dirty = true;
  3034. }
  3035. void SoftwareGLContext::gl_color_material(GLenum face, GLenum mode)
  3036. {
  3037. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_color_material, face, mode);
  3038. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  3039. RETURN_WITH_ERROR_IF(face != GL_FRONT
  3040. && face != GL_BACK
  3041. && face != GL_FRONT_AND_BACK,
  3042. GL_INVALID_ENUM);
  3043. RETURN_WITH_ERROR_IF(mode != GL_EMISSION
  3044. && mode != GL_AMBIENT
  3045. && mode != GL_DIFFUSE
  3046. && mode != GL_SPECULAR
  3047. && mode != GL_AMBIENT_AND_DIFFUSE,
  3048. GL_INVALID_ENUM);
  3049. m_color_material_face = face;
  3050. m_color_material_mode = mode;
  3051. m_light_state_is_dirty = true;
  3052. }
  3053. void SoftwareGLContext::gl_get_light(GLenum light, GLenum pname, void* params, GLenum type)
  3054. {
  3055. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_get_light, light, pname, params, type);
  3056. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  3057. RETURN_WITH_ERROR_IF(light < GL_LIGHT0 || light > GL_LIGHT0 + m_device_info.num_lights, GL_INVALID_ENUM);
  3058. RETURN_WITH_ERROR_IF(!(pname == GL_AMBIENT || pname == GL_DIFFUSE || pname == GL_SPECULAR || pname == GL_SPOT_DIRECTION || pname == GL_SPOT_EXPONENT || pname == GL_SPOT_CUTOFF || pname == GL_CONSTANT_ATTENUATION || pname == GL_LINEAR_ATTENUATION || pname == GL_QUADRATIC_ATTENUATION), GL_INVALID_ENUM);
  3059. if (type == GL_FLOAT)
  3060. get_light_param<GLfloat>(light, pname, static_cast<GLfloat*>(params));
  3061. else if (type == GL_INT)
  3062. get_light_param<GLint>(light, pname, static_cast<GLint*>(params));
  3063. else
  3064. VERIFY_NOT_REACHED();
  3065. }
  3066. template<typename T>
  3067. void SoftwareGLContext::get_light_param(GLenum light, GLenum pname, T* params)
  3068. {
  3069. auto const& light_state = m_light_states[light - GL_LIGHT0];
  3070. switch (pname) {
  3071. case GL_AMBIENT:
  3072. params[0] = light_state.ambient_intensity.x();
  3073. params[1] = light_state.ambient_intensity.y();
  3074. params[2] = light_state.ambient_intensity.z();
  3075. params[3] = light_state.ambient_intensity.w();
  3076. break;
  3077. case GL_DIFFUSE:
  3078. params[0] = light_state.diffuse_intensity.x();
  3079. params[1] = light_state.diffuse_intensity.y();
  3080. params[2] = light_state.diffuse_intensity.z();
  3081. params[3] = light_state.diffuse_intensity.w();
  3082. break;
  3083. case GL_SPECULAR:
  3084. params[0] = light_state.specular_intensity.x();
  3085. params[1] = light_state.specular_intensity.y();
  3086. params[2] = light_state.specular_intensity.z();
  3087. params[3] = light_state.specular_intensity.w();
  3088. break;
  3089. case GL_SPOT_DIRECTION:
  3090. params[0] = light_state.spotlight_direction.x();
  3091. params[1] = light_state.spotlight_direction.y();
  3092. params[2] = light_state.spotlight_direction.z();
  3093. break;
  3094. case GL_SPOT_EXPONENT:
  3095. *params = light_state.spotlight_exponent;
  3096. break;
  3097. case GL_SPOT_CUTOFF:
  3098. *params = light_state.spotlight_cutoff_angle;
  3099. break;
  3100. case GL_CONSTANT_ATTENUATION:
  3101. *params = light_state.constant_attenuation;
  3102. break;
  3103. case GL_LINEAR_ATTENUATION:
  3104. *params = light_state.linear_attenuation;
  3105. break;
  3106. case GL_QUADRATIC_ATTENUATION:
  3107. *params = light_state.quadratic_attenuation;
  3108. break;
  3109. }
  3110. }
  3111. void SoftwareGLContext::gl_get_material(GLenum face, GLenum pname, void* params, GLenum type)
  3112. {
  3113. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_get_material, face, pname, params, type);
  3114. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  3115. RETURN_WITH_ERROR_IF(!(pname == GL_AMBIENT || pname == GL_DIFFUSE || pname == GL_SPECULAR || pname == GL_EMISSION), GL_INVALID_ENUM);
  3116. RETURN_WITH_ERROR_IF(!(face == GL_FRONT || face == GL_BACK), GL_INVALID_ENUM);
  3117. Face material_face = Front;
  3118. switch (face) {
  3119. case GL_FRONT:
  3120. material_face = Front;
  3121. break;
  3122. case GL_BACK:
  3123. material_face = Back;
  3124. break;
  3125. }
  3126. if (type == GL_FLOAT)
  3127. get_material_param<GLfloat>(material_face, pname, static_cast<GLfloat*>(params));
  3128. else if (type == GL_INT)
  3129. get_material_param<GLint>(material_face, pname, static_cast<GLint*>(params));
  3130. else
  3131. VERIFY_NOT_REACHED();
  3132. }
  3133. template<typename T>
  3134. void SoftwareGLContext::get_material_param(Face face, GLenum pname, T* params)
  3135. {
  3136. auto const& material = m_material_states[face];
  3137. switch (pname) {
  3138. case GL_AMBIENT:
  3139. params[0] = static_cast<T>(material.ambient.x());
  3140. params[1] = static_cast<T>(material.ambient.y());
  3141. params[2] = static_cast<T>(material.ambient.z());
  3142. params[3] = static_cast<T>(material.ambient.w());
  3143. break;
  3144. case GL_DIFFUSE:
  3145. params[0] = static_cast<T>(material.diffuse.x());
  3146. params[1] = static_cast<T>(material.diffuse.y());
  3147. params[2] = static_cast<T>(material.diffuse.z());
  3148. params[3] = static_cast<T>(material.diffuse.w());
  3149. break;
  3150. case GL_SPECULAR:
  3151. params[0] = static_cast<T>(material.specular.x());
  3152. params[1] = static_cast<T>(material.specular.y());
  3153. params[2] = static_cast<T>(material.specular.z());
  3154. params[3] = static_cast<T>(material.specular.w());
  3155. break;
  3156. case GL_EMISSION:
  3157. params[0] = static_cast<T>(material.emissive.x());
  3158. params[1] = static_cast<T>(material.emissive.y());
  3159. params[2] = static_cast<T>(material.emissive.z());
  3160. params[3] = static_cast<T>(material.emissive.w());
  3161. break;
  3162. case GL_SHININESS:
  3163. *params = material.shininess;
  3164. break;
  3165. }
  3166. }
  3167. }