SoftwareGLContext.cpp 135 KB

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