ContextParameter.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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/Debug.h>
  9. #include <LibGL/GLContext.h>
  10. namespace GL {
  11. Optional<ContextParameter> GLContext::get_context_parameter(GLenum name)
  12. {
  13. switch (name) {
  14. case GL_ACTIVE_TEXTURE:
  15. return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast<GLint>(GL_TEXTURE0 + m_active_texture_unit_index) } };
  16. case GL_ALPHA_BITS:
  17. return ContextParameter { .type = GL_INT, .value = { .integer_value = sizeof(u8) * 8 } };
  18. case GL_ALPHA_TEST:
  19. return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_alpha_test_enabled } };
  20. case GL_BLEND:
  21. return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_blend_enabled } };
  22. case GL_BLEND_DST:
  23. case GL_BLEND_DST_ALPHA:
  24. return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast<GLint>(m_blend_destination_factor) } };
  25. case GL_BLEND_SRC:
  26. case GL_BLEND_SRC_ALPHA:
  27. return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast<GLint>(m_blend_source_factor) } };
  28. case GL_BLUE_BITS:
  29. return ContextParameter { .type = GL_INT, .value = { .integer_value = sizeof(u8) * 8 } };
  30. case GL_CLIENT_ACTIVE_TEXTURE:
  31. return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast<GLint>(GL_TEXTURE0 + m_client_active_texture) } };
  32. case GL_COLOR_CLEAR_VALUE:
  33. return ContextParameter {
  34. .type = GL_DOUBLE,
  35. .count = 4,
  36. .value = {
  37. .double_list = {
  38. static_cast<GLdouble>(m_clear_color.x()),
  39. static_cast<GLdouble>(m_clear_color.y()),
  40. static_cast<GLdouble>(m_clear_color.z()),
  41. static_cast<GLdouble>(m_clear_color.w()),
  42. } }
  43. };
  44. case GL_COLOR_MATERIAL:
  45. return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_color_material_enabled } };
  46. case GL_COLOR_MATERIAL_FACE:
  47. return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast<GLint>(m_color_material_face) } };
  48. case GL_COLOR_MATERIAL_MODE:
  49. return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast<GLint>(m_color_material_mode) } };
  50. case GL_CURRENT_COLOR:
  51. return ContextParameter {
  52. .type = GL_DOUBLE,
  53. .count = 4,
  54. .value = {
  55. .double_list = {
  56. static_cast<double>(m_current_vertex_color.x()),
  57. static_cast<double>(m_current_vertex_color.y()),
  58. static_cast<double>(m_current_vertex_color.z()),
  59. static_cast<double>(m_current_vertex_color.w()),
  60. } }
  61. };
  62. case GL_CULL_FACE:
  63. return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_cull_faces } };
  64. case GL_DEPTH_BITS:
  65. return ContextParameter { .type = GL_INT, .value = { .integer_value = sizeof(float) * 8 } };
  66. case GL_DEPTH_CLEAR_VALUE:
  67. return ContextParameter { .type = GL_DOUBLE, .value = { .double_value = static_cast<GLdouble>(m_clear_depth) } };
  68. case GL_DEPTH_TEST:
  69. return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_depth_test_enabled } };
  70. case GL_DITHER:
  71. return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_dither_enabled } };
  72. case GL_DOUBLEBUFFER:
  73. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = true } };
  74. case GL_FOG: {
  75. auto fog_enabled = m_rasterizer->options().fog_enabled;
  76. return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = fog_enabled } };
  77. }
  78. case GL_GREEN_BITS:
  79. return ContextParameter { .type = GL_INT, .value = { .integer_value = sizeof(u8) * 8 } };
  80. case GL_LIGHTING:
  81. return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_lighting_enabled } };
  82. case GL_LINE_SMOOTH:
  83. return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_line_smooth } };
  84. case GL_MAX_CLIP_PLANES:
  85. return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast<GLint>(m_device_info.max_clip_planes) } };
  86. case GL_MAX_LIGHTS:
  87. return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast<GLint>(m_device_info.num_lights) } };
  88. case GL_MAX_MODELVIEW_STACK_DEPTH:
  89. return ContextParameter { .type = GL_INT, .value = { .integer_value = MODELVIEW_MATRIX_STACK_LIMIT } };
  90. case GL_MAX_PROJECTION_STACK_DEPTH:
  91. return ContextParameter { .type = GL_INT, .value = { .integer_value = PROJECTION_MATRIX_STACK_LIMIT } };
  92. case GL_MAX_TEXTURE_LOD_BIAS:
  93. return ContextParameter { .type = GL_DOUBLE, .value = { .double_value = static_cast<GLdouble>(m_device_info.max_texture_lod_bias) } };
  94. case GL_MAX_TEXTURE_SIZE:
  95. return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast<GLint>(m_device_info.max_texture_size) } };
  96. case GL_MAX_TEXTURE_STACK_DEPTH:
  97. return ContextParameter { .type = GL_INT, .value = { .integer_value = TEXTURE_MATRIX_STACK_LIMIT } };
  98. case GL_MAX_TEXTURE_UNITS:
  99. return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast<GLint>(m_texture_units.size()) } };
  100. case GL_NORMAL_ARRAY_TYPE:
  101. return ContextParameter { .type = GL_INT, .value = { .integer_value = GL_FLOAT } };
  102. case GL_NORMALIZE:
  103. return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_normalize } };
  104. case GL_PACK_ALIGNMENT:
  105. return ContextParameter { .type = GL_INT, .value = { .integer_value = m_packing_parameters.pack_alignment } };
  106. case GL_PACK_IMAGE_HEIGHT:
  107. return ContextParameter { .type = GL_INT, .value = { .integer_value = m_packing_parameters.image_height } };
  108. case GL_PACK_LSB_FIRST:
  109. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_packing_parameters.least_significant_bit_first } };
  110. case GL_PACK_ROW_LENGTH:
  111. return ContextParameter { .type = GL_INT, .value = { .integer_value = m_packing_parameters.row_length } };
  112. case GL_PACK_SKIP_IMAGES:
  113. return ContextParameter { .type = GL_INT, .value = { .integer_value = m_packing_parameters.skip_images } };
  114. case GL_PACK_SKIP_PIXELS:
  115. return ContextParameter { .type = GL_INT, .value = { .integer_value = m_packing_parameters.skip_pixels } };
  116. case GL_PACK_SKIP_ROWS:
  117. return ContextParameter { .type = GL_INT, .value = { .integer_value = m_packing_parameters.skip_rows } };
  118. case GL_PACK_SWAP_BYTES:
  119. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_packing_parameters.swap_bytes } };
  120. case GL_POINT_SMOOTH:
  121. return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_point_smooth } };
  122. case GL_POINT_SIZE:
  123. return ContextParameter { .type = GL_DOUBLE, .value = { .double_value = static_cast<GLdouble>(m_point_size) } };
  124. case GL_POLYGON_OFFSET_FILL:
  125. return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_depth_offset_enabled } };
  126. case GL_RED_BITS:
  127. return ContextParameter { .type = GL_INT, .value = { .integer_value = sizeof(u8) * 8 } };
  128. case GL_SAMPLE_BUFFERS:
  129. return ContextParameter { .type = GL_INT, .value = { .integer_value = 0 } };
  130. case GL_SAMPLES:
  131. return ContextParameter { .type = GL_INT, .value = { .integer_value = 1 } };
  132. case GL_SCISSOR_BOX: {
  133. auto scissor_box = m_rasterizer->options().scissor_box;
  134. return ContextParameter {
  135. .type = GL_INT,
  136. .count = 4,
  137. .value = {
  138. .integer_list = {
  139. scissor_box.x(),
  140. scissor_box.y(),
  141. scissor_box.width(),
  142. scissor_box.height(),
  143. } }
  144. };
  145. }
  146. case GL_SCISSOR_TEST: {
  147. auto scissor_enabled = m_rasterizer->options().scissor_enabled;
  148. return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = scissor_enabled } };
  149. }
  150. case GL_STENCIL_BITS:
  151. return ContextParameter { .type = GL_INT, .value = { .integer_value = m_device_info.stencil_bits } };
  152. case GL_STENCIL_CLEAR_VALUE:
  153. return ContextParameter { .type = GL_INT, .value = { .integer_value = m_clear_stencil } };
  154. case GL_STENCIL_TEST:
  155. return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_stencil_test_enabled } };
  156. case GL_TEXTURE_1D:
  157. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_active_texture_unit->texture_1d_enabled() } };
  158. case GL_TEXTURE_2D:
  159. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_active_texture_unit->texture_2d_enabled() } };
  160. case GL_TEXTURE_3D:
  161. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_active_texture_unit->texture_3d_enabled() } };
  162. case GL_TEXTURE_CUBE_MAP:
  163. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_active_texture_unit->texture_cube_map_enabled() } };
  164. case GL_TEXTURE_GEN_Q:
  165. case GL_TEXTURE_GEN_R:
  166. case GL_TEXTURE_GEN_S:
  167. case GL_TEXTURE_GEN_T: {
  168. auto generation_enabled = texture_coordinate_generation(m_active_texture_unit_index, name).enabled;
  169. return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = generation_enabled } };
  170. }
  171. case GL_UNPACK_ALIGNMENT:
  172. return ContextParameter { .type = GL_INT, .value = { .integer_value = m_unpacking_parameters.pack_alignment } };
  173. case GL_UNPACK_IMAGE_HEIGHT:
  174. return ContextParameter { .type = GL_INT, .value = { .integer_value = m_unpacking_parameters.image_height } };
  175. case GL_UNPACK_LSB_FIRST:
  176. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_unpacking_parameters.least_significant_bit_first } };
  177. case GL_UNPACK_ROW_LENGTH:
  178. return ContextParameter { .type = GL_INT, .value = { .integer_value = m_unpacking_parameters.row_length } };
  179. case GL_UNPACK_SKIP_IMAGES:
  180. return ContextParameter { .type = GL_INT, .value = { .integer_value = m_unpacking_parameters.skip_images } };
  181. case GL_UNPACK_SKIP_PIXELS:
  182. return ContextParameter { .type = GL_INT, .value = { .integer_value = m_unpacking_parameters.skip_pixels } };
  183. case GL_UNPACK_SKIP_ROWS:
  184. return ContextParameter { .type = GL_INT, .value = { .integer_value = m_unpacking_parameters.skip_rows } };
  185. case GL_UNPACK_SWAP_BYTES:
  186. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_unpacking_parameters.swap_bytes } };
  187. case GL_VIEWPORT:
  188. return ContextParameter {
  189. .type = GL_INT,
  190. .count = 4,
  191. .value = {
  192. .integer_list = {
  193. m_viewport.x(),
  194. m_viewport.y(),
  195. m_viewport.width(),
  196. m_viewport.height(),
  197. } }
  198. };
  199. default:
  200. dbgln_if(GL_DEBUG, "get_context_parameter({:#x}): unknown context parameter", name);
  201. return {};
  202. }
  203. }
  204. void GLContext::gl_disable(GLenum capability)
  205. {
  206. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_disable, capability);
  207. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  208. auto rasterizer_options = m_rasterizer->options();
  209. bool update_rasterizer_options = false;
  210. switch (capability) {
  211. case GL_CLIP_PLANE0:
  212. case GL_CLIP_PLANE1:
  213. case GL_CLIP_PLANE2:
  214. case GL_CLIP_PLANE3:
  215. case GL_CLIP_PLANE4:
  216. case GL_CLIP_PLANE5: {
  217. auto plane_idx = static_cast<size_t>(capability) - GL_CLIP_PLANE0;
  218. m_clip_plane_attributes.enabled &= ~(1 << plane_idx);
  219. m_clip_planes_dirty = true;
  220. break;
  221. }
  222. case GL_COLOR_MATERIAL:
  223. m_color_material_enabled = false;
  224. break;
  225. case GL_CULL_FACE:
  226. m_cull_faces = false;
  227. rasterizer_options.enable_culling = false;
  228. update_rasterizer_options = true;
  229. break;
  230. case GL_DEPTH_TEST:
  231. m_depth_test_enabled = false;
  232. rasterizer_options.enable_depth_test = false;
  233. update_rasterizer_options = true;
  234. break;
  235. case GL_BLEND:
  236. m_blend_enabled = false;
  237. rasterizer_options.enable_blending = false;
  238. update_rasterizer_options = true;
  239. break;
  240. case GL_ALPHA_TEST:
  241. m_alpha_test_enabled = false;
  242. rasterizer_options.enable_alpha_test = false;
  243. update_rasterizer_options = true;
  244. break;
  245. case GL_DITHER:
  246. m_dither_enabled = false;
  247. break;
  248. case GL_FOG:
  249. rasterizer_options.fog_enabled = false;
  250. update_rasterizer_options = true;
  251. break;
  252. case GL_LIGHTING:
  253. m_lighting_enabled = false;
  254. rasterizer_options.lighting_enabled = false;
  255. update_rasterizer_options = true;
  256. break;
  257. case GL_LIGHT0:
  258. case GL_LIGHT1:
  259. case GL_LIGHT2:
  260. case GL_LIGHT3:
  261. case GL_LIGHT4:
  262. case GL_LIGHT5:
  263. case GL_LIGHT6:
  264. case GL_LIGHT7:
  265. m_light_states.at(capability - GL_LIGHT0).is_enabled = false;
  266. m_light_state_is_dirty = true;
  267. break;
  268. case GL_LINE_SMOOTH:
  269. m_line_smooth = false;
  270. rasterizer_options.line_smooth = false;
  271. update_rasterizer_options = true;
  272. break;
  273. case GL_NORMALIZE:
  274. m_normalize = false;
  275. rasterizer_options.normalization_enabled = false;
  276. update_rasterizer_options = true;
  277. break;
  278. case GL_POINT_SMOOTH:
  279. m_point_smooth = false;
  280. rasterizer_options.point_smooth = false;
  281. update_rasterizer_options = true;
  282. break;
  283. case GL_POLYGON_OFFSET_FILL:
  284. m_depth_offset_enabled = false;
  285. rasterizer_options.depth_offset_enabled = false;
  286. update_rasterizer_options = true;
  287. break;
  288. case GL_SCISSOR_TEST:
  289. rasterizer_options.scissor_enabled = false;
  290. update_rasterizer_options = true;
  291. break;
  292. case GL_STENCIL_TEST:
  293. m_stencil_test_enabled = false;
  294. rasterizer_options.enable_stencil_test = false;
  295. update_rasterizer_options = true;
  296. break;
  297. case GL_TEXTURE_1D:
  298. m_active_texture_unit->set_texture_1d_enabled(false);
  299. m_sampler_config_is_dirty = true;
  300. m_texture_units_dirty = true;
  301. break;
  302. case GL_TEXTURE_2D:
  303. m_active_texture_unit->set_texture_2d_enabled(false);
  304. m_sampler_config_is_dirty = true;
  305. m_texture_units_dirty = true;
  306. break;
  307. case GL_TEXTURE_3D:
  308. m_active_texture_unit->set_texture_3d_enabled(false);
  309. m_sampler_config_is_dirty = true;
  310. m_texture_units_dirty = true;
  311. break;
  312. case GL_TEXTURE_CUBE_MAP:
  313. m_active_texture_unit->set_texture_cube_map_enabled(false);
  314. m_sampler_config_is_dirty = true;
  315. m_texture_units_dirty = true;
  316. break;
  317. case GL_TEXTURE_GEN_Q:
  318. case GL_TEXTURE_GEN_R:
  319. case GL_TEXTURE_GEN_S:
  320. case GL_TEXTURE_GEN_T:
  321. texture_coordinate_generation(m_active_texture_unit_index, capability).enabled = false;
  322. m_texture_units_dirty = true;
  323. break;
  324. default:
  325. dbgln_if(GL_DEBUG, "gl_disable({:#x}): unknown parameter", capability);
  326. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  327. }
  328. if (update_rasterizer_options)
  329. m_rasterizer->set_options(rasterizer_options);
  330. }
  331. void GLContext::gl_disable_client_state(GLenum cap)
  332. {
  333. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  334. switch (cap) {
  335. case GL_COLOR_ARRAY:
  336. m_client_side_color_array_enabled = false;
  337. break;
  338. case GL_NORMAL_ARRAY:
  339. m_client_side_normal_array_enabled = false;
  340. break;
  341. case GL_TEXTURE_COORD_ARRAY:
  342. m_client_side_texture_coord_array_enabled[m_client_active_texture] = false;
  343. break;
  344. case GL_VERTEX_ARRAY:
  345. m_client_side_vertex_array_enabled = false;
  346. break;
  347. default:
  348. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  349. }
  350. }
  351. void GLContext::gl_enable(GLenum capability)
  352. {
  353. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_enable, capability);
  354. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  355. auto rasterizer_options = m_rasterizer->options();
  356. bool update_rasterizer_options = false;
  357. switch (capability) {
  358. case GL_CLIP_PLANE0:
  359. case GL_CLIP_PLANE1:
  360. case GL_CLIP_PLANE2:
  361. case GL_CLIP_PLANE3:
  362. case GL_CLIP_PLANE4:
  363. case GL_CLIP_PLANE5: {
  364. auto plane_idx = static_cast<size_t>(capability) - GL_CLIP_PLANE0;
  365. m_clip_plane_attributes.enabled |= (1 << plane_idx);
  366. m_clip_planes_dirty = true;
  367. break;
  368. }
  369. case GL_COLOR_MATERIAL:
  370. m_color_material_enabled = true;
  371. break;
  372. case GL_CULL_FACE:
  373. m_cull_faces = true;
  374. rasterizer_options.enable_culling = true;
  375. update_rasterizer_options = true;
  376. break;
  377. case GL_DEPTH_TEST:
  378. m_depth_test_enabled = true;
  379. rasterizer_options.enable_depth_test = true;
  380. update_rasterizer_options = true;
  381. break;
  382. case GL_BLEND:
  383. m_blend_enabled = true;
  384. rasterizer_options.enable_blending = true;
  385. update_rasterizer_options = true;
  386. break;
  387. case GL_ALPHA_TEST:
  388. m_alpha_test_enabled = true;
  389. rasterizer_options.enable_alpha_test = true;
  390. update_rasterizer_options = true;
  391. break;
  392. case GL_DITHER:
  393. m_dither_enabled = true;
  394. break;
  395. case GL_FOG:
  396. rasterizer_options.fog_enabled = true;
  397. update_rasterizer_options = true;
  398. break;
  399. case GL_LIGHTING:
  400. m_lighting_enabled = true;
  401. rasterizer_options.lighting_enabled = true;
  402. update_rasterizer_options = true;
  403. break;
  404. case GL_LIGHT0:
  405. case GL_LIGHT1:
  406. case GL_LIGHT2:
  407. case GL_LIGHT3:
  408. case GL_LIGHT4:
  409. case GL_LIGHT5:
  410. case GL_LIGHT6:
  411. case GL_LIGHT7:
  412. m_light_states.at(capability - GL_LIGHT0).is_enabled = true;
  413. m_light_state_is_dirty = true;
  414. break;
  415. case GL_LINE_SMOOTH:
  416. m_line_smooth = true;
  417. rasterizer_options.line_smooth = true;
  418. update_rasterizer_options = true;
  419. break;
  420. case GL_NORMALIZE:
  421. m_normalize = true;
  422. rasterizer_options.normalization_enabled = true;
  423. update_rasterizer_options = true;
  424. break;
  425. case GL_POINT_SMOOTH:
  426. m_point_smooth = true;
  427. rasterizer_options.point_smooth = true;
  428. update_rasterizer_options = true;
  429. break;
  430. case GL_POLYGON_OFFSET_FILL:
  431. m_depth_offset_enabled = true;
  432. rasterizer_options.depth_offset_enabled = true;
  433. update_rasterizer_options = true;
  434. break;
  435. case GL_SCISSOR_TEST:
  436. rasterizer_options.scissor_enabled = true;
  437. update_rasterizer_options = true;
  438. break;
  439. case GL_STENCIL_TEST:
  440. m_stencil_test_enabled = true;
  441. rasterizer_options.enable_stencil_test = true;
  442. update_rasterizer_options = true;
  443. break;
  444. case GL_TEXTURE_1D:
  445. m_active_texture_unit->set_texture_1d_enabled(true);
  446. m_sampler_config_is_dirty = true;
  447. m_texture_units_dirty = true;
  448. break;
  449. case GL_TEXTURE_2D:
  450. m_active_texture_unit->set_texture_2d_enabled(true);
  451. m_sampler_config_is_dirty = true;
  452. m_texture_units_dirty = true;
  453. break;
  454. case GL_TEXTURE_3D:
  455. m_active_texture_unit->set_texture_3d_enabled(true);
  456. m_sampler_config_is_dirty = true;
  457. m_texture_units_dirty = true;
  458. break;
  459. case GL_TEXTURE_CUBE_MAP:
  460. m_active_texture_unit->set_texture_cube_map_enabled(true);
  461. m_sampler_config_is_dirty = true;
  462. m_texture_units_dirty = true;
  463. break;
  464. case GL_TEXTURE_GEN_Q:
  465. case GL_TEXTURE_GEN_R:
  466. case GL_TEXTURE_GEN_S:
  467. case GL_TEXTURE_GEN_T:
  468. texture_coordinate_generation(m_active_texture_unit_index, capability).enabled = true;
  469. m_texture_units_dirty = true;
  470. break;
  471. default:
  472. dbgln_if(GL_DEBUG, "gl_enable({:#x}): unknown parameter", capability);
  473. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  474. }
  475. if (update_rasterizer_options)
  476. m_rasterizer->set_options(rasterizer_options);
  477. }
  478. void GLContext::gl_enable_client_state(GLenum cap)
  479. {
  480. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  481. switch (cap) {
  482. case GL_COLOR_ARRAY:
  483. m_client_side_color_array_enabled = true;
  484. break;
  485. case GL_NORMAL_ARRAY:
  486. m_client_side_normal_array_enabled = true;
  487. break;
  488. case GL_TEXTURE_COORD_ARRAY:
  489. m_client_side_texture_coord_array_enabled[m_client_active_texture] = true;
  490. break;
  491. case GL_VERTEX_ARRAY:
  492. m_client_side_vertex_array_enabled = true;
  493. break;
  494. default:
  495. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  496. }
  497. }
  498. void GLContext::gl_get_booleanv(GLenum pname, GLboolean* data)
  499. {
  500. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  501. auto optional_parameter = get_context_parameter(pname);
  502. RETURN_WITH_ERROR_IF(!optional_parameter.has_value(), GL_INVALID_ENUM);
  503. auto parameter = optional_parameter.release_value();
  504. switch (parameter.type) {
  505. case GL_BOOL:
  506. *data = parameter.value.boolean_value ? GL_TRUE : GL_FALSE;
  507. break;
  508. case GL_DOUBLE:
  509. *data = (parameter.value.double_value == 0.0) ? GL_FALSE : GL_TRUE;
  510. break;
  511. case GL_INT:
  512. *data = (parameter.value.integer_value == 0) ? GL_FALSE : GL_TRUE;
  513. break;
  514. default:
  515. VERIFY_NOT_REACHED();
  516. }
  517. }
  518. void GLContext::gl_get_doublev(GLenum pname, GLdouble* params)
  519. {
  520. get_floating_point(pname, params);
  521. }
  522. template<typename T>
  523. void GLContext::get_floating_point(GLenum pname, T* params)
  524. {
  525. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  526. // Handle matrix retrieval first
  527. auto flatten_and_assign_matrix = [&params](FloatMatrix4x4 const& matrix) {
  528. auto elements = matrix.elements();
  529. for (size_t i = 0; i < 4; ++i) {
  530. for (size_t j = 0; j < 4; ++j) {
  531. // Return transposed matrix since OpenGL defines them as column-major
  532. params[i * 4 + j] = static_cast<T>(elements[j][i]);
  533. }
  534. }
  535. };
  536. switch (pname) {
  537. case GL_MODELVIEW_MATRIX:
  538. flatten_and_assign_matrix(model_view_matrix());
  539. return;
  540. case GL_PROJECTION_MATRIX:
  541. flatten_and_assign_matrix(projection_matrix());
  542. return;
  543. }
  544. // Regular parameters
  545. auto optional_parameter = get_context_parameter(pname);
  546. RETURN_WITH_ERROR_IF(!optional_parameter.has_value(), GL_INVALID_ENUM);
  547. auto parameter = optional_parameter.release_value();
  548. switch (parameter.type) {
  549. case GL_BOOL:
  550. *params = parameter.value.boolean_value ? GL_TRUE : GL_FALSE;
  551. break;
  552. case GL_DOUBLE:
  553. for (size_t i = 0; i < parameter.count; ++i)
  554. params[i] = parameter.value.double_list[i];
  555. break;
  556. case GL_INT:
  557. for (size_t i = 0; i < parameter.count; ++i)
  558. params[i] = parameter.value.integer_list[i];
  559. break;
  560. default:
  561. VERIFY_NOT_REACHED();
  562. }
  563. }
  564. void GLContext::gl_get_floatv(GLenum pname, GLfloat* params)
  565. {
  566. get_floating_point(pname, params);
  567. }
  568. void GLContext::gl_get_integerv(GLenum pname, GLint* data)
  569. {
  570. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  571. auto optional_parameter = get_context_parameter(pname);
  572. RETURN_WITH_ERROR_IF(!optional_parameter.has_value(), GL_INVALID_ENUM);
  573. auto parameter = optional_parameter.release_value();
  574. switch (parameter.type) {
  575. case GL_BOOL:
  576. *data = parameter.value.boolean_value ? GL_TRUE : GL_FALSE;
  577. break;
  578. case GL_DOUBLE: {
  579. double const int_range = static_cast<double>(NumericLimits<GLint>::max()) - NumericLimits<GLint>::min();
  580. for (size_t i = 0; i < parameter.count; ++i) {
  581. double const result_factor = (clamp(parameter.value.double_list[i], -1.0, 1.0) + 1.0) / 2.0;
  582. data[i] = static_cast<GLint>(NumericLimits<GLint>::min() + result_factor * int_range);
  583. }
  584. break;
  585. }
  586. case GL_INT:
  587. for (size_t i = 0; i < parameter.count; ++i)
  588. data[i] = parameter.value.integer_list[i];
  589. break;
  590. default:
  591. VERIFY_NOT_REACHED();
  592. }
  593. }
  594. GLboolean GLContext::gl_is_enabled(GLenum capability)
  595. {
  596. RETURN_VALUE_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION, 0);
  597. auto optional_parameter = get_context_parameter(capability);
  598. RETURN_VALUE_WITH_ERROR_IF(!optional_parameter.has_value(), GL_INVALID_ENUM, 0);
  599. auto parameter = optional_parameter.release_value();
  600. RETURN_VALUE_WITH_ERROR_IF(!parameter.is_capability, GL_INVALID_ENUM, 0);
  601. return parameter.value.boolean_value ? GL_TRUE : GL_FALSE;
  602. }
  603. GPU::PackingSpecification GLContext::get_packing_specification(PackingType packing_type)
  604. {
  605. // FIXME: add support for .least_significant_bit_first, .skip_images, .skip_pixels and .skip_rows
  606. auto const& pixel_parameters = (packing_type == PackingType::Pack) ? m_packing_parameters : m_unpacking_parameters;
  607. return {
  608. .depth_stride = static_cast<u32>(pixel_parameters.image_height),
  609. .row_stride = static_cast<u32>(pixel_parameters.row_length),
  610. .byte_alignment = pixel_parameters.pack_alignment,
  611. .component_bytes_order = pixel_parameters.swap_bytes ? GPU::ComponentBytesOrder::Reversed : GPU::ComponentBytesOrder::Normal,
  612. };
  613. }
  614. }