SoftwareGLContext.cpp 73 KB

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