SoftwareGLContext.cpp 89 KB

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