SoftwareGLContext.cpp 127 KB

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