SoftwareGLContext.cpp 129 KB

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