SoftwareGLContext.cpp 140 KB

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