SoftwareGLContext.cpp 128 KB

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