SoftwareGLContext.cpp 111 KB

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