SoftwareGLContext.cpp 62 KB

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