SoftwareGLContext.cpp 106 KB

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