SoftwareGLContext.cpp 128 KB

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