SoftwareGLContext.cpp 121 KB

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