GLContext.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  1. /*
  2. * Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
  3. * Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
  4. * Copyright (c) 2022, Jelle Raaijmakers <jelle@gmta.nl>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/Assertions.h>
  9. #include <AK/Debug.h>
  10. #include <AK/Format.h>
  11. #include <AK/Vector.h>
  12. #include <LibGL/GLContext.h>
  13. #include <LibGPU/Device.h>
  14. #include <LibGPU/Enums.h>
  15. #include <LibGPU/ImageFormat.h>
  16. #include <LibGfx/Bitmap.h>
  17. #include <LibGfx/Vector3.h>
  18. __attribute__((visibility("hidden"))) GL::GLContext* g_gl_context;
  19. namespace GL {
  20. GLContext::GLContext(RefPtr<GPU::Driver> driver, NonnullOwnPtr<GPU::Device> device, Gfx::Bitmap& frontbuffer)
  21. : m_viewport { frontbuffer.rect() }
  22. , m_frontbuffer { frontbuffer }
  23. , m_driver { driver }
  24. , m_rasterizer { move(device) }
  25. , m_device_info { m_rasterizer->info() }
  26. {
  27. m_texture_units.resize(m_device_info.num_texture_units);
  28. m_active_texture_unit = &m_texture_units[0];
  29. // All texture units are initialized with default textures for all targets; these
  30. // can be referenced later on with texture name 0 in operations like glBindTexture().
  31. auto default_texture_2d = adopt_ref(*new Texture2D());
  32. m_default_textures.set(GL_TEXTURE_2D, default_texture_2d);
  33. for (auto& texture_unit : m_texture_units)
  34. texture_unit.set_texture_2d_target_texture(default_texture_2d);
  35. // Query the number lights from the device and set set up their state
  36. // locally in the GL
  37. m_light_states.resize(m_device_info.num_lights);
  38. // Set-up light0's state, as it has a different default state
  39. // to the other lights, as per the OpenGL 1.5 spec
  40. auto& light0 = m_light_states.at(0);
  41. light0.diffuse_intensity = { 1.0f, 1.0f, 1.0f, 1.0f };
  42. light0.specular_intensity = { 1.0f, 1.0f, 1.0f, 1.0f };
  43. m_light_state_is_dirty = true;
  44. m_client_side_texture_coord_array_enabled.resize(m_device_info.num_texture_units);
  45. m_client_tex_coord_pointer.resize(m_device_info.num_texture_units);
  46. m_current_vertex_tex_coord.resize(m_device_info.num_texture_units);
  47. for (auto& tex_coord : m_current_vertex_tex_coord)
  48. tex_coord = { 0.0f, 0.0f, 0.0f, 1.0f };
  49. // Initialize the texture coordinate generation coefficients
  50. // Indices 0,1,2,3 refer to the S,T,R and Q coordinate of the respective texture
  51. // coordinate generation config.
  52. m_texture_coordinate_generation.resize(m_device_info.num_texture_units);
  53. for (auto& texture_coordinate_generation : m_texture_coordinate_generation) {
  54. texture_coordinate_generation[0].object_plane_coefficients = { 1.0f, 0.0f, 0.0f, 0.0f };
  55. texture_coordinate_generation[0].eye_plane_coefficients = { 1.0f, 0.0f, 0.0f, 0.0f };
  56. texture_coordinate_generation[1].object_plane_coefficients = { 0.0f, 1.0f, 0.0f, 0.0f };
  57. texture_coordinate_generation[1].eye_plane_coefficients = { 0.0f, 1.0f, 0.0f, 0.0f };
  58. texture_coordinate_generation[2].object_plane_coefficients = { 0.0f, 0.0f, 0.0f, 0.0f };
  59. texture_coordinate_generation[2].eye_plane_coefficients = { 0.0f, 0.0f, 0.0f, 0.0f };
  60. texture_coordinate_generation[3].object_plane_coefficients = { 0.0f, 0.0f, 0.0f, 0.0f };
  61. texture_coordinate_generation[3].eye_plane_coefficients = { 0.0f, 0.0f, 0.0f, 0.0f };
  62. }
  63. build_extension_string();
  64. }
  65. GLContext::~GLContext()
  66. {
  67. dbgln_if(GL_DEBUG, "GLContext::~GLContext() {:p}", this);
  68. if (g_gl_context == this)
  69. make_context_current(nullptr);
  70. }
  71. void GLContext::gl_begin(GLenum mode)
  72. {
  73. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_begin, mode);
  74. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  75. RETURN_WITH_ERROR_IF(mode > GL_POLYGON, GL_INVALID_ENUM);
  76. m_current_draw_mode = mode;
  77. m_in_draw_state = true; // Certain commands will now generate an error
  78. }
  79. void GLContext::gl_clear(GLbitfield mask)
  80. {
  81. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_clear, mask);
  82. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  83. RETURN_WITH_ERROR_IF(mask & ~(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT), GL_INVALID_ENUM);
  84. if (mask & GL_COLOR_BUFFER_BIT)
  85. m_rasterizer->clear_color(m_clear_color);
  86. if (mask & GL_DEPTH_BUFFER_BIT)
  87. m_rasterizer->clear_depth(m_clear_depth);
  88. if (mask & GL_STENCIL_BUFFER_BIT)
  89. m_rasterizer->clear_stencil(m_clear_stencil);
  90. }
  91. void GLContext::gl_clear_color(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
  92. {
  93. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_clear_color, red, green, blue, alpha);
  94. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  95. m_clear_color = { red, green, blue, alpha };
  96. m_clear_color.clamp(0.f, 1.f);
  97. }
  98. void GLContext::gl_clear_depth(GLdouble depth)
  99. {
  100. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_clear_depth, depth);
  101. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  102. m_clear_depth = clamp(static_cast<float>(depth), 0.f, 1.f);
  103. }
  104. void GLContext::gl_end()
  105. {
  106. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_end);
  107. // Make sure we had a `glBegin` before this call...
  108. RETURN_WITH_ERROR_IF(!m_in_draw_state, GL_INVALID_OPERATION);
  109. m_in_draw_state = false;
  110. Vector<size_t, 32> enabled_texture_units;
  111. for (size_t i = 0; i < m_texture_units.size(); ++i) {
  112. if (m_texture_units[i].texture_2d_enabled())
  113. enabled_texture_units.append(i);
  114. }
  115. sync_device_config();
  116. GPU::PrimitiveType primitive_type;
  117. switch (m_current_draw_mode) {
  118. case GL_LINE_LOOP:
  119. primitive_type = GPU::PrimitiveType::LineLoop;
  120. break;
  121. case GL_LINE_STRIP:
  122. primitive_type = GPU::PrimitiveType::LineStrip;
  123. break;
  124. case GL_LINES:
  125. primitive_type = GPU::PrimitiveType::Lines;
  126. break;
  127. case GL_POINTS:
  128. primitive_type = GPU::PrimitiveType::Points;
  129. break;
  130. case GL_TRIANGLES:
  131. primitive_type = GPU::PrimitiveType::Triangles;
  132. break;
  133. case GL_TRIANGLE_STRIP:
  134. case GL_QUAD_STRIP:
  135. primitive_type = GPU::PrimitiveType::TriangleStrip;
  136. break;
  137. case GL_TRIANGLE_FAN:
  138. case GL_POLYGON:
  139. primitive_type = GPU::PrimitiveType::TriangleFan;
  140. break;
  141. case GL_QUADS:
  142. primitive_type = GPU::PrimitiveType::Quads;
  143. break;
  144. default:
  145. VERIFY_NOT_REACHED();
  146. }
  147. m_rasterizer->draw_primitives(primitive_type, m_model_view_matrix, m_projection_matrix, m_texture_matrix, m_vertex_list, enabled_texture_units);
  148. m_vertex_list.clear_with_capacity();
  149. }
  150. GLenum GLContext::gl_get_error()
  151. {
  152. if (m_in_draw_state)
  153. return GL_INVALID_OPERATION;
  154. auto last_error = m_error;
  155. m_error = GL_NO_ERROR;
  156. return last_error;
  157. }
  158. GLubyte* GLContext::gl_get_string(GLenum name)
  159. {
  160. RETURN_VALUE_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION, nullptr);
  161. switch (name) {
  162. case GL_VENDOR:
  163. return reinterpret_cast<GLubyte*>(const_cast<char*>(m_device_info.vendor_name.characters()));
  164. case GL_RENDERER:
  165. return reinterpret_cast<GLubyte*>(const_cast<char*>(m_device_info.device_name.characters()));
  166. case GL_VERSION:
  167. return reinterpret_cast<GLubyte*>(const_cast<char*>("1.5"));
  168. case GL_EXTENSIONS:
  169. return reinterpret_cast<GLubyte*>(const_cast<char*>(m_extensions.characters()));
  170. case GL_SHADING_LANGUAGE_VERSION:
  171. return reinterpret_cast<GLubyte*>(const_cast<char*>("0.0"));
  172. default:
  173. dbgln_if(GL_DEBUG, "gl_get_string({:#x}): unknown name", name);
  174. break;
  175. }
  176. RETURN_VALUE_WITH_ERROR_IF(true, GL_INVALID_ENUM, nullptr);
  177. }
  178. void GLContext::gl_viewport(GLint x, GLint y, GLsizei width, GLsizei height)
  179. {
  180. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_viewport, x, y, width, height);
  181. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  182. RETURN_WITH_ERROR_IF(width < 0 || height < 0, GL_INVALID_VALUE);
  183. m_viewport = { x, y, width, height };
  184. auto rasterizer_options = m_rasterizer->options();
  185. rasterizer_options.viewport = m_viewport;
  186. m_rasterizer->set_options(rasterizer_options);
  187. }
  188. void GLContext::gl_front_face(GLenum face)
  189. {
  190. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_front_face, face);
  191. RETURN_WITH_ERROR_IF(face < GL_CW || face > GL_CCW, GL_INVALID_ENUM);
  192. m_front_face = face;
  193. auto rasterizer_options = m_rasterizer->options();
  194. rasterizer_options.front_face = (face == GL_CW) ? GPU::WindingOrder::Clockwise : GPU::WindingOrder::CounterClockwise;
  195. m_rasterizer->set_options(rasterizer_options);
  196. }
  197. void GLContext::gl_cull_face(GLenum cull_mode)
  198. {
  199. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_cull_face, cull_mode);
  200. RETURN_WITH_ERROR_IF(cull_mode != GL_FRONT && cull_mode != GL_BACK && cull_mode != GL_FRONT_AND_BACK, GL_INVALID_ENUM);
  201. m_culled_sides = cull_mode;
  202. auto rasterizer_options = m_rasterizer->options();
  203. rasterizer_options.cull_back = cull_mode == GL_BACK || cull_mode == GL_FRONT_AND_BACK;
  204. rasterizer_options.cull_front = cull_mode == GL_FRONT || cull_mode == GL_FRONT_AND_BACK;
  205. m_rasterizer->set_options(rasterizer_options);
  206. }
  207. void GLContext::gl_flush()
  208. {
  209. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  210. // No-op since GLContext is completely synchronous at the moment
  211. }
  212. void GLContext::gl_finish()
  213. {
  214. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  215. // No-op since GLContext is completely synchronous at the moment
  216. }
  217. void GLContext::gl_blend_func(GLenum src_factor, GLenum dst_factor)
  218. {
  219. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_blend_func, src_factor, dst_factor);
  220. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  221. // FIXME: The list of allowed enums differs between API versions
  222. // This was taken from the 2.0 spec on https://docs.gl/gl2/glBlendFunc
  223. RETURN_WITH_ERROR_IF(!(src_factor == GL_ZERO
  224. || src_factor == GL_ONE
  225. || src_factor == GL_SRC_COLOR
  226. || src_factor == GL_ONE_MINUS_SRC_COLOR
  227. || src_factor == GL_DST_COLOR
  228. || src_factor == GL_ONE_MINUS_DST_COLOR
  229. || src_factor == GL_SRC_ALPHA
  230. || src_factor == GL_ONE_MINUS_SRC_ALPHA
  231. || src_factor == GL_DST_ALPHA
  232. || src_factor == GL_ONE_MINUS_DST_ALPHA
  233. || src_factor == GL_CONSTANT_COLOR
  234. || src_factor == GL_ONE_MINUS_CONSTANT_COLOR
  235. || src_factor == GL_CONSTANT_ALPHA
  236. || src_factor == GL_ONE_MINUS_CONSTANT_ALPHA
  237. || src_factor == GL_SRC_ALPHA_SATURATE),
  238. GL_INVALID_ENUM);
  239. RETURN_WITH_ERROR_IF(!(dst_factor == GL_ZERO
  240. || dst_factor == GL_ONE
  241. || dst_factor == GL_SRC_COLOR
  242. || dst_factor == GL_ONE_MINUS_SRC_COLOR
  243. || dst_factor == GL_DST_COLOR
  244. || dst_factor == GL_ONE_MINUS_DST_COLOR
  245. || dst_factor == GL_SRC_ALPHA
  246. || dst_factor == GL_ONE_MINUS_SRC_ALPHA
  247. || dst_factor == GL_DST_ALPHA
  248. || dst_factor == GL_ONE_MINUS_DST_ALPHA
  249. || dst_factor == GL_CONSTANT_COLOR
  250. || dst_factor == GL_ONE_MINUS_CONSTANT_COLOR
  251. || dst_factor == GL_CONSTANT_ALPHA
  252. || dst_factor == GL_ONE_MINUS_CONSTANT_ALPHA),
  253. GL_INVALID_ENUM);
  254. m_blend_source_factor = src_factor;
  255. m_blend_destination_factor = dst_factor;
  256. auto map_gl_blend_factor_to_device = [](GLenum factor) constexpr
  257. {
  258. switch (factor) {
  259. case GL_ZERO:
  260. return GPU::BlendFactor::Zero;
  261. case GL_ONE:
  262. return GPU::BlendFactor::One;
  263. case GL_SRC_ALPHA:
  264. return GPU::BlendFactor::SrcAlpha;
  265. case GL_ONE_MINUS_SRC_ALPHA:
  266. return GPU::BlendFactor::OneMinusSrcAlpha;
  267. case GL_SRC_COLOR:
  268. return GPU::BlendFactor::SrcColor;
  269. case GL_ONE_MINUS_SRC_COLOR:
  270. return GPU::BlendFactor::OneMinusSrcColor;
  271. case GL_DST_ALPHA:
  272. return GPU::BlendFactor::DstAlpha;
  273. case GL_ONE_MINUS_DST_ALPHA:
  274. return GPU::BlendFactor::OneMinusDstAlpha;
  275. case GL_DST_COLOR:
  276. return GPU::BlendFactor::DstColor;
  277. case GL_ONE_MINUS_DST_COLOR:
  278. return GPU::BlendFactor::OneMinusDstColor;
  279. case GL_SRC_ALPHA_SATURATE:
  280. return GPU::BlendFactor::SrcAlphaSaturate;
  281. default:
  282. VERIFY_NOT_REACHED();
  283. }
  284. };
  285. auto options = m_rasterizer->options();
  286. options.blend_source_factor = map_gl_blend_factor_to_device(m_blend_source_factor);
  287. options.blend_destination_factor = map_gl_blend_factor_to_device(m_blend_destination_factor);
  288. m_rasterizer->set_options(options);
  289. }
  290. void GLContext::gl_alpha_func(GLenum func, GLclampf ref)
  291. {
  292. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_alpha_func, func, ref);
  293. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  294. RETURN_WITH_ERROR_IF(func < GL_NEVER || func > GL_ALWAYS, GL_INVALID_ENUM);
  295. m_alpha_test_func = func;
  296. m_alpha_test_ref_value = ref;
  297. auto options = m_rasterizer->options();
  298. switch (func) {
  299. case GL_NEVER:
  300. options.alpha_test_func = GPU::AlphaTestFunction::Never;
  301. break;
  302. case GL_ALWAYS:
  303. options.alpha_test_func = GPU::AlphaTestFunction::Always;
  304. break;
  305. case GL_LESS:
  306. options.alpha_test_func = GPU::AlphaTestFunction::Less;
  307. break;
  308. case GL_LEQUAL:
  309. options.alpha_test_func = GPU::AlphaTestFunction::LessOrEqual;
  310. break;
  311. case GL_EQUAL:
  312. options.alpha_test_func = GPU::AlphaTestFunction::Equal;
  313. break;
  314. case GL_NOTEQUAL:
  315. options.alpha_test_func = GPU::AlphaTestFunction::NotEqual;
  316. break;
  317. case GL_GEQUAL:
  318. options.alpha_test_func = GPU::AlphaTestFunction::GreaterOrEqual;
  319. break;
  320. case GL_GREATER:
  321. options.alpha_test_func = GPU::AlphaTestFunction::Greater;
  322. break;
  323. default:
  324. VERIFY_NOT_REACHED();
  325. }
  326. options.alpha_test_ref_value = m_alpha_test_ref_value;
  327. m_rasterizer->set_options(options);
  328. }
  329. void GLContext::gl_hint(GLenum target, GLenum mode)
  330. {
  331. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_hint, target, mode);
  332. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  333. RETURN_WITH_ERROR_IF(target != GL_PERSPECTIVE_CORRECTION_HINT
  334. && target != GL_POINT_SMOOTH_HINT
  335. && target != GL_LINE_SMOOTH_HINT
  336. && target != GL_POLYGON_SMOOTH_HINT
  337. && target != GL_FOG_HINT
  338. && target != GL_GENERATE_MIPMAP_HINT
  339. && target != GL_TEXTURE_COMPRESSION_HINT,
  340. GL_INVALID_ENUM);
  341. RETURN_WITH_ERROR_IF(mode != GL_DONT_CARE
  342. && mode != GL_FASTEST
  343. && mode != GL_NICEST,
  344. GL_INVALID_ENUM);
  345. // According to the spec implementors are free to ignore glHint. So we do.
  346. }
  347. void GLContext::gl_read_buffer(GLenum mode)
  348. {
  349. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_read_buffer, mode);
  350. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  351. // FIXME: Also allow aux buffers GL_AUX0 through GL_AUX3 here
  352. // plus any aux buffer between 0 and GL_AUX_BUFFERS
  353. RETURN_WITH_ERROR_IF(mode != GL_FRONT_LEFT
  354. && mode != GL_FRONT_RIGHT
  355. && mode != GL_BACK_LEFT
  356. && mode != GL_BACK_RIGHT
  357. && mode != GL_FRONT
  358. && mode != GL_BACK
  359. && mode != GL_LEFT
  360. && mode != GL_RIGHT,
  361. GL_INVALID_ENUM);
  362. // FIXME: We do not currently have aux buffers, so make it an invalid
  363. // operation to select anything but front or back buffers. Also we do
  364. // not allow selecting the stereoscopic RIGHT buffers since we do not
  365. // have them configured.
  366. RETURN_WITH_ERROR_IF(mode != GL_FRONT_LEFT
  367. && mode != GL_FRONT
  368. && mode != GL_BACK_LEFT
  369. && mode != GL_BACK
  370. && mode != GL_FRONT
  371. && mode != GL_BACK
  372. && mode != GL_LEFT,
  373. GL_INVALID_OPERATION);
  374. m_current_read_buffer = mode;
  375. }
  376. void GLContext::gl_draw_buffer(GLenum buffer)
  377. {
  378. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_draw_buffer, buffer);
  379. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  380. // FIXME: Also allow aux buffers GL_AUX0 through GL_AUX3 here
  381. // plus any aux buffer between 0 and GL_AUX_BUFFERS
  382. RETURN_WITH_ERROR_IF(buffer != GL_NONE
  383. && buffer != GL_FRONT_LEFT
  384. && buffer != GL_FRONT_RIGHT
  385. && buffer != GL_BACK_LEFT
  386. && buffer != GL_BACK_RIGHT
  387. && buffer != GL_FRONT
  388. && buffer != GL_BACK
  389. && buffer != GL_LEFT
  390. && buffer != GL_RIGHT,
  391. GL_INVALID_ENUM);
  392. // FIXME: We do not currently have aux buffers, so make it an invalid
  393. // operation to select anything but front or back buffers. Also we do
  394. // not allow selecting the stereoscopic RIGHT buffers since we do not
  395. // have them configured.
  396. RETURN_WITH_ERROR_IF(buffer != GL_NONE
  397. && buffer != GL_FRONT_LEFT
  398. && buffer != GL_FRONT
  399. && buffer != GL_BACK_LEFT
  400. && buffer != GL_BACK
  401. && buffer != GL_FRONT
  402. && buffer != GL_BACK
  403. && buffer != GL_LEFT,
  404. GL_INVALID_OPERATION);
  405. m_current_draw_buffer = buffer;
  406. auto rasterizer_options = m_rasterizer->options();
  407. // FIXME: We only have a single draw buffer in SoftGPU at the moment,
  408. // so we simply disable color writes if GL_NONE is selected
  409. rasterizer_options.enable_color_write = m_current_draw_buffer != GL_NONE;
  410. m_rasterizer->set_options(rasterizer_options);
  411. }
  412. void GLContext::gl_read_pixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid* pixels)
  413. {
  414. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  415. RETURN_WITH_ERROR_IF(width < 0 || height < 0, GL_INVALID_VALUE);
  416. RETURN_WITH_ERROR_IF(format != GL_COLOR_INDEX
  417. && format != GL_STENCIL_INDEX
  418. && format != GL_DEPTH_COMPONENT
  419. && format != GL_RED
  420. && format != GL_GREEN
  421. && format != GL_BLUE
  422. && format != GL_ALPHA
  423. && format != GL_RGB
  424. && format != GL_RGBA
  425. && format != GL_LUMINANCE
  426. && format != GL_LUMINANCE_ALPHA,
  427. GL_INVALID_ENUM);
  428. RETURN_WITH_ERROR_IF(type != GL_UNSIGNED_BYTE
  429. && type != GL_BYTE
  430. && type != GL_BITMAP
  431. && type != GL_UNSIGNED_SHORT
  432. && type != GL_SHORT
  433. && type != GL_BLUE
  434. && type != GL_UNSIGNED_INT
  435. && type != GL_INT
  436. && type != GL_FLOAT,
  437. GL_INVALID_ENUM);
  438. // FIXME: We only support RGBA buffers for now.
  439. // Once we add support for indexed color modes do the correct check here
  440. RETURN_WITH_ERROR_IF(format == GL_COLOR_INDEX, GL_INVALID_OPERATION);
  441. // FIXME: We do not have stencil buffers yet
  442. // Once we add support for stencil buffers do the correct check here
  443. RETURN_WITH_ERROR_IF(format == GL_STENCIL_INDEX, GL_INVALID_OPERATION);
  444. if (format == GL_DEPTH_COMPONENT) {
  445. // FIXME: This check needs to be a bit more sophisticated. Currently the buffers
  446. // are hardcoded. Once we add proper structures for them we need to correct this check
  447. // Error because only back buffer has a depth buffer
  448. RETURN_WITH_ERROR_IF(m_current_read_buffer == GL_FRONT
  449. || m_current_read_buffer == GL_FRONT_LEFT
  450. || m_current_read_buffer == GL_FRONT_RIGHT,
  451. GL_INVALID_OPERATION);
  452. }
  453. // Some helper functions for converting float values to integer types
  454. auto float_to_i8 = [](float f) -> GLchar {
  455. return static_cast<GLchar>((0x7f * min(max(f, 0.0f), 1.0f) - 1) / 2);
  456. };
  457. auto float_to_i16 = [](float f) -> GLshort {
  458. return static_cast<GLshort>((0x7fff * min(max(f, 0.0f), 1.0f) - 1) / 2);
  459. };
  460. auto float_to_i32 = [](float f) -> GLint {
  461. return static_cast<GLint>((0x7fffffff * min(max(f, 0.0f), 1.0f) - 1) / 2);
  462. };
  463. auto float_to_u8 = [](float f) -> GLubyte {
  464. return static_cast<GLubyte>(0xff * min(max(f, 0.0f), 1.0f));
  465. };
  466. auto float_to_u16 = [](float f) -> GLushort {
  467. return static_cast<GLushort>(0xffff * min(max(f, 0.0f), 1.0f));
  468. };
  469. auto float_to_u32 = [](float f) -> GLuint {
  470. return static_cast<GLuint>(0xffffffff * min(max(f, 0.0f), 1.0f));
  471. };
  472. u8 component_size = 0;
  473. switch (type) {
  474. case GL_BYTE:
  475. case GL_UNSIGNED_BYTE:
  476. component_size = 1;
  477. break;
  478. case GL_SHORT:
  479. case GL_UNSIGNED_SHORT:
  480. component_size = 2;
  481. break;
  482. case GL_INT:
  483. case GL_UNSIGNED_INT:
  484. case GL_FLOAT:
  485. component_size = 4;
  486. break;
  487. }
  488. if (format == GL_DEPTH_COMPONENT) {
  489. auto const row_stride = (width * component_size + m_pack_alignment - 1) / m_pack_alignment * m_pack_alignment;
  490. // Read from depth buffer
  491. for (GLsizei i = 0; i < height; ++i) {
  492. for (GLsizei j = 0; j < width; ++j) {
  493. float depth = m_rasterizer->get_depthbuffer_value(x + j, y + i);
  494. auto char_ptr = reinterpret_cast<char*>(pixels) + i * row_stride + j * component_size;
  495. switch (type) {
  496. case GL_BYTE:
  497. *reinterpret_cast<GLchar*>(char_ptr) = float_to_i8(depth);
  498. break;
  499. case GL_SHORT:
  500. *reinterpret_cast<GLshort*>(char_ptr) = float_to_i16(depth);
  501. break;
  502. case GL_INT:
  503. *reinterpret_cast<GLint*>(char_ptr) = float_to_i32(depth);
  504. break;
  505. case GL_UNSIGNED_BYTE:
  506. *reinterpret_cast<GLubyte*>(char_ptr) = float_to_u8(depth);
  507. break;
  508. case GL_UNSIGNED_SHORT:
  509. *reinterpret_cast<GLushort*>(char_ptr) = float_to_u16(depth);
  510. break;
  511. case GL_UNSIGNED_INT:
  512. *reinterpret_cast<GLuint*>(char_ptr) = float_to_u32(depth);
  513. break;
  514. case GL_FLOAT:
  515. *reinterpret_cast<GLfloat*>(char_ptr) = min(max(depth, 0.0f), 1.0f);
  516. break;
  517. }
  518. }
  519. }
  520. return;
  521. }
  522. bool write_red = false;
  523. bool write_green = false;
  524. bool write_blue = false;
  525. bool write_alpha = false;
  526. size_t component_count = 0;
  527. size_t red_offset = 0;
  528. size_t green_offset = 0;
  529. size_t blue_offset = 0;
  530. size_t alpha_offset = 0;
  531. char* red_ptr = nullptr;
  532. char* green_ptr = nullptr;
  533. char* blue_ptr = nullptr;
  534. char* alpha_ptr = nullptr;
  535. switch (format) {
  536. case GL_RGB:
  537. write_red = true;
  538. write_green = true;
  539. write_blue = true;
  540. component_count = 3;
  541. red_offset = 2;
  542. green_offset = 1;
  543. blue_offset = 0;
  544. break;
  545. case GL_RGBA:
  546. write_red = true;
  547. write_green = true;
  548. write_blue = true;
  549. write_alpha = true;
  550. component_count = 4;
  551. red_offset = 3;
  552. green_offset = 2;
  553. blue_offset = 1;
  554. alpha_offset = 0;
  555. break;
  556. case GL_RED:
  557. write_red = true;
  558. component_count = 1;
  559. red_offset = 0;
  560. break;
  561. case GL_GREEN:
  562. write_green = true;
  563. component_count = 1;
  564. green_offset = 0;
  565. break;
  566. case GL_BLUE:
  567. write_blue = true;
  568. component_count = 1;
  569. blue_offset = 0;
  570. break;
  571. case GL_ALPHA:
  572. write_alpha = true;
  573. component_count = 1;
  574. alpha_offset = 0;
  575. break;
  576. }
  577. auto const pixel_bytes = component_size * component_count;
  578. auto const row_alignment_bytes = (m_pack_alignment - ((width * pixel_bytes) % m_pack_alignment)) % m_pack_alignment;
  579. char* out_ptr = reinterpret_cast<char*>(pixels);
  580. for (int i = 0; i < (int)height; ++i) {
  581. for (int j = 0; j < (int)width; ++j) {
  582. Gfx::ARGB32 color {};
  583. if (m_current_read_buffer == GL_FRONT || m_current_read_buffer == GL_LEFT || m_current_read_buffer == GL_FRONT_LEFT) {
  584. if (y + i >= m_frontbuffer->width() || x + j >= m_frontbuffer->height())
  585. color = 0;
  586. else
  587. color = m_frontbuffer->scanline(y + i)[x + j];
  588. } else {
  589. color = m_rasterizer->get_color_buffer_pixel(x + j, y + i);
  590. }
  591. float red = ((color >> 24) & 0xff) / 255.0f;
  592. float green = ((color >> 16) & 0xff) / 255.0f;
  593. float blue = ((color >> 8) & 0xff) / 255.0f;
  594. float alpha = (color & 0xff) / 255.0f;
  595. // FIXME: Set up write pointers based on selected endianness (glPixelStore)
  596. red_ptr = out_ptr + (component_size * red_offset);
  597. green_ptr = out_ptr + (component_size * green_offset);
  598. blue_ptr = out_ptr + (component_size * blue_offset);
  599. alpha_ptr = out_ptr + (component_size * alpha_offset);
  600. switch (type) {
  601. case GL_BYTE:
  602. if (write_red)
  603. *reinterpret_cast<GLchar*>(red_ptr) = float_to_i8(red);
  604. if (write_green)
  605. *reinterpret_cast<GLchar*>(green_ptr) = float_to_i8(green);
  606. if (write_blue)
  607. *reinterpret_cast<GLchar*>(blue_ptr) = float_to_i8(blue);
  608. if (write_alpha)
  609. *reinterpret_cast<GLchar*>(alpha_ptr) = float_to_i8(alpha);
  610. break;
  611. case GL_UNSIGNED_BYTE:
  612. if (write_red)
  613. *reinterpret_cast<GLubyte*>(red_ptr) = float_to_u8(red);
  614. if (write_green)
  615. *reinterpret_cast<GLubyte*>(green_ptr) = float_to_u8(green);
  616. if (write_blue)
  617. *reinterpret_cast<GLubyte*>(blue_ptr) = float_to_u8(blue);
  618. if (write_alpha)
  619. *reinterpret_cast<GLubyte*>(alpha_ptr) = float_to_u8(alpha);
  620. break;
  621. case GL_SHORT:
  622. if (write_red)
  623. *reinterpret_cast<GLshort*>(red_ptr) = float_to_i16(red);
  624. if (write_green)
  625. *reinterpret_cast<GLshort*>(green_ptr) = float_to_i16(green);
  626. if (write_blue)
  627. *reinterpret_cast<GLshort*>(blue_ptr) = float_to_i16(blue);
  628. if (write_alpha)
  629. *reinterpret_cast<GLshort*>(alpha_ptr) = float_to_i16(alpha);
  630. break;
  631. case GL_UNSIGNED_SHORT:
  632. if (write_red)
  633. *reinterpret_cast<GLushort*>(red_ptr) = float_to_u16(red);
  634. if (write_green)
  635. *reinterpret_cast<GLushort*>(green_ptr) = float_to_u16(green);
  636. if (write_blue)
  637. *reinterpret_cast<GLushort*>(blue_ptr) = float_to_u16(blue);
  638. if (write_alpha)
  639. *reinterpret_cast<GLushort*>(alpha_ptr) = float_to_u16(alpha);
  640. break;
  641. case GL_INT:
  642. if (write_red)
  643. *reinterpret_cast<GLint*>(red_ptr) = float_to_i32(red);
  644. if (write_green)
  645. *reinterpret_cast<GLint*>(green_ptr) = float_to_i32(green);
  646. if (write_blue)
  647. *reinterpret_cast<GLint*>(blue_ptr) = float_to_i32(blue);
  648. if (write_alpha)
  649. *reinterpret_cast<GLint*>(alpha_ptr) = float_to_i32(alpha);
  650. break;
  651. case GL_UNSIGNED_INT:
  652. if (write_red)
  653. *reinterpret_cast<GLuint*>(red_ptr) = float_to_u32(red);
  654. if (write_green)
  655. *reinterpret_cast<GLuint*>(green_ptr) = float_to_u32(green);
  656. if (write_blue)
  657. *reinterpret_cast<GLuint*>(blue_ptr) = float_to_u32(blue);
  658. if (write_alpha)
  659. *reinterpret_cast<GLuint*>(alpha_ptr) = float_to_u32(alpha);
  660. break;
  661. case GL_FLOAT:
  662. if (write_red)
  663. *reinterpret_cast<GLfloat*>(red_ptr) = min(max(red, 0.0f), 1.0f);
  664. if (write_green)
  665. *reinterpret_cast<GLfloat*>(green_ptr) = min(max(green, 0.0f), 1.0f);
  666. if (write_blue)
  667. *reinterpret_cast<GLfloat*>(blue_ptr) = min(max(blue, 0.0f), 1.0f);
  668. if (write_alpha)
  669. *reinterpret_cast<GLfloat*>(alpha_ptr) = min(max(alpha, 0.0f), 1.0f);
  670. break;
  671. }
  672. out_ptr += pixel_bytes;
  673. }
  674. out_ptr += row_alignment_bytes;
  675. }
  676. }
  677. void GLContext::gl_depth_mask(GLboolean flag)
  678. {
  679. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_depth_mask, flag);
  680. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  681. auto options = m_rasterizer->options();
  682. options.enable_depth_write = (flag != GL_FALSE);
  683. m_rasterizer->set_options(options);
  684. }
  685. void GLContext::gl_draw_pixels(GLsizei width, GLsizei height, GLenum format, GLenum type, void const* data)
  686. {
  687. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_draw_pixels, width, height, format, type, data);
  688. RETURN_WITH_ERROR_IF(format < GL_COLOR_INDEX || format > GL_BGRA, GL_INVALID_ENUM);
  689. RETURN_WITH_ERROR_IF((type < GL_BYTE || type > GL_FLOAT)
  690. && (type < GL_UNSIGNED_BYTE_3_3_2 || type > GL_UNSIGNED_INT_10_10_10_2)
  691. && (type < GL_UNSIGNED_BYTE_2_3_3_REV || type > GL_UNSIGNED_INT_2_10_10_10_REV),
  692. GL_INVALID_ENUM);
  693. RETURN_WITH_ERROR_IF(type == GL_BITMAP && !(format == GL_COLOR_INDEX || format == GL_STENCIL_INDEX), GL_INVALID_ENUM);
  694. RETURN_WITH_ERROR_IF(width < 0 || height < 0, GL_INVALID_VALUE);
  695. // FIXME: GL_INVALID_OPERATION is generated if format is GL_STENCIL_INDEX and there is no stencil buffer
  696. // FIXME: GL_INVALID_OPERATION is generated if format is GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA, GL_RGB, GL_RGBA,
  697. // GL_BGR, GL_BGRA, GL_LUMINANCE, or GL_LUMINANCE_ALPHA, and the GL is in color index mode
  698. RETURN_WITH_ERROR_IF(format != GL_RGB
  699. && (type == GL_UNSIGNED_BYTE_3_3_2
  700. || type == GL_UNSIGNED_BYTE_2_3_3_REV
  701. || type == GL_UNSIGNED_SHORT_5_6_5
  702. || type == GL_UNSIGNED_SHORT_5_6_5_REV),
  703. GL_INVALID_OPERATION);
  704. RETURN_WITH_ERROR_IF(!(format == GL_RGBA || format == GL_BGRA)
  705. && (type == GL_UNSIGNED_SHORT_4_4_4_4
  706. || type == GL_UNSIGNED_SHORT_4_4_4_4_REV
  707. || type == GL_UNSIGNED_SHORT_5_5_5_1
  708. || type == GL_UNSIGNED_SHORT_1_5_5_5_REV
  709. || type == GL_UNSIGNED_INT_8_8_8_8
  710. || type == GL_UNSIGNED_INT_8_8_8_8_REV
  711. || type == GL_UNSIGNED_INT_10_10_10_2
  712. || type == GL_UNSIGNED_INT_2_10_10_10_REV),
  713. GL_INVALID_OPERATION);
  714. // FIXME: GL_INVALID_OPERATION is generated if a non-zero buffer object name is bound to the GL_PIXEL_UNPACK_BUFFER
  715. // target and the buffer object's data store is currently mapped.
  716. // FIXME: GL_INVALID_OPERATION is generated if a non-zero buffer object name is bound to the GL_PIXEL_UNPACK_BUFFER
  717. // target and the data would be unpacked from the buffer object such that the memory reads required would
  718. // exceed the data store size.
  719. // FIXME: GL_INVALID_OPERATION is generated if a non-zero buffer object name is bound to the GL_PIXEL_UNPACK_BUFFER
  720. // target and data is not evenly divisible into the number of bytes needed to store in memory a datum
  721. // indicated by type.
  722. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  723. // FIXME: we only support RGBA + UNSIGNED_BYTE and DEPTH_COMPONENT + UNSIGNED_SHORT, implement all combinations!
  724. if (!((format == GL_RGBA && type == GL_UNSIGNED_BYTE) || (format == GL_DEPTH_COMPONENT && type == GL_UNSIGNED_SHORT))) {
  725. dbgln_if(GL_DEBUG, "gl_draw_pixels(): support for format {:#x} and/or type {:#x} not implemented", format, type);
  726. return;
  727. }
  728. // FIXME: implement support for pixel parameters such as GL_UNPACK_ALIGNMENT
  729. if (format == GL_RGBA) {
  730. auto bitmap_or_error = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, { width, height });
  731. RETURN_WITH_ERROR_IF(bitmap_or_error.is_error(), GL_OUT_OF_MEMORY);
  732. auto bitmap = bitmap_or_error.release_value();
  733. auto pixel_data = static_cast<u32 const*>(data);
  734. for (int y = 0; y < height; ++y)
  735. for (int x = 0; x < width; ++x)
  736. bitmap->set_pixel(x, y, Color::from_argb(*(pixel_data++)));
  737. m_rasterizer->blit_to_color_buffer_at_raster_position(bitmap);
  738. } else if (format == GL_DEPTH_COMPONENT) {
  739. Vector<float> depth_values;
  740. depth_values.ensure_capacity(width * height);
  741. auto depth_data = static_cast<u16 const*>(data);
  742. for (int y = 0; y < height; ++y) {
  743. for (int x = 0; x < width; ++x) {
  744. auto u16_value = *(depth_data++);
  745. auto float_value = static_cast<float>(u16_value) / NumericLimits<u16>::max();
  746. depth_values.append(float_value);
  747. }
  748. }
  749. m_rasterizer->blit_to_depth_buffer_at_raster_position(depth_values, width, height);
  750. } else {
  751. VERIFY_NOT_REACHED();
  752. }
  753. }
  754. void GLContext::gl_depth_range(GLdouble min, GLdouble max)
  755. {
  756. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_depth_range, min, max);
  757. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  758. auto options = m_rasterizer->options();
  759. options.depth_min = clamp<float>(min, 0.f, 1.f);
  760. options.depth_max = clamp<float>(max, 0.f, 1.f);
  761. m_rasterizer->set_options(options);
  762. }
  763. void GLContext::gl_depth_func(GLenum func)
  764. {
  765. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_depth_func, func);
  766. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  767. RETURN_WITH_ERROR_IF(!(func == GL_NEVER
  768. || func == GL_LESS
  769. || func == GL_EQUAL
  770. || func == GL_LEQUAL
  771. || func == GL_GREATER
  772. || func == GL_NOTEQUAL
  773. || func == GL_GEQUAL
  774. || func == GL_ALWAYS),
  775. GL_INVALID_ENUM);
  776. auto options = m_rasterizer->options();
  777. switch (func) {
  778. case GL_NEVER:
  779. options.depth_func = GPU::DepthTestFunction::Never;
  780. break;
  781. case GL_ALWAYS:
  782. options.depth_func = GPU::DepthTestFunction::Always;
  783. break;
  784. case GL_LESS:
  785. options.depth_func = GPU::DepthTestFunction::Less;
  786. break;
  787. case GL_LEQUAL:
  788. options.depth_func = GPU::DepthTestFunction::LessOrEqual;
  789. break;
  790. case GL_EQUAL:
  791. options.depth_func = GPU::DepthTestFunction::Equal;
  792. break;
  793. case GL_NOTEQUAL:
  794. options.depth_func = GPU::DepthTestFunction::NotEqual;
  795. break;
  796. case GL_GEQUAL:
  797. options.depth_func = GPU::DepthTestFunction::GreaterOrEqual;
  798. break;
  799. case GL_GREATER:
  800. options.depth_func = GPU::DepthTestFunction::Greater;
  801. break;
  802. default:
  803. VERIFY_NOT_REACHED();
  804. }
  805. m_rasterizer->set_options(options);
  806. }
  807. void GLContext::gl_color_mask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha)
  808. {
  809. auto options = m_rasterizer->options();
  810. auto mask = options.color_mask;
  811. if (!red)
  812. mask &= ~0x000000ff;
  813. else
  814. mask |= 0x000000ff;
  815. if (!green)
  816. mask &= ~0x0000ff00;
  817. else
  818. mask |= 0x0000ff00;
  819. if (!blue)
  820. mask &= ~0x00ff0000;
  821. else
  822. mask |= 0x00ff0000;
  823. if (!alpha)
  824. mask &= ~0xff000000;
  825. else
  826. mask |= 0xff000000;
  827. options.color_mask = mask;
  828. m_rasterizer->set_options(options);
  829. }
  830. void GLContext::gl_polygon_mode(GLenum face, GLenum mode)
  831. {
  832. RETURN_WITH_ERROR_IF(!(face == GL_BACK || face == GL_FRONT || face == GL_FRONT_AND_BACK), GL_INVALID_ENUM);
  833. RETURN_WITH_ERROR_IF(!(mode == GL_POINT || mode == GL_LINE || mode == GL_FILL), GL_INVALID_ENUM);
  834. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  835. auto options = m_rasterizer->options();
  836. // FIXME: This must support different polygon modes for front- and backside
  837. if (face == GL_BACK) {
  838. dbgln_if(GL_DEBUG, "gl_polygon_mode(GL_BACK, {:#x}): unimplemented", mode);
  839. return;
  840. }
  841. auto map_mode = [](GLenum mode) -> GPU::PolygonMode {
  842. switch (mode) {
  843. case GL_FILL:
  844. return GPU::PolygonMode::Fill;
  845. case GL_LINE:
  846. return GPU::PolygonMode::Line;
  847. case GL_POINT:
  848. return GPU::PolygonMode::Point;
  849. default:
  850. VERIFY_NOT_REACHED();
  851. }
  852. };
  853. options.polygon_mode = map_mode(mode);
  854. m_rasterizer->set_options(options);
  855. }
  856. void GLContext::gl_polygon_offset(GLfloat factor, GLfloat units)
  857. {
  858. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_polygon_offset, factor, units);
  859. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  860. auto rasterizer_options = m_rasterizer->options();
  861. rasterizer_options.depth_offset_factor = factor;
  862. rasterizer_options.depth_offset_constant = units;
  863. m_rasterizer->set_options(rasterizer_options);
  864. }
  865. void GLContext::gl_fogfv(GLenum pname, GLfloat* params)
  866. {
  867. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_fogfv, pname, params);
  868. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  869. auto options = m_rasterizer->options();
  870. switch (pname) {
  871. case GL_FOG_COLOR:
  872. options.fog_color = { params[0], params[1], params[2], params[3] };
  873. break;
  874. default:
  875. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  876. }
  877. m_rasterizer->set_options(options);
  878. }
  879. void GLContext::gl_fogf(GLenum pname, GLfloat param)
  880. {
  881. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_fogf, pname, param);
  882. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  883. RETURN_WITH_ERROR_IF(param < 0.0f, GL_INVALID_VALUE);
  884. auto options = m_rasterizer->options();
  885. switch (pname) {
  886. case GL_FOG_DENSITY:
  887. options.fog_density = param;
  888. break;
  889. case GL_FOG_END:
  890. options.fog_end = param;
  891. break;
  892. case GL_FOG_START:
  893. options.fog_start = param;
  894. break;
  895. default:
  896. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  897. }
  898. m_rasterizer->set_options(options);
  899. }
  900. void GLContext::gl_fogi(GLenum pname, GLint param)
  901. {
  902. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_fogi, pname, param);
  903. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  904. RETURN_WITH_ERROR_IF(param != GL_LINEAR && param != GL_EXP && param != GL_EXP2, GL_INVALID_ENUM);
  905. auto options = m_rasterizer->options();
  906. switch (pname) {
  907. case GL_FOG_MODE:
  908. switch (param) {
  909. case GL_LINEAR:
  910. options.fog_mode = GPU::FogMode::Linear;
  911. break;
  912. case GL_EXP:
  913. options.fog_mode = GPU::FogMode::Exp;
  914. break;
  915. case GL_EXP2:
  916. options.fog_mode = GPU::FogMode::Exp2;
  917. break;
  918. }
  919. break;
  920. default:
  921. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  922. }
  923. m_rasterizer->set_options(options);
  924. }
  925. void GLContext::gl_pixel_storei(GLenum pname, GLint param)
  926. {
  927. // FIXME: Implement missing parameters
  928. switch (pname) {
  929. case GL_PACK_ALIGNMENT:
  930. RETURN_WITH_ERROR_IF(param != 1 && param != 2 && param != 4 && param != 8, GL_INVALID_VALUE);
  931. m_pack_alignment = param;
  932. break;
  933. case GL_UNPACK_ROW_LENGTH:
  934. RETURN_WITH_ERROR_IF(param < 0, GL_INVALID_VALUE);
  935. m_unpack_row_length = static_cast<size_t>(param);
  936. break;
  937. case GL_UNPACK_ALIGNMENT:
  938. RETURN_WITH_ERROR_IF(param != 1 && param != 2 && param != 4 && param != 8, GL_INVALID_VALUE);
  939. m_unpack_alignment = param;
  940. break;
  941. default:
  942. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  943. }
  944. }
  945. void GLContext::gl_scissor(GLint x, GLint y, GLsizei width, GLsizei height)
  946. {
  947. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_scissor, x, y, width, height);
  948. RETURN_WITH_ERROR_IF(width < 0 || height < 0, GL_INVALID_VALUE);
  949. auto options = m_rasterizer->options();
  950. options.scissor_box = { x, y, width, height };
  951. m_rasterizer->set_options(options);
  952. }
  953. void GLContext::gl_raster_pos(GLfloat x, GLfloat y, GLfloat z, GLfloat w)
  954. {
  955. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_raster_pos, x, y, z, w);
  956. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  957. m_rasterizer->set_raster_position({ x, y, z, w }, m_model_view_matrix, m_projection_matrix);
  958. }
  959. void GLContext::gl_line_width(GLfloat width)
  960. {
  961. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_line_width, width);
  962. RETURN_WITH_ERROR_IF(width <= 0, GL_INVALID_VALUE);
  963. m_line_width = width;
  964. auto options = m_rasterizer->options();
  965. options.line_width = width;
  966. m_rasterizer->set_options(options);
  967. }
  968. void GLContext::gl_push_attrib(GLbitfield mask)
  969. {
  970. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_push_attrib, mask);
  971. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  972. // FIXME: implement
  973. dbgln_if(GL_DEBUG, "GLContext FIXME: implement gl_push_attrib({})", mask);
  974. }
  975. void GLContext::gl_pop_attrib()
  976. {
  977. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_pop_attrib);
  978. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  979. // FIXME: implement
  980. dbgln_if(GL_DEBUG, "GLContext FIXME: implement gl_pop_attrib()");
  981. }
  982. void GLContext::gl_bitmap(GLsizei width, GLsizei height, GLfloat xorig, GLfloat yorig, GLfloat xmove, GLfloat ymove, GLubyte const* bitmap)
  983. {
  984. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_bitmap, width, height, xorig, yorig, xmove, ymove, bitmap);
  985. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  986. if (bitmap != nullptr) {
  987. // FIXME: implement
  988. dbgln_if(GL_DEBUG, "gl_bitmap({}, {}, {}, {}, {}, {}, {}): unimplemented", width, height, xorig, yorig, xmove, ymove, bitmap);
  989. }
  990. auto raster_position = m_rasterizer->raster_position();
  991. raster_position.window_coordinates += { xmove, ymove, 0.f, 0.f };
  992. m_rasterizer->set_raster_position(raster_position);
  993. }
  994. void GLContext::gl_rect(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2)
  995. {
  996. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_rect, x1, y1, x2, y2);
  997. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  998. gl_begin(GL_POLYGON);
  999. gl_vertex(x1, y1, 0.0, 1.0);
  1000. gl_vertex(x2, y1, 0.0, 1.0);
  1001. gl_vertex(x2, y2, 0.0, 1.0);
  1002. gl_vertex(x1, y2, 0.0, 1.0);
  1003. gl_end();
  1004. }
  1005. void GLContext::gl_point_size(GLfloat size)
  1006. {
  1007. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_point_size, size);
  1008. RETURN_WITH_ERROR_IF(size <= 0.f, GL_INVALID_VALUE);
  1009. m_point_size = size;
  1010. auto rasterizer_options = m_rasterizer->options();
  1011. rasterizer_options.point_size = size;
  1012. m_rasterizer->set_options(rasterizer_options);
  1013. }
  1014. void GLContext::present()
  1015. {
  1016. m_rasterizer->blit_color_buffer_to(*m_frontbuffer);
  1017. }
  1018. void GLContext::sync_device_config()
  1019. {
  1020. sync_device_sampler_config();
  1021. sync_device_texcoord_config();
  1022. sync_light_state();
  1023. sync_stencil_configuration();
  1024. sync_clip_planes();
  1025. }
  1026. void GLContext::build_extension_string()
  1027. {
  1028. Vector<StringView> extensions;
  1029. // FIXME: npot texture support became a required core feature starting with OpenGL 2.0 (https://www.khronos.org/opengl/wiki/NPOT_Texture)
  1030. // Ideally we would verify if the selected device adheres to the requested OpenGL context version before context creation
  1031. // and refuse to create a context if it doesn't.
  1032. if (m_device_info.supports_npot_textures)
  1033. extensions.append("GL_ARB_texture_non_power_of_two");
  1034. if (m_device_info.num_texture_units > 1)
  1035. extensions.append("GL_ARB_multitexture");
  1036. m_extensions = String::join(" ", extensions);
  1037. }
  1038. NonnullOwnPtr<GLContext> create_context(Gfx::Bitmap& bitmap)
  1039. {
  1040. // FIXME: Make driver selectable. This is currently hardcoded to LibSoftGPU
  1041. auto driver = MUST(GPU::Driver::try_create("softgpu"));
  1042. auto device = MUST(driver->try_create_device(bitmap.size()));
  1043. auto context = make<GLContext>(driver, move(device), bitmap);
  1044. dbgln_if(GL_DEBUG, "GL::create_context({}) -> {:p}", bitmap.size(), context.ptr());
  1045. if (!g_gl_context)
  1046. make_context_current(context);
  1047. return context;
  1048. }
  1049. void make_context_current(GLContext* context)
  1050. {
  1051. if (g_gl_context == context)
  1052. return;
  1053. dbgln_if(GL_DEBUG, "GL::make_context_current({:p})", context);
  1054. g_gl_context = context;
  1055. }
  1056. void present_context(GLContext* context)
  1057. {
  1058. context->present();
  1059. }
  1060. }