SoftwareGLContext.cpp 95 KB

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