SoftwareGLContext.cpp 76 KB

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