SoftwareGLContext.cpp 132 KB

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