SoftwareGLContext.cpp 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490
  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 <AK/Assertions.h>
  8. #include <AK/Debug.h>
  9. #include <AK/Format.h>
  10. #include <AK/QuickSort.h>
  11. #include <AK/TemporaryChange.h>
  12. #include <AK/Variant.h>
  13. #include <AK/Vector.h>
  14. #include <LibGL/SoftwareGLContext.h>
  15. #include <LibGfx/Bitmap.h>
  16. #include <LibGfx/Painter.h>
  17. #include <LibGfx/Vector4.h>
  18. #include <LibSoftGPU/Device.h>
  19. using AK::dbgln;
  20. namespace GL {
  21. static constexpr size_t MODELVIEW_MATRIX_STACK_LIMIT = 64;
  22. static constexpr size_t PROJECTION_MATRIX_STACK_LIMIT = 8;
  23. static constexpr size_t TEXTURE_MATRIX_STACK_LIMIT = 8;
  24. #define APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(name, ...) \
  25. if (should_append_to_listing()) { \
  26. append_to_listing<&SoftwareGLContext::name>(__VA_ARGS__); \
  27. if (!should_execute_after_appending_to_listing()) \
  28. return; \
  29. }
  30. #define APPEND_TO_CALL_LIST_WITH_ARG_AND_RETURN_IF_NEEDED(name, arg) \
  31. if (should_append_to_listing()) { \
  32. auto ptr = store_in_listing(arg); \
  33. append_to_listing<&SoftwareGLContext::name>(*ptr); \
  34. if (!should_execute_after_appending_to_listing()) \
  35. return; \
  36. }
  37. #define RETURN_WITH_ERROR_IF(condition, error) \
  38. if (condition) { \
  39. if (m_error == GL_NO_ERROR) \
  40. m_error = error; \
  41. return; \
  42. }
  43. #define RETURN_VALUE_WITH_ERROR_IF(condition, error, return_value) \
  44. if (condition) { \
  45. if (m_error == GL_NO_ERROR) \
  46. m_error = error; \
  47. return return_value; \
  48. }
  49. SoftwareGLContext::SoftwareGLContext(Gfx::Bitmap& frontbuffer)
  50. : m_frontbuffer(frontbuffer)
  51. , m_rasterizer(frontbuffer.size())
  52. {
  53. }
  54. Optional<ContextParameter> SoftwareGLContext::get_context_parameter(GLenum name)
  55. {
  56. switch (name) {
  57. case GL_ALPHA_BITS:
  58. return ContextParameter { .type = GL_INT, .value = { .integer_value = sizeof(float) * 8 } };
  59. case GL_ALPHA_TEST:
  60. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_alpha_test_enabled } };
  61. case GL_BLEND:
  62. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_blend_enabled } };
  63. case GL_BLEND_DST_ALPHA:
  64. return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast<GLint>(m_blend_destination_factor) } };
  65. case GL_BLEND_SRC_ALPHA:
  66. return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast<GLint>(m_blend_source_factor) } };
  67. case GL_BLUE_BITS:
  68. return ContextParameter { .type = GL_INT, .value = { .integer_value = sizeof(float) * 8 } };
  69. case GL_CULL_FACE:
  70. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_cull_faces } };
  71. case GL_DEPTH_BITS:
  72. return ContextParameter { .type = GL_INT, .value = { .integer_value = sizeof(float) * 8 } };
  73. case GL_DEPTH_TEST:
  74. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_depth_test_enabled } };
  75. case GL_DITHER:
  76. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_dither_enabled } };
  77. case GL_DOUBLEBUFFER:
  78. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = true } };
  79. case GL_GREEN_BITS:
  80. return ContextParameter { .type = GL_INT, .value = { .integer_value = sizeof(float) * 8 } };
  81. case GL_LIGHTING:
  82. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_lighting_enabled } };
  83. case GL_MAX_MODELVIEW_STACK_DEPTH:
  84. return ContextParameter { .type = GL_INT, .value = { .integer_value = MODELVIEW_MATRIX_STACK_LIMIT } };
  85. case GL_MAX_PROJECTION_STACK_DEPTH:
  86. return ContextParameter { .type = GL_INT, .value = { .integer_value = PROJECTION_MATRIX_STACK_LIMIT } };
  87. case GL_MAX_TEXTURE_SIZE:
  88. return ContextParameter { .type = GL_INT, .value = { .integer_value = 4096 } };
  89. case GL_MAX_TEXTURE_STACK_DEPTH:
  90. return ContextParameter { .type = GL_INT, .value = { .integer_value = TEXTURE_MATRIX_STACK_LIMIT } };
  91. case GL_MAX_TEXTURE_UNITS:
  92. return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast<GLint>(m_texture_units.size()) } };
  93. case GL_PACK_ALIGNMENT:
  94. return ContextParameter { .type = GL_INT, .value = { .integer_value = m_pack_alignment } };
  95. case GL_RED_BITS:
  96. return ContextParameter { .type = GL_INT, .value = { .integer_value = sizeof(float) * 8 } };
  97. case GL_SCISSOR_BOX: {
  98. auto scissor_box = m_rasterizer.options().scissor_box;
  99. return ContextParameter {
  100. .type = GL_INT,
  101. .count = 4,
  102. .value = {
  103. .integer_list = {
  104. scissor_box.x(),
  105. scissor_box.y(),
  106. scissor_box.width(),
  107. scissor_box.height(),
  108. } }
  109. };
  110. } break;
  111. case GL_STENCIL_BITS:
  112. return ContextParameter { .type = GL_INT, .value = { .integer_value = sizeof(float) * 8 } };
  113. case GL_STENCIL_TEST:
  114. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_stencil_test_enabled } };
  115. case GL_TEXTURE_1D:
  116. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_active_texture_unit->texture_1d_enabled() } };
  117. case GL_TEXTURE_2D:
  118. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_active_texture_unit->texture_2d_enabled() } };
  119. case GL_TEXTURE_3D:
  120. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_active_texture_unit->texture_3d_enabled() } };
  121. case GL_TEXTURE_CUBE_MAP:
  122. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_active_texture_unit->texture_cube_map_enabled() } };
  123. case GL_UNPACK_ALIGNMENT:
  124. return ContextParameter { .type = GL_INT, .value = { .integer_value = m_unpack_alignment } };
  125. case GL_UNPACK_ROW_LENGTH:
  126. return ContextParameter { .type = GL_INT, .value = { .integer_value = m_unpack_row_length } };
  127. default:
  128. dbgln_if(GL_DEBUG, "get_context_parameter({:#x}): unknown context parameter", name);
  129. return {};
  130. }
  131. }
  132. void SoftwareGLContext::gl_begin(GLenum mode)
  133. {
  134. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_begin, mode);
  135. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  136. RETURN_WITH_ERROR_IF(mode < GL_TRIANGLES || mode > GL_POLYGON, GL_INVALID_ENUM);
  137. m_current_draw_mode = mode;
  138. m_in_draw_state = true; // Certain commands will now generate an error
  139. }
  140. void SoftwareGLContext::gl_clear(GLbitfield mask)
  141. {
  142. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_clear, mask);
  143. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  144. RETURN_WITH_ERROR_IF(mask & ~(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT), GL_INVALID_ENUM);
  145. if (mask & GL_COLOR_BUFFER_BIT)
  146. m_rasterizer.clear_color(m_clear_color);
  147. if (mask & GL_DEPTH_BUFFER_BIT)
  148. m_rasterizer.clear_depth(static_cast<float>(m_clear_depth));
  149. // FIXME: implement GL_STENCIL_BUFFER_BIT
  150. }
  151. void SoftwareGLContext::gl_clear_color(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
  152. {
  153. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_clear_color, red, green, blue, alpha);
  154. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  155. m_clear_color = { red, green, blue, alpha };
  156. }
  157. void SoftwareGLContext::gl_clear_depth(GLdouble depth)
  158. {
  159. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_clear_depth, depth);
  160. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  161. m_clear_depth = depth;
  162. }
  163. void SoftwareGLContext::gl_clear_stencil(GLint s)
  164. {
  165. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_clear_stencil, s);
  166. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  167. // FIXME: "s is masked with 2^m - 1 , where m is the number of bits in the stencil buffer"
  168. m_clear_stencil = s;
  169. }
  170. void SoftwareGLContext::gl_color(GLdouble r, GLdouble g, GLdouble b, GLdouble a)
  171. {
  172. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_color, r, g, b, a);
  173. m_current_vertex_color = { (float)r, (float)g, (float)b, (float)a };
  174. }
  175. void SoftwareGLContext::gl_end()
  176. {
  177. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_end);
  178. // Make sure we had a `glBegin` before this call...
  179. RETURN_WITH_ERROR_IF(!m_in_draw_state, GL_INVALID_OPERATION);
  180. m_in_draw_state = false;
  181. // FIXME: Add support for the remaining primitive types.
  182. if (m_current_draw_mode != GL_TRIANGLES
  183. && m_current_draw_mode != GL_TRIANGLE_FAN
  184. && m_current_draw_mode != GL_TRIANGLE_STRIP
  185. && m_current_draw_mode != GL_QUADS
  186. && m_current_draw_mode != GL_POLYGON) {
  187. m_vertex_list.clear_with_capacity();
  188. dbgln_if(GL_DEBUG, "gl_end: draw mode {:#x} unsupported", m_current_draw_mode);
  189. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  190. }
  191. m_bound_texture_units.clear();
  192. for (auto& texture_unit : m_texture_units) {
  193. if (texture_unit.is_bound())
  194. m_bound_texture_units.append(texture_unit);
  195. }
  196. m_rasterizer.draw_primitives(m_current_draw_mode, m_projection_matrix * m_model_view_matrix, m_texture_matrix, m_vertex_list, m_bound_texture_units);
  197. m_vertex_list.clear_with_capacity();
  198. }
  199. void SoftwareGLContext::gl_frustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near_val, GLdouble far_val)
  200. {
  201. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_frustum, left, right, bottom, top, near_val, far_val);
  202. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  203. // Let's do some math!
  204. // FIXME: Are we losing too much precision by doing this?
  205. float a = static_cast<float>((right + left) / (right - left));
  206. float b = static_cast<float>((top + bottom) / (top - bottom));
  207. float c = static_cast<float>(-((far_val + near_val) / (far_val - near_val)));
  208. float d = static_cast<float>(-((2 * (far_val * near_val)) / (far_val - near_val)));
  209. FloatMatrix4x4 frustum {
  210. ((2 * (float)near_val) / ((float)right - (float)left)), 0, a, 0,
  211. 0, ((2 * (float)near_val) / ((float)top - (float)bottom)), b, 0,
  212. 0, 0, c, d,
  213. 0, 0, -1, 0
  214. };
  215. if (m_current_matrix_mode == GL_PROJECTION)
  216. m_projection_matrix = m_projection_matrix * frustum;
  217. else if (m_current_matrix_mode == GL_MODELVIEW)
  218. m_projection_matrix = m_model_view_matrix * frustum;
  219. else if (m_current_matrix_mode == GL_TEXTURE)
  220. m_texture_matrix = m_texture_matrix * frustum;
  221. else
  222. VERIFY_NOT_REACHED();
  223. }
  224. void SoftwareGLContext::gl_ortho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near_val, GLdouble far_val)
  225. {
  226. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_ortho, left, right, bottom, top, near_val, far_val);
  227. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  228. RETURN_WITH_ERROR_IF(left == right || bottom == top || near_val == far_val, GL_INVALID_VALUE);
  229. auto rl = right - left;
  230. auto tb = top - bottom;
  231. auto fn = far_val - near_val;
  232. auto tx = -(right + left) / rl;
  233. auto ty = -(top + bottom) / tb;
  234. auto tz = -(far_val + near_val) / fn;
  235. FloatMatrix4x4 projection {
  236. static_cast<float>(2 / rl), 0, 0, static_cast<float>(tx),
  237. 0, static_cast<float>(2 / tb), 0, static_cast<float>(ty),
  238. 0, 0, static_cast<float>(-2 / fn), static_cast<float>(tz),
  239. 0, 0, 0, 1
  240. };
  241. if (m_current_matrix_mode == GL_PROJECTION)
  242. m_projection_matrix = m_projection_matrix * projection;
  243. else if (m_current_matrix_mode == GL_MODELVIEW)
  244. m_projection_matrix = m_model_view_matrix * projection;
  245. else if (m_current_matrix_mode == GL_TEXTURE)
  246. m_texture_matrix = m_texture_matrix * projection;
  247. else
  248. VERIFY_NOT_REACHED();
  249. }
  250. GLenum SoftwareGLContext::gl_get_error()
  251. {
  252. if (m_in_draw_state)
  253. return GL_INVALID_OPERATION;
  254. auto last_error = m_error;
  255. m_error = GL_NO_ERROR;
  256. return last_error;
  257. }
  258. GLubyte* SoftwareGLContext::gl_get_string(GLenum name)
  259. {
  260. RETURN_VALUE_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION, nullptr);
  261. switch (name) {
  262. case GL_VENDOR:
  263. return reinterpret_cast<GLubyte*>(const_cast<char*>("The SerenityOS Developers"));
  264. case GL_RENDERER:
  265. return reinterpret_cast<GLubyte*>(const_cast<char*>("SerenityOS OpenGL"));
  266. case GL_VERSION:
  267. return reinterpret_cast<GLubyte*>(const_cast<char*>("1.5"));
  268. case GL_EXTENSIONS:
  269. return reinterpret_cast<GLubyte*>(const_cast<char*>(""));
  270. case GL_SHADING_LANGUAGE_VERSION:
  271. return reinterpret_cast<GLubyte*>(const_cast<char*>("0.0"));
  272. default:
  273. dbgln_if(GL_DEBUG, "gl_get_string({:#x}): unknown name", name);
  274. break;
  275. }
  276. RETURN_VALUE_WITH_ERROR_IF(true, GL_INVALID_ENUM, nullptr);
  277. }
  278. void SoftwareGLContext::gl_load_identity()
  279. {
  280. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_load_identity);
  281. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  282. if (m_current_matrix_mode == GL_PROJECTION)
  283. m_projection_matrix = FloatMatrix4x4::identity();
  284. else if (m_current_matrix_mode == GL_MODELVIEW)
  285. m_model_view_matrix = FloatMatrix4x4::identity();
  286. else if (m_current_matrix_mode == GL_TEXTURE)
  287. m_texture_matrix = FloatMatrix4x4::identity();
  288. else
  289. VERIFY_NOT_REACHED();
  290. }
  291. void SoftwareGLContext::gl_load_matrix(const FloatMatrix4x4& matrix)
  292. {
  293. APPEND_TO_CALL_LIST_WITH_ARG_AND_RETURN_IF_NEEDED(gl_load_matrix, matrix);
  294. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  295. if (m_current_matrix_mode == GL_PROJECTION)
  296. m_projection_matrix = matrix;
  297. else if (m_current_matrix_mode == GL_MODELVIEW)
  298. m_model_view_matrix = matrix;
  299. else if (m_current_matrix_mode == GL_TEXTURE)
  300. m_texture_matrix = matrix;
  301. else
  302. VERIFY_NOT_REACHED();
  303. }
  304. void SoftwareGLContext::gl_matrix_mode(GLenum mode)
  305. {
  306. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_matrix_mode, mode);
  307. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  308. RETURN_WITH_ERROR_IF(mode < GL_MODELVIEW || mode > GL_TEXTURE, GL_INVALID_ENUM);
  309. m_current_matrix_mode = mode;
  310. }
  311. void SoftwareGLContext::gl_push_matrix()
  312. {
  313. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_push_matrix);
  314. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  315. dbgln_if(GL_DEBUG, "glPushMatrix(): Pushing matrix to the matrix stack (matrix_mode {})", m_current_matrix_mode);
  316. switch (m_current_matrix_mode) {
  317. case GL_PROJECTION:
  318. RETURN_WITH_ERROR_IF(m_projection_matrix_stack.size() >= PROJECTION_MATRIX_STACK_LIMIT, GL_STACK_OVERFLOW);
  319. m_projection_matrix_stack.append(m_projection_matrix);
  320. break;
  321. case GL_MODELVIEW:
  322. RETURN_WITH_ERROR_IF(m_model_view_matrix_stack.size() >= MODELVIEW_MATRIX_STACK_LIMIT, GL_STACK_OVERFLOW);
  323. m_model_view_matrix_stack.append(m_model_view_matrix);
  324. break;
  325. case GL_TEXTURE:
  326. RETURN_WITH_ERROR_IF(m_texture_matrix_stack.size() >= TEXTURE_MATRIX_STACK_LIMIT, GL_STACK_OVERFLOW);
  327. m_texture_matrix_stack.append(m_texture_matrix);
  328. break;
  329. default:
  330. VERIFY_NOT_REACHED();
  331. }
  332. }
  333. void SoftwareGLContext::gl_pop_matrix()
  334. {
  335. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_pop_matrix);
  336. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  337. dbgln_if(GL_DEBUG, "glPopMatrix(): Popping matrix from matrix stack (matrix_mode = {})", m_current_matrix_mode);
  338. switch (m_current_matrix_mode) {
  339. case GL_PROJECTION:
  340. RETURN_WITH_ERROR_IF(m_projection_matrix_stack.size() == 0, GL_STACK_UNDERFLOW);
  341. m_projection_matrix = m_projection_matrix_stack.take_last();
  342. break;
  343. case GL_MODELVIEW:
  344. RETURN_WITH_ERROR_IF(m_model_view_matrix_stack.size() == 0, GL_STACK_UNDERFLOW);
  345. m_model_view_matrix = m_model_view_matrix_stack.take_last();
  346. break;
  347. case GL_TEXTURE:
  348. RETURN_WITH_ERROR_IF(m_texture_matrix_stack.size() == 0, GL_STACK_UNDERFLOW);
  349. m_texture_matrix = m_texture_matrix_stack.take_last();
  350. break;
  351. default:
  352. VERIFY_NOT_REACHED();
  353. }
  354. }
  355. void SoftwareGLContext::gl_mult_matrix(FloatMatrix4x4 const& matrix)
  356. {
  357. APPEND_TO_CALL_LIST_WITH_ARG_AND_RETURN_IF_NEEDED(gl_mult_matrix, matrix);
  358. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  359. if (m_current_matrix_mode == GL_MODELVIEW)
  360. m_model_view_matrix = m_model_view_matrix * matrix;
  361. else if (m_current_matrix_mode == GL_PROJECTION)
  362. m_projection_matrix = m_projection_matrix * matrix;
  363. else if (m_current_matrix_mode == GL_TEXTURE)
  364. m_texture_matrix = m_texture_matrix * matrix;
  365. else
  366. VERIFY_NOT_REACHED();
  367. }
  368. void SoftwareGLContext::gl_rotate(GLdouble angle, GLdouble x, GLdouble y, GLdouble z)
  369. {
  370. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_rotate, angle, x, y, z);
  371. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  372. FloatVector3 axis = { (float)x, (float)y, (float)z };
  373. axis.normalize();
  374. auto rotation_mat = Gfx::rotation_matrix(axis, static_cast<float>(angle * M_PI * 2 / 360));
  375. if (m_current_matrix_mode == GL_MODELVIEW)
  376. m_model_view_matrix = m_model_view_matrix * rotation_mat;
  377. else if (m_current_matrix_mode == GL_PROJECTION)
  378. m_projection_matrix = m_projection_matrix * rotation_mat;
  379. else if (m_current_matrix_mode == GL_TEXTURE)
  380. m_texture_matrix = m_texture_matrix * rotation_mat;
  381. else
  382. VERIFY_NOT_REACHED();
  383. }
  384. void SoftwareGLContext::gl_scale(GLdouble x, GLdouble y, GLdouble z)
  385. {
  386. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_scale, x, y, z);
  387. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  388. auto scale_matrix = Gfx::scale_matrix(FloatVector3 { static_cast<float>(x), static_cast<float>(y), static_cast<float>(z) });
  389. if (m_current_matrix_mode == GL_MODELVIEW)
  390. m_model_view_matrix = m_model_view_matrix * scale_matrix;
  391. else if (m_current_matrix_mode == GL_PROJECTION)
  392. m_projection_matrix = m_projection_matrix * scale_matrix;
  393. else if (m_current_matrix_mode == GL_TEXTURE)
  394. m_texture_matrix = m_texture_matrix * scale_matrix;
  395. else
  396. VERIFY_NOT_REACHED();
  397. }
  398. void SoftwareGLContext::gl_translate(GLdouble x, GLdouble y, GLdouble z)
  399. {
  400. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_translate, x, y, z);
  401. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  402. auto translation_matrix = Gfx::translation_matrix(FloatVector3 { static_cast<float>(x), static_cast<float>(y), static_cast<float>(z) });
  403. if (m_current_matrix_mode == GL_MODELVIEW)
  404. m_model_view_matrix = m_model_view_matrix * translation_matrix;
  405. else if (m_current_matrix_mode == GL_PROJECTION)
  406. m_projection_matrix = m_projection_matrix * translation_matrix;
  407. else if (m_current_matrix_mode == GL_TEXTURE)
  408. m_texture_matrix = m_texture_matrix * translation_matrix;
  409. else
  410. VERIFY_NOT_REACHED();
  411. }
  412. void SoftwareGLContext::gl_vertex(GLdouble x, GLdouble y, GLdouble z, GLdouble w)
  413. {
  414. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_vertex, x, y, z, w);
  415. SoftGPU::Vertex vertex;
  416. vertex.position = { static_cast<float>(x), static_cast<float>(y), static_cast<float>(z), static_cast<float>(w) };
  417. vertex.color = m_current_vertex_color;
  418. vertex.tex_coord = m_current_vertex_tex_coord;
  419. vertex.normal = m_current_vertex_normal;
  420. m_vertex_list.append(vertex);
  421. }
  422. // FIXME: We need to add `r` and `q` to our GLVertex?!
  423. void SoftwareGLContext::gl_tex_coord(GLfloat s, GLfloat t, GLfloat r, GLfloat q)
  424. {
  425. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_coord, s, t, r, q);
  426. m_current_vertex_tex_coord = { s, t, r, q };
  427. }
  428. void SoftwareGLContext::gl_viewport(GLint x, GLint y, GLsizei width, GLsizei height)
  429. {
  430. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_viewport, x, y, width, height);
  431. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  432. (void)(x);
  433. (void)(y);
  434. (void)(width);
  435. (void)(height);
  436. }
  437. void SoftwareGLContext::gl_enable(GLenum capability)
  438. {
  439. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_enable, capability);
  440. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  441. auto rasterizer_options = m_rasterizer.options();
  442. bool update_rasterizer_options = false;
  443. switch (capability) {
  444. case GL_CULL_FACE:
  445. m_cull_faces = true;
  446. rasterizer_options.enable_culling = true;
  447. update_rasterizer_options = true;
  448. break;
  449. case GL_DEPTH_TEST:
  450. m_depth_test_enabled = true;
  451. rasterizer_options.enable_depth_test = true;
  452. update_rasterizer_options = true;
  453. break;
  454. case GL_BLEND:
  455. m_blend_enabled = true;
  456. rasterizer_options.enable_blending = true;
  457. update_rasterizer_options = true;
  458. break;
  459. case GL_ALPHA_TEST:
  460. m_alpha_test_enabled = true;
  461. rasterizer_options.enable_alpha_test = true;
  462. update_rasterizer_options = true;
  463. break;
  464. case GL_DITHER:
  465. m_dither_enabled = true;
  466. break;
  467. case GL_FOG:
  468. rasterizer_options.fog_enabled = true;
  469. update_rasterizer_options = true;
  470. break;
  471. case GL_LIGHTING:
  472. m_lighting_enabled = true;
  473. break;
  474. case GL_SCISSOR_TEST:
  475. rasterizer_options.scissor_enabled = true;
  476. update_rasterizer_options = true;
  477. break;
  478. case GL_STENCIL_TEST:
  479. m_stencil_test_enabled = true;
  480. break;
  481. case GL_TEXTURE_1D:
  482. m_active_texture_unit->set_texture_1d_enabled(true);
  483. break;
  484. case GL_TEXTURE_2D:
  485. m_active_texture_unit->set_texture_2d_enabled(true);
  486. break;
  487. case GL_TEXTURE_3D:
  488. m_active_texture_unit->set_texture_3d_enabled(true);
  489. break;
  490. case GL_TEXTURE_CUBE_MAP:
  491. m_active_texture_unit->set_texture_cube_map_enabled(true);
  492. break;
  493. default:
  494. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  495. }
  496. if (update_rasterizer_options)
  497. m_rasterizer.set_options(rasterizer_options);
  498. }
  499. void SoftwareGLContext::gl_disable(GLenum capability)
  500. {
  501. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_disable, capability);
  502. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  503. auto rasterizer_options = m_rasterizer.options();
  504. bool update_rasterizer_options = false;
  505. switch (capability) {
  506. case GL_CULL_FACE:
  507. m_cull_faces = false;
  508. rasterizer_options.enable_culling = false;
  509. update_rasterizer_options = true;
  510. break;
  511. case GL_DEPTH_TEST:
  512. m_depth_test_enabled = false;
  513. rasterizer_options.enable_depth_test = false;
  514. update_rasterizer_options = true;
  515. break;
  516. case GL_BLEND:
  517. m_blend_enabled = false;
  518. rasterizer_options.enable_blending = false;
  519. update_rasterizer_options = true;
  520. break;
  521. case GL_ALPHA_TEST:
  522. m_alpha_test_enabled = false;
  523. rasterizer_options.enable_alpha_test = false;
  524. update_rasterizer_options = true;
  525. break;
  526. case GL_DITHER:
  527. m_dither_enabled = false;
  528. break;
  529. case GL_FOG:
  530. rasterizer_options.fog_enabled = false;
  531. update_rasterizer_options = true;
  532. break;
  533. case GL_LIGHTING:
  534. m_lighting_enabled = false;
  535. break;
  536. case GL_SCISSOR_TEST:
  537. rasterizer_options.scissor_enabled = false;
  538. update_rasterizer_options = true;
  539. break;
  540. case GL_STENCIL_TEST:
  541. m_stencil_test_enabled = false;
  542. break;
  543. case GL_TEXTURE_1D:
  544. m_active_texture_unit->set_texture_1d_enabled(false);
  545. break;
  546. case GL_TEXTURE_2D:
  547. m_active_texture_unit->set_texture_2d_enabled(false);
  548. break;
  549. case GL_TEXTURE_3D:
  550. m_active_texture_unit->set_texture_3d_enabled(false);
  551. break;
  552. case GL_TEXTURE_CUBE_MAP:
  553. m_active_texture_unit->set_texture_cube_map_enabled(false);
  554. break;
  555. default:
  556. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  557. }
  558. if (update_rasterizer_options)
  559. m_rasterizer.set_options(rasterizer_options);
  560. }
  561. GLboolean SoftwareGLContext::gl_is_enabled(GLenum capability)
  562. {
  563. RETURN_VALUE_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION, 0);
  564. auto rasterizer_options = m_rasterizer.options();
  565. switch (capability) {
  566. case GL_CULL_FACE:
  567. return m_cull_faces;
  568. case GL_DEPTH_TEST:
  569. return m_depth_test_enabled;
  570. case GL_BLEND:
  571. return m_blend_enabled;
  572. case GL_ALPHA_TEST:
  573. return m_alpha_test_enabled;
  574. case GL_DITHER:
  575. return m_dither_enabled;
  576. case GL_FOG:
  577. return rasterizer_options.fog_enabled;
  578. case GL_LIGHTING:
  579. return m_lighting_enabled;
  580. case GL_SCISSOR_TEST:
  581. return rasterizer_options.scissor_enabled;
  582. case GL_STENCIL_TEST:
  583. return m_stencil_test_enabled;
  584. }
  585. RETURN_VALUE_WITH_ERROR_IF(true, GL_INVALID_ENUM, 0);
  586. }
  587. void SoftwareGLContext::gl_gen_textures(GLsizei n, GLuint* textures)
  588. {
  589. RETURN_WITH_ERROR_IF(n < 0, GL_INVALID_VALUE);
  590. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  591. m_name_allocator.allocate(n, textures);
  592. // Initialize all texture names with a nullptr
  593. for (auto i = 0; i < n; i++) {
  594. GLuint name = textures[i];
  595. m_allocated_textures.set(name, nullptr);
  596. }
  597. }
  598. void SoftwareGLContext::gl_delete_textures(GLsizei n, const GLuint* textures)
  599. {
  600. RETURN_WITH_ERROR_IF(n < 0, GL_INVALID_VALUE);
  601. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  602. for (auto i = 0; i < n; i++) {
  603. GLuint name = textures[i];
  604. if (name == 0)
  605. continue;
  606. m_name_allocator.free(name);
  607. auto texture_object = m_allocated_textures.find(name);
  608. if (texture_object == m_allocated_textures.end() || texture_object->value.is_null())
  609. continue;
  610. // Check all texture units
  611. for (auto& texture_unit : m_texture_units) {
  612. if (texture_object->value == texture_unit.bound_texture())
  613. texture_unit.bind_texture_to_target(GL_TEXTURE_2D, nullptr);
  614. }
  615. m_allocated_textures.remove(name);
  616. }
  617. }
  618. 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)
  619. {
  620. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  621. // We only support GL_TEXTURE_2D for now
  622. RETURN_WITH_ERROR_IF(target != GL_TEXTURE_2D, GL_INVALID_ENUM);
  623. // Check if there is actually a texture bound
  624. RETURN_WITH_ERROR_IF(target == GL_TEXTURE_2D && m_active_texture_unit->currently_bound_target() != GL_TEXTURE_2D, GL_INVALID_OPERATION);
  625. // Internal format can also be a number between 1 and 4. Symbolic formats were only added with EXT_texture, promoted to core in OpenGL 1.1
  626. if (internal_format == 1)
  627. internal_format = GL_ALPHA;
  628. else if (internal_format == 2)
  629. internal_format = GL_LUMINANCE_ALPHA;
  630. else if (internal_format == 3)
  631. internal_format = GL_RGB;
  632. else if (internal_format == 4)
  633. internal_format = GL_RGBA;
  634. // We only support symbolic constants for now
  635. RETURN_WITH_ERROR_IF(!(internal_format == GL_RGB || internal_format == GL_RGBA), GL_INVALID_ENUM);
  636. RETURN_WITH_ERROR_IF(!(type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_SHORT_5_6_5), GL_INVALID_VALUE);
  637. RETURN_WITH_ERROR_IF(level < 0 || level > Texture2D::LOG2_MAX_TEXTURE_SIZE, GL_INVALID_VALUE);
  638. RETURN_WITH_ERROR_IF(width < 0 || height < 0 || width > (2 + Texture2D::MAX_TEXTURE_SIZE) || height > (2 + Texture2D::MAX_TEXTURE_SIZE), GL_INVALID_VALUE);
  639. // Check if width and height are a power of 2
  640. RETURN_WITH_ERROR_IF((width & (width - 1)) != 0, GL_INVALID_VALUE);
  641. RETURN_WITH_ERROR_IF((height & (height - 1)) != 0, GL_INVALID_VALUE);
  642. RETURN_WITH_ERROR_IF(border < 0 || border > 1, GL_INVALID_VALUE);
  643. if (level == 0) {
  644. // FIXME: OpenGL has the concept of texture and mipmap completeness. A texture has to fulfill certain criteria to be considered complete.
  645. // Trying to render while an incomplete texture is bound will result in an error.
  646. // Here we simply create a complete device image when mipmap level 0 is attached to the texture object. This has the unfortunate side effect
  647. // that constructing GL textures in any but the default mipmap order, going from level 0 upwards will cause mip levels to stay uninitialized.
  648. // To be spec compliant we should create the device image once the texture has become complete and is used for rendering the first time.
  649. // All images that were attached before the device image was created need to be stored somewhere to be used to initialize the device image once complete.
  650. SoftGPU::ImageFormat device_format;
  651. switch (internal_format) {
  652. case GL_RGB:
  653. device_format = SoftGPU::ImageFormat::RGB888;
  654. break;
  655. case GL_RGBA:
  656. device_format = SoftGPU::ImageFormat::RGBA8888;
  657. break;
  658. default:
  659. VERIFY_NOT_REACHED();
  660. }
  661. m_active_texture_unit->bound_texture_2d()->set_device_image(m_rasterizer.create_image(device_format, width, height, 1, 999, 1));
  662. }
  663. m_active_texture_unit->bound_texture_2d()->upload_texture_data(level, internal_format, width, height, border, format, type, data, m_unpack_row_length, m_unpack_alignment);
  664. }
  665. void SoftwareGLContext::gl_tex_sub_image_2d(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid* data)
  666. {
  667. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  668. // We only support GL_TEXTURE_2D for now
  669. RETURN_WITH_ERROR_IF(target != GL_TEXTURE_2D, GL_INVALID_ENUM);
  670. // Check if there is actually a texture bound
  671. RETURN_WITH_ERROR_IF(target == GL_TEXTURE_2D && m_active_texture_unit->currently_bound_target() != GL_TEXTURE_2D, GL_INVALID_OPERATION);
  672. // We only support symbolic constants for now
  673. RETURN_WITH_ERROR_IF(!(format == GL_RGBA || format == GL_RGB), GL_INVALID_VALUE);
  674. RETURN_WITH_ERROR_IF(!(type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_SHORT_5_6_5), GL_INVALID_VALUE);
  675. RETURN_WITH_ERROR_IF(level < 0 || level > Texture2D::LOG2_MAX_TEXTURE_SIZE, GL_INVALID_VALUE);
  676. RETURN_WITH_ERROR_IF(width < 0 || height < 0 || width > (2 + Texture2D::MAX_TEXTURE_SIZE) || height > (2 + Texture2D::MAX_TEXTURE_SIZE), GL_INVALID_VALUE);
  677. auto texture = m_active_texture_unit->bound_texture_2d();
  678. RETURN_WITH_ERROR_IF(xoffset < 0 || yoffset < 0 || xoffset + width > texture->width_at_lod(level) || yoffset + height > texture->height_at_lod(level), GL_INVALID_VALUE);
  679. texture->replace_sub_texture_data(level, xoffset, yoffset, width, height, format, type, data, m_unpack_row_length, m_unpack_alignment);
  680. }
  681. void SoftwareGLContext::gl_tex_parameter(GLenum target, GLenum pname, GLfloat param)
  682. {
  683. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_parameter, target, pname, param);
  684. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  685. // FIXME: We currently only support GL_TETXURE_2D targets. 1D, 3D and CUBE should also be supported (https://docs.gl/gl2/glTexParameter)
  686. RETURN_WITH_ERROR_IF(target != GL_TEXTURE_2D, GL_INVALID_ENUM);
  687. // FIXME: implement the remaining parameters. (https://docs.gl/gl2/glTexParameter)
  688. RETURN_WITH_ERROR_IF(!(pname == GL_TEXTURE_MIN_FILTER
  689. || pname == GL_TEXTURE_MAG_FILTER
  690. || pname == GL_TEXTURE_WRAP_S
  691. || pname == GL_TEXTURE_WRAP_T),
  692. GL_INVALID_ENUM);
  693. if (target == GL_TEXTURE_2D) {
  694. auto texture2d = m_active_texture_unit->bound_texture_2d();
  695. if (texture2d.is_null())
  696. return;
  697. switch (pname) {
  698. case GL_TEXTURE_MIN_FILTER:
  699. RETURN_WITH_ERROR_IF(!(param == GL_NEAREST
  700. || param == GL_LINEAR
  701. || param == GL_NEAREST_MIPMAP_NEAREST
  702. || param == GL_LINEAR_MIPMAP_NEAREST
  703. || param == GL_NEAREST_MIPMAP_LINEAR
  704. || param == GL_LINEAR_MIPMAP_LINEAR),
  705. GL_INVALID_ENUM);
  706. texture2d->sampler().set_min_filter(param);
  707. break;
  708. case GL_TEXTURE_MAG_FILTER:
  709. RETURN_WITH_ERROR_IF(!(param == GL_NEAREST
  710. || param == GL_LINEAR),
  711. GL_INVALID_ENUM);
  712. texture2d->sampler().set_mag_filter(param);
  713. break;
  714. case GL_TEXTURE_WRAP_S:
  715. RETURN_WITH_ERROR_IF(!(param == GL_CLAMP
  716. || param == GL_CLAMP_TO_BORDER
  717. || param == GL_CLAMP_TO_EDGE
  718. || param == GL_MIRRORED_REPEAT
  719. || param == GL_REPEAT),
  720. GL_INVALID_ENUM);
  721. texture2d->sampler().set_wrap_s_mode(param);
  722. break;
  723. case GL_TEXTURE_WRAP_T:
  724. RETURN_WITH_ERROR_IF(!(param == GL_CLAMP
  725. || param == GL_CLAMP_TO_BORDER
  726. || param == GL_CLAMP_TO_EDGE
  727. || param == GL_MIRRORED_REPEAT
  728. || param == GL_REPEAT),
  729. GL_INVALID_ENUM);
  730. texture2d->sampler().set_wrap_t_mode(param);
  731. break;
  732. default:
  733. VERIFY_NOT_REACHED();
  734. }
  735. }
  736. }
  737. void SoftwareGLContext::gl_front_face(GLenum face)
  738. {
  739. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_front_face, face);
  740. RETURN_WITH_ERROR_IF(face < GL_CW || face > GL_CCW, GL_INVALID_ENUM);
  741. m_front_face = face;
  742. auto rasterizer_options = m_rasterizer.options();
  743. rasterizer_options.front_face = face;
  744. m_rasterizer.set_options(rasterizer_options);
  745. }
  746. void SoftwareGLContext::gl_cull_face(GLenum cull_mode)
  747. {
  748. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_cull_face, cull_mode);
  749. RETURN_WITH_ERROR_IF(cull_mode < GL_FRONT || cull_mode > GL_FRONT_AND_BACK, GL_INVALID_ENUM);
  750. m_culled_sides = cull_mode;
  751. auto rasterizer_options = m_rasterizer.options();
  752. rasterizer_options.culled_sides = cull_mode;
  753. m_rasterizer.set_options(rasterizer_options);
  754. }
  755. GLuint SoftwareGLContext::gl_gen_lists(GLsizei range)
  756. {
  757. RETURN_VALUE_WITH_ERROR_IF(range <= 0, GL_INVALID_VALUE, 0);
  758. RETURN_VALUE_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION, 0);
  759. auto initial_entry = m_listings.size();
  760. m_listings.resize(range + initial_entry);
  761. return initial_entry + 1;
  762. }
  763. void SoftwareGLContext::invoke_list(size_t list_index)
  764. {
  765. auto& listing = m_listings[list_index - 1];
  766. for (auto& entry : listing.entries) {
  767. entry.function.visit([&](auto& function) {
  768. entry.arguments.visit([&](auto& arguments) {
  769. auto apply = [&]<typename... Args>(Args && ... args)
  770. {
  771. if constexpr (requires { (this->*function)(forward<Args>(args)...); })
  772. (this->*function)(forward<Args>(args)...);
  773. };
  774. arguments.apply_as_args(apply);
  775. });
  776. });
  777. }
  778. }
  779. void SoftwareGLContext::gl_call_list(GLuint list)
  780. {
  781. if (m_gl_call_depth > max_allowed_gl_call_depth)
  782. return;
  783. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_call_list, list);
  784. if (m_listings.size() < list)
  785. return;
  786. TemporaryChange change { m_gl_call_depth, m_gl_call_depth + 1 };
  787. invoke_list(list);
  788. }
  789. void SoftwareGLContext::gl_call_lists(GLsizei n, GLenum type, void const* lists)
  790. {
  791. if (m_gl_call_depth > max_allowed_gl_call_depth)
  792. return;
  793. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_call_lists, n, type, lists);
  794. RETURN_WITH_ERROR_IF(n < 0, GL_INVALID_VALUE);
  795. RETURN_WITH_ERROR_IF(!(type == GL_BYTE
  796. || type == GL_UNSIGNED_BYTE
  797. || type == GL_SHORT
  798. || type == GL_UNSIGNED_SHORT
  799. || type == GL_INT
  800. || type == GL_UNSIGNED_INT
  801. || type == GL_FLOAT
  802. || type == GL_2_BYTES
  803. || type == GL_3_BYTES
  804. || type == GL_4_BYTES),
  805. GL_INVALID_ENUM);
  806. TemporaryChange change { m_gl_call_depth, m_gl_call_depth + 1 };
  807. auto invoke_all_lists = [&]<typename T>(T const* lists) {
  808. for (int i = 0; i < n; ++i) {
  809. auto list = static_cast<size_t>(lists[i]);
  810. invoke_list(m_list_base + list);
  811. }
  812. };
  813. switch (type) {
  814. case GL_BYTE:
  815. invoke_all_lists(static_cast<GLbyte const*>(lists));
  816. break;
  817. case GL_UNSIGNED_BYTE:
  818. invoke_all_lists(static_cast<GLubyte const*>(lists));
  819. break;
  820. case GL_SHORT:
  821. invoke_all_lists(static_cast<GLshort const*>(lists));
  822. break;
  823. case GL_UNSIGNED_SHORT:
  824. invoke_all_lists(static_cast<GLushort const*>(lists));
  825. break;
  826. case GL_INT:
  827. invoke_all_lists(static_cast<GLint const*>(lists));
  828. break;
  829. case GL_UNSIGNED_INT:
  830. invoke_all_lists(static_cast<GLuint const*>(lists));
  831. break;
  832. case GL_FLOAT:
  833. invoke_all_lists(static_cast<GLfloat const*>(lists));
  834. break;
  835. case GL_2_BYTES:
  836. case GL_3_BYTES:
  837. case GL_4_BYTES:
  838. dbgln("SoftwareGLContext FIXME: unimplemented glCallLists() with type {}", type);
  839. break;
  840. default:
  841. VERIFY_NOT_REACHED();
  842. }
  843. }
  844. void SoftwareGLContext::gl_delete_lists(GLuint list, GLsizei range)
  845. {
  846. if (m_listings.size() < list || m_listings.size() <= list + range)
  847. return;
  848. for (auto& entry : m_listings.span().slice(list - 1, range))
  849. entry.entries.clear_with_capacity();
  850. }
  851. void SoftwareGLContext::gl_list_base(GLuint base)
  852. {
  853. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_list_base, base);
  854. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  855. m_list_base = base;
  856. }
  857. void SoftwareGLContext::gl_end_list()
  858. {
  859. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  860. RETURN_WITH_ERROR_IF(!m_current_listing_index.has_value(), GL_INVALID_OPERATION);
  861. m_listings[m_current_listing_index->index] = move(m_current_listing_index->listing);
  862. m_current_listing_index.clear();
  863. }
  864. void SoftwareGLContext::gl_new_list(GLuint list, GLenum mode)
  865. {
  866. RETURN_WITH_ERROR_IF(list == 0, GL_INVALID_VALUE);
  867. RETURN_WITH_ERROR_IF(mode != GL_COMPILE && mode != GL_COMPILE_AND_EXECUTE, GL_INVALID_ENUM);
  868. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  869. RETURN_WITH_ERROR_IF(m_current_listing_index.has_value(), GL_INVALID_OPERATION);
  870. if (m_listings.size() < list)
  871. return;
  872. m_current_listing_index = CurrentListing { {}, static_cast<size_t>(list - 1), mode };
  873. }
  874. GLboolean SoftwareGLContext::gl_is_list(GLuint list)
  875. {
  876. RETURN_VALUE_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION, GL_FALSE);
  877. return list < m_listings.size() ? GL_TRUE : GL_FALSE;
  878. }
  879. void SoftwareGLContext::gl_flush()
  880. {
  881. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  882. // No-op since SoftwareGLContext is completely synchronous at the moment
  883. }
  884. void SoftwareGLContext::gl_finish()
  885. {
  886. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  887. // No-op since SoftwareGLContext is completely synchronous at the moment
  888. }
  889. void SoftwareGLContext::gl_blend_func(GLenum src_factor, GLenum dst_factor)
  890. {
  891. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_blend_func, src_factor, dst_factor);
  892. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  893. // FIXME: The list of allowed enums differs between API versions
  894. // This was taken from the 2.0 spec on https://docs.gl/gl2/glBlendFunc
  895. RETURN_WITH_ERROR_IF(!(src_factor == GL_ZERO
  896. || src_factor == GL_ONE
  897. || src_factor == GL_SRC_COLOR
  898. || src_factor == GL_ONE_MINUS_SRC_COLOR
  899. || src_factor == GL_DST_COLOR
  900. || src_factor == GL_ONE_MINUS_DST_COLOR
  901. || src_factor == GL_SRC_ALPHA
  902. || src_factor == GL_ONE_MINUS_SRC_ALPHA
  903. || src_factor == GL_DST_ALPHA
  904. || src_factor == GL_ONE_MINUS_DST_ALPHA
  905. || src_factor == GL_CONSTANT_COLOR
  906. || src_factor == GL_ONE_MINUS_CONSTANT_COLOR
  907. || src_factor == GL_CONSTANT_ALPHA
  908. || src_factor == GL_ONE_MINUS_CONSTANT_ALPHA
  909. || src_factor == GL_SRC_ALPHA_SATURATE),
  910. GL_INVALID_ENUM);
  911. RETURN_WITH_ERROR_IF(!(dst_factor == GL_ZERO
  912. || dst_factor == GL_ONE
  913. || dst_factor == GL_SRC_COLOR
  914. || dst_factor == GL_ONE_MINUS_SRC_COLOR
  915. || dst_factor == GL_DST_COLOR
  916. || dst_factor == GL_ONE_MINUS_DST_COLOR
  917. || dst_factor == GL_SRC_ALPHA
  918. || dst_factor == GL_ONE_MINUS_SRC_ALPHA
  919. || dst_factor == GL_DST_ALPHA
  920. || dst_factor == GL_ONE_MINUS_DST_ALPHA
  921. || dst_factor == GL_CONSTANT_COLOR
  922. || dst_factor == GL_ONE_MINUS_CONSTANT_COLOR
  923. || dst_factor == GL_CONSTANT_ALPHA
  924. || dst_factor == GL_ONE_MINUS_CONSTANT_ALPHA),
  925. GL_INVALID_ENUM);
  926. m_blend_source_factor = src_factor;
  927. m_blend_destination_factor = dst_factor;
  928. auto options = m_rasterizer.options();
  929. options.blend_source_factor = m_blend_source_factor;
  930. options.blend_destination_factor = m_blend_destination_factor;
  931. m_rasterizer.set_options(options);
  932. }
  933. void SoftwareGLContext::gl_shade_model(GLenum mode)
  934. {
  935. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_shade_model, mode);
  936. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  937. RETURN_WITH_ERROR_IF(mode != GL_FLAT && mode != GL_SMOOTH, GL_INVALID_ENUM);
  938. auto options = m_rasterizer.options();
  939. options.shade_smooth = (mode == GL_SMOOTH);
  940. m_rasterizer.set_options(options);
  941. }
  942. void SoftwareGLContext::gl_alpha_func(GLenum func, GLclampf ref)
  943. {
  944. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_alpha_func, func, ref);
  945. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  946. RETURN_WITH_ERROR_IF(func < GL_NEVER || func > GL_ALWAYS, GL_INVALID_ENUM);
  947. m_alpha_test_func = func;
  948. m_alpha_test_ref_value = ref;
  949. auto options = m_rasterizer.options();
  950. options.alpha_test_func = m_alpha_test_func;
  951. options.alpha_test_ref_value = m_alpha_test_ref_value;
  952. m_rasterizer.set_options(options);
  953. }
  954. void SoftwareGLContext::gl_hint(GLenum target, GLenum mode)
  955. {
  956. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_hint, target, mode);
  957. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  958. RETURN_WITH_ERROR_IF(target != GL_PERSPECTIVE_CORRECTION_HINT
  959. && target != GL_POINT_SMOOTH_HINT
  960. && target != GL_LINE_SMOOTH_HINT
  961. && target != GL_POLYGON_SMOOTH_HINT
  962. && target != GL_FOG_HINT
  963. && target != GL_GENERATE_MIPMAP_HINT
  964. && target != GL_TEXTURE_COMPRESSION_HINT,
  965. GL_INVALID_ENUM);
  966. RETURN_WITH_ERROR_IF(mode != GL_DONT_CARE
  967. && mode != GL_FASTEST
  968. && mode != GL_NICEST,
  969. GL_INVALID_ENUM);
  970. // According to the spec implementors are free to ignore glHint. So we do.
  971. }
  972. void SoftwareGLContext::gl_read_buffer(GLenum mode)
  973. {
  974. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_read_buffer, mode);
  975. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  976. // FIXME: Also allow aux buffers GL_AUX0 through GL_AUX3 here
  977. // plus any aux buffer between 0 and GL_AUX_BUFFERS
  978. RETURN_WITH_ERROR_IF(mode != GL_FRONT_LEFT
  979. && mode != GL_FRONT_RIGHT
  980. && mode != GL_BACK_LEFT
  981. && mode != GL_BACK_RIGHT
  982. && mode != GL_FRONT
  983. && mode != GL_BACK
  984. && mode != GL_LEFT
  985. && mode != GL_RIGHT,
  986. GL_INVALID_ENUM);
  987. // FIXME: We do not currently have aux buffers, so make it an invalid
  988. // operation to select anything but front or back buffers. Also we do
  989. // not allow selecting the stereoscopic RIGHT buffers since we do not
  990. // have them configured.
  991. RETURN_WITH_ERROR_IF(mode != GL_FRONT_LEFT
  992. && mode != GL_FRONT
  993. && mode != GL_BACK_LEFT
  994. && mode != GL_BACK
  995. && mode != GL_FRONT
  996. && mode != GL_BACK
  997. && mode != GL_LEFT,
  998. GL_INVALID_OPERATION);
  999. m_current_read_buffer = mode;
  1000. }
  1001. void SoftwareGLContext::gl_draw_buffer(GLenum buffer)
  1002. {
  1003. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_draw_buffer, buffer);
  1004. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1005. // FIXME: Also allow aux buffers GL_AUX0 through GL_AUX3 here
  1006. // plus any aux buffer between 0 and GL_AUX_BUFFERS
  1007. RETURN_WITH_ERROR_IF(buffer != GL_NONE
  1008. && buffer != GL_FRONT_LEFT
  1009. && buffer != GL_FRONT_RIGHT
  1010. && buffer != GL_BACK_LEFT
  1011. && buffer != GL_BACK_RIGHT
  1012. && buffer != GL_FRONT
  1013. && buffer != GL_BACK
  1014. && buffer != GL_LEFT
  1015. && buffer != GL_RIGHT,
  1016. GL_INVALID_ENUM);
  1017. // FIXME: We do not currently have aux buffers, so make it an invalid
  1018. // operation to select anything but front or back buffers. Also we do
  1019. // not allow selecting the stereoscopic RIGHT buffers since we do not
  1020. // have them configured.
  1021. RETURN_WITH_ERROR_IF(buffer != GL_NONE
  1022. && buffer != GL_FRONT_LEFT
  1023. && buffer != GL_FRONT
  1024. && buffer != GL_BACK_LEFT
  1025. && buffer != GL_BACK
  1026. && buffer != GL_FRONT
  1027. && buffer != GL_BACK
  1028. && buffer != GL_LEFT,
  1029. GL_INVALID_OPERATION);
  1030. m_current_draw_buffer = buffer;
  1031. auto rasterizer_options = m_rasterizer.options();
  1032. rasterizer_options.draw_buffer = m_current_draw_buffer;
  1033. m_rasterizer.set_options(rasterizer_options);
  1034. }
  1035. void SoftwareGLContext::gl_read_pixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid* pixels)
  1036. {
  1037. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1038. RETURN_WITH_ERROR_IF(width < 0 || height < 0, GL_INVALID_VALUE);
  1039. RETURN_WITH_ERROR_IF(format != GL_COLOR_INDEX
  1040. && format != GL_STENCIL_INDEX
  1041. && format != GL_DEPTH_COMPONENT
  1042. && format != GL_RED
  1043. && format != GL_GREEN
  1044. && format != GL_BLUE
  1045. && format != GL_ALPHA
  1046. && format != GL_RGB
  1047. && format != GL_RGBA
  1048. && format != GL_LUMINANCE
  1049. && format != GL_LUMINANCE_ALPHA,
  1050. GL_INVALID_ENUM);
  1051. RETURN_WITH_ERROR_IF(type != GL_UNSIGNED_BYTE
  1052. && type != GL_BYTE
  1053. && type != GL_BITMAP
  1054. && type != GL_UNSIGNED_SHORT
  1055. && type != GL_SHORT
  1056. && type != GL_BLUE
  1057. && type != GL_UNSIGNED_INT
  1058. && type != GL_INT
  1059. && type != GL_FLOAT,
  1060. GL_INVALID_ENUM);
  1061. // FIXME: We only support RGBA buffers for now.
  1062. // Once we add support for indexed color modes do the correct check here
  1063. RETURN_WITH_ERROR_IF(format == GL_COLOR_INDEX, GL_INVALID_OPERATION);
  1064. // FIXME: We do not have stencil buffers yet
  1065. // Once we add support for stencil buffers do the correct check here
  1066. RETURN_WITH_ERROR_IF(format == GL_STENCIL_INDEX, GL_INVALID_OPERATION);
  1067. if (format == GL_DEPTH_COMPONENT) {
  1068. // FIXME: This check needs to be a bit more sophisticated. Currently the buffers
  1069. // are hardcoded. Once we add proper structures for them we need to correct this check
  1070. // Error because only back buffer has a depth buffer
  1071. RETURN_WITH_ERROR_IF(m_current_read_buffer == GL_FRONT
  1072. || m_current_read_buffer == GL_FRONT_LEFT
  1073. || m_current_read_buffer == GL_FRONT_RIGHT,
  1074. GL_INVALID_OPERATION);
  1075. }
  1076. // Some helper functions for converting float values to integer types
  1077. auto float_to_i8 = [](float f) -> GLchar {
  1078. return static_cast<GLchar>((0x7f * min(max(f, 0.0f), 1.0f) - 1) / 2);
  1079. };
  1080. auto float_to_i16 = [](float f) -> GLshort {
  1081. return static_cast<GLshort>((0x7fff * min(max(f, 0.0f), 1.0f) - 1) / 2);
  1082. };
  1083. auto float_to_i32 = [](float f) -> GLint {
  1084. return static_cast<GLint>((0x7fffffff * min(max(f, 0.0f), 1.0f) - 1) / 2);
  1085. };
  1086. auto float_to_u8 = [](float f) -> GLubyte {
  1087. return static_cast<GLubyte>(0xff * min(max(f, 0.0f), 1.0f));
  1088. };
  1089. auto float_to_u16 = [](float f) -> GLushort {
  1090. return static_cast<GLushort>(0xffff * min(max(f, 0.0f), 1.0f));
  1091. };
  1092. auto float_to_u32 = [](float f) -> GLuint {
  1093. return static_cast<GLuint>(0xffffffff * min(max(f, 0.0f), 1.0f));
  1094. };
  1095. u8 component_size = 0;
  1096. switch (type) {
  1097. case GL_BYTE:
  1098. case GL_UNSIGNED_BYTE:
  1099. component_size = 1;
  1100. break;
  1101. case GL_SHORT:
  1102. case GL_UNSIGNED_SHORT:
  1103. component_size = 2;
  1104. break;
  1105. case GL_INT:
  1106. case GL_UNSIGNED_INT:
  1107. case GL_FLOAT:
  1108. component_size = 4;
  1109. break;
  1110. }
  1111. if (format == GL_DEPTH_COMPONENT) {
  1112. auto const row_stride = (width * component_size + m_pack_alignment - 1) / m_pack_alignment * m_pack_alignment;
  1113. // Read from depth buffer
  1114. for (GLsizei i = 0; i < height; ++i) {
  1115. for (GLsizei j = 0; j < width; ++j) {
  1116. float depth = m_rasterizer.get_depthbuffer_value(x + j, y + i);
  1117. auto char_ptr = reinterpret_cast<char*>(pixels) + i * row_stride + j * component_size;
  1118. switch (type) {
  1119. case GL_BYTE:
  1120. *reinterpret_cast<GLchar*>(char_ptr) = float_to_i8(depth);
  1121. break;
  1122. case GL_SHORT:
  1123. *reinterpret_cast<GLshort*>(char_ptr) = float_to_i16(depth);
  1124. break;
  1125. case GL_INT:
  1126. *reinterpret_cast<GLint*>(char_ptr) = float_to_i32(depth);
  1127. break;
  1128. case GL_UNSIGNED_BYTE:
  1129. *reinterpret_cast<GLubyte*>(char_ptr) = float_to_u8(depth);
  1130. break;
  1131. case GL_UNSIGNED_SHORT:
  1132. *reinterpret_cast<GLushort*>(char_ptr) = float_to_u16(depth);
  1133. break;
  1134. case GL_UNSIGNED_INT:
  1135. *reinterpret_cast<GLuint*>(char_ptr) = float_to_u32(depth);
  1136. break;
  1137. case GL_FLOAT:
  1138. *reinterpret_cast<GLfloat*>(char_ptr) = min(max(depth, 0.0f), 1.0f);
  1139. break;
  1140. }
  1141. }
  1142. }
  1143. return;
  1144. }
  1145. bool write_red = false;
  1146. bool write_green = false;
  1147. bool write_blue = false;
  1148. bool write_alpha = false;
  1149. size_t component_count = 0;
  1150. size_t red_offset = 0;
  1151. size_t green_offset = 0;
  1152. size_t blue_offset = 0;
  1153. size_t alpha_offset = 0;
  1154. char* red_ptr = nullptr;
  1155. char* green_ptr = nullptr;
  1156. char* blue_ptr = nullptr;
  1157. char* alpha_ptr = nullptr;
  1158. switch (format) {
  1159. case GL_RGB:
  1160. write_red = true;
  1161. write_green = true;
  1162. write_blue = true;
  1163. component_count = 3;
  1164. red_offset = 2;
  1165. green_offset = 1;
  1166. blue_offset = 0;
  1167. break;
  1168. case GL_RGBA:
  1169. write_red = true;
  1170. write_green = true;
  1171. write_blue = true;
  1172. write_alpha = true;
  1173. component_count = 4;
  1174. red_offset = 3;
  1175. green_offset = 2;
  1176. blue_offset = 1;
  1177. alpha_offset = 0;
  1178. break;
  1179. case GL_RED:
  1180. write_red = true;
  1181. component_count = 1;
  1182. red_offset = 0;
  1183. break;
  1184. case GL_GREEN:
  1185. write_green = true;
  1186. component_count = 1;
  1187. green_offset = 0;
  1188. break;
  1189. case GL_BLUE:
  1190. write_blue = true;
  1191. component_count = 1;
  1192. blue_offset = 0;
  1193. break;
  1194. case GL_ALPHA:
  1195. write_alpha = true;
  1196. component_count = 1;
  1197. alpha_offset = 0;
  1198. break;
  1199. }
  1200. auto const pixel_bytes = component_size * component_count;
  1201. auto const row_alignment_bytes = (m_pack_alignment - ((width * pixel_bytes) % m_pack_alignment)) % m_pack_alignment;
  1202. char* out_ptr = reinterpret_cast<char*>(pixels);
  1203. for (int i = 0; i < (int)height; ++i) {
  1204. for (int j = 0; j < (int)width; ++j) {
  1205. Gfx::RGBA32 color {};
  1206. if (m_current_read_buffer == GL_FRONT || m_current_read_buffer == GL_LEFT || m_current_read_buffer == GL_FRONT_LEFT) {
  1207. if (y + i >= m_frontbuffer->width() || x + j >= m_frontbuffer->height())
  1208. color = 0;
  1209. else
  1210. color = m_frontbuffer->scanline(y + i)[x + j];
  1211. } else {
  1212. color = m_rasterizer.get_backbuffer_pixel(x + j, y + i);
  1213. }
  1214. float red = ((color >> 24) & 0xff) / 255.0f;
  1215. float green = ((color >> 16) & 0xff) / 255.0f;
  1216. float blue = ((color >> 8) & 0xff) / 255.0f;
  1217. float alpha = (color & 0xff) / 255.0f;
  1218. // FIXME: Set up write pointers based on selected endianness (glPixelStore)
  1219. red_ptr = out_ptr + (component_size * red_offset);
  1220. green_ptr = out_ptr + (component_size * green_offset);
  1221. blue_ptr = out_ptr + (component_size * blue_offset);
  1222. alpha_ptr = out_ptr + (component_size * alpha_offset);
  1223. switch (type) {
  1224. case GL_BYTE:
  1225. if (write_red)
  1226. *reinterpret_cast<GLchar*>(red_ptr) = float_to_i8(red);
  1227. if (write_green)
  1228. *reinterpret_cast<GLchar*>(green_ptr) = float_to_i8(green);
  1229. if (write_blue)
  1230. *reinterpret_cast<GLchar*>(blue_ptr) = float_to_i8(blue);
  1231. if (write_alpha)
  1232. *reinterpret_cast<GLchar*>(alpha_ptr) = float_to_i8(alpha);
  1233. break;
  1234. case GL_UNSIGNED_BYTE:
  1235. if (write_red)
  1236. *reinterpret_cast<GLubyte*>(red_ptr) = float_to_u8(red);
  1237. if (write_green)
  1238. *reinterpret_cast<GLubyte*>(green_ptr) = float_to_u8(green);
  1239. if (write_blue)
  1240. *reinterpret_cast<GLubyte*>(blue_ptr) = float_to_u8(blue);
  1241. if (write_alpha)
  1242. *reinterpret_cast<GLubyte*>(alpha_ptr) = float_to_u8(alpha);
  1243. break;
  1244. case GL_SHORT:
  1245. if (write_red)
  1246. *reinterpret_cast<GLshort*>(red_ptr) = float_to_i16(red);
  1247. if (write_green)
  1248. *reinterpret_cast<GLshort*>(green_ptr) = float_to_i16(green);
  1249. if (write_blue)
  1250. *reinterpret_cast<GLshort*>(blue_ptr) = float_to_i16(blue);
  1251. if (write_alpha)
  1252. *reinterpret_cast<GLshort*>(alpha_ptr) = float_to_i16(alpha);
  1253. break;
  1254. case GL_UNSIGNED_SHORT:
  1255. if (write_red)
  1256. *reinterpret_cast<GLushort*>(red_ptr) = float_to_u16(red);
  1257. if (write_green)
  1258. *reinterpret_cast<GLushort*>(green_ptr) = float_to_u16(green);
  1259. if (write_blue)
  1260. *reinterpret_cast<GLushort*>(blue_ptr) = float_to_u16(blue);
  1261. if (write_alpha)
  1262. *reinterpret_cast<GLushort*>(alpha_ptr) = float_to_u16(alpha);
  1263. break;
  1264. case GL_INT:
  1265. if (write_red)
  1266. *reinterpret_cast<GLint*>(red_ptr) = float_to_i32(red);
  1267. if (write_green)
  1268. *reinterpret_cast<GLint*>(green_ptr) = float_to_i32(green);
  1269. if (write_blue)
  1270. *reinterpret_cast<GLint*>(blue_ptr) = float_to_i32(blue);
  1271. if (write_alpha)
  1272. *reinterpret_cast<GLint*>(alpha_ptr) = float_to_i32(alpha);
  1273. break;
  1274. case GL_UNSIGNED_INT:
  1275. if (write_red)
  1276. *reinterpret_cast<GLuint*>(red_ptr) = float_to_u32(red);
  1277. if (write_green)
  1278. *reinterpret_cast<GLuint*>(green_ptr) = float_to_u32(green);
  1279. if (write_blue)
  1280. *reinterpret_cast<GLuint*>(blue_ptr) = float_to_u32(blue);
  1281. if (write_alpha)
  1282. *reinterpret_cast<GLuint*>(alpha_ptr) = float_to_u32(alpha);
  1283. break;
  1284. case GL_FLOAT:
  1285. if (write_red)
  1286. *reinterpret_cast<GLfloat*>(red_ptr) = min(max(red, 0.0f), 1.0f);
  1287. if (write_green)
  1288. *reinterpret_cast<GLfloat*>(green_ptr) = min(max(green, 0.0f), 1.0f);
  1289. if (write_blue)
  1290. *reinterpret_cast<GLfloat*>(blue_ptr) = min(max(blue, 0.0f), 1.0f);
  1291. if (write_alpha)
  1292. *reinterpret_cast<GLfloat*>(alpha_ptr) = min(max(alpha, 0.0f), 1.0f);
  1293. break;
  1294. }
  1295. out_ptr += pixel_bytes;
  1296. }
  1297. out_ptr += row_alignment_bytes;
  1298. }
  1299. }
  1300. void SoftwareGLContext::gl_bind_texture(GLenum target, GLuint texture)
  1301. {
  1302. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1303. // FIXME: We only support GL_TEXTURE_2D for now
  1304. RETURN_WITH_ERROR_IF(target != GL_TEXTURE_2D, GL_INVALID_ENUM);
  1305. if (texture == 0) {
  1306. switch (target) {
  1307. case GL_TEXTURE_2D:
  1308. m_active_texture_unit->bind_texture_to_target(target, nullptr);
  1309. return;
  1310. default:
  1311. VERIFY_NOT_REACHED();
  1312. return;
  1313. }
  1314. }
  1315. auto it = m_allocated_textures.find(texture);
  1316. // The texture name does not exist
  1317. RETURN_WITH_ERROR_IF(it == m_allocated_textures.end(), GL_INVALID_VALUE);
  1318. auto texture_object = it->value;
  1319. // Binding a texture to a different target than it was first bound is an invalid operation
  1320. // FIXME: We only support GL_TEXTURE_2D for now
  1321. RETURN_WITH_ERROR_IF(target == GL_TEXTURE_2D && !texture_object.is_null() && !texture_object->is_texture_2d(), GL_INVALID_OPERATION);
  1322. if (!texture_object) {
  1323. // This is the first time the texture is bound. Allocate an actual texture object
  1324. switch (target) {
  1325. case GL_TEXTURE_2D:
  1326. texture_object = adopt_ref(*new Texture2D());
  1327. break;
  1328. default:
  1329. VERIFY_NOT_REACHED();
  1330. }
  1331. m_allocated_textures.set(texture, texture_object);
  1332. }
  1333. switch (target) {
  1334. case GL_TEXTURE_2D:
  1335. m_active_texture_unit->bind_texture_to_target(target, texture_object);
  1336. break;
  1337. }
  1338. }
  1339. void SoftwareGLContext::gl_active_texture(GLenum texture)
  1340. {
  1341. RETURN_WITH_ERROR_IF(texture < GL_TEXTURE0 || texture > GL_TEXTURE31, GL_INVALID_ENUM);
  1342. m_active_texture_unit = &m_texture_units.at(texture - GL_TEXTURE0);
  1343. }
  1344. void SoftwareGLContext::gl_get_booleanv(GLenum pname, GLboolean* data)
  1345. {
  1346. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1347. auto optional_parameter = get_context_parameter(pname);
  1348. RETURN_WITH_ERROR_IF(!optional_parameter.has_value(), GL_INVALID_ENUM);
  1349. auto parameter = optional_parameter.release_value();
  1350. switch (parameter.type) {
  1351. case GL_BOOL:
  1352. *data = parameter.value.boolean_value ? GL_TRUE : GL_FALSE;
  1353. break;
  1354. case GL_DOUBLE:
  1355. *data = (parameter.value.double_value == 0.0) ? GL_FALSE : GL_TRUE;
  1356. break;
  1357. case GL_INT:
  1358. *data = (parameter.value.integer_value == 0) ? GL_FALSE : GL_TRUE;
  1359. break;
  1360. default:
  1361. VERIFY_NOT_REACHED();
  1362. }
  1363. }
  1364. void SoftwareGLContext::gl_get_doublev(GLenum pname, GLdouble* params)
  1365. {
  1366. get_floating_point(pname, params);
  1367. }
  1368. void SoftwareGLContext::gl_get_floatv(GLenum pname, GLfloat* params)
  1369. {
  1370. get_floating_point(pname, params);
  1371. }
  1372. template<typename T>
  1373. void SoftwareGLContext::get_floating_point(GLenum pname, T* params)
  1374. {
  1375. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1376. // Handle special matrix cases first
  1377. auto flatten_and_assign_matrix = [&params](const FloatMatrix4x4& matrix) {
  1378. auto elements = matrix.elements();
  1379. for (size_t i = 0; i < 4; ++i)
  1380. for (size_t j = 0; j < 4; ++j)
  1381. params[i * 4 + j] = static_cast<T>(elements[i][j]);
  1382. };
  1383. switch (pname) {
  1384. case GL_MODELVIEW_MATRIX:
  1385. if (m_current_matrix_mode == GL_MODELVIEW)
  1386. flatten_and_assign_matrix(m_model_view_matrix);
  1387. else if (m_model_view_matrix_stack.is_empty())
  1388. flatten_and_assign_matrix(FloatMatrix4x4::identity());
  1389. else
  1390. flatten_and_assign_matrix(m_model_view_matrix_stack.last());
  1391. return;
  1392. case GL_PROJECTION_MATRIX:
  1393. if (m_current_matrix_mode == GL_PROJECTION)
  1394. flatten_and_assign_matrix(m_projection_matrix);
  1395. else if (m_projection_matrix_stack.is_empty())
  1396. flatten_and_assign_matrix(FloatMatrix4x4::identity());
  1397. else
  1398. flatten_and_assign_matrix(m_projection_matrix_stack.last());
  1399. return;
  1400. }
  1401. // Regular parameters
  1402. auto optional_parameter = get_context_parameter(pname);
  1403. RETURN_WITH_ERROR_IF(!optional_parameter.has_value(), GL_INVALID_ENUM);
  1404. auto parameter = optional_parameter.release_value();
  1405. switch (parameter.type) {
  1406. case GL_BOOL:
  1407. *params = parameter.value.boolean_value ? GL_TRUE : GL_FALSE;
  1408. break;
  1409. case GL_DOUBLE:
  1410. for (size_t i = 0; i < parameter.count; ++i) {
  1411. params[i] = parameter.value.double_list[i];
  1412. }
  1413. break;
  1414. case GL_INT:
  1415. for (size_t i = 0; i < parameter.count; ++i) {
  1416. params[i] = parameter.value.integer_list[i];
  1417. }
  1418. break;
  1419. default:
  1420. VERIFY_NOT_REACHED();
  1421. }
  1422. }
  1423. void SoftwareGLContext::gl_get_integerv(GLenum pname, GLint* data)
  1424. {
  1425. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1426. auto optional_parameter = get_context_parameter(pname);
  1427. RETURN_WITH_ERROR_IF(!optional_parameter.has_value(), GL_INVALID_ENUM);
  1428. auto parameter = optional_parameter.release_value();
  1429. switch (parameter.type) {
  1430. case GL_BOOL:
  1431. *data = parameter.value.boolean_value ? GL_TRUE : GL_FALSE;
  1432. break;
  1433. case GL_DOUBLE: {
  1434. double const int_range = static_cast<double>(NumericLimits<GLint>::max()) - NumericLimits<GLint>::min();
  1435. for (size_t i = 0; i < parameter.count; ++i) {
  1436. double const result_factor = (clamp(parameter.value.double_list[i], -1.0, 1.0) + 1.0) / 2.0;
  1437. data[i] = static_cast<GLint>(NumericLimits<GLint>::min() + result_factor * int_range);
  1438. }
  1439. break;
  1440. }
  1441. case GL_INT:
  1442. for (size_t i = 0; i < parameter.count; ++i) {
  1443. data[i] = parameter.value.integer_list[i];
  1444. }
  1445. break;
  1446. default:
  1447. VERIFY_NOT_REACHED();
  1448. }
  1449. }
  1450. void SoftwareGLContext::gl_depth_mask(GLboolean flag)
  1451. {
  1452. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_depth_mask, flag);
  1453. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1454. auto options = m_rasterizer.options();
  1455. options.enable_depth_write = (flag != GL_FALSE);
  1456. m_rasterizer.set_options(options);
  1457. }
  1458. void SoftwareGLContext::gl_enable_client_state(GLenum cap)
  1459. {
  1460. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1461. switch (cap) {
  1462. case GL_VERTEX_ARRAY:
  1463. m_client_side_vertex_array_enabled = true;
  1464. break;
  1465. case GL_COLOR_ARRAY:
  1466. m_client_side_color_array_enabled = true;
  1467. break;
  1468. case GL_TEXTURE_COORD_ARRAY:
  1469. m_client_side_texture_coord_array_enabled = true;
  1470. break;
  1471. default:
  1472. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1473. }
  1474. }
  1475. void SoftwareGLContext::gl_disable_client_state(GLenum cap)
  1476. {
  1477. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1478. switch (cap) {
  1479. case GL_VERTEX_ARRAY:
  1480. m_client_side_vertex_array_enabled = false;
  1481. break;
  1482. case GL_COLOR_ARRAY:
  1483. m_client_side_color_array_enabled = false;
  1484. break;
  1485. case GL_TEXTURE_COORD_ARRAY:
  1486. m_client_side_texture_coord_array_enabled = false;
  1487. break;
  1488. default:
  1489. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1490. }
  1491. }
  1492. void SoftwareGLContext::gl_vertex_pointer(GLint size, GLenum type, GLsizei stride, const void* pointer)
  1493. {
  1494. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1495. RETURN_WITH_ERROR_IF(!(size == 2 || size == 3 || size == 4), GL_INVALID_VALUE);
  1496. RETURN_WITH_ERROR_IF(!(type == GL_SHORT || type == GL_INT || type == GL_FLOAT || type == GL_DOUBLE), GL_INVALID_ENUM);
  1497. RETURN_WITH_ERROR_IF(stride < 0, GL_INVALID_VALUE);
  1498. m_client_vertex_pointer.size = size;
  1499. m_client_vertex_pointer.type = type;
  1500. m_client_vertex_pointer.stride = stride;
  1501. m_client_vertex_pointer.pointer = pointer;
  1502. }
  1503. void SoftwareGLContext::gl_color_pointer(GLint size, GLenum type, GLsizei stride, const void* pointer)
  1504. {
  1505. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1506. RETURN_WITH_ERROR_IF(!(size == 3 || size == 4), GL_INVALID_VALUE);
  1507. RETURN_WITH_ERROR_IF(!(type == GL_BYTE
  1508. || type == GL_UNSIGNED_BYTE
  1509. || type == GL_SHORT
  1510. || type == GL_UNSIGNED_SHORT
  1511. || type == GL_INT
  1512. || type == GL_UNSIGNED_INT
  1513. || type == GL_FLOAT
  1514. || type == GL_DOUBLE),
  1515. GL_INVALID_ENUM);
  1516. RETURN_WITH_ERROR_IF(stride < 0, GL_INVALID_VALUE);
  1517. m_client_color_pointer.size = size;
  1518. m_client_color_pointer.type = type;
  1519. m_client_color_pointer.stride = stride;
  1520. m_client_color_pointer.pointer = pointer;
  1521. }
  1522. void SoftwareGLContext::gl_tex_coord_pointer(GLint size, GLenum type, GLsizei stride, const void* pointer)
  1523. {
  1524. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1525. RETURN_WITH_ERROR_IF(!(size == 1 || size == 2 || size == 3 || size == 4), GL_INVALID_VALUE);
  1526. RETURN_WITH_ERROR_IF(!(type == GL_SHORT || type == GL_INT || type == GL_FLOAT || type == GL_DOUBLE), GL_INVALID_ENUM);
  1527. RETURN_WITH_ERROR_IF(stride < 0, GL_INVALID_VALUE);
  1528. m_client_tex_coord_pointer.size = size;
  1529. m_client_tex_coord_pointer.type = type;
  1530. m_client_tex_coord_pointer.stride = stride;
  1531. m_client_tex_coord_pointer.pointer = pointer;
  1532. }
  1533. void SoftwareGLContext::gl_tex_env(GLenum target, GLenum pname, GLfloat param)
  1534. {
  1535. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_env, target, pname, param);
  1536. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1537. if (target == GL_TEXTURE_ENV) {
  1538. if (pname == GL_TEXTURE_ENV_MODE) {
  1539. auto param_enum = static_cast<GLenum>(param);
  1540. switch (param_enum) {
  1541. case GL_MODULATE:
  1542. case GL_REPLACE:
  1543. case GL_DECAL:
  1544. m_active_texture_unit->set_env_mode(param_enum);
  1545. break;
  1546. default:
  1547. // FIXME: We currently only support a subset of possible param values. Implement the rest!
  1548. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1549. break;
  1550. }
  1551. } else {
  1552. // FIXME: We currently only support a subset of possible pname values. Implement the rest!
  1553. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1554. }
  1555. } else {
  1556. // FIXME: We currently only support a subset of possible target values. Implement the rest!
  1557. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1558. }
  1559. }
  1560. void SoftwareGLContext::gl_draw_arrays(GLenum mode, GLint first, GLsizei count)
  1561. {
  1562. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_draw_arrays, mode, first, count);
  1563. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1564. // FIXME: Some modes are still missing (GL_POINTS, GL_LINE_STRIP, GL_LINE_LOOP, GL_LINES,GL_QUAD_STRIP)
  1565. RETURN_WITH_ERROR_IF(!(mode == GL_TRIANGLE_STRIP
  1566. || mode == GL_TRIANGLE_FAN
  1567. || mode == GL_TRIANGLES
  1568. || mode == GL_QUADS
  1569. || mode == GL_POLYGON),
  1570. GL_INVALID_ENUM);
  1571. RETURN_WITH_ERROR_IF(count < 0, GL_INVALID_VALUE);
  1572. // At least the vertex array needs to be enabled
  1573. if (!m_client_side_vertex_array_enabled)
  1574. return;
  1575. auto last = first + count;
  1576. gl_begin(mode);
  1577. for (int i = first; i < last; i++) {
  1578. if (m_client_side_texture_coord_array_enabled) {
  1579. float tex_coords[4] { 0, 0, 0, 0 };
  1580. read_from_vertex_attribute_pointer(m_client_tex_coord_pointer, i, tex_coords, false);
  1581. gl_tex_coord(tex_coords[0], tex_coords[1], tex_coords[2], tex_coords[3]);
  1582. }
  1583. if (m_client_side_color_array_enabled) {
  1584. float color[4] { 0, 0, 0, 1 };
  1585. read_from_vertex_attribute_pointer(m_client_color_pointer, i, color, true);
  1586. glColor4fv(color);
  1587. }
  1588. float vertex[4] { 0, 0, 0, 1 };
  1589. read_from_vertex_attribute_pointer(m_client_vertex_pointer, i, vertex, false);
  1590. glVertex4fv(vertex);
  1591. }
  1592. gl_end();
  1593. }
  1594. void SoftwareGLContext::gl_draw_elements(GLenum mode, GLsizei count, GLenum type, const void* indices)
  1595. {
  1596. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_draw_elements, mode, count, type, indices);
  1597. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1598. // FIXME: Some modes are still missing (GL_POINTS, GL_LINE_STRIP, GL_LINE_LOOP, GL_LINES,GL_QUAD_STRIP)
  1599. RETURN_WITH_ERROR_IF(!(mode == GL_TRIANGLE_STRIP
  1600. || mode == GL_TRIANGLE_FAN
  1601. || mode == GL_TRIANGLES
  1602. || mode == GL_QUADS
  1603. || mode == GL_POLYGON),
  1604. GL_INVALID_ENUM);
  1605. RETURN_WITH_ERROR_IF(!(type == GL_UNSIGNED_BYTE
  1606. || type == GL_UNSIGNED_SHORT
  1607. || type == GL_UNSIGNED_INT),
  1608. GL_INVALID_ENUM);
  1609. RETURN_WITH_ERROR_IF(count < 0, GL_INVALID_VALUE);
  1610. // At least the vertex array needs to be enabled
  1611. if (!m_client_side_vertex_array_enabled)
  1612. return;
  1613. gl_begin(mode);
  1614. for (int index = 0; index < count; index++) {
  1615. int i = 0;
  1616. switch (type) {
  1617. case GL_UNSIGNED_BYTE:
  1618. i = reinterpret_cast<const GLubyte*>(indices)[index];
  1619. break;
  1620. case GL_UNSIGNED_SHORT:
  1621. i = reinterpret_cast<const GLushort*>(indices)[index];
  1622. break;
  1623. case GL_UNSIGNED_INT:
  1624. i = reinterpret_cast<const GLuint*>(indices)[index];
  1625. break;
  1626. }
  1627. if (m_client_side_texture_coord_array_enabled) {
  1628. float tex_coords[4] { 0, 0, 0, 0 };
  1629. read_from_vertex_attribute_pointer(m_client_tex_coord_pointer, i, tex_coords, false);
  1630. gl_tex_coord(tex_coords[0], tex_coords[1], tex_coords[2], tex_coords[3]);
  1631. }
  1632. if (m_client_side_color_array_enabled) {
  1633. float color[4] { 0, 0, 0, 1 };
  1634. read_from_vertex_attribute_pointer(m_client_color_pointer, i, color, true);
  1635. glColor4fv(color);
  1636. }
  1637. float vertex[4] { 0, 0, 0, 1 };
  1638. read_from_vertex_attribute_pointer(m_client_vertex_pointer, i, vertex, false);
  1639. glVertex4fv(vertex);
  1640. }
  1641. gl_end();
  1642. }
  1643. void SoftwareGLContext::gl_draw_pixels(GLsizei width, GLsizei height, GLenum format, GLenum type, const void* data)
  1644. {
  1645. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_draw_pixels, width, height, format, type, data);
  1646. RETURN_WITH_ERROR_IF(format < GL_COLOR_INDEX || format > GL_BGRA, GL_INVALID_ENUM);
  1647. RETURN_WITH_ERROR_IF((type < GL_BYTE || type > GL_FLOAT)
  1648. && (type < GL_UNSIGNED_BYTE_3_3_2 || type > GL_UNSIGNED_INT_10_10_10_2)
  1649. && (type < GL_UNSIGNED_BYTE_2_3_3_REV || type > GL_UNSIGNED_INT_2_10_10_10_REV),
  1650. GL_INVALID_ENUM);
  1651. RETURN_WITH_ERROR_IF(type == GL_BITMAP && !(format == GL_COLOR_INDEX || format == GL_STENCIL_INDEX), GL_INVALID_ENUM);
  1652. RETURN_WITH_ERROR_IF(width < 0 || height < 0, GL_INVALID_VALUE);
  1653. // FIXME: GL_INVALID_OPERATION is generated if format is GL_STENCIL_INDEX and there is no stencil buffer
  1654. // FIXME: GL_INVALID_OPERATION is generated if format is GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA, GL_RGB, GL_RGBA,
  1655. // GL_BGR, GL_BGRA, GL_LUMINANCE, or GL_LUMINANCE_ALPHA, and the GL is in color index mode
  1656. RETURN_WITH_ERROR_IF(format != GL_RGB
  1657. && (type == GL_UNSIGNED_BYTE_3_3_2
  1658. || type == GL_UNSIGNED_BYTE_2_3_3_REV
  1659. || type == GL_UNSIGNED_SHORT_5_6_5
  1660. || type == GL_UNSIGNED_SHORT_5_6_5_REV),
  1661. GL_INVALID_OPERATION);
  1662. RETURN_WITH_ERROR_IF(!(format == GL_RGBA || format == GL_BGRA)
  1663. && (type == GL_UNSIGNED_SHORT_4_4_4_4
  1664. || type == GL_UNSIGNED_SHORT_4_4_4_4_REV
  1665. || type == GL_UNSIGNED_SHORT_5_5_5_1
  1666. || type == GL_UNSIGNED_SHORT_1_5_5_5_REV
  1667. || type == GL_UNSIGNED_INT_8_8_8_8
  1668. || type == GL_UNSIGNED_INT_8_8_8_8_REV
  1669. || type == GL_UNSIGNED_INT_10_10_10_2
  1670. || type == GL_UNSIGNED_INT_2_10_10_10_REV),
  1671. GL_INVALID_OPERATION);
  1672. // FIXME: GL_INVALID_OPERATION is generated if a non-zero buffer object name is bound to the GL_PIXEL_UNPACK_BUFFER
  1673. // target and the buffer object's data store is currently mapped.
  1674. // FIXME: GL_INVALID_OPERATION is generated if a non-zero buffer object name is bound to the GL_PIXEL_UNPACK_BUFFER
  1675. // target and the data would be unpacked from the buffer object such that the memory reads required would
  1676. // exceed the data store size.
  1677. // FIXME: GL_INVALID_OPERATION is generated if a non-zero buffer object name is bound to the GL_PIXEL_UNPACK_BUFFER
  1678. // target and data is not evenly divisible into the number of bytes needed to store in memory a datum
  1679. // indicated by type.
  1680. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1681. // FIXME: we only support RGBA + GL_UNSIGNED_BYTE, implement all the others!
  1682. if (format != GL_RGBA) {
  1683. dbgln_if(GL_DEBUG, "gl_draw_pixels(): support for format {:#x} not implemented", format);
  1684. return;
  1685. } else if (type != GL_UNSIGNED_BYTE) {
  1686. dbgln_if(GL_DEBUG, "gl_draw_pixels(): support for type {:#x} not implemented", type);
  1687. return;
  1688. }
  1689. auto bitmap_or_error = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, { width, height });
  1690. RETURN_WITH_ERROR_IF(bitmap_or_error.is_error(), GL_OUT_OF_MEMORY);
  1691. auto bitmap = bitmap_or_error.release_value();
  1692. // FIXME: implement support for GL_UNPACK_ALIGNMENT and other pixel parameters
  1693. auto pixel_data = static_cast<u32 const*>(data);
  1694. for (int y = 0; y < height; ++y)
  1695. for (int x = 0; x < width; ++x)
  1696. bitmap->set_pixel(x, y, Color::from_rgba(*(pixel_data++)));
  1697. m_rasterizer.blit(
  1698. bitmap,
  1699. static_cast<int>(m_current_raster_position.window_coordinates.x()),
  1700. static_cast<int>(m_current_raster_position.window_coordinates.y()));
  1701. }
  1702. void SoftwareGLContext::gl_depth_range(GLdouble min, GLdouble max)
  1703. {
  1704. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_depth_range, min, max);
  1705. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1706. auto options = m_rasterizer.options();
  1707. options.depth_min = clamp(min, 0.f, 1.f);
  1708. options.depth_max = clamp(max, 0.f, 1.f);
  1709. m_rasterizer.set_options(options);
  1710. }
  1711. void SoftwareGLContext::gl_depth_func(GLenum func)
  1712. {
  1713. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_depth_func, func);
  1714. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1715. RETURN_WITH_ERROR_IF(!(func == GL_NEVER
  1716. || func == GL_LESS
  1717. || func == GL_EQUAL
  1718. || func == GL_LEQUAL
  1719. || func == GL_GREATER
  1720. || func == GL_NOTEQUAL
  1721. || func == GL_GEQUAL
  1722. || func == GL_ALWAYS),
  1723. GL_INVALID_ENUM);
  1724. auto options = m_rasterizer.options();
  1725. options.depth_func = func;
  1726. m_rasterizer.set_options(options);
  1727. }
  1728. // General helper function to read arbitrary vertex attribute data into a float array
  1729. void SoftwareGLContext::read_from_vertex_attribute_pointer(VertexAttribPointer const& attrib, int index, float* elements, bool normalize)
  1730. {
  1731. auto byte_ptr = reinterpret_cast<const char*>(attrib.pointer);
  1732. size_t stride = attrib.stride;
  1733. switch (attrib.type) {
  1734. case GL_BYTE: {
  1735. if (stride == 0)
  1736. stride = sizeof(GLbyte) * attrib.size;
  1737. for (int i = 0; i < attrib.size; i++) {
  1738. elements[i] = *(reinterpret_cast<const GLbyte*>(byte_ptr + stride * index) + i);
  1739. if (normalize)
  1740. elements[i] /= 0x80;
  1741. }
  1742. break;
  1743. }
  1744. case GL_UNSIGNED_BYTE: {
  1745. if (stride == 0)
  1746. stride = sizeof(GLubyte) * attrib.size;
  1747. for (int i = 0; i < attrib.size; i++) {
  1748. elements[i] = *(reinterpret_cast<const GLubyte*>(byte_ptr + stride * index) + i);
  1749. if (normalize)
  1750. elements[i] /= 0xff;
  1751. }
  1752. break;
  1753. }
  1754. case GL_SHORT: {
  1755. if (stride == 0)
  1756. stride = sizeof(GLshort) * attrib.size;
  1757. for (int i = 0; i < attrib.size; i++) {
  1758. elements[i] = *(reinterpret_cast<const GLshort*>(byte_ptr + stride * index) + i);
  1759. if (normalize)
  1760. elements[i] /= 0x8000;
  1761. }
  1762. break;
  1763. }
  1764. case GL_UNSIGNED_SHORT: {
  1765. if (stride == 0)
  1766. stride = sizeof(GLushort) * attrib.size;
  1767. for (int i = 0; i < attrib.size; i++) {
  1768. elements[i] = *(reinterpret_cast<const GLushort*>(byte_ptr + stride * index) + i);
  1769. if (normalize)
  1770. elements[i] /= 0xffff;
  1771. }
  1772. break;
  1773. }
  1774. case GL_INT: {
  1775. if (stride == 0)
  1776. stride = sizeof(GLint) * attrib.size;
  1777. for (int i = 0; i < attrib.size; i++) {
  1778. elements[i] = *(reinterpret_cast<const GLint*>(byte_ptr + stride * index) + i);
  1779. if (normalize)
  1780. elements[i] /= 0x80000000;
  1781. }
  1782. break;
  1783. }
  1784. case GL_UNSIGNED_INT: {
  1785. if (stride == 0)
  1786. stride = sizeof(GLuint) * attrib.size;
  1787. for (int i = 0; i < attrib.size; i++) {
  1788. elements[i] = *(reinterpret_cast<const GLuint*>(byte_ptr + stride * index) + i);
  1789. if (normalize)
  1790. elements[i] /= 0xffffffff;
  1791. }
  1792. break;
  1793. }
  1794. case GL_FLOAT: {
  1795. if (stride == 0)
  1796. stride = sizeof(GLfloat) * attrib.size;
  1797. for (int i = 0; i < attrib.size; i++) {
  1798. elements[i] = *(reinterpret_cast<const GLfloat*>(byte_ptr + stride * index) + i);
  1799. }
  1800. break;
  1801. }
  1802. case GL_DOUBLE: {
  1803. if (stride == 0)
  1804. stride = sizeof(GLdouble) * attrib.size;
  1805. for (int i = 0; i < attrib.size; i++) {
  1806. elements[i] = static_cast<float>(*(reinterpret_cast<const GLdouble*>(byte_ptr + stride * index) + i));
  1807. }
  1808. break;
  1809. }
  1810. }
  1811. }
  1812. void SoftwareGLContext::gl_color_mask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha)
  1813. {
  1814. auto options = m_rasterizer.options();
  1815. auto mask = options.color_mask;
  1816. if (!red)
  1817. mask &= ~0x000000ff;
  1818. else
  1819. mask |= 0x000000ff;
  1820. if (!green)
  1821. mask &= ~0x0000ff00;
  1822. else
  1823. mask |= 0x0000ff00;
  1824. if (!blue)
  1825. mask &= ~0x00ff0000;
  1826. else
  1827. mask |= 0x00ff0000;
  1828. if (!alpha)
  1829. mask &= ~0xff000000;
  1830. else
  1831. mask |= 0xff000000;
  1832. options.color_mask = mask;
  1833. m_rasterizer.set_options(options);
  1834. }
  1835. void SoftwareGLContext::gl_polygon_mode(GLenum face, GLenum mode)
  1836. {
  1837. RETURN_WITH_ERROR_IF(!(face == GL_BACK || face == GL_FRONT || face == GL_FRONT_AND_BACK), GL_INVALID_ENUM);
  1838. RETURN_WITH_ERROR_IF(!(mode == GL_POINT || mode == GL_LINE || mode == GL_FILL), GL_INVALID_ENUM);
  1839. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1840. auto options = m_rasterizer.options();
  1841. options.polygon_mode = mode;
  1842. m_rasterizer.set_options(options);
  1843. }
  1844. void SoftwareGLContext::gl_polygon_offset(GLfloat factor, GLfloat units)
  1845. {
  1846. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_polygon_offset, factor, units);
  1847. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1848. auto rasterizer_options = m_rasterizer.options();
  1849. rasterizer_options.depth_offset_factor = factor;
  1850. rasterizer_options.depth_offset_constant = units;
  1851. m_rasterizer.set_options(rasterizer_options);
  1852. }
  1853. void SoftwareGLContext::gl_fogfv(GLenum pname, GLfloat* params)
  1854. {
  1855. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1856. auto options = m_rasterizer.options();
  1857. switch (pname) {
  1858. case GL_FOG_COLOR:
  1859. // Set rasterizer options fog color
  1860. // NOTE: We purposefully don't check for `nullptr` here (as with other calls). The spec states nothing
  1861. // about us checking for such things. If the programmer does so and hits SIGSEGV, that's on them.
  1862. options.fog_color = FloatVector4 { params[0], params[1], params[2], params[3] };
  1863. break;
  1864. default:
  1865. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1866. }
  1867. m_rasterizer.set_options(options);
  1868. }
  1869. void SoftwareGLContext::gl_fogf(GLenum pname, GLfloat param)
  1870. {
  1871. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1872. RETURN_WITH_ERROR_IF(param < 0.0f, GL_INVALID_VALUE);
  1873. auto options = m_rasterizer.options();
  1874. switch (pname) {
  1875. case GL_FOG_DENSITY:
  1876. options.fog_density = param;
  1877. break;
  1878. default:
  1879. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1880. }
  1881. m_rasterizer.set_options(options);
  1882. }
  1883. void SoftwareGLContext::gl_fogi(GLenum pname, GLint param)
  1884. {
  1885. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1886. RETURN_WITH_ERROR_IF(!(param == GL_EXP || param == GL_EXP2 || param != GL_LINEAR), GL_INVALID_ENUM);
  1887. auto options = m_rasterizer.options();
  1888. switch (pname) {
  1889. case GL_FOG_MODE:
  1890. options.fog_mode = param;
  1891. break;
  1892. default:
  1893. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1894. }
  1895. m_rasterizer.set_options(options);
  1896. }
  1897. void SoftwareGLContext::gl_pixel_storei(GLenum pname, GLint param)
  1898. {
  1899. // FIXME: Implement missing parameters
  1900. switch (pname) {
  1901. case GL_PACK_ALIGNMENT:
  1902. RETURN_WITH_ERROR_IF(param != 1 && param != 2 && param != 4 && param != 8, GL_INVALID_VALUE);
  1903. m_pack_alignment = param;
  1904. break;
  1905. case GL_UNPACK_ROW_LENGTH:
  1906. RETURN_WITH_ERROR_IF(param < 0, GL_INVALID_VALUE);
  1907. m_unpack_row_length = static_cast<size_t>(param);
  1908. break;
  1909. case GL_UNPACK_ALIGNMENT:
  1910. RETURN_WITH_ERROR_IF(param != 1 && param != 2 && param != 4 && param != 8, GL_INVALID_VALUE);
  1911. m_unpack_alignment = param;
  1912. break;
  1913. default:
  1914. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1915. break;
  1916. }
  1917. }
  1918. void SoftwareGLContext::gl_scissor(GLint x, GLint y, GLsizei width, GLsizei height)
  1919. {
  1920. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_scissor, x, y, width, height);
  1921. RETURN_WITH_ERROR_IF(width < 0 || height < 0, GL_INVALID_VALUE);
  1922. auto options = m_rasterizer.options();
  1923. options.scissor_box = { x, y, width, height };
  1924. m_rasterizer.set_options(options);
  1925. }
  1926. void SoftwareGLContext::gl_stencil_func_separate(GLenum face, GLenum func, GLint ref, GLuint mask)
  1927. {
  1928. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_stencil_func_separate, face, func, ref, mask);
  1929. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1930. RETURN_WITH_ERROR_IF(!(face == GL_FRONT || face == GL_BACK || face == GL_FRONT_AND_BACK), GL_INVALID_ENUM);
  1931. RETURN_WITH_ERROR_IF(!(func == GL_NEVER
  1932. || func == GL_LESS
  1933. || func == GL_LEQUAL
  1934. || func == GL_GREATER
  1935. || func == GL_GEQUAL
  1936. || func == GL_EQUAL
  1937. || func == GL_NOTEQUAL
  1938. || func == GL_ALWAYS),
  1939. GL_INVALID_ENUM);
  1940. // FIXME: "ref is clamped to the range 02^n - 1 , where n is the number of bitplanes in the stencil buffer"
  1941. StencilFunctionOptions new_options = { func, ref, mask };
  1942. if (face == GL_FRONT || face == GL_FRONT_AND_BACK)
  1943. m_stencil_frontfacing_func = new_options;
  1944. if (face == GL_BACK || face == GL_FRONT_AND_BACK)
  1945. m_stencil_backfacing_func = new_options;
  1946. }
  1947. void SoftwareGLContext::gl_stencil_op_separate(GLenum face, GLenum sfail, GLenum dpfail, GLenum dppass)
  1948. {
  1949. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_stencil_op_separate, face, sfail, dpfail, dppass);
  1950. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1951. RETURN_WITH_ERROR_IF(!(face == GL_FRONT || face == GL_BACK || face == GL_FRONT_AND_BACK), GL_INVALID_ENUM);
  1952. RETURN_WITH_ERROR_IF(!(sfail == GL_KEEP
  1953. || sfail == GL_ZERO
  1954. || sfail == GL_REPLACE
  1955. || sfail == GL_INCR
  1956. || sfail == GL_INCR_WRAP
  1957. || sfail == GL_DECR
  1958. || sfail == GL_DECR_WRAP
  1959. || sfail == GL_INVERT),
  1960. GL_INVALID_ENUM);
  1961. RETURN_WITH_ERROR_IF(!(dpfail == GL_KEEP
  1962. || dpfail == GL_ZERO
  1963. || dpfail == GL_REPLACE
  1964. || dpfail == GL_INCR
  1965. || dpfail == GL_INCR_WRAP
  1966. || dpfail == GL_DECR
  1967. || dpfail == GL_DECR_WRAP
  1968. || dpfail == GL_INVERT),
  1969. GL_INVALID_ENUM);
  1970. RETURN_WITH_ERROR_IF(!(dppass == GL_KEEP
  1971. || dppass == GL_ZERO
  1972. || dppass == GL_REPLACE
  1973. || dppass == GL_INCR
  1974. || dppass == GL_INCR_WRAP
  1975. || dppass == GL_DECR
  1976. || dppass == GL_DECR_WRAP
  1977. || dppass == GL_INVERT),
  1978. GL_INVALID_ENUM);
  1979. StencilOperationOptions new_options = { sfail, dpfail, dppass };
  1980. if (face == GL_FRONT || face == GL_FRONT_AND_BACK)
  1981. m_stencil_frontfacing_op = new_options;
  1982. if (face == GL_BACK || face == GL_FRONT_AND_BACK)
  1983. m_stencil_backfacing_op = new_options;
  1984. }
  1985. void SoftwareGLContext::gl_normal(GLfloat nx, GLfloat ny, GLfloat nz)
  1986. {
  1987. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_normal, nx, ny, nz);
  1988. m_current_vertex_normal = { nx, ny, nz };
  1989. }
  1990. void SoftwareGLContext::gl_raster_pos(GLfloat x, GLfloat y, GLfloat z, GLfloat w)
  1991. {
  1992. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_raster_pos, x, y, z, w);
  1993. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1994. m_current_raster_position.window_coordinates = { x, y, z };
  1995. m_current_raster_position.clip_coordinate_value = w;
  1996. }
  1997. void SoftwareGLContext::gl_materialv(GLenum face, GLenum pname, GLfloat const* params)
  1998. {
  1999. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_materialv, face, pname, params);
  2000. RETURN_WITH_ERROR_IF(!(face == GL_FRONT || face == GL_BACK || face == GL_FRONT_AND_BACK), GL_INVALID_ENUM);
  2001. RETURN_WITH_ERROR_IF(!(pname == GL_AMBIENT
  2002. || pname == GL_DIFFUSE
  2003. || pname == GL_SPECULAR
  2004. || pname == GL_EMISSION
  2005. || pname == GL_SHININESS
  2006. || pname == GL_AMBIENT_AND_DIFFUSE
  2007. || pname == GL_COLOR_INDEXES),
  2008. GL_INVALID_ENUM);
  2009. GLfloat x, y, z, w;
  2010. switch (pname) {
  2011. case GL_SHININESS:
  2012. x = params[0];
  2013. y = 0.0f;
  2014. z = 0.0f;
  2015. w = 0.0f;
  2016. break;
  2017. case GL_COLOR_INDEXES:
  2018. x = params[0];
  2019. y = params[1];
  2020. z = params[2];
  2021. w = 0.0f;
  2022. break;
  2023. default:
  2024. x = params[0];
  2025. y = params[1];
  2026. z = params[2];
  2027. w = params[3];
  2028. }
  2029. // FIXME: implement this method
  2030. dbgln_if(GL_DEBUG, "SoftwareGLContext FIXME: gl_materialv({}, {}, {}, {}, {}, {})", face, pname, x, y, z, w);
  2031. }
  2032. void SoftwareGLContext::gl_line_width(GLfloat width)
  2033. {
  2034. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_line_width, width);
  2035. RETURN_WITH_ERROR_IF(width <= 0, GL_INVALID_VALUE);
  2036. m_line_width = width;
  2037. }
  2038. void SoftwareGLContext::gl_push_attrib(GLbitfield mask)
  2039. {
  2040. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_push_attrib, mask);
  2041. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2042. // FIXME: implement
  2043. dbgln_if(GL_DEBUG, "SoftwareGLContext FIXME: implement gl_push_attrib({})", mask);
  2044. }
  2045. void SoftwareGLContext::gl_pop_attrib()
  2046. {
  2047. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_pop_attrib);
  2048. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2049. // FIXME: implement
  2050. dbgln_if(GL_DEBUG, "SoftwareGLContext FIXME: implement gl_pop_attrib()");
  2051. }
  2052. void SoftwareGLContext::gl_light_model(GLenum pname, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
  2053. {
  2054. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_light_model, pname, x, y, z, w);
  2055. RETURN_WITH_ERROR_IF(!(pname == GL_LIGHT_MODEL_AMBIENT
  2056. || pname == GL_LIGHT_MODEL_TWO_SIDE),
  2057. GL_INVALID_ENUM);
  2058. switch (pname) {
  2059. case GL_LIGHT_MODEL_AMBIENT:
  2060. m_light_model_ambient = { x, y, z, w };
  2061. break;
  2062. case GL_LIGHT_MODEL_TWO_SIDE:
  2063. VERIFY(y == 0.0f && z == 0.0f && w == 0.0f);
  2064. m_light_model_two_side = x;
  2065. break;
  2066. default:
  2067. VERIFY_NOT_REACHED();
  2068. }
  2069. }
  2070. void SoftwareGLContext::gl_bitmap(GLsizei width, GLsizei height, GLfloat xorig, GLfloat yorig, GLfloat xmove, GLfloat ymove, GLubyte const* bitmap)
  2071. {
  2072. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_bitmap, width, height, xorig, yorig, xmove, ymove, bitmap);
  2073. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2074. // FIXME: implement
  2075. dbgln_if(GL_DEBUG, "SoftwareGLContext FIXME: implement gl_bitmap({}, {}, {}, {}, {}, {}, {})", width, height, xorig, yorig, xmove, ymove, bitmap);
  2076. }
  2077. void SoftwareGLContext::gl_copy_tex_image_2d(GLenum target, GLint level, GLenum internalformat, GLint x, GLint y, GLsizei width, GLsizei height, GLint border)
  2078. {
  2079. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_copy_tex_image_2d, target, level, internalformat, x, y, width, height, border);
  2080. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2081. // FIXME: implement
  2082. dbgln_if(GL_DEBUG, "SoftwareGLContext FIXME: implement gl_copy_tex_image_2d({:#x}, {}, {:#x}, {}, {}, {}, {}, {})",
  2083. target, level, internalformat, x, y, width, height, border);
  2084. }
  2085. void SoftwareGLContext::present()
  2086. {
  2087. m_rasterizer.blit_to(*m_frontbuffer);
  2088. }
  2089. }