SoftwareGLContext.cpp 94 KB

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