SoftwareGLContext.cpp 130 KB

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