SoftwareGLContext.cpp 82 KB

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