SoftwareGLContext.cpp 140 KB

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