SoftwareGLContext.cpp 111 KB

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