SoftwareGLContext.cpp 141 KB

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