SoftwareGLContext.cpp 131 KB

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