SoftwareGLContext.cpp 110 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012
  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. , m_device_info(m_rasterizer.info())
  53. {
  54. m_texture_units.resize(m_device_info.num_texture_units);
  55. m_active_texture_unit = &m_texture_units[0];
  56. }
  57. Optional<ContextParameter> SoftwareGLContext::get_context_parameter(GLenum name)
  58. {
  59. switch (name) {
  60. case GL_ALPHA_BITS:
  61. return ContextParameter { .type = GL_INT, .value = { .integer_value = sizeof(float) * 8 } };
  62. case GL_ALPHA_TEST:
  63. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_alpha_test_enabled } };
  64. case GL_BLEND:
  65. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_blend_enabled } };
  66. case GL_BLEND_DST_ALPHA:
  67. return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast<GLint>(m_blend_destination_factor) } };
  68. case GL_BLEND_SRC_ALPHA:
  69. return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast<GLint>(m_blend_source_factor) } };
  70. case GL_BLUE_BITS:
  71. return ContextParameter { .type = GL_INT, .value = { .integer_value = sizeof(float) * 8 } };
  72. case GL_CULL_FACE:
  73. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_cull_faces } };
  74. case GL_DEPTH_BITS:
  75. return ContextParameter { .type = GL_INT, .value = { .integer_value = sizeof(float) * 8 } };
  76. case GL_DEPTH_TEST:
  77. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_depth_test_enabled } };
  78. case GL_DITHER:
  79. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_dither_enabled } };
  80. case GL_DOUBLEBUFFER:
  81. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = true } };
  82. case GL_GREEN_BITS:
  83. return ContextParameter { .type = GL_INT, .value = { .integer_value = sizeof(float) * 8 } };
  84. case GL_LIGHTING:
  85. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_lighting_enabled } };
  86. case GL_MAX_MODELVIEW_STACK_DEPTH:
  87. return ContextParameter { .type = GL_INT, .value = { .integer_value = MODELVIEW_MATRIX_STACK_LIMIT } };
  88. case GL_MAX_PROJECTION_STACK_DEPTH:
  89. return ContextParameter { .type = GL_INT, .value = { .integer_value = PROJECTION_MATRIX_STACK_LIMIT } };
  90. case GL_MAX_TEXTURE_SIZE:
  91. return ContextParameter { .type = GL_INT, .value = { .integer_value = 4096 } };
  92. case GL_MAX_TEXTURE_STACK_DEPTH:
  93. return ContextParameter { .type = GL_INT, .value = { .integer_value = TEXTURE_MATRIX_STACK_LIMIT } };
  94. case GL_MAX_TEXTURE_UNITS:
  95. return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast<GLint>(m_texture_units.size()) } };
  96. case GL_PACK_ALIGNMENT:
  97. return ContextParameter { .type = GL_INT, .value = { .integer_value = m_pack_alignment } };
  98. case GL_PACK_IMAGE_HEIGHT:
  99. return ContextParameter { .type = GL_BOOL, .value = { .integer_value = 0 } };
  100. case GL_PACK_LSB_FIRST:
  101. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = false } };
  102. case GL_PACK_ROW_LENGTH:
  103. return ContextParameter { .type = GL_INT, .value = { .integer_value = 0 } };
  104. case GL_PACK_SKIP_PIXELS:
  105. return ContextParameter { .type = GL_INT, .value = { .integer_value = 0 } };
  106. case GL_PACK_SKIP_ROWS:
  107. return ContextParameter { .type = GL_INT, .value = { .integer_value = 0 } };
  108. case GL_PACK_SWAP_BYTES:
  109. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = false } };
  110. case GL_RED_BITS:
  111. return ContextParameter { .type = GL_INT, .value = { .integer_value = sizeof(float) * 8 } };
  112. case GL_SCISSOR_BOX: {
  113. auto scissor_box = m_rasterizer.options().scissor_box;
  114. return ContextParameter {
  115. .type = GL_INT,
  116. .count = 4,
  117. .value = {
  118. .integer_list = {
  119. scissor_box.x(),
  120. scissor_box.y(),
  121. scissor_box.width(),
  122. scissor_box.height(),
  123. } }
  124. };
  125. } break;
  126. case GL_STENCIL_BITS:
  127. return ContextParameter { .type = GL_INT, .value = { .integer_value = sizeof(float) * 8 } };
  128. case GL_STENCIL_TEST:
  129. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_stencil_test_enabled } };
  130. case GL_TEXTURE_1D:
  131. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_active_texture_unit->texture_1d_enabled() } };
  132. case GL_TEXTURE_2D:
  133. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_active_texture_unit->texture_2d_enabled() } };
  134. case GL_TEXTURE_3D:
  135. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_active_texture_unit->texture_3d_enabled() } };
  136. case GL_TEXTURE_CUBE_MAP:
  137. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_active_texture_unit->texture_cube_map_enabled() } };
  138. case GL_UNPACK_ALIGNMENT:
  139. return ContextParameter { .type = GL_INT, .value = { .integer_value = m_unpack_alignment } };
  140. case GL_UNPACK_IMAGE_HEIGHT:
  141. return ContextParameter { .type = GL_BOOL, .value = { .integer_value = 0 } };
  142. case GL_UNPACK_LSB_FIRST:
  143. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = false } };
  144. case GL_UNPACK_ROW_LENGTH:
  145. return ContextParameter { .type = GL_INT, .value = { .integer_value = m_unpack_row_length } };
  146. case GL_UNPACK_SKIP_PIXELS:
  147. return ContextParameter { .type = GL_INT, .value = { .integer_value = 0 } };
  148. case GL_UNPACK_SKIP_ROWS:
  149. return ContextParameter { .type = GL_INT, .value = { .integer_value = 0 } };
  150. case GL_UNPACK_SWAP_BYTES:
  151. return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = false } };
  152. default:
  153. dbgln_if(GL_DEBUG, "get_context_parameter({:#x}): unknown context parameter", name);
  154. return {};
  155. }
  156. }
  157. void SoftwareGLContext::gl_begin(GLenum mode)
  158. {
  159. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_begin, mode);
  160. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  161. RETURN_WITH_ERROR_IF(mode < GL_TRIANGLES || mode > GL_POLYGON, GL_INVALID_ENUM);
  162. m_current_draw_mode = mode;
  163. m_in_draw_state = true; // Certain commands will now generate an error
  164. }
  165. void SoftwareGLContext::gl_clear(GLbitfield mask)
  166. {
  167. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_clear, mask);
  168. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  169. RETURN_WITH_ERROR_IF(mask & ~(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT), GL_INVALID_ENUM);
  170. if (mask & GL_COLOR_BUFFER_BIT)
  171. m_rasterizer.clear_color(m_clear_color);
  172. if (mask & GL_DEPTH_BUFFER_BIT)
  173. m_rasterizer.clear_depth(static_cast<float>(m_clear_depth));
  174. // FIXME: implement GL_STENCIL_BUFFER_BIT
  175. if (mask & GL_STENCIL_BUFFER_BIT)
  176. dbgln_if(GL_DEBUG, "gl_clear(): GL_STENCIL_BUFFER_BIT is unimplemented");
  177. }
  178. void SoftwareGLContext::gl_clear_color(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
  179. {
  180. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_clear_color, red, green, blue, alpha);
  181. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  182. m_clear_color = { red, green, blue, alpha };
  183. }
  184. void SoftwareGLContext::gl_clear_depth(GLdouble depth)
  185. {
  186. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_clear_depth, depth);
  187. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  188. m_clear_depth = depth;
  189. }
  190. void SoftwareGLContext::gl_clear_stencil(GLint s)
  191. {
  192. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_clear_stencil, s);
  193. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  194. // FIXME: "s is masked with 2^m - 1 , where m is the number of bits in the stencil buffer"
  195. m_clear_stencil = s;
  196. }
  197. void SoftwareGLContext::gl_color(GLdouble r, GLdouble g, GLdouble b, GLdouble a)
  198. {
  199. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_color, r, g, b, a);
  200. m_current_vertex_color = { (float)r, (float)g, (float)b, (float)a };
  201. }
  202. void SoftwareGLContext::gl_end()
  203. {
  204. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_end);
  205. // Make sure we had a `glBegin` before this call...
  206. RETURN_WITH_ERROR_IF(!m_in_draw_state, GL_INVALID_OPERATION);
  207. m_in_draw_state = false;
  208. // FIXME: Add support for the remaining primitive types.
  209. if (m_current_draw_mode != GL_TRIANGLES
  210. && m_current_draw_mode != GL_TRIANGLE_FAN
  211. && m_current_draw_mode != GL_TRIANGLE_STRIP
  212. && m_current_draw_mode != GL_QUADS
  213. && m_current_draw_mode != GL_QUAD_STRIP
  214. && m_current_draw_mode != GL_POLYGON) {
  215. m_vertex_list.clear_with_capacity();
  216. dbgln_if(GL_DEBUG, "gl_end: draw mode {:#x} unsupported", m_current_draw_mode);
  217. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  218. }
  219. Vector<size_t, 32> enabled_texture_units;
  220. for (size_t i = 0; i < m_texture_units.size(); ++i) {
  221. if (m_texture_units[i].texture_2d_enabled())
  222. enabled_texture_units.append(i);
  223. }
  224. sync_device_config();
  225. SoftGPU::PrimitiveType primitive_type;
  226. switch (m_current_draw_mode) {
  227. case GL_TRIANGLES:
  228. primitive_type = SoftGPU::PrimitiveType::Triangles;
  229. break;
  230. case GL_TRIANGLE_STRIP:
  231. case GL_QUAD_STRIP:
  232. primitive_type = SoftGPU::PrimitiveType::TriangleStrip;
  233. break;
  234. case GL_TRIANGLE_FAN:
  235. case GL_POLYGON:
  236. primitive_type = SoftGPU::PrimitiveType::TriangleFan;
  237. break;
  238. case GL_QUADS:
  239. primitive_type = SoftGPU::PrimitiveType::Quads;
  240. break;
  241. default:
  242. VERIFY_NOT_REACHED();
  243. }
  244. // Set up normals transform by taking the upper left 3x3 elements from the model view matrix
  245. // See section 2.11.3 of the OpenGL 1.5 spec
  246. auto const& mv_elements = m_model_view_matrix.elements();
  247. auto normal_transform = FloatMatrix3x3(
  248. mv_elements[0][0], mv_elements[0][1], mv_elements[0][2],
  249. mv_elements[1][0], mv_elements[1][1], mv_elements[1][2],
  250. mv_elements[2][0], mv_elements[2][1], mv_elements[2][2]);
  251. normal_transform = normal_transform.inverse();
  252. m_rasterizer.draw_primitives(primitive_type, m_model_view_matrix, normal_transform, m_projection_matrix, m_texture_matrix, m_vertex_list, enabled_texture_units);
  253. m_vertex_list.clear_with_capacity();
  254. }
  255. void SoftwareGLContext::gl_frustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near_val, GLdouble far_val)
  256. {
  257. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_frustum, left, right, bottom, top, near_val, far_val);
  258. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  259. // Let's do some math!
  260. // FIXME: Are we losing too much precision by doing this?
  261. float a = static_cast<float>((right + left) / (right - left));
  262. float b = static_cast<float>((top + bottom) / (top - bottom));
  263. float c = static_cast<float>(-((far_val + near_val) / (far_val - near_val)));
  264. float d = static_cast<float>(-((2 * (far_val * near_val)) / (far_val - near_val)));
  265. FloatMatrix4x4 frustum {
  266. ((2 * (float)near_val) / ((float)right - (float)left)), 0, a, 0,
  267. 0, ((2 * (float)near_val) / ((float)top - (float)bottom)), b, 0,
  268. 0, 0, c, d,
  269. 0, 0, -1, 0
  270. };
  271. if (m_current_matrix_mode == GL_PROJECTION)
  272. m_projection_matrix = m_projection_matrix * frustum;
  273. else if (m_current_matrix_mode == GL_MODELVIEW)
  274. m_projection_matrix = m_model_view_matrix * frustum;
  275. else if (m_current_matrix_mode == GL_TEXTURE)
  276. m_texture_matrix = m_texture_matrix * frustum;
  277. else
  278. VERIFY_NOT_REACHED();
  279. }
  280. void SoftwareGLContext::gl_ortho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near_val, GLdouble far_val)
  281. {
  282. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_ortho, left, right, bottom, top, near_val, far_val);
  283. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  284. RETURN_WITH_ERROR_IF(left == right || bottom == top || near_val == far_val, GL_INVALID_VALUE);
  285. auto rl = right - left;
  286. auto tb = top - bottom;
  287. auto fn = far_val - near_val;
  288. auto tx = -(right + left) / rl;
  289. auto ty = -(top + bottom) / tb;
  290. auto tz = -(far_val + near_val) / fn;
  291. FloatMatrix4x4 projection {
  292. static_cast<float>(2 / rl), 0, 0, static_cast<float>(tx),
  293. 0, static_cast<float>(2 / tb), 0, static_cast<float>(ty),
  294. 0, 0, static_cast<float>(-2 / fn), static_cast<float>(tz),
  295. 0, 0, 0, 1
  296. };
  297. if (m_current_matrix_mode == GL_PROJECTION)
  298. m_projection_matrix = m_projection_matrix * projection;
  299. else if (m_current_matrix_mode == GL_MODELVIEW)
  300. m_projection_matrix = m_model_view_matrix * projection;
  301. else if (m_current_matrix_mode == GL_TEXTURE)
  302. m_texture_matrix = m_texture_matrix * projection;
  303. else
  304. VERIFY_NOT_REACHED();
  305. }
  306. GLenum SoftwareGLContext::gl_get_error()
  307. {
  308. if (m_in_draw_state)
  309. return GL_INVALID_OPERATION;
  310. auto last_error = m_error;
  311. m_error = GL_NO_ERROR;
  312. return last_error;
  313. }
  314. GLubyte* SoftwareGLContext::gl_get_string(GLenum name)
  315. {
  316. RETURN_VALUE_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION, nullptr);
  317. switch (name) {
  318. case GL_VENDOR:
  319. return reinterpret_cast<GLubyte*>(const_cast<char*>(m_device_info.vendor_name.characters()));
  320. case GL_RENDERER:
  321. return reinterpret_cast<GLubyte*>(const_cast<char*>(m_device_info.device_name.characters()));
  322. case GL_VERSION:
  323. return reinterpret_cast<GLubyte*>(const_cast<char*>("1.5"));
  324. case GL_EXTENSIONS:
  325. return reinterpret_cast<GLubyte*>(const_cast<char*>(""));
  326. case GL_SHADING_LANGUAGE_VERSION:
  327. return reinterpret_cast<GLubyte*>(const_cast<char*>("0.0"));
  328. default:
  329. dbgln_if(GL_DEBUG, "gl_get_string({:#x}): unknown name", name);
  330. break;
  331. }
  332. RETURN_VALUE_WITH_ERROR_IF(true, GL_INVALID_ENUM, nullptr);
  333. }
  334. void SoftwareGLContext::gl_load_identity()
  335. {
  336. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_load_identity);
  337. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  338. if (m_current_matrix_mode == GL_PROJECTION)
  339. m_projection_matrix = FloatMatrix4x4::identity();
  340. else if (m_current_matrix_mode == GL_MODELVIEW)
  341. m_model_view_matrix = FloatMatrix4x4::identity();
  342. else if (m_current_matrix_mode == GL_TEXTURE)
  343. m_texture_matrix = FloatMatrix4x4::identity();
  344. else
  345. VERIFY_NOT_REACHED();
  346. }
  347. void SoftwareGLContext::gl_load_matrix(const FloatMatrix4x4& matrix)
  348. {
  349. APPEND_TO_CALL_LIST_WITH_ARG_AND_RETURN_IF_NEEDED(gl_load_matrix, matrix);
  350. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  351. if (m_current_matrix_mode == GL_PROJECTION)
  352. m_projection_matrix = matrix;
  353. else if (m_current_matrix_mode == GL_MODELVIEW)
  354. m_model_view_matrix = matrix;
  355. else if (m_current_matrix_mode == GL_TEXTURE)
  356. m_texture_matrix = matrix;
  357. else
  358. VERIFY_NOT_REACHED();
  359. }
  360. void SoftwareGLContext::gl_matrix_mode(GLenum mode)
  361. {
  362. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_matrix_mode, mode);
  363. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  364. RETURN_WITH_ERROR_IF(mode < GL_MODELVIEW || mode > GL_TEXTURE, GL_INVALID_ENUM);
  365. m_current_matrix_mode = mode;
  366. }
  367. void SoftwareGLContext::gl_push_matrix()
  368. {
  369. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_push_matrix);
  370. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  371. switch (m_current_matrix_mode) {
  372. case GL_PROJECTION:
  373. RETURN_WITH_ERROR_IF(m_projection_matrix_stack.size() >= PROJECTION_MATRIX_STACK_LIMIT, GL_STACK_OVERFLOW);
  374. m_projection_matrix_stack.append(m_projection_matrix);
  375. break;
  376. case GL_MODELVIEW:
  377. RETURN_WITH_ERROR_IF(m_model_view_matrix_stack.size() >= MODELVIEW_MATRIX_STACK_LIMIT, GL_STACK_OVERFLOW);
  378. m_model_view_matrix_stack.append(m_model_view_matrix);
  379. break;
  380. case GL_TEXTURE:
  381. RETURN_WITH_ERROR_IF(m_texture_matrix_stack.size() >= TEXTURE_MATRIX_STACK_LIMIT, GL_STACK_OVERFLOW);
  382. m_texture_matrix_stack.append(m_texture_matrix);
  383. break;
  384. default:
  385. VERIFY_NOT_REACHED();
  386. }
  387. }
  388. void SoftwareGLContext::gl_pop_matrix()
  389. {
  390. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_pop_matrix);
  391. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  392. switch (m_current_matrix_mode) {
  393. case GL_PROJECTION:
  394. RETURN_WITH_ERROR_IF(m_projection_matrix_stack.size() == 0, GL_STACK_UNDERFLOW);
  395. m_projection_matrix = m_projection_matrix_stack.take_last();
  396. break;
  397. case GL_MODELVIEW:
  398. RETURN_WITH_ERROR_IF(m_model_view_matrix_stack.size() == 0, GL_STACK_UNDERFLOW);
  399. m_model_view_matrix = m_model_view_matrix_stack.take_last();
  400. break;
  401. case GL_TEXTURE:
  402. RETURN_WITH_ERROR_IF(m_texture_matrix_stack.size() == 0, GL_STACK_UNDERFLOW);
  403. m_texture_matrix = m_texture_matrix_stack.take_last();
  404. break;
  405. default:
  406. VERIFY_NOT_REACHED();
  407. }
  408. }
  409. void SoftwareGLContext::gl_mult_matrix(FloatMatrix4x4 const& matrix)
  410. {
  411. APPEND_TO_CALL_LIST_WITH_ARG_AND_RETURN_IF_NEEDED(gl_mult_matrix, matrix);
  412. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  413. if (m_current_matrix_mode == GL_MODELVIEW)
  414. m_model_view_matrix = m_model_view_matrix * matrix;
  415. else if (m_current_matrix_mode == GL_PROJECTION)
  416. m_projection_matrix = m_projection_matrix * matrix;
  417. else if (m_current_matrix_mode == GL_TEXTURE)
  418. m_texture_matrix = m_texture_matrix * matrix;
  419. else
  420. VERIFY_NOT_REACHED();
  421. }
  422. void SoftwareGLContext::gl_rotate(GLdouble angle, GLdouble x, GLdouble y, GLdouble z)
  423. {
  424. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_rotate, angle, x, y, z);
  425. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  426. FloatVector3 axis = { (float)x, (float)y, (float)z };
  427. axis.normalize();
  428. auto rotation_mat = Gfx::rotation_matrix(axis, static_cast<float>(angle * M_PI * 2 / 360));
  429. if (m_current_matrix_mode == GL_MODELVIEW)
  430. m_model_view_matrix = m_model_view_matrix * rotation_mat;
  431. else if (m_current_matrix_mode == GL_PROJECTION)
  432. m_projection_matrix = m_projection_matrix * rotation_mat;
  433. else if (m_current_matrix_mode == GL_TEXTURE)
  434. m_texture_matrix = m_texture_matrix * rotation_mat;
  435. else
  436. VERIFY_NOT_REACHED();
  437. }
  438. void SoftwareGLContext::gl_scale(GLdouble x, GLdouble y, GLdouble z)
  439. {
  440. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_scale, x, y, z);
  441. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  442. auto scale_matrix = Gfx::scale_matrix(FloatVector3 { static_cast<float>(x), static_cast<float>(y), static_cast<float>(z) });
  443. if (m_current_matrix_mode == GL_MODELVIEW)
  444. m_model_view_matrix = m_model_view_matrix * scale_matrix;
  445. else if (m_current_matrix_mode == GL_PROJECTION)
  446. m_projection_matrix = m_projection_matrix * scale_matrix;
  447. else if (m_current_matrix_mode == GL_TEXTURE)
  448. m_texture_matrix = m_texture_matrix * scale_matrix;
  449. else
  450. VERIFY_NOT_REACHED();
  451. }
  452. void SoftwareGLContext::gl_translate(GLdouble x, GLdouble y, GLdouble z)
  453. {
  454. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_translate, x, y, z);
  455. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  456. auto translation_matrix = Gfx::translation_matrix(FloatVector3 { static_cast<float>(x), static_cast<float>(y), static_cast<float>(z) });
  457. if (m_current_matrix_mode == GL_MODELVIEW)
  458. m_model_view_matrix = m_model_view_matrix * translation_matrix;
  459. else if (m_current_matrix_mode == GL_PROJECTION)
  460. m_projection_matrix = m_projection_matrix * translation_matrix;
  461. else if (m_current_matrix_mode == GL_TEXTURE)
  462. m_texture_matrix = m_texture_matrix * translation_matrix;
  463. else
  464. VERIFY_NOT_REACHED();
  465. }
  466. void SoftwareGLContext::gl_vertex(GLdouble x, GLdouble y, GLdouble z, GLdouble w)
  467. {
  468. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_vertex, x, y, z, w);
  469. SoftGPU::Vertex vertex;
  470. vertex.position = { static_cast<float>(x), static_cast<float>(y), static_cast<float>(z), static_cast<float>(w) };
  471. vertex.color = m_current_vertex_color;
  472. vertex.tex_coord = m_current_vertex_tex_coord;
  473. vertex.normal = m_current_vertex_normal;
  474. m_vertex_list.append(vertex);
  475. }
  476. // FIXME: We need to add `r` and `q` to our GLVertex?!
  477. void SoftwareGLContext::gl_tex_coord(GLfloat s, GLfloat t, GLfloat r, GLfloat q)
  478. {
  479. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_coord, s, t, r, q);
  480. m_current_vertex_tex_coord = { s, t, r, q };
  481. }
  482. void SoftwareGLContext::gl_viewport(GLint x, GLint y, GLsizei width, GLsizei height)
  483. {
  484. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_viewport, x, y, width, height);
  485. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  486. (void)(x);
  487. (void)(y);
  488. (void)(width);
  489. (void)(height);
  490. }
  491. void SoftwareGLContext::gl_enable(GLenum capability)
  492. {
  493. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_enable, capability);
  494. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  495. auto rasterizer_options = m_rasterizer.options();
  496. bool update_rasterizer_options = false;
  497. switch (capability) {
  498. case GL_CULL_FACE:
  499. m_cull_faces = true;
  500. rasterizer_options.enable_culling = true;
  501. update_rasterizer_options = true;
  502. break;
  503. case GL_DEPTH_TEST:
  504. m_depth_test_enabled = true;
  505. rasterizer_options.enable_depth_test = true;
  506. update_rasterizer_options = true;
  507. break;
  508. case GL_BLEND:
  509. m_blend_enabled = true;
  510. rasterizer_options.enable_blending = true;
  511. update_rasterizer_options = true;
  512. break;
  513. case GL_ALPHA_TEST:
  514. m_alpha_test_enabled = true;
  515. rasterizer_options.enable_alpha_test = true;
  516. update_rasterizer_options = true;
  517. break;
  518. case GL_DITHER:
  519. m_dither_enabled = true;
  520. break;
  521. case GL_FOG:
  522. rasterizer_options.fog_enabled = true;
  523. update_rasterizer_options = true;
  524. break;
  525. case GL_LIGHTING:
  526. m_lighting_enabled = true;
  527. break;
  528. case GL_NORMALIZE:
  529. m_normalize = true;
  530. rasterizer_options.normalization_enabled = true;
  531. update_rasterizer_options = true;
  532. break;
  533. case GL_SCISSOR_TEST:
  534. rasterizer_options.scissor_enabled = true;
  535. update_rasterizer_options = true;
  536. break;
  537. case GL_STENCIL_TEST:
  538. m_stencil_test_enabled = true;
  539. break;
  540. case GL_TEXTURE_1D:
  541. m_active_texture_unit->set_texture_1d_enabled(true);
  542. m_sampler_config_is_dirty = true;
  543. break;
  544. case GL_TEXTURE_2D:
  545. m_active_texture_unit->set_texture_2d_enabled(true);
  546. m_sampler_config_is_dirty = true;
  547. break;
  548. case GL_TEXTURE_3D:
  549. m_active_texture_unit->set_texture_3d_enabled(true);
  550. m_sampler_config_is_dirty = true;
  551. break;
  552. case GL_TEXTURE_CUBE_MAP:
  553. m_active_texture_unit->set_texture_cube_map_enabled(true);
  554. m_sampler_config_is_dirty = true;
  555. break;
  556. case GL_TEXTURE_GEN_Q:
  557. case GL_TEXTURE_GEN_R:
  558. case GL_TEXTURE_GEN_S:
  559. case GL_TEXTURE_GEN_T:
  560. texture_coordinate_generation(capability).enabled = true;
  561. m_texcoord_generation_dirty = true;
  562. break;
  563. default:
  564. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  565. }
  566. if (update_rasterizer_options)
  567. m_rasterizer.set_options(rasterizer_options);
  568. }
  569. void SoftwareGLContext::gl_disable(GLenum capability)
  570. {
  571. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_disable, capability);
  572. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  573. auto rasterizer_options = m_rasterizer.options();
  574. bool update_rasterizer_options = false;
  575. switch (capability) {
  576. case GL_CULL_FACE:
  577. m_cull_faces = false;
  578. rasterizer_options.enable_culling = false;
  579. update_rasterizer_options = true;
  580. break;
  581. case GL_DEPTH_TEST:
  582. m_depth_test_enabled = false;
  583. rasterizer_options.enable_depth_test = false;
  584. update_rasterizer_options = true;
  585. break;
  586. case GL_BLEND:
  587. m_blend_enabled = false;
  588. rasterizer_options.enable_blending = false;
  589. update_rasterizer_options = true;
  590. break;
  591. case GL_ALPHA_TEST:
  592. m_alpha_test_enabled = false;
  593. rasterizer_options.enable_alpha_test = false;
  594. update_rasterizer_options = true;
  595. break;
  596. case GL_DITHER:
  597. m_dither_enabled = false;
  598. break;
  599. case GL_FOG:
  600. rasterizer_options.fog_enabled = false;
  601. update_rasterizer_options = true;
  602. break;
  603. case GL_LIGHTING:
  604. m_lighting_enabled = false;
  605. break;
  606. case GL_NORMALIZE:
  607. m_normalize = false;
  608. rasterizer_options.normalization_enabled = false;
  609. update_rasterizer_options = true;
  610. break;
  611. case GL_SCISSOR_TEST:
  612. rasterizer_options.scissor_enabled = false;
  613. update_rasterizer_options = true;
  614. break;
  615. case GL_STENCIL_TEST:
  616. m_stencil_test_enabled = false;
  617. break;
  618. case GL_TEXTURE_1D:
  619. m_active_texture_unit->set_texture_1d_enabled(false);
  620. m_sampler_config_is_dirty = true;
  621. break;
  622. case GL_TEXTURE_2D:
  623. m_active_texture_unit->set_texture_2d_enabled(false);
  624. m_sampler_config_is_dirty = true;
  625. break;
  626. case GL_TEXTURE_3D:
  627. m_active_texture_unit->set_texture_3d_enabled(false);
  628. m_sampler_config_is_dirty = true;
  629. break;
  630. case GL_TEXTURE_CUBE_MAP:
  631. m_active_texture_unit->set_texture_cube_map_enabled(false);
  632. m_sampler_config_is_dirty = true;
  633. break;
  634. case GL_TEXTURE_GEN_Q:
  635. case GL_TEXTURE_GEN_R:
  636. case GL_TEXTURE_GEN_S:
  637. case GL_TEXTURE_GEN_T:
  638. texture_coordinate_generation(capability).enabled = false;
  639. m_texcoord_generation_dirty = true;
  640. break;
  641. default:
  642. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  643. }
  644. if (update_rasterizer_options)
  645. m_rasterizer.set_options(rasterizer_options);
  646. }
  647. GLboolean SoftwareGLContext::gl_is_enabled(GLenum capability)
  648. {
  649. RETURN_VALUE_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION, 0);
  650. auto rasterizer_options = m_rasterizer.options();
  651. switch (capability) {
  652. case GL_CULL_FACE:
  653. return m_cull_faces;
  654. case GL_DEPTH_TEST:
  655. return m_depth_test_enabled;
  656. case GL_BLEND:
  657. return m_blend_enabled;
  658. case GL_ALPHA_TEST:
  659. return m_alpha_test_enabled;
  660. case GL_DITHER:
  661. return m_dither_enabled;
  662. case GL_FOG:
  663. return rasterizer_options.fog_enabled;
  664. case GL_LIGHTING:
  665. return m_lighting_enabled;
  666. case GL_NORMALIZE:
  667. return m_normalize;
  668. case GL_SCISSOR_TEST:
  669. return rasterizer_options.scissor_enabled;
  670. case GL_STENCIL_TEST:
  671. return m_stencil_test_enabled;
  672. case GL_TEXTURE_GEN_Q:
  673. case GL_TEXTURE_GEN_R:
  674. case GL_TEXTURE_GEN_S:
  675. case GL_TEXTURE_GEN_T:
  676. return texture_coordinate_generation(capability).enabled;
  677. }
  678. RETURN_VALUE_WITH_ERROR_IF(true, GL_INVALID_ENUM, 0);
  679. }
  680. void SoftwareGLContext::gl_gen_textures(GLsizei n, GLuint* textures)
  681. {
  682. RETURN_WITH_ERROR_IF(n < 0, GL_INVALID_VALUE);
  683. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  684. m_name_allocator.allocate(n, textures);
  685. // Initialize all texture names with a nullptr
  686. for (auto i = 0; i < n; i++) {
  687. GLuint name = textures[i];
  688. m_allocated_textures.set(name, nullptr);
  689. }
  690. }
  691. void SoftwareGLContext::gl_delete_textures(GLsizei n, const GLuint* textures)
  692. {
  693. RETURN_WITH_ERROR_IF(n < 0, GL_INVALID_VALUE);
  694. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  695. for (auto i = 0; i < n; i++) {
  696. GLuint name = textures[i];
  697. if (name == 0)
  698. continue;
  699. m_name_allocator.free(name);
  700. auto texture_object = m_allocated_textures.find(name);
  701. if (texture_object == m_allocated_textures.end() || texture_object->value.is_null())
  702. continue;
  703. // Check all texture units
  704. for (auto& texture_unit : m_texture_units) {
  705. if (texture_object->value == texture_unit.bound_texture())
  706. texture_unit.bind_texture_to_target(GL_TEXTURE_2D, nullptr);
  707. }
  708. m_allocated_textures.remove(name);
  709. }
  710. }
  711. 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)
  712. {
  713. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  714. // We only support GL_TEXTURE_2D for now
  715. RETURN_WITH_ERROR_IF(target != GL_TEXTURE_2D, GL_INVALID_ENUM);
  716. // Check if there is actually a texture bound
  717. RETURN_WITH_ERROR_IF(target == GL_TEXTURE_2D && m_active_texture_unit->currently_bound_target() != GL_TEXTURE_2D, GL_INVALID_OPERATION);
  718. // 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
  719. if (internal_format == 1)
  720. internal_format = GL_ALPHA;
  721. else if (internal_format == 2)
  722. internal_format = GL_LUMINANCE_ALPHA;
  723. else if (internal_format == 3)
  724. internal_format = GL_RGB;
  725. else if (internal_format == 4)
  726. internal_format = GL_RGBA;
  727. // We only support symbolic constants for now
  728. RETURN_WITH_ERROR_IF(!(internal_format == GL_RGB || internal_format == GL_RGBA), GL_INVALID_ENUM);
  729. RETURN_WITH_ERROR_IF(!(type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_SHORT_5_6_5), GL_INVALID_VALUE);
  730. RETURN_WITH_ERROR_IF(level < 0 || level > Texture2D::LOG2_MAX_TEXTURE_SIZE, GL_INVALID_VALUE);
  731. RETURN_WITH_ERROR_IF(width < 0 || height < 0 || width > (2 + Texture2D::MAX_TEXTURE_SIZE) || height > (2 + Texture2D::MAX_TEXTURE_SIZE), GL_INVALID_VALUE);
  732. // Check if width and height are a power of 2
  733. RETURN_WITH_ERROR_IF((width & (width - 1)) != 0, GL_INVALID_VALUE);
  734. RETURN_WITH_ERROR_IF((height & (height - 1)) != 0, GL_INVALID_VALUE);
  735. RETURN_WITH_ERROR_IF(border != 0, GL_INVALID_VALUE);
  736. if (level == 0) {
  737. // FIXME: OpenGL has the concept of texture and mipmap completeness. A texture has to fulfill certain criteria to be considered complete.
  738. // Trying to render while an incomplete texture is bound will result in an error.
  739. // Here we simply create a complete device image when mipmap level 0 is attached to the texture object. This has the unfortunate side effect
  740. // that constructing GL textures in any but the default mipmap order, going from level 0 upwards will cause mip levels to stay uninitialized.
  741. // To be spec compliant we should create the device image once the texture has become complete and is used for rendering the first time.
  742. // 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.
  743. SoftGPU::ImageFormat device_format;
  744. switch (internal_format) {
  745. case GL_RGB:
  746. device_format = SoftGPU::ImageFormat::RGB888;
  747. break;
  748. case GL_RGBA:
  749. device_format = SoftGPU::ImageFormat::RGBA8888;
  750. break;
  751. default:
  752. VERIFY_NOT_REACHED();
  753. }
  754. m_active_texture_unit->bound_texture_2d()->set_device_image(m_rasterizer.create_image(device_format, width, height, 1, 999, 1));
  755. m_sampler_config_is_dirty = true;
  756. }
  757. m_active_texture_unit->bound_texture_2d()->upload_texture_data(level, internal_format, width, height, format, type, data, m_unpack_row_length, m_unpack_alignment);
  758. }
  759. 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)
  760. {
  761. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  762. // We only support GL_TEXTURE_2D for now
  763. RETURN_WITH_ERROR_IF(target != GL_TEXTURE_2D, GL_INVALID_ENUM);
  764. // Check if there is actually a texture bound
  765. RETURN_WITH_ERROR_IF(target == GL_TEXTURE_2D && m_active_texture_unit->currently_bound_target() != GL_TEXTURE_2D, GL_INVALID_OPERATION);
  766. // We only support symbolic constants for now
  767. RETURN_WITH_ERROR_IF(!(format == GL_RGBA || format == GL_RGB), GL_INVALID_VALUE);
  768. RETURN_WITH_ERROR_IF(!(type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_SHORT_5_6_5), GL_INVALID_VALUE);
  769. RETURN_WITH_ERROR_IF(level < 0 || level > Texture2D::LOG2_MAX_TEXTURE_SIZE, GL_INVALID_VALUE);
  770. RETURN_WITH_ERROR_IF(width < 0 || height < 0 || width > (2 + Texture2D::MAX_TEXTURE_SIZE) || height > (2 + Texture2D::MAX_TEXTURE_SIZE), GL_INVALID_VALUE);
  771. auto texture = m_active_texture_unit->bound_texture_2d();
  772. 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);
  773. texture->replace_sub_texture_data(level, xoffset, yoffset, width, height, format, type, data, m_unpack_row_length, m_unpack_alignment);
  774. }
  775. void SoftwareGLContext::gl_tex_parameter(GLenum target, GLenum pname, GLfloat param)
  776. {
  777. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_parameter, target, pname, param);
  778. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  779. // FIXME: We currently only support GL_TETXURE_2D targets. 1D, 3D and CUBE should also be supported (https://docs.gl/gl2/glTexParameter)
  780. RETURN_WITH_ERROR_IF(target != GL_TEXTURE_2D, GL_INVALID_ENUM);
  781. // FIXME: implement the remaining parameters. (https://docs.gl/gl2/glTexParameter)
  782. RETURN_WITH_ERROR_IF(!(pname == GL_TEXTURE_MIN_FILTER
  783. || pname == GL_TEXTURE_MAG_FILTER
  784. || pname == GL_TEXTURE_WRAP_S
  785. || pname == GL_TEXTURE_WRAP_T),
  786. GL_INVALID_ENUM);
  787. if (target == GL_TEXTURE_2D) {
  788. auto texture2d = m_active_texture_unit->bound_texture_2d();
  789. if (texture2d.is_null())
  790. return;
  791. switch (pname) {
  792. case GL_TEXTURE_MIN_FILTER:
  793. RETURN_WITH_ERROR_IF(!(param == GL_NEAREST
  794. || param == GL_LINEAR
  795. || param == GL_NEAREST_MIPMAP_NEAREST
  796. || param == GL_LINEAR_MIPMAP_NEAREST
  797. || param == GL_NEAREST_MIPMAP_LINEAR
  798. || param == GL_LINEAR_MIPMAP_LINEAR),
  799. GL_INVALID_ENUM);
  800. texture2d->sampler().set_min_filter(param);
  801. break;
  802. case GL_TEXTURE_MAG_FILTER:
  803. RETURN_WITH_ERROR_IF(!(param == GL_NEAREST
  804. || param == GL_LINEAR),
  805. GL_INVALID_ENUM);
  806. texture2d->sampler().set_mag_filter(param);
  807. break;
  808. case GL_TEXTURE_WRAP_S:
  809. RETURN_WITH_ERROR_IF(!(param == GL_CLAMP
  810. || param == GL_CLAMP_TO_BORDER
  811. || param == GL_CLAMP_TO_EDGE
  812. || param == GL_MIRRORED_REPEAT
  813. || param == GL_REPEAT),
  814. GL_INVALID_ENUM);
  815. texture2d->sampler().set_wrap_s_mode(param);
  816. break;
  817. case GL_TEXTURE_WRAP_T:
  818. RETURN_WITH_ERROR_IF(!(param == GL_CLAMP
  819. || param == GL_CLAMP_TO_BORDER
  820. || param == GL_CLAMP_TO_EDGE
  821. || param == GL_MIRRORED_REPEAT
  822. || param == GL_REPEAT),
  823. GL_INVALID_ENUM);
  824. texture2d->sampler().set_wrap_t_mode(param);
  825. break;
  826. default:
  827. VERIFY_NOT_REACHED();
  828. }
  829. }
  830. m_sampler_config_is_dirty = true;
  831. }
  832. void SoftwareGLContext::gl_front_face(GLenum face)
  833. {
  834. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_front_face, face);
  835. RETURN_WITH_ERROR_IF(face < GL_CW || face > GL_CCW, GL_INVALID_ENUM);
  836. m_front_face = face;
  837. auto rasterizer_options = m_rasterizer.options();
  838. rasterizer_options.front_face = (face == GL_CW) ? SoftGPU::WindingOrder::Clockwise : SoftGPU::WindingOrder::CounterClockwise;
  839. m_rasterizer.set_options(rasterizer_options);
  840. }
  841. void SoftwareGLContext::gl_cull_face(GLenum cull_mode)
  842. {
  843. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_cull_face, cull_mode);
  844. RETURN_WITH_ERROR_IF(cull_mode < GL_FRONT || cull_mode > GL_FRONT_AND_BACK, GL_INVALID_ENUM);
  845. m_culled_sides = cull_mode;
  846. auto rasterizer_options = m_rasterizer.options();
  847. rasterizer_options.cull_back = cull_mode == GL_BACK || cull_mode == GL_FRONT_AND_BACK;
  848. rasterizer_options.cull_front = cull_mode == GL_FRONT || cull_mode == GL_FRONT_AND_BACK;
  849. m_rasterizer.set_options(rasterizer_options);
  850. }
  851. GLuint SoftwareGLContext::gl_gen_lists(GLsizei range)
  852. {
  853. RETURN_VALUE_WITH_ERROR_IF(range <= 0, GL_INVALID_VALUE, 0);
  854. RETURN_VALUE_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION, 0);
  855. auto initial_entry = m_listings.size();
  856. m_listings.resize(range + initial_entry);
  857. return initial_entry + 1;
  858. }
  859. void SoftwareGLContext::invoke_list(size_t list_index)
  860. {
  861. auto& listing = m_listings[list_index - 1];
  862. for (auto& entry : listing.entries) {
  863. entry.function.visit([&](auto& function) {
  864. entry.arguments.visit([&](auto& arguments) {
  865. auto apply = [&]<typename... Args>(Args && ... args)
  866. {
  867. if constexpr (requires { (this->*function)(forward<Args>(args)...); })
  868. (this->*function)(forward<Args>(args)...);
  869. };
  870. arguments.apply_as_args(apply);
  871. });
  872. });
  873. }
  874. }
  875. void SoftwareGLContext::gl_call_list(GLuint list)
  876. {
  877. if (m_gl_call_depth > max_allowed_gl_call_depth)
  878. return;
  879. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_call_list, list);
  880. if (m_listings.size() < list)
  881. return;
  882. TemporaryChange change { m_gl_call_depth, m_gl_call_depth + 1 };
  883. invoke_list(list);
  884. }
  885. void SoftwareGLContext::gl_call_lists(GLsizei n, GLenum type, void const* lists)
  886. {
  887. if (m_gl_call_depth > max_allowed_gl_call_depth)
  888. return;
  889. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_call_lists, n, type, lists);
  890. RETURN_WITH_ERROR_IF(n < 0, GL_INVALID_VALUE);
  891. RETURN_WITH_ERROR_IF(!(type == GL_BYTE
  892. || type == GL_UNSIGNED_BYTE
  893. || type == GL_SHORT
  894. || type == GL_UNSIGNED_SHORT
  895. || type == GL_INT
  896. || type == GL_UNSIGNED_INT
  897. || type == GL_FLOAT
  898. || type == GL_2_BYTES
  899. || type == GL_3_BYTES
  900. || type == GL_4_BYTES),
  901. GL_INVALID_ENUM);
  902. TemporaryChange change { m_gl_call_depth, m_gl_call_depth + 1 };
  903. auto invoke_all_lists = [&]<typename T>(T const* lists) {
  904. for (int i = 0; i < n; ++i) {
  905. auto list = static_cast<size_t>(lists[i]);
  906. invoke_list(m_list_base + list);
  907. }
  908. };
  909. switch (type) {
  910. case GL_BYTE:
  911. invoke_all_lists(static_cast<GLbyte const*>(lists));
  912. break;
  913. case GL_UNSIGNED_BYTE:
  914. invoke_all_lists(static_cast<GLubyte const*>(lists));
  915. break;
  916. case GL_SHORT:
  917. invoke_all_lists(static_cast<GLshort const*>(lists));
  918. break;
  919. case GL_UNSIGNED_SHORT:
  920. invoke_all_lists(static_cast<GLushort const*>(lists));
  921. break;
  922. case GL_INT:
  923. invoke_all_lists(static_cast<GLint const*>(lists));
  924. break;
  925. case GL_UNSIGNED_INT:
  926. invoke_all_lists(static_cast<GLuint const*>(lists));
  927. break;
  928. case GL_FLOAT:
  929. invoke_all_lists(static_cast<GLfloat const*>(lists));
  930. break;
  931. case GL_2_BYTES:
  932. case GL_3_BYTES:
  933. case GL_4_BYTES:
  934. dbgln("SoftwareGLContext FIXME: unimplemented glCallLists() with type {}", type);
  935. break;
  936. default:
  937. VERIFY_NOT_REACHED();
  938. }
  939. }
  940. void SoftwareGLContext::gl_delete_lists(GLuint list, GLsizei range)
  941. {
  942. if (m_listings.size() < list || m_listings.size() <= list + range)
  943. return;
  944. for (auto& entry : m_listings.span().slice(list - 1, range))
  945. entry.entries.clear_with_capacity();
  946. }
  947. void SoftwareGLContext::gl_list_base(GLuint base)
  948. {
  949. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_list_base, base);
  950. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  951. m_list_base = base;
  952. }
  953. void SoftwareGLContext::gl_end_list()
  954. {
  955. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  956. RETURN_WITH_ERROR_IF(!m_current_listing_index.has_value(), GL_INVALID_OPERATION);
  957. m_listings[m_current_listing_index->index] = move(m_current_listing_index->listing);
  958. m_current_listing_index.clear();
  959. }
  960. void SoftwareGLContext::gl_new_list(GLuint list, GLenum mode)
  961. {
  962. RETURN_WITH_ERROR_IF(list == 0, GL_INVALID_VALUE);
  963. RETURN_WITH_ERROR_IF(mode != GL_COMPILE && mode != GL_COMPILE_AND_EXECUTE, GL_INVALID_ENUM);
  964. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  965. RETURN_WITH_ERROR_IF(m_current_listing_index.has_value(), GL_INVALID_OPERATION);
  966. if (m_listings.size() < list)
  967. return;
  968. m_current_listing_index = CurrentListing { {}, static_cast<size_t>(list - 1), mode };
  969. }
  970. GLboolean SoftwareGLContext::gl_is_list(GLuint list)
  971. {
  972. RETURN_VALUE_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION, GL_FALSE);
  973. return list < m_listings.size() ? GL_TRUE : GL_FALSE;
  974. }
  975. void SoftwareGLContext::gl_flush()
  976. {
  977. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  978. // No-op since SoftwareGLContext is completely synchronous at the moment
  979. }
  980. void SoftwareGLContext::gl_finish()
  981. {
  982. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  983. // No-op since SoftwareGLContext is completely synchronous at the moment
  984. }
  985. void SoftwareGLContext::gl_blend_func(GLenum src_factor, GLenum dst_factor)
  986. {
  987. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_blend_func, src_factor, dst_factor);
  988. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  989. // FIXME: The list of allowed enums differs between API versions
  990. // This was taken from the 2.0 spec on https://docs.gl/gl2/glBlendFunc
  991. RETURN_WITH_ERROR_IF(!(src_factor == GL_ZERO
  992. || src_factor == GL_ONE
  993. || src_factor == GL_SRC_COLOR
  994. || src_factor == GL_ONE_MINUS_SRC_COLOR
  995. || src_factor == GL_DST_COLOR
  996. || src_factor == GL_ONE_MINUS_DST_COLOR
  997. || src_factor == GL_SRC_ALPHA
  998. || src_factor == GL_ONE_MINUS_SRC_ALPHA
  999. || src_factor == GL_DST_ALPHA
  1000. || src_factor == GL_ONE_MINUS_DST_ALPHA
  1001. || src_factor == GL_CONSTANT_COLOR
  1002. || src_factor == GL_ONE_MINUS_CONSTANT_COLOR
  1003. || src_factor == GL_CONSTANT_ALPHA
  1004. || src_factor == GL_ONE_MINUS_CONSTANT_ALPHA
  1005. || src_factor == GL_SRC_ALPHA_SATURATE),
  1006. GL_INVALID_ENUM);
  1007. RETURN_WITH_ERROR_IF(!(dst_factor == GL_ZERO
  1008. || dst_factor == GL_ONE
  1009. || dst_factor == GL_SRC_COLOR
  1010. || dst_factor == GL_ONE_MINUS_SRC_COLOR
  1011. || dst_factor == GL_DST_COLOR
  1012. || dst_factor == GL_ONE_MINUS_DST_COLOR
  1013. || dst_factor == GL_SRC_ALPHA
  1014. || dst_factor == GL_ONE_MINUS_SRC_ALPHA
  1015. || dst_factor == GL_DST_ALPHA
  1016. || dst_factor == GL_ONE_MINUS_DST_ALPHA
  1017. || dst_factor == GL_CONSTANT_COLOR
  1018. || dst_factor == GL_ONE_MINUS_CONSTANT_COLOR
  1019. || dst_factor == GL_CONSTANT_ALPHA
  1020. || dst_factor == GL_ONE_MINUS_CONSTANT_ALPHA),
  1021. GL_INVALID_ENUM);
  1022. m_blend_source_factor = src_factor;
  1023. m_blend_destination_factor = dst_factor;
  1024. auto map_gl_blend_factor_to_device = [](GLenum factor) constexpr
  1025. {
  1026. switch (factor) {
  1027. case GL_ZERO:
  1028. return SoftGPU::BlendFactor::Zero;
  1029. case GL_ONE:
  1030. return SoftGPU::BlendFactor::One;
  1031. case GL_SRC_ALPHA:
  1032. return SoftGPU::BlendFactor::SrcAlpha;
  1033. case GL_ONE_MINUS_SRC_ALPHA:
  1034. return SoftGPU::BlendFactor::OneMinusSrcAlpha;
  1035. case GL_SRC_COLOR:
  1036. return SoftGPU::BlendFactor::SrcColor;
  1037. case GL_ONE_MINUS_SRC_COLOR:
  1038. return SoftGPU::BlendFactor::OneMinusSrcColor;
  1039. case GL_DST_ALPHA:
  1040. return SoftGPU::BlendFactor::DstAlpha;
  1041. case GL_ONE_MINUS_DST_ALPHA:
  1042. return SoftGPU::BlendFactor::OneMinusDstAlpha;
  1043. case GL_DST_COLOR:
  1044. return SoftGPU::BlendFactor::DstColor;
  1045. case GL_ONE_MINUS_DST_COLOR:
  1046. return SoftGPU::BlendFactor::OneMinusDstColor;
  1047. case GL_SRC_ALPHA_SATURATE:
  1048. return SoftGPU::BlendFactor::SrcAlphaSaturate;
  1049. default:
  1050. VERIFY_NOT_REACHED();
  1051. }
  1052. };
  1053. auto options = m_rasterizer.options();
  1054. options.blend_source_factor = map_gl_blend_factor_to_device(m_blend_source_factor);
  1055. options.blend_destination_factor = map_gl_blend_factor_to_device(m_blend_destination_factor);
  1056. m_rasterizer.set_options(options);
  1057. }
  1058. void SoftwareGLContext::gl_shade_model(GLenum mode)
  1059. {
  1060. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_shade_model, mode);
  1061. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1062. RETURN_WITH_ERROR_IF(mode != GL_FLAT && mode != GL_SMOOTH, GL_INVALID_ENUM);
  1063. auto options = m_rasterizer.options();
  1064. options.shade_smooth = (mode == GL_SMOOTH);
  1065. m_rasterizer.set_options(options);
  1066. }
  1067. void SoftwareGLContext::gl_alpha_func(GLenum func, GLclampf ref)
  1068. {
  1069. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_alpha_func, func, ref);
  1070. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1071. RETURN_WITH_ERROR_IF(func < GL_NEVER || func > GL_ALWAYS, GL_INVALID_ENUM);
  1072. m_alpha_test_func = func;
  1073. m_alpha_test_ref_value = ref;
  1074. auto options = m_rasterizer.options();
  1075. switch (func) {
  1076. case GL_NEVER:
  1077. options.alpha_test_func = SoftGPU::AlphaTestFunction::Never;
  1078. break;
  1079. case GL_ALWAYS:
  1080. options.alpha_test_func = SoftGPU::AlphaTestFunction::Always;
  1081. break;
  1082. case GL_LESS:
  1083. options.alpha_test_func = SoftGPU::AlphaTestFunction::Less;
  1084. break;
  1085. case GL_LEQUAL:
  1086. options.alpha_test_func = SoftGPU::AlphaTestFunction::LessOrEqual;
  1087. break;
  1088. case GL_EQUAL:
  1089. options.alpha_test_func = SoftGPU::AlphaTestFunction::Equal;
  1090. break;
  1091. case GL_NOTEQUAL:
  1092. options.alpha_test_func = SoftGPU::AlphaTestFunction::NotEqual;
  1093. break;
  1094. case GL_GEQUAL:
  1095. options.alpha_test_func = SoftGPU::AlphaTestFunction::GreaterOrEqual;
  1096. break;
  1097. case GL_GREATER:
  1098. options.alpha_test_func = SoftGPU::AlphaTestFunction::Greater;
  1099. break;
  1100. default:
  1101. VERIFY_NOT_REACHED();
  1102. }
  1103. options.alpha_test_ref_value = m_alpha_test_ref_value;
  1104. m_rasterizer.set_options(options);
  1105. }
  1106. void SoftwareGLContext::gl_hint(GLenum target, GLenum mode)
  1107. {
  1108. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_hint, target, mode);
  1109. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1110. RETURN_WITH_ERROR_IF(target != GL_PERSPECTIVE_CORRECTION_HINT
  1111. && target != GL_POINT_SMOOTH_HINT
  1112. && target != GL_LINE_SMOOTH_HINT
  1113. && target != GL_POLYGON_SMOOTH_HINT
  1114. && target != GL_FOG_HINT
  1115. && target != GL_GENERATE_MIPMAP_HINT
  1116. && target != GL_TEXTURE_COMPRESSION_HINT,
  1117. GL_INVALID_ENUM);
  1118. RETURN_WITH_ERROR_IF(mode != GL_DONT_CARE
  1119. && mode != GL_FASTEST
  1120. && mode != GL_NICEST,
  1121. GL_INVALID_ENUM);
  1122. // According to the spec implementors are free to ignore glHint. So we do.
  1123. }
  1124. void SoftwareGLContext::gl_read_buffer(GLenum mode)
  1125. {
  1126. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_read_buffer, mode);
  1127. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1128. // FIXME: Also allow aux buffers GL_AUX0 through GL_AUX3 here
  1129. // plus any aux buffer between 0 and GL_AUX_BUFFERS
  1130. RETURN_WITH_ERROR_IF(mode != GL_FRONT_LEFT
  1131. && mode != GL_FRONT_RIGHT
  1132. && mode != GL_BACK_LEFT
  1133. && mode != GL_BACK_RIGHT
  1134. && mode != GL_FRONT
  1135. && mode != GL_BACK
  1136. && mode != GL_LEFT
  1137. && mode != GL_RIGHT,
  1138. GL_INVALID_ENUM);
  1139. // FIXME: We do not currently have aux buffers, so make it an invalid
  1140. // operation to select anything but front or back buffers. Also we do
  1141. // not allow selecting the stereoscopic RIGHT buffers since we do not
  1142. // have them configured.
  1143. RETURN_WITH_ERROR_IF(mode != GL_FRONT_LEFT
  1144. && mode != GL_FRONT
  1145. && mode != GL_BACK_LEFT
  1146. && mode != GL_BACK
  1147. && mode != GL_FRONT
  1148. && mode != GL_BACK
  1149. && mode != GL_LEFT,
  1150. GL_INVALID_OPERATION);
  1151. m_current_read_buffer = mode;
  1152. }
  1153. void SoftwareGLContext::gl_draw_buffer(GLenum buffer)
  1154. {
  1155. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_draw_buffer, buffer);
  1156. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1157. // FIXME: Also allow aux buffers GL_AUX0 through GL_AUX3 here
  1158. // plus any aux buffer between 0 and GL_AUX_BUFFERS
  1159. RETURN_WITH_ERROR_IF(buffer != GL_NONE
  1160. && buffer != GL_FRONT_LEFT
  1161. && buffer != GL_FRONT_RIGHT
  1162. && buffer != GL_BACK_LEFT
  1163. && buffer != GL_BACK_RIGHT
  1164. && buffer != GL_FRONT
  1165. && buffer != GL_BACK
  1166. && buffer != GL_LEFT
  1167. && buffer != GL_RIGHT,
  1168. GL_INVALID_ENUM);
  1169. // FIXME: We do not currently have aux buffers, so make it an invalid
  1170. // operation to select anything but front or back buffers. Also we do
  1171. // not allow selecting the stereoscopic RIGHT buffers since we do not
  1172. // have them configured.
  1173. RETURN_WITH_ERROR_IF(buffer != GL_NONE
  1174. && buffer != GL_FRONT_LEFT
  1175. && buffer != GL_FRONT
  1176. && buffer != GL_BACK_LEFT
  1177. && buffer != GL_BACK
  1178. && buffer != GL_FRONT
  1179. && buffer != GL_BACK
  1180. && buffer != GL_LEFT,
  1181. GL_INVALID_OPERATION);
  1182. m_current_draw_buffer = buffer;
  1183. auto rasterizer_options = m_rasterizer.options();
  1184. // FIXME: We only have a single draw buffer in SoftGPU at the moment,
  1185. // so we simply disable color writes if GL_NONE is selected
  1186. rasterizer_options.enable_color_write = m_current_draw_buffer != GL_NONE;
  1187. m_rasterizer.set_options(rasterizer_options);
  1188. }
  1189. void SoftwareGLContext::gl_read_pixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid* pixels)
  1190. {
  1191. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1192. RETURN_WITH_ERROR_IF(width < 0 || height < 0, GL_INVALID_VALUE);
  1193. RETURN_WITH_ERROR_IF(format != GL_COLOR_INDEX
  1194. && format != GL_STENCIL_INDEX
  1195. && format != GL_DEPTH_COMPONENT
  1196. && format != GL_RED
  1197. && format != GL_GREEN
  1198. && format != GL_BLUE
  1199. && format != GL_ALPHA
  1200. && format != GL_RGB
  1201. && format != GL_RGBA
  1202. && format != GL_LUMINANCE
  1203. && format != GL_LUMINANCE_ALPHA,
  1204. GL_INVALID_ENUM);
  1205. RETURN_WITH_ERROR_IF(type != GL_UNSIGNED_BYTE
  1206. && type != GL_BYTE
  1207. && type != GL_BITMAP
  1208. && type != GL_UNSIGNED_SHORT
  1209. && type != GL_SHORT
  1210. && type != GL_BLUE
  1211. && type != GL_UNSIGNED_INT
  1212. && type != GL_INT
  1213. && type != GL_FLOAT,
  1214. GL_INVALID_ENUM);
  1215. // FIXME: We only support RGBA buffers for now.
  1216. // Once we add support for indexed color modes do the correct check here
  1217. RETURN_WITH_ERROR_IF(format == GL_COLOR_INDEX, GL_INVALID_OPERATION);
  1218. // FIXME: We do not have stencil buffers yet
  1219. // Once we add support for stencil buffers do the correct check here
  1220. RETURN_WITH_ERROR_IF(format == GL_STENCIL_INDEX, GL_INVALID_OPERATION);
  1221. if (format == GL_DEPTH_COMPONENT) {
  1222. // FIXME: This check needs to be a bit more sophisticated. Currently the buffers
  1223. // are hardcoded. Once we add proper structures for them we need to correct this check
  1224. // Error because only back buffer has a depth buffer
  1225. RETURN_WITH_ERROR_IF(m_current_read_buffer == GL_FRONT
  1226. || m_current_read_buffer == GL_FRONT_LEFT
  1227. || m_current_read_buffer == GL_FRONT_RIGHT,
  1228. GL_INVALID_OPERATION);
  1229. }
  1230. // Some helper functions for converting float values to integer types
  1231. auto float_to_i8 = [](float f) -> GLchar {
  1232. return static_cast<GLchar>((0x7f * min(max(f, 0.0f), 1.0f) - 1) / 2);
  1233. };
  1234. auto float_to_i16 = [](float f) -> GLshort {
  1235. return static_cast<GLshort>((0x7fff * min(max(f, 0.0f), 1.0f) - 1) / 2);
  1236. };
  1237. auto float_to_i32 = [](float f) -> GLint {
  1238. return static_cast<GLint>((0x7fffffff * min(max(f, 0.0f), 1.0f) - 1) / 2);
  1239. };
  1240. auto float_to_u8 = [](float f) -> GLubyte {
  1241. return static_cast<GLubyte>(0xff * min(max(f, 0.0f), 1.0f));
  1242. };
  1243. auto float_to_u16 = [](float f) -> GLushort {
  1244. return static_cast<GLushort>(0xffff * min(max(f, 0.0f), 1.0f));
  1245. };
  1246. auto float_to_u32 = [](float f) -> GLuint {
  1247. return static_cast<GLuint>(0xffffffff * min(max(f, 0.0f), 1.0f));
  1248. };
  1249. u8 component_size = 0;
  1250. switch (type) {
  1251. case GL_BYTE:
  1252. case GL_UNSIGNED_BYTE:
  1253. component_size = 1;
  1254. break;
  1255. case GL_SHORT:
  1256. case GL_UNSIGNED_SHORT:
  1257. component_size = 2;
  1258. break;
  1259. case GL_INT:
  1260. case GL_UNSIGNED_INT:
  1261. case GL_FLOAT:
  1262. component_size = 4;
  1263. break;
  1264. }
  1265. if (format == GL_DEPTH_COMPONENT) {
  1266. auto const row_stride = (width * component_size + m_pack_alignment - 1) / m_pack_alignment * m_pack_alignment;
  1267. // Read from depth buffer
  1268. for (GLsizei i = 0; i < height; ++i) {
  1269. for (GLsizei j = 0; j < width; ++j) {
  1270. float depth = m_rasterizer.get_depthbuffer_value(x + j, y + i);
  1271. auto char_ptr = reinterpret_cast<char*>(pixels) + i * row_stride + j * component_size;
  1272. switch (type) {
  1273. case GL_BYTE:
  1274. *reinterpret_cast<GLchar*>(char_ptr) = float_to_i8(depth);
  1275. break;
  1276. case GL_SHORT:
  1277. *reinterpret_cast<GLshort*>(char_ptr) = float_to_i16(depth);
  1278. break;
  1279. case GL_INT:
  1280. *reinterpret_cast<GLint*>(char_ptr) = float_to_i32(depth);
  1281. break;
  1282. case GL_UNSIGNED_BYTE:
  1283. *reinterpret_cast<GLubyte*>(char_ptr) = float_to_u8(depth);
  1284. break;
  1285. case GL_UNSIGNED_SHORT:
  1286. *reinterpret_cast<GLushort*>(char_ptr) = float_to_u16(depth);
  1287. break;
  1288. case GL_UNSIGNED_INT:
  1289. *reinterpret_cast<GLuint*>(char_ptr) = float_to_u32(depth);
  1290. break;
  1291. case GL_FLOAT:
  1292. *reinterpret_cast<GLfloat*>(char_ptr) = min(max(depth, 0.0f), 1.0f);
  1293. break;
  1294. }
  1295. }
  1296. }
  1297. return;
  1298. }
  1299. bool write_red = false;
  1300. bool write_green = false;
  1301. bool write_blue = false;
  1302. bool write_alpha = false;
  1303. size_t component_count = 0;
  1304. size_t red_offset = 0;
  1305. size_t green_offset = 0;
  1306. size_t blue_offset = 0;
  1307. size_t alpha_offset = 0;
  1308. char* red_ptr = nullptr;
  1309. char* green_ptr = nullptr;
  1310. char* blue_ptr = nullptr;
  1311. char* alpha_ptr = nullptr;
  1312. switch (format) {
  1313. case GL_RGB:
  1314. write_red = true;
  1315. write_green = true;
  1316. write_blue = true;
  1317. component_count = 3;
  1318. red_offset = 2;
  1319. green_offset = 1;
  1320. blue_offset = 0;
  1321. break;
  1322. case GL_RGBA:
  1323. write_red = true;
  1324. write_green = true;
  1325. write_blue = true;
  1326. write_alpha = true;
  1327. component_count = 4;
  1328. red_offset = 3;
  1329. green_offset = 2;
  1330. blue_offset = 1;
  1331. alpha_offset = 0;
  1332. break;
  1333. case GL_RED:
  1334. write_red = true;
  1335. component_count = 1;
  1336. red_offset = 0;
  1337. break;
  1338. case GL_GREEN:
  1339. write_green = true;
  1340. component_count = 1;
  1341. green_offset = 0;
  1342. break;
  1343. case GL_BLUE:
  1344. write_blue = true;
  1345. component_count = 1;
  1346. blue_offset = 0;
  1347. break;
  1348. case GL_ALPHA:
  1349. write_alpha = true;
  1350. component_count = 1;
  1351. alpha_offset = 0;
  1352. break;
  1353. }
  1354. auto const pixel_bytes = component_size * component_count;
  1355. auto const row_alignment_bytes = (m_pack_alignment - ((width * pixel_bytes) % m_pack_alignment)) % m_pack_alignment;
  1356. char* out_ptr = reinterpret_cast<char*>(pixels);
  1357. for (int i = 0; i < (int)height; ++i) {
  1358. for (int j = 0; j < (int)width; ++j) {
  1359. Gfx::RGBA32 color {};
  1360. if (m_current_read_buffer == GL_FRONT || m_current_read_buffer == GL_LEFT || m_current_read_buffer == GL_FRONT_LEFT) {
  1361. if (y + i >= m_frontbuffer->width() || x + j >= m_frontbuffer->height())
  1362. color = 0;
  1363. else
  1364. color = m_frontbuffer->scanline(y + i)[x + j];
  1365. } else {
  1366. color = m_rasterizer.get_backbuffer_pixel(x + j, y + i);
  1367. }
  1368. float red = ((color >> 24) & 0xff) / 255.0f;
  1369. float green = ((color >> 16) & 0xff) / 255.0f;
  1370. float blue = ((color >> 8) & 0xff) / 255.0f;
  1371. float alpha = (color & 0xff) / 255.0f;
  1372. // FIXME: Set up write pointers based on selected endianness (glPixelStore)
  1373. red_ptr = out_ptr + (component_size * red_offset);
  1374. green_ptr = out_ptr + (component_size * green_offset);
  1375. blue_ptr = out_ptr + (component_size * blue_offset);
  1376. alpha_ptr = out_ptr + (component_size * alpha_offset);
  1377. switch (type) {
  1378. case GL_BYTE:
  1379. if (write_red)
  1380. *reinterpret_cast<GLchar*>(red_ptr) = float_to_i8(red);
  1381. if (write_green)
  1382. *reinterpret_cast<GLchar*>(green_ptr) = float_to_i8(green);
  1383. if (write_blue)
  1384. *reinterpret_cast<GLchar*>(blue_ptr) = float_to_i8(blue);
  1385. if (write_alpha)
  1386. *reinterpret_cast<GLchar*>(alpha_ptr) = float_to_i8(alpha);
  1387. break;
  1388. case GL_UNSIGNED_BYTE:
  1389. if (write_red)
  1390. *reinterpret_cast<GLubyte*>(red_ptr) = float_to_u8(red);
  1391. if (write_green)
  1392. *reinterpret_cast<GLubyte*>(green_ptr) = float_to_u8(green);
  1393. if (write_blue)
  1394. *reinterpret_cast<GLubyte*>(blue_ptr) = float_to_u8(blue);
  1395. if (write_alpha)
  1396. *reinterpret_cast<GLubyte*>(alpha_ptr) = float_to_u8(alpha);
  1397. break;
  1398. case GL_SHORT:
  1399. if (write_red)
  1400. *reinterpret_cast<GLshort*>(red_ptr) = float_to_i16(red);
  1401. if (write_green)
  1402. *reinterpret_cast<GLshort*>(green_ptr) = float_to_i16(green);
  1403. if (write_blue)
  1404. *reinterpret_cast<GLshort*>(blue_ptr) = float_to_i16(blue);
  1405. if (write_alpha)
  1406. *reinterpret_cast<GLshort*>(alpha_ptr) = float_to_i16(alpha);
  1407. break;
  1408. case GL_UNSIGNED_SHORT:
  1409. if (write_red)
  1410. *reinterpret_cast<GLushort*>(red_ptr) = float_to_u16(red);
  1411. if (write_green)
  1412. *reinterpret_cast<GLushort*>(green_ptr) = float_to_u16(green);
  1413. if (write_blue)
  1414. *reinterpret_cast<GLushort*>(blue_ptr) = float_to_u16(blue);
  1415. if (write_alpha)
  1416. *reinterpret_cast<GLushort*>(alpha_ptr) = float_to_u16(alpha);
  1417. break;
  1418. case GL_INT:
  1419. if (write_red)
  1420. *reinterpret_cast<GLint*>(red_ptr) = float_to_i32(red);
  1421. if (write_green)
  1422. *reinterpret_cast<GLint*>(green_ptr) = float_to_i32(green);
  1423. if (write_blue)
  1424. *reinterpret_cast<GLint*>(blue_ptr) = float_to_i32(blue);
  1425. if (write_alpha)
  1426. *reinterpret_cast<GLint*>(alpha_ptr) = float_to_i32(alpha);
  1427. break;
  1428. case GL_UNSIGNED_INT:
  1429. if (write_red)
  1430. *reinterpret_cast<GLuint*>(red_ptr) = float_to_u32(red);
  1431. if (write_green)
  1432. *reinterpret_cast<GLuint*>(green_ptr) = float_to_u32(green);
  1433. if (write_blue)
  1434. *reinterpret_cast<GLuint*>(blue_ptr) = float_to_u32(blue);
  1435. if (write_alpha)
  1436. *reinterpret_cast<GLuint*>(alpha_ptr) = float_to_u32(alpha);
  1437. break;
  1438. case GL_FLOAT:
  1439. if (write_red)
  1440. *reinterpret_cast<GLfloat*>(red_ptr) = min(max(red, 0.0f), 1.0f);
  1441. if (write_green)
  1442. *reinterpret_cast<GLfloat*>(green_ptr) = min(max(green, 0.0f), 1.0f);
  1443. if (write_blue)
  1444. *reinterpret_cast<GLfloat*>(blue_ptr) = min(max(blue, 0.0f), 1.0f);
  1445. if (write_alpha)
  1446. *reinterpret_cast<GLfloat*>(alpha_ptr) = min(max(alpha, 0.0f), 1.0f);
  1447. break;
  1448. }
  1449. out_ptr += pixel_bytes;
  1450. }
  1451. out_ptr += row_alignment_bytes;
  1452. }
  1453. }
  1454. void SoftwareGLContext::gl_bind_texture(GLenum target, GLuint texture)
  1455. {
  1456. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1457. // FIXME: We only support GL_TEXTURE_2D for now
  1458. RETURN_WITH_ERROR_IF(target != GL_TEXTURE_2D, GL_INVALID_ENUM);
  1459. if (texture == 0) {
  1460. switch (target) {
  1461. case GL_TEXTURE_2D:
  1462. m_active_texture_unit->bind_texture_to_target(target, nullptr);
  1463. m_sampler_config_is_dirty = true;
  1464. return;
  1465. default:
  1466. VERIFY_NOT_REACHED();
  1467. return;
  1468. }
  1469. }
  1470. auto it = m_allocated_textures.find(texture);
  1471. // The texture name does not exist
  1472. RETURN_WITH_ERROR_IF(it == m_allocated_textures.end(), GL_INVALID_VALUE);
  1473. auto texture_object = it->value;
  1474. // Binding a texture to a different target than it was first bound is an invalid operation
  1475. // FIXME: We only support GL_TEXTURE_2D for now
  1476. RETURN_WITH_ERROR_IF(target == GL_TEXTURE_2D && !texture_object.is_null() && !texture_object->is_texture_2d(), GL_INVALID_OPERATION);
  1477. if (!texture_object) {
  1478. // This is the first time the texture is bound. Allocate an actual texture object
  1479. switch (target) {
  1480. case GL_TEXTURE_2D:
  1481. texture_object = adopt_ref(*new Texture2D());
  1482. break;
  1483. default:
  1484. VERIFY_NOT_REACHED();
  1485. }
  1486. m_allocated_textures.set(texture, texture_object);
  1487. }
  1488. switch (target) {
  1489. case GL_TEXTURE_2D:
  1490. m_active_texture_unit->bind_texture_to_target(target, texture_object);
  1491. break;
  1492. }
  1493. m_sampler_config_is_dirty = true;
  1494. }
  1495. void SoftwareGLContext::gl_active_texture(GLenum texture)
  1496. {
  1497. RETURN_WITH_ERROR_IF(texture < GL_TEXTURE0 || texture > GL_TEXTURE31, GL_INVALID_ENUM);
  1498. m_active_texture_unit = &m_texture_units.at(texture - GL_TEXTURE0);
  1499. }
  1500. void SoftwareGLContext::gl_get_booleanv(GLenum pname, GLboolean* data)
  1501. {
  1502. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1503. auto optional_parameter = get_context_parameter(pname);
  1504. RETURN_WITH_ERROR_IF(!optional_parameter.has_value(), GL_INVALID_ENUM);
  1505. auto parameter = optional_parameter.release_value();
  1506. switch (parameter.type) {
  1507. case GL_BOOL:
  1508. *data = parameter.value.boolean_value ? GL_TRUE : GL_FALSE;
  1509. break;
  1510. case GL_DOUBLE:
  1511. *data = (parameter.value.double_value == 0.0) ? GL_FALSE : GL_TRUE;
  1512. break;
  1513. case GL_INT:
  1514. *data = (parameter.value.integer_value == 0) ? GL_FALSE : GL_TRUE;
  1515. break;
  1516. default:
  1517. VERIFY_NOT_REACHED();
  1518. }
  1519. }
  1520. void SoftwareGLContext::gl_get_doublev(GLenum pname, GLdouble* params)
  1521. {
  1522. get_floating_point(pname, params);
  1523. }
  1524. void SoftwareGLContext::gl_get_floatv(GLenum pname, GLfloat* params)
  1525. {
  1526. get_floating_point(pname, params);
  1527. }
  1528. template<typename T>
  1529. void SoftwareGLContext::get_floating_point(GLenum pname, T* params)
  1530. {
  1531. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1532. // Handle special matrix cases first
  1533. auto flatten_and_assign_matrix = [&params](const FloatMatrix4x4& matrix) {
  1534. auto elements = matrix.elements();
  1535. for (size_t i = 0; i < 4; ++i)
  1536. for (size_t j = 0; j < 4; ++j)
  1537. params[i * 4 + j] = static_cast<T>(elements[i][j]);
  1538. };
  1539. switch (pname) {
  1540. case GL_MODELVIEW_MATRIX:
  1541. if (m_current_matrix_mode == GL_MODELVIEW)
  1542. flatten_and_assign_matrix(m_model_view_matrix);
  1543. else if (m_model_view_matrix_stack.is_empty())
  1544. flatten_and_assign_matrix(FloatMatrix4x4::identity());
  1545. else
  1546. flatten_and_assign_matrix(m_model_view_matrix_stack.last());
  1547. return;
  1548. case GL_PROJECTION_MATRIX:
  1549. if (m_current_matrix_mode == GL_PROJECTION)
  1550. flatten_and_assign_matrix(m_projection_matrix);
  1551. else if (m_projection_matrix_stack.is_empty())
  1552. flatten_and_assign_matrix(FloatMatrix4x4::identity());
  1553. else
  1554. flatten_and_assign_matrix(m_projection_matrix_stack.last());
  1555. return;
  1556. }
  1557. // Regular parameters
  1558. auto optional_parameter = get_context_parameter(pname);
  1559. RETURN_WITH_ERROR_IF(!optional_parameter.has_value(), GL_INVALID_ENUM);
  1560. auto parameter = optional_parameter.release_value();
  1561. switch (parameter.type) {
  1562. case GL_BOOL:
  1563. *params = parameter.value.boolean_value ? GL_TRUE : GL_FALSE;
  1564. break;
  1565. case GL_DOUBLE:
  1566. for (size_t i = 0; i < parameter.count; ++i) {
  1567. params[i] = parameter.value.double_list[i];
  1568. }
  1569. break;
  1570. case GL_INT:
  1571. for (size_t i = 0; i < parameter.count; ++i) {
  1572. params[i] = parameter.value.integer_list[i];
  1573. }
  1574. break;
  1575. default:
  1576. VERIFY_NOT_REACHED();
  1577. }
  1578. }
  1579. void SoftwareGLContext::gl_get_integerv(GLenum pname, GLint* data)
  1580. {
  1581. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1582. auto optional_parameter = get_context_parameter(pname);
  1583. RETURN_WITH_ERROR_IF(!optional_parameter.has_value(), GL_INVALID_ENUM);
  1584. auto parameter = optional_parameter.release_value();
  1585. switch (parameter.type) {
  1586. case GL_BOOL:
  1587. *data = parameter.value.boolean_value ? GL_TRUE : GL_FALSE;
  1588. break;
  1589. case GL_DOUBLE: {
  1590. double const int_range = static_cast<double>(NumericLimits<GLint>::max()) - NumericLimits<GLint>::min();
  1591. for (size_t i = 0; i < parameter.count; ++i) {
  1592. double const result_factor = (clamp(parameter.value.double_list[i], -1.0, 1.0) + 1.0) / 2.0;
  1593. data[i] = static_cast<GLint>(NumericLimits<GLint>::min() + result_factor * int_range);
  1594. }
  1595. break;
  1596. }
  1597. case GL_INT:
  1598. for (size_t i = 0; i < parameter.count; ++i) {
  1599. data[i] = parameter.value.integer_list[i];
  1600. }
  1601. break;
  1602. default:
  1603. VERIFY_NOT_REACHED();
  1604. }
  1605. }
  1606. void SoftwareGLContext::gl_depth_mask(GLboolean flag)
  1607. {
  1608. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_depth_mask, flag);
  1609. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1610. auto options = m_rasterizer.options();
  1611. options.enable_depth_write = (flag != GL_FALSE);
  1612. m_rasterizer.set_options(options);
  1613. }
  1614. void SoftwareGLContext::gl_enable_client_state(GLenum cap)
  1615. {
  1616. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1617. switch (cap) {
  1618. case GL_VERTEX_ARRAY:
  1619. m_client_side_vertex_array_enabled = true;
  1620. break;
  1621. case GL_COLOR_ARRAY:
  1622. m_client_side_color_array_enabled = true;
  1623. break;
  1624. case GL_TEXTURE_COORD_ARRAY:
  1625. m_client_side_texture_coord_array_enabled = true;
  1626. break;
  1627. default:
  1628. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1629. }
  1630. }
  1631. void SoftwareGLContext::gl_disable_client_state(GLenum cap)
  1632. {
  1633. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1634. switch (cap) {
  1635. case GL_VERTEX_ARRAY:
  1636. m_client_side_vertex_array_enabled = false;
  1637. break;
  1638. case GL_COLOR_ARRAY:
  1639. m_client_side_color_array_enabled = false;
  1640. break;
  1641. case GL_TEXTURE_COORD_ARRAY:
  1642. m_client_side_texture_coord_array_enabled = false;
  1643. break;
  1644. default:
  1645. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1646. }
  1647. }
  1648. void SoftwareGLContext::gl_vertex_pointer(GLint size, GLenum type, GLsizei stride, const void* pointer)
  1649. {
  1650. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1651. RETURN_WITH_ERROR_IF(!(size == 2 || size == 3 || size == 4), GL_INVALID_VALUE);
  1652. RETURN_WITH_ERROR_IF(!(type == GL_SHORT || type == GL_INT || type == GL_FLOAT || type == GL_DOUBLE), GL_INVALID_ENUM);
  1653. RETURN_WITH_ERROR_IF(stride < 0, GL_INVALID_VALUE);
  1654. m_client_vertex_pointer.size = size;
  1655. m_client_vertex_pointer.type = type;
  1656. m_client_vertex_pointer.stride = stride;
  1657. m_client_vertex_pointer.pointer = pointer;
  1658. }
  1659. void SoftwareGLContext::gl_color_pointer(GLint size, GLenum type, GLsizei stride, const void* pointer)
  1660. {
  1661. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1662. RETURN_WITH_ERROR_IF(!(size == 3 || size == 4), GL_INVALID_VALUE);
  1663. RETURN_WITH_ERROR_IF(!(type == GL_BYTE
  1664. || type == GL_UNSIGNED_BYTE
  1665. || type == GL_SHORT
  1666. || type == GL_UNSIGNED_SHORT
  1667. || type == GL_INT
  1668. || type == GL_UNSIGNED_INT
  1669. || type == GL_FLOAT
  1670. || type == GL_DOUBLE),
  1671. GL_INVALID_ENUM);
  1672. RETURN_WITH_ERROR_IF(stride < 0, GL_INVALID_VALUE);
  1673. m_client_color_pointer.size = size;
  1674. m_client_color_pointer.type = type;
  1675. m_client_color_pointer.stride = stride;
  1676. m_client_color_pointer.pointer = pointer;
  1677. }
  1678. void SoftwareGLContext::gl_tex_coord_pointer(GLint size, GLenum type, GLsizei stride, const void* pointer)
  1679. {
  1680. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1681. RETURN_WITH_ERROR_IF(!(size == 1 || size == 2 || size == 3 || size == 4), GL_INVALID_VALUE);
  1682. RETURN_WITH_ERROR_IF(!(type == GL_SHORT || type == GL_INT || type == GL_FLOAT || type == GL_DOUBLE), GL_INVALID_ENUM);
  1683. RETURN_WITH_ERROR_IF(stride < 0, GL_INVALID_VALUE);
  1684. m_client_tex_coord_pointer.size = size;
  1685. m_client_tex_coord_pointer.type = type;
  1686. m_client_tex_coord_pointer.stride = stride;
  1687. m_client_tex_coord_pointer.pointer = pointer;
  1688. }
  1689. void SoftwareGLContext::gl_tex_env(GLenum target, GLenum pname, GLfloat param)
  1690. {
  1691. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_env, target, pname, param);
  1692. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1693. // FIXME: We currently only support a subset of possible target values. Implement the rest!
  1694. RETURN_WITH_ERROR_IF(target != GL_TEXTURE_ENV, GL_INVALID_ENUM);
  1695. // FIXME: We currently only support a subset of possible pname values. Implement the rest!
  1696. RETURN_WITH_ERROR_IF(pname != GL_TEXTURE_ENV_MODE, GL_INVALID_ENUM);
  1697. auto param_enum = static_cast<GLenum>(param);
  1698. switch (param_enum) {
  1699. case GL_MODULATE:
  1700. case GL_REPLACE:
  1701. case GL_DECAL:
  1702. m_active_texture_unit->set_env_mode(param_enum);
  1703. break;
  1704. default:
  1705. // FIXME: We currently only support a subset of possible param values. Implement the rest!
  1706. dbgln_if(GL_DEBUG, "gl_tex_env({:#x}, {:#x}, {}): param unimplemented", target, pname, param);
  1707. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  1708. }
  1709. }
  1710. void SoftwareGLContext::gl_draw_arrays(GLenum mode, GLint first, GLsizei count)
  1711. {
  1712. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_draw_arrays, mode, first, count);
  1713. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1714. // FIXME: Some modes are still missing (GL_POINTS, GL_LINE_STRIP, GL_LINE_LOOP, GL_LINES)
  1715. RETURN_WITH_ERROR_IF(!(mode == GL_TRIANGLE_STRIP
  1716. || mode == GL_TRIANGLE_FAN
  1717. || mode == GL_TRIANGLES
  1718. || mode == GL_QUADS
  1719. || mode == GL_QUAD_STRIP
  1720. || mode == GL_POLYGON),
  1721. GL_INVALID_ENUM);
  1722. RETURN_WITH_ERROR_IF(count < 0, GL_INVALID_VALUE);
  1723. // At least the vertex array needs to be enabled
  1724. if (!m_client_side_vertex_array_enabled)
  1725. return;
  1726. auto last = first + count;
  1727. gl_begin(mode);
  1728. for (int i = first; i < last; i++) {
  1729. if (m_client_side_texture_coord_array_enabled) {
  1730. float tex_coords[4] { 0, 0, 0, 0 };
  1731. read_from_vertex_attribute_pointer(m_client_tex_coord_pointer, i, tex_coords, false);
  1732. gl_tex_coord(tex_coords[0], tex_coords[1], tex_coords[2], tex_coords[3]);
  1733. }
  1734. if (m_client_side_color_array_enabled) {
  1735. float color[4] { 0, 0, 0, 1 };
  1736. read_from_vertex_attribute_pointer(m_client_color_pointer, i, color, true);
  1737. glColor4fv(color);
  1738. }
  1739. float vertex[4] { 0, 0, 0, 1 };
  1740. read_from_vertex_attribute_pointer(m_client_vertex_pointer, i, vertex, false);
  1741. glVertex4fv(vertex);
  1742. }
  1743. gl_end();
  1744. }
  1745. void SoftwareGLContext::gl_draw_elements(GLenum mode, GLsizei count, GLenum type, const void* indices)
  1746. {
  1747. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_draw_elements, mode, count, type, indices);
  1748. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1749. // FIXME: Some modes are still missing (GL_POINTS, GL_LINE_STRIP, GL_LINE_LOOP, GL_LINES)
  1750. RETURN_WITH_ERROR_IF(!(mode == GL_TRIANGLE_STRIP
  1751. || mode == GL_TRIANGLE_FAN
  1752. || mode == GL_TRIANGLES
  1753. || mode == GL_QUADS
  1754. || mode == GL_QUAD_STRIP
  1755. || mode == GL_POLYGON),
  1756. GL_INVALID_ENUM);
  1757. RETURN_WITH_ERROR_IF(!(type == GL_UNSIGNED_BYTE
  1758. || type == GL_UNSIGNED_SHORT
  1759. || type == GL_UNSIGNED_INT),
  1760. GL_INVALID_ENUM);
  1761. RETURN_WITH_ERROR_IF(count < 0, GL_INVALID_VALUE);
  1762. // At least the vertex array needs to be enabled
  1763. if (!m_client_side_vertex_array_enabled)
  1764. return;
  1765. gl_begin(mode);
  1766. for (int index = 0; index < count; index++) {
  1767. int i = 0;
  1768. switch (type) {
  1769. case GL_UNSIGNED_BYTE:
  1770. i = reinterpret_cast<const GLubyte*>(indices)[index];
  1771. break;
  1772. case GL_UNSIGNED_SHORT:
  1773. i = reinterpret_cast<const GLushort*>(indices)[index];
  1774. break;
  1775. case GL_UNSIGNED_INT:
  1776. i = reinterpret_cast<const GLuint*>(indices)[index];
  1777. break;
  1778. }
  1779. if (m_client_side_texture_coord_array_enabled) {
  1780. float tex_coords[4] { 0, 0, 0, 0 };
  1781. read_from_vertex_attribute_pointer(m_client_tex_coord_pointer, i, tex_coords, false);
  1782. gl_tex_coord(tex_coords[0], tex_coords[1], tex_coords[2], tex_coords[3]);
  1783. }
  1784. if (m_client_side_color_array_enabled) {
  1785. float color[4] { 0, 0, 0, 1 };
  1786. read_from_vertex_attribute_pointer(m_client_color_pointer, i, color, true);
  1787. glColor4fv(color);
  1788. }
  1789. float vertex[4] { 0, 0, 0, 1 };
  1790. read_from_vertex_attribute_pointer(m_client_vertex_pointer, i, vertex, false);
  1791. glVertex4fv(vertex);
  1792. }
  1793. gl_end();
  1794. }
  1795. void SoftwareGLContext::gl_draw_pixels(GLsizei width, GLsizei height, GLenum format, GLenum type, const void* data)
  1796. {
  1797. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_draw_pixels, width, height, format, type, data);
  1798. RETURN_WITH_ERROR_IF(format < GL_COLOR_INDEX || format > GL_BGRA, GL_INVALID_ENUM);
  1799. RETURN_WITH_ERROR_IF((type < GL_BYTE || type > GL_FLOAT)
  1800. && (type < GL_UNSIGNED_BYTE_3_3_2 || type > GL_UNSIGNED_INT_10_10_10_2)
  1801. && (type < GL_UNSIGNED_BYTE_2_3_3_REV || type > GL_UNSIGNED_INT_2_10_10_10_REV),
  1802. GL_INVALID_ENUM);
  1803. RETURN_WITH_ERROR_IF(type == GL_BITMAP && !(format == GL_COLOR_INDEX || format == GL_STENCIL_INDEX), GL_INVALID_ENUM);
  1804. RETURN_WITH_ERROR_IF(width < 0 || height < 0, GL_INVALID_VALUE);
  1805. // FIXME: GL_INVALID_OPERATION is generated if format is GL_STENCIL_INDEX and there is no stencil buffer
  1806. // FIXME: GL_INVALID_OPERATION is generated if format is GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA, GL_RGB, GL_RGBA,
  1807. // GL_BGR, GL_BGRA, GL_LUMINANCE, or GL_LUMINANCE_ALPHA, and the GL is in color index mode
  1808. RETURN_WITH_ERROR_IF(format != GL_RGB
  1809. && (type == GL_UNSIGNED_BYTE_3_3_2
  1810. || type == GL_UNSIGNED_BYTE_2_3_3_REV
  1811. || type == GL_UNSIGNED_SHORT_5_6_5
  1812. || type == GL_UNSIGNED_SHORT_5_6_5_REV),
  1813. GL_INVALID_OPERATION);
  1814. RETURN_WITH_ERROR_IF(!(format == GL_RGBA || format == GL_BGRA)
  1815. && (type == GL_UNSIGNED_SHORT_4_4_4_4
  1816. || type == GL_UNSIGNED_SHORT_4_4_4_4_REV
  1817. || type == GL_UNSIGNED_SHORT_5_5_5_1
  1818. || type == GL_UNSIGNED_SHORT_1_5_5_5_REV
  1819. || type == GL_UNSIGNED_INT_8_8_8_8
  1820. || type == GL_UNSIGNED_INT_8_8_8_8_REV
  1821. || type == GL_UNSIGNED_INT_10_10_10_2
  1822. || type == GL_UNSIGNED_INT_2_10_10_10_REV),
  1823. GL_INVALID_OPERATION);
  1824. // FIXME: GL_INVALID_OPERATION is generated if a non-zero buffer object name is bound to the GL_PIXEL_UNPACK_BUFFER
  1825. // target and the buffer object's data store is currently mapped.
  1826. // FIXME: GL_INVALID_OPERATION is generated if a non-zero buffer object name is bound to the GL_PIXEL_UNPACK_BUFFER
  1827. // target and the data would be unpacked from the buffer object such that the memory reads required would
  1828. // exceed the data store size.
  1829. // FIXME: GL_INVALID_OPERATION is generated if a non-zero buffer object name is bound to the GL_PIXEL_UNPACK_BUFFER
  1830. // target and data is not evenly divisible into the number of bytes needed to store in memory a datum
  1831. // indicated by type.
  1832. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1833. // FIXME: we only support RGBA + GL_UNSIGNED_BYTE, implement all the others!
  1834. if (format != GL_RGBA) {
  1835. dbgln_if(GL_DEBUG, "gl_draw_pixels(): support for format {:#x} not implemented", format);
  1836. return;
  1837. } else if (type != GL_UNSIGNED_BYTE) {
  1838. dbgln_if(GL_DEBUG, "gl_draw_pixels(): support for type {:#x} not implemented", type);
  1839. return;
  1840. }
  1841. auto bitmap_or_error = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, { width, height });
  1842. RETURN_WITH_ERROR_IF(bitmap_or_error.is_error(), GL_OUT_OF_MEMORY);
  1843. auto bitmap = bitmap_or_error.release_value();
  1844. // FIXME: implement support for GL_UNPACK_ALIGNMENT and other pixel parameters
  1845. auto pixel_data = static_cast<u32 const*>(data);
  1846. for (int y = 0; y < height; ++y)
  1847. for (int x = 0; x < width; ++x)
  1848. bitmap->set_pixel(x, y, Color::from_rgba(*(pixel_data++)));
  1849. m_rasterizer.blit(
  1850. bitmap,
  1851. static_cast<int>(m_current_raster_position.window_coordinates.x()),
  1852. static_cast<int>(m_current_raster_position.window_coordinates.y()));
  1853. }
  1854. void SoftwareGLContext::gl_depth_range(GLdouble min, GLdouble max)
  1855. {
  1856. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_depth_range, min, max);
  1857. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1858. auto options = m_rasterizer.options();
  1859. options.depth_min = clamp(min, 0.f, 1.f);
  1860. options.depth_max = clamp(max, 0.f, 1.f);
  1861. m_rasterizer.set_options(options);
  1862. }
  1863. void SoftwareGLContext::gl_depth_func(GLenum func)
  1864. {
  1865. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_depth_func, func);
  1866. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  1867. RETURN_WITH_ERROR_IF(!(func == GL_NEVER
  1868. || func == GL_LESS
  1869. || func == GL_EQUAL
  1870. || func == GL_LEQUAL
  1871. || func == GL_GREATER
  1872. || func == GL_NOTEQUAL
  1873. || func == GL_GEQUAL
  1874. || func == GL_ALWAYS),
  1875. GL_INVALID_ENUM);
  1876. auto options = m_rasterizer.options();
  1877. switch (func) {
  1878. case GL_NEVER:
  1879. options.depth_func = SoftGPU::DepthTestFunction::Never;
  1880. break;
  1881. case GL_ALWAYS:
  1882. options.depth_func = SoftGPU::DepthTestFunction::Always;
  1883. break;
  1884. case GL_LESS:
  1885. options.depth_func = SoftGPU::DepthTestFunction::Less;
  1886. break;
  1887. case GL_LEQUAL:
  1888. options.depth_func = SoftGPU::DepthTestFunction::LessOrEqual;
  1889. break;
  1890. case GL_EQUAL:
  1891. options.depth_func = SoftGPU::DepthTestFunction::Equal;
  1892. break;
  1893. case GL_NOTEQUAL:
  1894. options.depth_func = SoftGPU::DepthTestFunction::NotEqual;
  1895. break;
  1896. case GL_GEQUAL:
  1897. options.depth_func = SoftGPU::DepthTestFunction::GreaterOrEqual;
  1898. break;
  1899. case GL_GREATER:
  1900. options.depth_func = SoftGPU::DepthTestFunction::Greater;
  1901. break;
  1902. default:
  1903. VERIFY_NOT_REACHED();
  1904. }
  1905. m_rasterizer.set_options(options);
  1906. }
  1907. // General helper function to read arbitrary vertex attribute data into a float array
  1908. void SoftwareGLContext::read_from_vertex_attribute_pointer(VertexAttribPointer const& attrib, int index, float* elements, bool normalize)
  1909. {
  1910. auto byte_ptr = reinterpret_cast<const char*>(attrib.pointer);
  1911. size_t stride = attrib.stride;
  1912. switch (attrib.type) {
  1913. case GL_BYTE: {
  1914. if (stride == 0)
  1915. stride = sizeof(GLbyte) * attrib.size;
  1916. for (int i = 0; i < attrib.size; i++) {
  1917. elements[i] = *(reinterpret_cast<const GLbyte*>(byte_ptr + stride * index) + i);
  1918. if (normalize)
  1919. elements[i] /= 0x80;
  1920. }
  1921. break;
  1922. }
  1923. case GL_UNSIGNED_BYTE: {
  1924. if (stride == 0)
  1925. stride = sizeof(GLubyte) * attrib.size;
  1926. for (int i = 0; i < attrib.size; i++) {
  1927. elements[i] = *(reinterpret_cast<const GLubyte*>(byte_ptr + stride * index) + i);
  1928. if (normalize)
  1929. elements[i] /= 0xff;
  1930. }
  1931. break;
  1932. }
  1933. case GL_SHORT: {
  1934. if (stride == 0)
  1935. stride = sizeof(GLshort) * attrib.size;
  1936. for (int i = 0; i < attrib.size; i++) {
  1937. elements[i] = *(reinterpret_cast<const GLshort*>(byte_ptr + stride * index) + i);
  1938. if (normalize)
  1939. elements[i] /= 0x8000;
  1940. }
  1941. break;
  1942. }
  1943. case GL_UNSIGNED_SHORT: {
  1944. if (stride == 0)
  1945. stride = sizeof(GLushort) * attrib.size;
  1946. for (int i = 0; i < attrib.size; i++) {
  1947. elements[i] = *(reinterpret_cast<const GLushort*>(byte_ptr + stride * index) + i);
  1948. if (normalize)
  1949. elements[i] /= 0xffff;
  1950. }
  1951. break;
  1952. }
  1953. case GL_INT: {
  1954. if (stride == 0)
  1955. stride = sizeof(GLint) * attrib.size;
  1956. for (int i = 0; i < attrib.size; i++) {
  1957. elements[i] = *(reinterpret_cast<const GLint*>(byte_ptr + stride * index) + i);
  1958. if (normalize)
  1959. elements[i] /= 0x80000000;
  1960. }
  1961. break;
  1962. }
  1963. case GL_UNSIGNED_INT: {
  1964. if (stride == 0)
  1965. stride = sizeof(GLuint) * attrib.size;
  1966. for (int i = 0; i < attrib.size; i++) {
  1967. elements[i] = *(reinterpret_cast<const GLuint*>(byte_ptr + stride * index) + i);
  1968. if (normalize)
  1969. elements[i] /= 0xffffffff;
  1970. }
  1971. break;
  1972. }
  1973. case GL_FLOAT: {
  1974. if (stride == 0)
  1975. stride = sizeof(GLfloat) * attrib.size;
  1976. for (int i = 0; i < attrib.size; i++) {
  1977. elements[i] = *(reinterpret_cast<const GLfloat*>(byte_ptr + stride * index) + i);
  1978. }
  1979. break;
  1980. }
  1981. case GL_DOUBLE: {
  1982. if (stride == 0)
  1983. stride = sizeof(GLdouble) * attrib.size;
  1984. for (int i = 0; i < attrib.size; i++) {
  1985. elements[i] = static_cast<float>(*(reinterpret_cast<const GLdouble*>(byte_ptr + stride * index) + i));
  1986. }
  1987. break;
  1988. }
  1989. }
  1990. }
  1991. void SoftwareGLContext::gl_color_mask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha)
  1992. {
  1993. auto options = m_rasterizer.options();
  1994. auto mask = options.color_mask;
  1995. if (!red)
  1996. mask &= ~0x000000ff;
  1997. else
  1998. mask |= 0x000000ff;
  1999. if (!green)
  2000. mask &= ~0x0000ff00;
  2001. else
  2002. mask |= 0x0000ff00;
  2003. if (!blue)
  2004. mask &= ~0x00ff0000;
  2005. else
  2006. mask |= 0x00ff0000;
  2007. if (!alpha)
  2008. mask &= ~0xff000000;
  2009. else
  2010. mask |= 0xff000000;
  2011. options.color_mask = mask;
  2012. m_rasterizer.set_options(options);
  2013. }
  2014. void SoftwareGLContext::gl_polygon_mode(GLenum face, GLenum mode)
  2015. {
  2016. RETURN_WITH_ERROR_IF(!(face == GL_BACK || face == GL_FRONT || face == GL_FRONT_AND_BACK), GL_INVALID_ENUM);
  2017. RETURN_WITH_ERROR_IF(!(mode == GL_POINT || mode == GL_LINE || mode == GL_FILL), GL_INVALID_ENUM);
  2018. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2019. auto options = m_rasterizer.options();
  2020. // FIXME: This must support different polygon modes for front- and backside
  2021. switch (mode) {
  2022. case GL_POINT:
  2023. options.polygon_mode = SoftGPU::PolygonMode::Point;
  2024. break;
  2025. case GL_LINE:
  2026. options.polygon_mode = SoftGPU::PolygonMode::Line;
  2027. break;
  2028. case GL_FILL:
  2029. options.polygon_mode = SoftGPU::PolygonMode::Fill;
  2030. break;
  2031. }
  2032. m_rasterizer.set_options(options);
  2033. }
  2034. void SoftwareGLContext::gl_polygon_offset(GLfloat factor, GLfloat units)
  2035. {
  2036. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_polygon_offset, factor, units);
  2037. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2038. auto rasterizer_options = m_rasterizer.options();
  2039. rasterizer_options.depth_offset_factor = factor;
  2040. rasterizer_options.depth_offset_constant = units;
  2041. m_rasterizer.set_options(rasterizer_options);
  2042. }
  2043. void SoftwareGLContext::gl_fogfv(GLenum pname, GLfloat* params)
  2044. {
  2045. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_fogfv, pname, params);
  2046. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2047. auto options = m_rasterizer.options();
  2048. switch (pname) {
  2049. case GL_FOG_COLOR:
  2050. options.fog_color = { params[0], params[1], params[2], params[3] };
  2051. break;
  2052. default:
  2053. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  2054. }
  2055. m_rasterizer.set_options(options);
  2056. }
  2057. void SoftwareGLContext::gl_fogf(GLenum pname, GLfloat param)
  2058. {
  2059. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_fogf, pname, param);
  2060. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2061. RETURN_WITH_ERROR_IF(param < 0.0f, GL_INVALID_VALUE);
  2062. auto options = m_rasterizer.options();
  2063. switch (pname) {
  2064. case GL_FOG_DENSITY:
  2065. options.fog_density = param;
  2066. break;
  2067. case GL_FOG_END:
  2068. options.fog_end = param;
  2069. break;
  2070. case GL_FOG_START:
  2071. options.fog_start = param;
  2072. break;
  2073. default:
  2074. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  2075. }
  2076. m_rasterizer.set_options(options);
  2077. }
  2078. void SoftwareGLContext::gl_fogi(GLenum pname, GLint param)
  2079. {
  2080. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_fogi, pname, param);
  2081. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2082. RETURN_WITH_ERROR_IF(param != GL_LINEAR && param != GL_EXP && param != GL_EXP2, GL_INVALID_ENUM);
  2083. auto options = m_rasterizer.options();
  2084. switch (pname) {
  2085. case GL_FOG_MODE:
  2086. switch (param) {
  2087. case GL_LINEAR:
  2088. options.fog_mode = SoftGPU::FogMode::Linear;
  2089. break;
  2090. case GL_EXP:
  2091. options.fog_mode = SoftGPU::FogMode::Exp;
  2092. break;
  2093. case GL_EXP2:
  2094. options.fog_mode = SoftGPU::FogMode::Exp2;
  2095. break;
  2096. }
  2097. break;
  2098. default:
  2099. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  2100. }
  2101. m_rasterizer.set_options(options);
  2102. }
  2103. void SoftwareGLContext::gl_pixel_storei(GLenum pname, GLint param)
  2104. {
  2105. // FIXME: Implement missing parameters
  2106. switch (pname) {
  2107. case GL_PACK_ALIGNMENT:
  2108. RETURN_WITH_ERROR_IF(param != 1 && param != 2 && param != 4 && param != 8, GL_INVALID_VALUE);
  2109. m_pack_alignment = param;
  2110. break;
  2111. case GL_UNPACK_ROW_LENGTH:
  2112. RETURN_WITH_ERROR_IF(param < 0, GL_INVALID_VALUE);
  2113. m_unpack_row_length = static_cast<size_t>(param);
  2114. break;
  2115. case GL_UNPACK_ALIGNMENT:
  2116. RETURN_WITH_ERROR_IF(param != 1 && param != 2 && param != 4 && param != 8, GL_INVALID_VALUE);
  2117. m_unpack_alignment = param;
  2118. break;
  2119. default:
  2120. RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM);
  2121. break;
  2122. }
  2123. }
  2124. void SoftwareGLContext::gl_scissor(GLint x, GLint y, GLsizei width, GLsizei height)
  2125. {
  2126. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_scissor, x, y, width, height);
  2127. RETURN_WITH_ERROR_IF(width < 0 || height < 0, GL_INVALID_VALUE);
  2128. auto options = m_rasterizer.options();
  2129. options.scissor_box = { x, y, width, height };
  2130. m_rasterizer.set_options(options);
  2131. }
  2132. void SoftwareGLContext::gl_stencil_func_separate(GLenum face, GLenum func, GLint ref, GLuint mask)
  2133. {
  2134. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_stencil_func_separate, face, func, ref, mask);
  2135. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2136. RETURN_WITH_ERROR_IF(!(face == GL_FRONT || face == GL_BACK || face == GL_FRONT_AND_BACK), GL_INVALID_ENUM);
  2137. RETURN_WITH_ERROR_IF(!(func == GL_NEVER
  2138. || func == GL_LESS
  2139. || func == GL_LEQUAL
  2140. || func == GL_GREATER
  2141. || func == GL_GEQUAL
  2142. || func == GL_EQUAL
  2143. || func == GL_NOTEQUAL
  2144. || func == GL_ALWAYS),
  2145. GL_INVALID_ENUM);
  2146. // FIXME: "ref is clamped to the range 02^n - 1 , where n is the number of bitplanes in the stencil buffer"
  2147. StencilFunctionOptions new_options = { func, ref, mask };
  2148. if (face == GL_FRONT || face == GL_FRONT_AND_BACK)
  2149. m_stencil_frontfacing_func = new_options;
  2150. if (face == GL_BACK || face == GL_FRONT_AND_BACK)
  2151. m_stencil_backfacing_func = new_options;
  2152. }
  2153. void SoftwareGLContext::gl_stencil_op_separate(GLenum face, GLenum sfail, GLenum dpfail, GLenum dppass)
  2154. {
  2155. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_stencil_op_separate, face, sfail, dpfail, dppass);
  2156. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2157. RETURN_WITH_ERROR_IF(!(face == GL_FRONT || face == GL_BACK || face == GL_FRONT_AND_BACK), GL_INVALID_ENUM);
  2158. RETURN_WITH_ERROR_IF(!(sfail == GL_KEEP
  2159. || sfail == GL_ZERO
  2160. || sfail == GL_REPLACE
  2161. || sfail == GL_INCR
  2162. || sfail == GL_INCR_WRAP
  2163. || sfail == GL_DECR
  2164. || sfail == GL_DECR_WRAP
  2165. || sfail == GL_INVERT),
  2166. GL_INVALID_ENUM);
  2167. RETURN_WITH_ERROR_IF(!(dpfail == GL_KEEP
  2168. || dpfail == GL_ZERO
  2169. || dpfail == GL_REPLACE
  2170. || dpfail == GL_INCR
  2171. || dpfail == GL_INCR_WRAP
  2172. || dpfail == GL_DECR
  2173. || dpfail == GL_DECR_WRAP
  2174. || dpfail == GL_INVERT),
  2175. GL_INVALID_ENUM);
  2176. RETURN_WITH_ERROR_IF(!(dppass == GL_KEEP
  2177. || dppass == GL_ZERO
  2178. || dppass == GL_REPLACE
  2179. || dppass == GL_INCR
  2180. || dppass == GL_INCR_WRAP
  2181. || dppass == GL_DECR
  2182. || dppass == GL_DECR_WRAP
  2183. || dppass == GL_INVERT),
  2184. GL_INVALID_ENUM);
  2185. StencilOperationOptions new_options = { sfail, dpfail, dppass };
  2186. if (face == GL_FRONT || face == GL_FRONT_AND_BACK)
  2187. m_stencil_frontfacing_op = new_options;
  2188. if (face == GL_BACK || face == GL_FRONT_AND_BACK)
  2189. m_stencil_backfacing_op = new_options;
  2190. }
  2191. void SoftwareGLContext::gl_normal(GLfloat nx, GLfloat ny, GLfloat nz)
  2192. {
  2193. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_normal, nx, ny, nz);
  2194. m_current_vertex_normal = { nx, ny, nz };
  2195. }
  2196. void SoftwareGLContext::gl_normal_pointer(GLenum type, GLsizei stride, void const* pointer)
  2197. {
  2198. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2199. RETURN_WITH_ERROR_IF(type != GL_BYTE
  2200. && type != GL_SHORT
  2201. && type != GL_INT
  2202. && type != GL_FLOAT
  2203. && type != GL_DOUBLE,
  2204. GL_INVALID_ENUM);
  2205. RETURN_WITH_ERROR_IF(stride < 0, GL_INVALID_VALUE);
  2206. dbgln_if(GL_DEBUG, "gl_normal_pointer({:#x}, {}, {:p}): unimplemented", type, stride, pointer);
  2207. }
  2208. void SoftwareGLContext::gl_raster_pos(GLfloat x, GLfloat y, GLfloat z, GLfloat w)
  2209. {
  2210. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_raster_pos, x, y, z, w);
  2211. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2212. m_current_raster_position.window_coordinates = { x, y, z };
  2213. m_current_raster_position.clip_coordinate_value = w;
  2214. }
  2215. void SoftwareGLContext::gl_materialv(GLenum face, GLenum pname, GLfloat const* params)
  2216. {
  2217. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_materialv, face, pname, params);
  2218. RETURN_WITH_ERROR_IF(!(face == GL_FRONT || face == GL_BACK || face == GL_FRONT_AND_BACK), GL_INVALID_ENUM);
  2219. RETURN_WITH_ERROR_IF(!(pname == GL_AMBIENT
  2220. || pname == GL_DIFFUSE
  2221. || pname == GL_SPECULAR
  2222. || pname == GL_EMISSION
  2223. || pname == GL_SHININESS
  2224. || pname == GL_AMBIENT_AND_DIFFUSE
  2225. || pname == GL_COLOR_INDEXES),
  2226. GL_INVALID_ENUM);
  2227. GLfloat x, y, z, w;
  2228. switch (pname) {
  2229. case GL_SHININESS:
  2230. x = params[0];
  2231. y = 0.0f;
  2232. z = 0.0f;
  2233. w = 0.0f;
  2234. break;
  2235. case GL_COLOR_INDEXES:
  2236. x = params[0];
  2237. y = params[1];
  2238. z = params[2];
  2239. w = 0.0f;
  2240. break;
  2241. default:
  2242. x = params[0];
  2243. y = params[1];
  2244. z = params[2];
  2245. w = params[3];
  2246. }
  2247. // FIXME: implement this method
  2248. dbgln_if(GL_DEBUG, "gl_materialv({}, {}, {}, {}, {}, {}): unimplemented", face, pname, x, y, z, w);
  2249. }
  2250. void SoftwareGLContext::gl_line_width(GLfloat width)
  2251. {
  2252. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_line_width, width);
  2253. RETURN_WITH_ERROR_IF(width <= 0, GL_INVALID_VALUE);
  2254. m_line_width = width;
  2255. }
  2256. void SoftwareGLContext::gl_push_attrib(GLbitfield mask)
  2257. {
  2258. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_push_attrib, mask);
  2259. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2260. // FIXME: implement
  2261. dbgln_if(GL_DEBUG, "SoftwareGLContext FIXME: implement gl_push_attrib({})", mask);
  2262. }
  2263. void SoftwareGLContext::gl_pop_attrib()
  2264. {
  2265. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_pop_attrib);
  2266. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2267. // FIXME: implement
  2268. dbgln_if(GL_DEBUG, "SoftwareGLContext FIXME: implement gl_pop_attrib()");
  2269. }
  2270. void SoftwareGLContext::gl_light_model(GLenum pname, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
  2271. {
  2272. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_light_model, pname, x, y, z, w);
  2273. RETURN_WITH_ERROR_IF(!(pname == GL_LIGHT_MODEL_AMBIENT
  2274. || pname == GL_LIGHT_MODEL_TWO_SIDE),
  2275. GL_INVALID_ENUM);
  2276. switch (pname) {
  2277. case GL_LIGHT_MODEL_AMBIENT:
  2278. m_light_model_ambient = { x, y, z, w };
  2279. break;
  2280. case GL_LIGHT_MODEL_TWO_SIDE:
  2281. VERIFY(y == 0.0f && z == 0.0f && w == 0.0f);
  2282. m_light_model_two_side = x;
  2283. break;
  2284. default:
  2285. VERIFY_NOT_REACHED();
  2286. }
  2287. }
  2288. void SoftwareGLContext::gl_bitmap(GLsizei width, GLsizei height, GLfloat xorig, GLfloat yorig, GLfloat xmove, GLfloat ymove, GLubyte const* bitmap)
  2289. {
  2290. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_bitmap, width, height, xorig, yorig, xmove, ymove, bitmap);
  2291. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2292. // FIXME: implement
  2293. dbgln_if(GL_DEBUG, "SoftwareGLContext FIXME: implement gl_bitmap({}, {}, {}, {}, {}, {}, {})", width, height, xorig, yorig, xmove, ymove, bitmap);
  2294. }
  2295. void SoftwareGLContext::gl_copy_tex_image_2d(GLenum target, GLint level, GLenum internalformat, GLint x, GLint y, GLsizei width, GLsizei height, GLint border)
  2296. {
  2297. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_copy_tex_image_2d, target, level, internalformat, x, y, width, height, border);
  2298. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2299. // FIXME: implement
  2300. dbgln_if(GL_DEBUG, "SoftwareGLContext FIXME: implement gl_copy_tex_image_2d({:#x}, {}, {:#x}, {}, {}, {}, {}, {})",
  2301. target, level, internalformat, x, y, width, height, border);
  2302. }
  2303. void SoftwareGLContext::gl_get_tex_parameter_integerv(GLenum target, GLint level, GLenum pname, GLint* params)
  2304. {
  2305. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2306. // FIXME: support targets other than GL_TEXTURE_2D
  2307. RETURN_WITH_ERROR_IF(target != GL_TEXTURE_2D, GL_INVALID_ENUM);
  2308. // FIXME: support other parameter names
  2309. RETURN_WITH_ERROR_IF(pname < GL_TEXTURE_WIDTH || pname > GL_TEXTURE_HEIGHT, GL_INVALID_ENUM);
  2310. RETURN_WITH_ERROR_IF(level < 0 || level > Texture2D::LOG2_MAX_TEXTURE_SIZE, GL_INVALID_VALUE);
  2311. // FIXME: GL_INVALID_VALUE is generated if target is GL_TEXTURE_BUFFER and level is not zero
  2312. // FIXME: GL_INVALID_OPERATION is generated if GL_TEXTURE_COMPRESSED_IMAGE_SIZE is queried on texture images with an uncompressed internal format or on proxy targets
  2313. switch (pname) {
  2314. case GL_TEXTURE_HEIGHT:
  2315. *params = m_active_texture_unit->bound_texture_2d()->height_at_lod(level);
  2316. break;
  2317. case GL_TEXTURE_WIDTH:
  2318. *params = m_active_texture_unit->bound_texture_2d()->width_at_lod(level);
  2319. break;
  2320. }
  2321. }
  2322. void SoftwareGLContext::gl_rect(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2)
  2323. {
  2324. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_rect, x1, y1, x2, y2);
  2325. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2326. gl_begin(GL_POLYGON);
  2327. gl_vertex(x1, y1, 0.0, 0.0);
  2328. gl_vertex(x2, y1, 0.0, 0.0);
  2329. gl_vertex(x2, y2, 0.0, 0.0);
  2330. gl_vertex(x1, y2, 0.0, 0.0);
  2331. gl_end();
  2332. }
  2333. void SoftwareGLContext::gl_tex_gen(GLenum coord, GLenum pname, GLint param)
  2334. {
  2335. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_gen, coord, pname, param);
  2336. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2337. RETURN_WITH_ERROR_IF(coord < GL_S || coord > GL_Q, GL_INVALID_ENUM);
  2338. RETURN_WITH_ERROR_IF(pname != GL_TEXTURE_GEN_MODE, GL_INVALID_ENUM);
  2339. RETURN_WITH_ERROR_IF(param != GL_EYE_LINEAR
  2340. && param != GL_OBJECT_LINEAR
  2341. && param != GL_SPHERE_MAP
  2342. && param != GL_NORMAL_MAP
  2343. && param != GL_REFLECTION_MAP,
  2344. GL_INVALID_ENUM);
  2345. RETURN_WITH_ERROR_IF((coord == GL_R || coord == GL_Q) && param == GL_SPHERE_MAP, GL_INVALID_ENUM);
  2346. RETURN_WITH_ERROR_IF(coord == GL_Q && (param == GL_REFLECTION_MAP || param == GL_NORMAL_MAP), GL_INVALID_ENUM);
  2347. GLenum const capability = GL_TEXTURE_GEN_S + (coord - GL_S);
  2348. texture_coordinate_generation(capability).generation_mode = param;
  2349. m_texcoord_generation_dirty = true;
  2350. }
  2351. void SoftwareGLContext::gl_tex_gen_floatv(GLenum coord, GLenum pname, GLfloat const* params)
  2352. {
  2353. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_gen_floatv, coord, pname, params);
  2354. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  2355. RETURN_WITH_ERROR_IF(coord < GL_S || coord > GL_Q, GL_INVALID_ENUM);
  2356. RETURN_WITH_ERROR_IF(pname != GL_TEXTURE_GEN_MODE
  2357. && pname != GL_OBJECT_PLANE
  2358. && pname != GL_EYE_PLANE,
  2359. GL_INVALID_ENUM);
  2360. GLenum const capability = GL_TEXTURE_GEN_S + (coord - GL_S);
  2361. switch (pname) {
  2362. case GL_TEXTURE_GEN_MODE: {
  2363. auto param = static_cast<GLenum>(params[0]);
  2364. RETURN_WITH_ERROR_IF(param != GL_EYE_LINEAR
  2365. && param != GL_OBJECT_LINEAR
  2366. && param != GL_SPHERE_MAP
  2367. && param != GL_NORMAL_MAP
  2368. && param != GL_REFLECTION_MAP,
  2369. GL_INVALID_ENUM);
  2370. RETURN_WITH_ERROR_IF((coord == GL_R || coord == GL_Q) && param == GL_SPHERE_MAP, GL_INVALID_ENUM);
  2371. RETURN_WITH_ERROR_IF(coord == GL_Q && (param == GL_REFLECTION_MAP || param == GL_NORMAL_MAP), GL_INVALID_ENUM);
  2372. texture_coordinate_generation(capability).generation_mode = param;
  2373. break;
  2374. }
  2375. case GL_OBJECT_PLANE:
  2376. texture_coordinate_generation(capability).object_plane_coefficients = { params[0], params[1], params[2], params[3] };
  2377. break;
  2378. case GL_EYE_PLANE: {
  2379. auto inverted_model_view_matrix = m_model_view_matrix.inverse();
  2380. auto input_coefficients = FloatVector4 { params[0], params[1], params[2], params[3] };
  2381. // Note: we are allowed to store transformed coefficients here, according to the documentation on
  2382. // `glGetTexGen`:
  2383. //
  2384. // "The returned values are those maintained in eye coordinates. They are not equal to the values
  2385. // specified using glTexGen, unless the modelview matrix was identity when glTexGen was called."
  2386. texture_coordinate_generation(capability).eye_plane_coefficients = inverted_model_view_matrix * input_coefficients;
  2387. break;
  2388. }
  2389. default:
  2390. VERIFY_NOT_REACHED();
  2391. }
  2392. m_texcoord_generation_dirty = true;
  2393. }
  2394. void SoftwareGLContext::present()
  2395. {
  2396. m_rasterizer.blit_to(*m_frontbuffer);
  2397. }
  2398. void SoftwareGLContext::sync_device_config()
  2399. {
  2400. sync_device_sampler_config();
  2401. sync_device_texcoord_config();
  2402. }
  2403. void SoftwareGLContext::sync_device_sampler_config()
  2404. {
  2405. if (!m_sampler_config_is_dirty)
  2406. return;
  2407. m_sampler_config_is_dirty = false;
  2408. for (unsigned i = 0; i < m_texture_units.size(); ++i) {
  2409. SoftGPU::SamplerConfig config;
  2410. if (!m_texture_units[i].texture_2d_enabled())
  2411. continue;
  2412. auto texture = m_texture_units[i].bound_texture_2d();
  2413. config.bound_image = texture.is_null() ? nullptr : texture->device_image();
  2414. auto const& sampler = texture->sampler();
  2415. switch (sampler.min_filter()) {
  2416. case GL_NEAREST:
  2417. config.texture_min_filter = SoftGPU::TextureFilter::Nearest;
  2418. config.mipmap_filter = SoftGPU::MipMapFilter::None;
  2419. break;
  2420. case GL_LINEAR:
  2421. config.texture_min_filter = SoftGPU::TextureFilter::Linear;
  2422. config.mipmap_filter = SoftGPU::MipMapFilter::None;
  2423. break;
  2424. case GL_NEAREST_MIPMAP_NEAREST:
  2425. config.texture_min_filter = SoftGPU::TextureFilter::Nearest;
  2426. config.mipmap_filter = SoftGPU::MipMapFilter::Nearest;
  2427. break;
  2428. case GL_LINEAR_MIPMAP_NEAREST:
  2429. config.texture_min_filter = SoftGPU::TextureFilter::Nearest;
  2430. config.mipmap_filter = SoftGPU::MipMapFilter::Linear;
  2431. break;
  2432. case GL_NEAREST_MIPMAP_LINEAR:
  2433. config.texture_min_filter = SoftGPU::TextureFilter::Linear;
  2434. config.mipmap_filter = SoftGPU::MipMapFilter::Nearest;
  2435. break;
  2436. case GL_LINEAR_MIPMAP_LINEAR:
  2437. config.texture_min_filter = SoftGPU::TextureFilter::Linear;
  2438. config.mipmap_filter = SoftGPU::MipMapFilter::Linear;
  2439. break;
  2440. default:
  2441. VERIFY_NOT_REACHED();
  2442. }
  2443. switch (sampler.mag_filter()) {
  2444. case GL_NEAREST:
  2445. config.texture_mag_filter = SoftGPU::TextureFilter::Nearest;
  2446. break;
  2447. case GL_LINEAR:
  2448. config.texture_mag_filter = SoftGPU::TextureFilter::Linear;
  2449. break;
  2450. default:
  2451. VERIFY_NOT_REACHED();
  2452. }
  2453. switch (sampler.wrap_s_mode()) {
  2454. case GL_CLAMP:
  2455. config.texture_wrap_u = SoftGPU::TextureWrapMode::Clamp;
  2456. break;
  2457. case GL_CLAMP_TO_BORDER:
  2458. config.texture_wrap_u = SoftGPU::TextureWrapMode::ClampToBorder;
  2459. break;
  2460. case GL_CLAMP_TO_EDGE:
  2461. config.texture_wrap_u = SoftGPU::TextureWrapMode::ClampToEdge;
  2462. break;
  2463. case GL_REPEAT:
  2464. config.texture_wrap_u = SoftGPU::TextureWrapMode::Repeat;
  2465. break;
  2466. case GL_MIRRORED_REPEAT:
  2467. config.texture_wrap_u = SoftGPU::TextureWrapMode::MirroredRepeat;
  2468. break;
  2469. default:
  2470. VERIFY_NOT_REACHED();
  2471. }
  2472. switch (sampler.wrap_t_mode()) {
  2473. case GL_CLAMP:
  2474. config.texture_wrap_v = SoftGPU::TextureWrapMode::Clamp;
  2475. break;
  2476. case GL_CLAMP_TO_BORDER:
  2477. config.texture_wrap_v = SoftGPU::TextureWrapMode::ClampToBorder;
  2478. break;
  2479. case GL_CLAMP_TO_EDGE:
  2480. config.texture_wrap_v = SoftGPU::TextureWrapMode::ClampToEdge;
  2481. break;
  2482. case GL_REPEAT:
  2483. config.texture_wrap_v = SoftGPU::TextureWrapMode::Repeat;
  2484. break;
  2485. case GL_MIRRORED_REPEAT:
  2486. config.texture_wrap_v = SoftGPU::TextureWrapMode::MirroredRepeat;
  2487. break;
  2488. default:
  2489. VERIFY_NOT_REACHED();
  2490. }
  2491. switch (m_texture_units[i].env_mode()) {
  2492. case GL_MODULATE:
  2493. config.fixed_function_texture_env_mode = SoftGPU::TextureEnvMode::Modulate;
  2494. break;
  2495. case GL_REPLACE:
  2496. config.fixed_function_texture_env_mode = SoftGPU::TextureEnvMode::Replace;
  2497. break;
  2498. case GL_DECAL:
  2499. config.fixed_function_texture_env_mode = SoftGPU::TextureEnvMode::Decal;
  2500. break;
  2501. default:
  2502. VERIFY_NOT_REACHED();
  2503. }
  2504. m_rasterizer.set_sampler_config(i, config);
  2505. }
  2506. }
  2507. void SoftwareGLContext::sync_device_texcoord_config()
  2508. {
  2509. if (!m_texcoord_generation_dirty)
  2510. return;
  2511. m_texcoord_generation_dirty = false;
  2512. auto options = m_rasterizer.options();
  2513. u8 enabled_coordinates = SoftGPU::TexCoordGenerationCoordinate::None;
  2514. for (GLenum capability = GL_TEXTURE_GEN_S; capability <= GL_TEXTURE_GEN_Q; ++capability) {
  2515. auto const context_coordinate_config = texture_coordinate_generation(capability);
  2516. if (!context_coordinate_config.enabled)
  2517. continue;
  2518. SoftGPU::TexCoordGenerationConfig* texcoord_generation_config;
  2519. switch (capability) {
  2520. case GL_TEXTURE_GEN_S:
  2521. enabled_coordinates |= SoftGPU::TexCoordGenerationCoordinate::S;
  2522. texcoord_generation_config = &options.texcoord_generation_config[0];
  2523. break;
  2524. case GL_TEXTURE_GEN_T:
  2525. enabled_coordinates |= SoftGPU::TexCoordGenerationCoordinate::T;
  2526. texcoord_generation_config = &options.texcoord_generation_config[1];
  2527. break;
  2528. case GL_TEXTURE_GEN_R:
  2529. enabled_coordinates |= SoftGPU::TexCoordGenerationCoordinate::R;
  2530. texcoord_generation_config = &options.texcoord_generation_config[2];
  2531. break;
  2532. case GL_TEXTURE_GEN_Q:
  2533. enabled_coordinates |= SoftGPU::TexCoordGenerationCoordinate::Q;
  2534. texcoord_generation_config = &options.texcoord_generation_config[3];
  2535. break;
  2536. default:
  2537. VERIFY_NOT_REACHED();
  2538. }
  2539. switch (context_coordinate_config.generation_mode) {
  2540. case GL_OBJECT_LINEAR:
  2541. texcoord_generation_config->mode = SoftGPU::TexCoordGenerationMode::ObjectLinear;
  2542. texcoord_generation_config->coefficients = context_coordinate_config.object_plane_coefficients;
  2543. break;
  2544. case GL_EYE_LINEAR:
  2545. texcoord_generation_config->mode = SoftGPU::TexCoordGenerationMode::EyeLinear;
  2546. texcoord_generation_config->coefficients = context_coordinate_config.eye_plane_coefficients;
  2547. break;
  2548. case GL_SPHERE_MAP:
  2549. texcoord_generation_config->mode = SoftGPU::TexCoordGenerationMode::SphereMap;
  2550. break;
  2551. case GL_REFLECTION_MAP:
  2552. texcoord_generation_config->mode = SoftGPU::TexCoordGenerationMode::ReflectionMap;
  2553. break;
  2554. case GL_NORMAL_MAP:
  2555. texcoord_generation_config->mode = SoftGPU::TexCoordGenerationMode::NormalMap;
  2556. break;
  2557. }
  2558. }
  2559. options.texcoord_generation_enabled_coordinates = enabled_coordinates;
  2560. m_rasterizer.set_options(options);
  2561. }
  2562. }