Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

r600_state_common.c 89KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940
  1. /*
  2. * Copyright 2010 Red Hat Inc.
  3. * 2010 Jerome Glisse
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * on the rights to use, copy, modify, merge, publish, distribute, sub
  9. * license, and/or sell copies of the Software, and to permit persons to whom
  10. * the Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
  20. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  21. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  22. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors: Dave Airlie <airlied@redhat.com>
  25. * Jerome Glisse <jglisse@redhat.com>
  26. */
  27. #include "r600_formats.h"
  28. #include "r600_shader.h"
  29. #include "r600d.h"
  30. #include "util/u_format_s3tc.h"
  31. #include "util/u_index_modify.h"
  32. #include "util/u_memory.h"
  33. #include "util/u_upload_mgr.h"
  34. #include "util/u_math.h"
  35. #include "tgsi/tgsi_parse.h"
  36. #include "tgsi/tgsi_scan.h"
  37. #include "tgsi/tgsi_ureg.h"
  38. void r600_init_command_buffer(struct r600_command_buffer *cb, unsigned num_dw)
  39. {
  40. assert(!cb->buf);
  41. cb->buf = CALLOC(1, 4 * num_dw);
  42. cb->max_num_dw = num_dw;
  43. }
  44. void r600_release_command_buffer(struct r600_command_buffer *cb)
  45. {
  46. FREE(cb->buf);
  47. }
  48. void r600_add_atom(struct r600_context *rctx,
  49. struct r600_atom *atom,
  50. unsigned id)
  51. {
  52. assert(id < R600_NUM_ATOMS);
  53. assert(rctx->atoms[id] == NULL);
  54. rctx->atoms[id] = atom;
  55. atom->id = id;
  56. }
  57. void r600_init_atom(struct r600_context *rctx,
  58. struct r600_atom *atom,
  59. unsigned id,
  60. void (*emit)(struct r600_context *ctx, struct r600_atom *state),
  61. unsigned num_dw)
  62. {
  63. atom->emit = (void*)emit;
  64. atom->num_dw = num_dw;
  65. r600_add_atom(rctx, atom, id);
  66. }
  67. void r600_emit_cso_state(struct r600_context *rctx, struct r600_atom *atom)
  68. {
  69. r600_emit_command_buffer(rctx->b.gfx.cs, ((struct r600_cso_state*)atom)->cb);
  70. }
  71. void r600_emit_alphatest_state(struct r600_context *rctx, struct r600_atom *atom)
  72. {
  73. struct radeon_winsys_cs *cs = rctx->b.gfx.cs;
  74. struct r600_alphatest_state *a = (struct r600_alphatest_state*)atom;
  75. unsigned alpha_ref = a->sx_alpha_ref;
  76. if (rctx->b.chip_class >= EVERGREEN && a->cb0_export_16bpc) {
  77. alpha_ref &= ~0x1FFF;
  78. }
  79. radeon_set_context_reg(cs, R_028410_SX_ALPHA_TEST_CONTROL,
  80. a->sx_alpha_test_control |
  81. S_028410_ALPHA_TEST_BYPASS(a->bypass));
  82. radeon_set_context_reg(cs, R_028438_SX_ALPHA_REF, alpha_ref);
  83. }
  84. static void r600_texture_barrier(struct pipe_context *ctx)
  85. {
  86. struct r600_context *rctx = (struct r600_context *)ctx;
  87. rctx->b.flags |= R600_CONTEXT_INV_TEX_CACHE |
  88. R600_CONTEXT_FLUSH_AND_INV_CB |
  89. R600_CONTEXT_FLUSH_AND_INV |
  90. R600_CONTEXT_WAIT_3D_IDLE;
  91. }
  92. static unsigned r600_conv_pipe_prim(unsigned prim)
  93. {
  94. static const unsigned prim_conv[] = {
  95. [PIPE_PRIM_POINTS] = V_008958_DI_PT_POINTLIST,
  96. [PIPE_PRIM_LINES] = V_008958_DI_PT_LINELIST,
  97. [PIPE_PRIM_LINE_LOOP] = V_008958_DI_PT_LINELOOP,
  98. [PIPE_PRIM_LINE_STRIP] = V_008958_DI_PT_LINESTRIP,
  99. [PIPE_PRIM_TRIANGLES] = V_008958_DI_PT_TRILIST,
  100. [PIPE_PRIM_TRIANGLE_STRIP] = V_008958_DI_PT_TRISTRIP,
  101. [PIPE_PRIM_TRIANGLE_FAN] = V_008958_DI_PT_TRIFAN,
  102. [PIPE_PRIM_QUADS] = V_008958_DI_PT_QUADLIST,
  103. [PIPE_PRIM_QUAD_STRIP] = V_008958_DI_PT_QUADSTRIP,
  104. [PIPE_PRIM_POLYGON] = V_008958_DI_PT_POLYGON,
  105. [PIPE_PRIM_LINES_ADJACENCY] = V_008958_DI_PT_LINELIST_ADJ,
  106. [PIPE_PRIM_LINE_STRIP_ADJACENCY] = V_008958_DI_PT_LINESTRIP_ADJ,
  107. [PIPE_PRIM_TRIANGLES_ADJACENCY] = V_008958_DI_PT_TRILIST_ADJ,
  108. [PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY] = V_008958_DI_PT_TRISTRIP_ADJ,
  109. [PIPE_PRIM_PATCHES] = V_008958_DI_PT_PATCH,
  110. [R600_PRIM_RECTANGLE_LIST] = V_008958_DI_PT_RECTLIST
  111. };
  112. assert(prim < ARRAY_SIZE(prim_conv));
  113. return prim_conv[prim];
  114. }
  115. unsigned r600_conv_prim_to_gs_out(unsigned mode)
  116. {
  117. static const int prim_conv[] = {
  118. [PIPE_PRIM_POINTS] = V_028A6C_OUTPRIM_TYPE_POINTLIST,
  119. [PIPE_PRIM_LINES] = V_028A6C_OUTPRIM_TYPE_LINESTRIP,
  120. [PIPE_PRIM_LINE_LOOP] = V_028A6C_OUTPRIM_TYPE_LINESTRIP,
  121. [PIPE_PRIM_LINE_STRIP] = V_028A6C_OUTPRIM_TYPE_LINESTRIP,
  122. [PIPE_PRIM_TRIANGLES] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
  123. [PIPE_PRIM_TRIANGLE_STRIP] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
  124. [PIPE_PRIM_TRIANGLE_FAN] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
  125. [PIPE_PRIM_QUADS] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
  126. [PIPE_PRIM_QUAD_STRIP] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
  127. [PIPE_PRIM_POLYGON] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
  128. [PIPE_PRIM_LINES_ADJACENCY] = V_028A6C_OUTPRIM_TYPE_LINESTRIP,
  129. [PIPE_PRIM_LINE_STRIP_ADJACENCY] = V_028A6C_OUTPRIM_TYPE_LINESTRIP,
  130. [PIPE_PRIM_TRIANGLES_ADJACENCY] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
  131. [PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
  132. [PIPE_PRIM_PATCHES] = V_028A6C_OUTPRIM_TYPE_POINTLIST,
  133. [R600_PRIM_RECTANGLE_LIST] = V_028A6C_OUTPRIM_TYPE_TRISTRIP
  134. };
  135. assert(mode < ARRAY_SIZE(prim_conv));
  136. return prim_conv[mode];
  137. }
  138. /* common state between evergreen and r600 */
  139. static void r600_bind_blend_state_internal(struct r600_context *rctx,
  140. struct r600_blend_state *blend, bool blend_disable)
  141. {
  142. unsigned color_control;
  143. bool update_cb = false;
  144. rctx->alpha_to_one = blend->alpha_to_one;
  145. rctx->dual_src_blend = blend->dual_src_blend;
  146. if (!blend_disable) {
  147. r600_set_cso_state_with_cb(rctx, &rctx->blend_state, blend, &blend->buffer);
  148. color_control = blend->cb_color_control;
  149. } else {
  150. /* Blending is disabled. */
  151. r600_set_cso_state_with_cb(rctx, &rctx->blend_state, blend, &blend->buffer_no_blend);
  152. color_control = blend->cb_color_control_no_blend;
  153. }
  154. /* Update derived states. */
  155. if (rctx->cb_misc_state.blend_colormask != blend->cb_target_mask) {
  156. rctx->cb_misc_state.blend_colormask = blend->cb_target_mask;
  157. update_cb = true;
  158. }
  159. if (rctx->b.chip_class <= R700 &&
  160. rctx->cb_misc_state.cb_color_control != color_control) {
  161. rctx->cb_misc_state.cb_color_control = color_control;
  162. update_cb = true;
  163. }
  164. if (rctx->cb_misc_state.dual_src_blend != blend->dual_src_blend) {
  165. rctx->cb_misc_state.dual_src_blend = blend->dual_src_blend;
  166. update_cb = true;
  167. }
  168. if (update_cb) {
  169. r600_mark_atom_dirty(rctx, &rctx->cb_misc_state.atom);
  170. }
  171. }
  172. static void r600_bind_blend_state(struct pipe_context *ctx, void *state)
  173. {
  174. struct r600_context *rctx = (struct r600_context *)ctx;
  175. struct r600_blend_state *blend = (struct r600_blend_state *)state;
  176. if (!blend) {
  177. r600_set_cso_state_with_cb(rctx, &rctx->blend_state, NULL, NULL);
  178. return;
  179. }
  180. r600_bind_blend_state_internal(rctx, blend, rctx->force_blend_disable);
  181. }
  182. static void r600_set_blend_color(struct pipe_context *ctx,
  183. const struct pipe_blend_color *state)
  184. {
  185. struct r600_context *rctx = (struct r600_context *)ctx;
  186. rctx->blend_color.state = *state;
  187. r600_mark_atom_dirty(rctx, &rctx->blend_color.atom);
  188. }
  189. void r600_emit_blend_color(struct r600_context *rctx, struct r600_atom *atom)
  190. {
  191. struct radeon_winsys_cs *cs = rctx->b.gfx.cs;
  192. struct pipe_blend_color *state = &rctx->blend_color.state;
  193. radeon_set_context_reg_seq(cs, R_028414_CB_BLEND_RED, 4);
  194. radeon_emit(cs, fui(state->color[0])); /* R_028414_CB_BLEND_RED */
  195. radeon_emit(cs, fui(state->color[1])); /* R_028418_CB_BLEND_GREEN */
  196. radeon_emit(cs, fui(state->color[2])); /* R_02841C_CB_BLEND_BLUE */
  197. radeon_emit(cs, fui(state->color[3])); /* R_028420_CB_BLEND_ALPHA */
  198. }
  199. void r600_emit_vgt_state(struct r600_context *rctx, struct r600_atom *atom)
  200. {
  201. struct radeon_winsys_cs *cs = rctx->b.gfx.cs;
  202. struct r600_vgt_state *a = (struct r600_vgt_state *)atom;
  203. radeon_set_context_reg(cs, R_028A94_VGT_MULTI_PRIM_IB_RESET_EN, a->vgt_multi_prim_ib_reset_en);
  204. radeon_set_context_reg_seq(cs, R_028408_VGT_INDX_OFFSET, 2);
  205. radeon_emit(cs, a->vgt_indx_offset); /* R_028408_VGT_INDX_OFFSET */
  206. radeon_emit(cs, a->vgt_multi_prim_ib_reset_indx); /* R_02840C_VGT_MULTI_PRIM_IB_RESET_INDX */
  207. if (a->last_draw_was_indirect) {
  208. a->last_draw_was_indirect = false;
  209. radeon_set_ctl_const(cs, R_03CFF0_SQ_VTX_BASE_VTX_LOC, 0);
  210. }
  211. }
  212. static void r600_set_clip_state(struct pipe_context *ctx,
  213. const struct pipe_clip_state *state)
  214. {
  215. struct r600_context *rctx = (struct r600_context *)ctx;
  216. rctx->clip_state.state = *state;
  217. r600_mark_atom_dirty(rctx, &rctx->clip_state.atom);
  218. rctx->driver_consts[PIPE_SHADER_VERTEX].vs_ucp_dirty = true;
  219. }
  220. static void r600_set_stencil_ref(struct pipe_context *ctx,
  221. const struct r600_stencil_ref *state)
  222. {
  223. struct r600_context *rctx = (struct r600_context *)ctx;
  224. rctx->stencil_ref.state = *state;
  225. r600_mark_atom_dirty(rctx, &rctx->stencil_ref.atom);
  226. }
  227. void r600_emit_stencil_ref(struct r600_context *rctx, struct r600_atom *atom)
  228. {
  229. struct radeon_winsys_cs *cs = rctx->b.gfx.cs;
  230. struct r600_stencil_ref_state *a = (struct r600_stencil_ref_state*)atom;
  231. radeon_set_context_reg_seq(cs, R_028430_DB_STENCILREFMASK, 2);
  232. radeon_emit(cs, /* R_028430_DB_STENCILREFMASK */
  233. S_028430_STENCILREF(a->state.ref_value[0]) |
  234. S_028430_STENCILMASK(a->state.valuemask[0]) |
  235. S_028430_STENCILWRITEMASK(a->state.writemask[0]));
  236. radeon_emit(cs, /* R_028434_DB_STENCILREFMASK_BF */
  237. S_028434_STENCILREF_BF(a->state.ref_value[1]) |
  238. S_028434_STENCILMASK_BF(a->state.valuemask[1]) |
  239. S_028434_STENCILWRITEMASK_BF(a->state.writemask[1]));
  240. }
  241. static void r600_set_pipe_stencil_ref(struct pipe_context *ctx,
  242. const struct pipe_stencil_ref *state)
  243. {
  244. struct r600_context *rctx = (struct r600_context *)ctx;
  245. struct r600_dsa_state *dsa = (struct r600_dsa_state*)rctx->dsa_state.cso;
  246. struct r600_stencil_ref ref;
  247. rctx->stencil_ref.pipe_state = *state;
  248. if (!dsa)
  249. return;
  250. ref.ref_value[0] = state->ref_value[0];
  251. ref.ref_value[1] = state->ref_value[1];
  252. ref.valuemask[0] = dsa->valuemask[0];
  253. ref.valuemask[1] = dsa->valuemask[1];
  254. ref.writemask[0] = dsa->writemask[0];
  255. ref.writemask[1] = dsa->writemask[1];
  256. r600_set_stencil_ref(ctx, &ref);
  257. }
  258. static void r600_bind_dsa_state(struct pipe_context *ctx, void *state)
  259. {
  260. struct r600_context *rctx = (struct r600_context *)ctx;
  261. struct r600_dsa_state *dsa = state;
  262. struct r600_stencil_ref ref;
  263. if (!state) {
  264. r600_set_cso_state_with_cb(rctx, &rctx->dsa_state, NULL, NULL);
  265. return;
  266. }
  267. r600_set_cso_state_with_cb(rctx, &rctx->dsa_state, dsa, &dsa->buffer);
  268. ref.ref_value[0] = rctx->stencil_ref.pipe_state.ref_value[0];
  269. ref.ref_value[1] = rctx->stencil_ref.pipe_state.ref_value[1];
  270. ref.valuemask[0] = dsa->valuemask[0];
  271. ref.valuemask[1] = dsa->valuemask[1];
  272. ref.writemask[0] = dsa->writemask[0];
  273. ref.writemask[1] = dsa->writemask[1];
  274. if (rctx->zwritemask != dsa->zwritemask) {
  275. rctx->zwritemask = dsa->zwritemask;
  276. if (rctx->b.chip_class >= EVERGREEN) {
  277. /* work around some issue when not writing to zbuffer
  278. * we are having lockup on evergreen so do not enable
  279. * hyperz when not writing zbuffer
  280. */
  281. r600_mark_atom_dirty(rctx, &rctx->db_misc_state.atom);
  282. }
  283. }
  284. r600_set_stencil_ref(ctx, &ref);
  285. /* Update alphatest state. */
  286. if (rctx->alphatest_state.sx_alpha_test_control != dsa->sx_alpha_test_control ||
  287. rctx->alphatest_state.sx_alpha_ref != dsa->alpha_ref) {
  288. rctx->alphatest_state.sx_alpha_test_control = dsa->sx_alpha_test_control;
  289. rctx->alphatest_state.sx_alpha_ref = dsa->alpha_ref;
  290. r600_mark_atom_dirty(rctx, &rctx->alphatest_state.atom);
  291. }
  292. }
  293. static void r600_bind_rs_state(struct pipe_context *ctx, void *state)
  294. {
  295. struct r600_rasterizer_state *rs = (struct r600_rasterizer_state *)state;
  296. struct r600_context *rctx = (struct r600_context *)ctx;
  297. if (!state)
  298. return;
  299. rctx->rasterizer = rs;
  300. r600_set_cso_state_with_cb(rctx, &rctx->rasterizer_state, rs, &rs->buffer);
  301. if (rs->offset_enable &&
  302. (rs->offset_units != rctx->poly_offset_state.offset_units ||
  303. rs->offset_scale != rctx->poly_offset_state.offset_scale ||
  304. rs->offset_units_unscaled != rctx->poly_offset_state.offset_units_unscaled)) {
  305. rctx->poly_offset_state.offset_units = rs->offset_units;
  306. rctx->poly_offset_state.offset_scale = rs->offset_scale;
  307. rctx->poly_offset_state.offset_units_unscaled = rs->offset_units_unscaled;
  308. r600_mark_atom_dirty(rctx, &rctx->poly_offset_state.atom);
  309. }
  310. /* Update clip_misc_state. */
  311. if (rctx->clip_misc_state.pa_cl_clip_cntl != rs->pa_cl_clip_cntl ||
  312. rctx->clip_misc_state.clip_plane_enable != rs->clip_plane_enable) {
  313. rctx->clip_misc_state.pa_cl_clip_cntl = rs->pa_cl_clip_cntl;
  314. rctx->clip_misc_state.clip_plane_enable = rs->clip_plane_enable;
  315. r600_mark_atom_dirty(rctx, &rctx->clip_misc_state.atom);
  316. }
  317. r600_set_scissor_enable(&rctx->b, rs->scissor_enable);
  318. /* Re-emit PA_SC_LINE_STIPPLE. */
  319. rctx->last_primitive_type = -1;
  320. }
  321. static void r600_delete_rs_state(struct pipe_context *ctx, void *state)
  322. {
  323. struct r600_rasterizer_state *rs = (struct r600_rasterizer_state *)state;
  324. r600_release_command_buffer(&rs->buffer);
  325. FREE(rs);
  326. }
  327. static void r600_sampler_view_destroy(struct pipe_context *ctx,
  328. struct pipe_sampler_view *state)
  329. {
  330. struct r600_pipe_sampler_view *view = (struct r600_pipe_sampler_view *)state;
  331. if (view->tex_resource->gpu_address &&
  332. view->tex_resource->b.b.target == PIPE_BUFFER)
  333. LIST_DELINIT(&view->list);
  334. pipe_resource_reference(&state->texture, NULL);
  335. FREE(view);
  336. }
  337. void r600_sampler_states_dirty(struct r600_context *rctx,
  338. struct r600_sampler_states *state)
  339. {
  340. if (state->dirty_mask) {
  341. if (state->dirty_mask & state->has_bordercolor_mask) {
  342. rctx->b.flags |= R600_CONTEXT_WAIT_3D_IDLE;
  343. }
  344. state->atom.num_dw =
  345. util_bitcount(state->dirty_mask & state->has_bordercolor_mask) * 11 +
  346. util_bitcount(state->dirty_mask & ~state->has_bordercolor_mask) * 5;
  347. r600_mark_atom_dirty(rctx, &state->atom);
  348. }
  349. }
  350. static void r600_bind_sampler_states(struct pipe_context *pipe,
  351. enum pipe_shader_type shader,
  352. unsigned start,
  353. unsigned count, void **states)
  354. {
  355. struct r600_context *rctx = (struct r600_context *)pipe;
  356. struct r600_textures_info *dst = &rctx->samplers[shader];
  357. struct r600_pipe_sampler_state **rstates = (struct r600_pipe_sampler_state**)states;
  358. int seamless_cube_map = -1;
  359. unsigned i;
  360. /* This sets 1-bit for states with index >= count. */
  361. uint32_t disable_mask = ~((1ull << count) - 1);
  362. /* These are the new states set by this function. */
  363. uint32_t new_mask = 0;
  364. assert(start == 0); /* XXX fix below */
  365. if (!states) {
  366. disable_mask = ~0u;
  367. count = 0;
  368. }
  369. for (i = 0; i < count; i++) {
  370. struct r600_pipe_sampler_state *rstate = rstates[i];
  371. if (rstate == dst->states.states[i]) {
  372. continue;
  373. }
  374. if (rstate) {
  375. if (rstate->border_color_use) {
  376. dst->states.has_bordercolor_mask |= 1 << i;
  377. } else {
  378. dst->states.has_bordercolor_mask &= ~(1 << i);
  379. }
  380. seamless_cube_map = rstate->seamless_cube_map;
  381. new_mask |= 1 << i;
  382. } else {
  383. disable_mask |= 1 << i;
  384. }
  385. }
  386. memcpy(dst->states.states, rstates, sizeof(void*) * count);
  387. memset(dst->states.states + count, 0, sizeof(void*) * (NUM_TEX_UNITS - count));
  388. dst->states.enabled_mask &= ~disable_mask;
  389. dst->states.dirty_mask &= dst->states.enabled_mask;
  390. dst->states.enabled_mask |= new_mask;
  391. dst->states.dirty_mask |= new_mask;
  392. dst->states.has_bordercolor_mask &= dst->states.enabled_mask;
  393. r600_sampler_states_dirty(rctx, &dst->states);
  394. /* Seamless cubemap state. */
  395. if (rctx->b.chip_class <= R700 &&
  396. seamless_cube_map != -1 &&
  397. seamless_cube_map != rctx->seamless_cube_map.enabled) {
  398. /* change in TA_CNTL_AUX need a pipeline flush */
  399. rctx->b.flags |= R600_CONTEXT_WAIT_3D_IDLE;
  400. rctx->seamless_cube_map.enabled = seamless_cube_map;
  401. r600_mark_atom_dirty(rctx, &rctx->seamless_cube_map.atom);
  402. }
  403. }
  404. static void r600_delete_sampler_state(struct pipe_context *ctx, void *state)
  405. {
  406. free(state);
  407. }
  408. static void r600_delete_blend_state(struct pipe_context *ctx, void *state)
  409. {
  410. struct r600_context *rctx = (struct r600_context *)ctx;
  411. struct r600_blend_state *blend = (struct r600_blend_state*)state;
  412. if (rctx->blend_state.cso == state) {
  413. ctx->bind_blend_state(ctx, NULL);
  414. }
  415. r600_release_command_buffer(&blend->buffer);
  416. r600_release_command_buffer(&blend->buffer_no_blend);
  417. FREE(blend);
  418. }
  419. static void r600_delete_dsa_state(struct pipe_context *ctx, void *state)
  420. {
  421. struct r600_context *rctx = (struct r600_context *)ctx;
  422. struct r600_dsa_state *dsa = (struct r600_dsa_state *)state;
  423. if (rctx->dsa_state.cso == state) {
  424. ctx->bind_depth_stencil_alpha_state(ctx, NULL);
  425. }
  426. r600_release_command_buffer(&dsa->buffer);
  427. free(dsa);
  428. }
  429. static void r600_bind_vertex_elements(struct pipe_context *ctx, void *state)
  430. {
  431. struct r600_context *rctx = (struct r600_context *)ctx;
  432. r600_set_cso_state(rctx, &rctx->vertex_fetch_shader, state);
  433. }
  434. static void r600_delete_vertex_elements(struct pipe_context *ctx, void *state)
  435. {
  436. struct r600_fetch_shader *shader = (struct r600_fetch_shader*)state;
  437. r600_resource_reference(&shader->buffer, NULL);
  438. FREE(shader);
  439. }
  440. static void r600_set_index_buffer(struct pipe_context *ctx,
  441. const struct pipe_index_buffer *ib)
  442. {
  443. struct r600_context *rctx = (struct r600_context *)ctx;
  444. if (ib) {
  445. pipe_resource_reference(&rctx->index_buffer.buffer, ib->buffer);
  446. memcpy(&rctx->index_buffer, ib, sizeof(*ib));
  447. r600_context_add_resource_size(ctx, ib->buffer);
  448. } else {
  449. pipe_resource_reference(&rctx->index_buffer.buffer, NULL);
  450. }
  451. }
  452. void r600_vertex_buffers_dirty(struct r600_context *rctx)
  453. {
  454. if (rctx->vertex_buffer_state.dirty_mask) {
  455. rctx->vertex_buffer_state.atom.num_dw = (rctx->b.chip_class >= EVERGREEN ? 12 : 11) *
  456. util_bitcount(rctx->vertex_buffer_state.dirty_mask);
  457. r600_mark_atom_dirty(rctx, &rctx->vertex_buffer_state.atom);
  458. }
  459. }
  460. static void r600_set_vertex_buffers(struct pipe_context *ctx,
  461. unsigned start_slot, unsigned count,
  462. const struct pipe_vertex_buffer *input)
  463. {
  464. struct r600_context *rctx = (struct r600_context *)ctx;
  465. struct r600_vertexbuf_state *state = &rctx->vertex_buffer_state;
  466. struct pipe_vertex_buffer *vb = state->vb + start_slot;
  467. unsigned i;
  468. uint32_t disable_mask = 0;
  469. /* These are the new buffers set by this function. */
  470. uint32_t new_buffer_mask = 0;
  471. /* Set vertex buffers. */
  472. if (input) {
  473. for (i = 0; i < count; i++) {
  474. if (memcmp(&input[i], &vb[i], sizeof(struct pipe_vertex_buffer))) {
  475. if (input[i].buffer) {
  476. vb[i].stride = input[i].stride;
  477. vb[i].buffer_offset = input[i].buffer_offset;
  478. pipe_resource_reference(&vb[i].buffer, input[i].buffer);
  479. new_buffer_mask |= 1 << i;
  480. r600_context_add_resource_size(ctx, input[i].buffer);
  481. } else {
  482. pipe_resource_reference(&vb[i].buffer, NULL);
  483. disable_mask |= 1 << i;
  484. }
  485. }
  486. }
  487. } else {
  488. for (i = 0; i < count; i++) {
  489. pipe_resource_reference(&vb[i].buffer, NULL);
  490. }
  491. disable_mask = ((1ull << count) - 1);
  492. }
  493. disable_mask <<= start_slot;
  494. new_buffer_mask <<= start_slot;
  495. rctx->vertex_buffer_state.enabled_mask &= ~disable_mask;
  496. rctx->vertex_buffer_state.dirty_mask &= rctx->vertex_buffer_state.enabled_mask;
  497. rctx->vertex_buffer_state.enabled_mask |= new_buffer_mask;
  498. rctx->vertex_buffer_state.dirty_mask |= new_buffer_mask;
  499. r600_vertex_buffers_dirty(rctx);
  500. }
  501. void r600_sampler_views_dirty(struct r600_context *rctx,
  502. struct r600_samplerview_state *state)
  503. {
  504. if (state->dirty_mask) {
  505. state->atom.num_dw = (rctx->b.chip_class >= EVERGREEN ? 14 : 13) *
  506. util_bitcount(state->dirty_mask);
  507. r600_mark_atom_dirty(rctx, &state->atom);
  508. }
  509. }
  510. static void r600_set_sampler_views(struct pipe_context *pipe,
  511. enum pipe_shader_type shader,
  512. unsigned start, unsigned count,
  513. struct pipe_sampler_view **views)
  514. {
  515. struct r600_context *rctx = (struct r600_context *) pipe;
  516. struct r600_textures_info *dst = &rctx->samplers[shader];
  517. struct r600_pipe_sampler_view **rviews = (struct r600_pipe_sampler_view **)views;
  518. uint32_t dirty_sampler_states_mask = 0;
  519. unsigned i;
  520. /* This sets 1-bit for textures with index >= count. */
  521. uint32_t disable_mask = ~((1ull << count) - 1);
  522. /* These are the new textures set by this function. */
  523. uint32_t new_mask = 0;
  524. /* Set textures with index >= count to NULL. */
  525. uint32_t remaining_mask;
  526. assert(start == 0); /* XXX fix below */
  527. if (!views) {
  528. disable_mask = ~0u;
  529. count = 0;
  530. }
  531. remaining_mask = dst->views.enabled_mask & disable_mask;
  532. while (remaining_mask) {
  533. i = u_bit_scan(&remaining_mask);
  534. assert(dst->views.views[i]);
  535. pipe_sampler_view_reference((struct pipe_sampler_view **)&dst->views.views[i], NULL);
  536. }
  537. for (i = 0; i < count; i++) {
  538. if (rviews[i] == dst->views.views[i]) {
  539. continue;
  540. }
  541. if (rviews[i]) {
  542. struct r600_texture *rtex =
  543. (struct r600_texture*)rviews[i]->base.texture;
  544. bool is_buffer = rviews[i]->base.texture->target == PIPE_BUFFER;
  545. if (!is_buffer && rtex->db_compatible) {
  546. dst->views.compressed_depthtex_mask |= 1 << i;
  547. } else {
  548. dst->views.compressed_depthtex_mask &= ~(1 << i);
  549. }
  550. /* Track compressed colorbuffers. */
  551. if (!is_buffer && rtex->cmask.size) {
  552. dst->views.compressed_colortex_mask |= 1 << i;
  553. } else {
  554. dst->views.compressed_colortex_mask &= ~(1 << i);
  555. }
  556. /* Changing from array to non-arrays textures and vice versa requires
  557. * updating TEX_ARRAY_OVERRIDE in sampler states on R6xx-R7xx. */
  558. if (rctx->b.chip_class <= R700 &&
  559. (dst->states.enabled_mask & (1 << i)) &&
  560. (rviews[i]->base.texture->target == PIPE_TEXTURE_1D_ARRAY ||
  561. rviews[i]->base.texture->target == PIPE_TEXTURE_2D_ARRAY) != dst->is_array_sampler[i]) {
  562. dirty_sampler_states_mask |= 1 << i;
  563. }
  564. pipe_sampler_view_reference((struct pipe_sampler_view **)&dst->views.views[i], views[i]);
  565. new_mask |= 1 << i;
  566. r600_context_add_resource_size(pipe, views[i]->texture);
  567. } else {
  568. pipe_sampler_view_reference((struct pipe_sampler_view **)&dst->views.views[i], NULL);
  569. disable_mask |= 1 << i;
  570. }
  571. }
  572. dst->views.enabled_mask &= ~disable_mask;
  573. dst->views.dirty_mask &= dst->views.enabled_mask;
  574. dst->views.enabled_mask |= new_mask;
  575. dst->views.dirty_mask |= new_mask;
  576. dst->views.compressed_depthtex_mask &= dst->views.enabled_mask;
  577. dst->views.compressed_colortex_mask &= dst->views.enabled_mask;
  578. dst->views.dirty_buffer_constants = TRUE;
  579. r600_sampler_views_dirty(rctx, &dst->views);
  580. if (dirty_sampler_states_mask) {
  581. dst->states.dirty_mask |= dirty_sampler_states_mask;
  582. r600_sampler_states_dirty(rctx, &dst->states);
  583. }
  584. }
  585. static void r600_update_compressed_colortex_mask(struct r600_samplerview_state *views)
  586. {
  587. uint32_t mask = views->enabled_mask;
  588. while (mask) {
  589. unsigned i = u_bit_scan(&mask);
  590. struct pipe_resource *res = views->views[i]->base.texture;
  591. if (res && res->target != PIPE_BUFFER) {
  592. struct r600_texture *rtex = (struct r600_texture *)res;
  593. if (rtex->cmask.size) {
  594. views->compressed_colortex_mask |= 1 << i;
  595. } else {
  596. views->compressed_colortex_mask &= ~(1 << i);
  597. }
  598. }
  599. }
  600. }
  601. /* Compute the key for the hw shader variant */
  602. static inline union r600_shader_key r600_shader_selector_key(struct pipe_context * ctx,
  603. struct r600_pipe_shader_selector * sel)
  604. {
  605. struct r600_context *rctx = (struct r600_context *)ctx;
  606. union r600_shader_key key;
  607. memset(&key, 0, sizeof(key));
  608. switch (sel->type) {
  609. case PIPE_SHADER_VERTEX: {
  610. key.vs.as_ls = (rctx->tes_shader != NULL);
  611. if (!key.vs.as_ls)
  612. key.vs.as_es = (rctx->gs_shader != NULL);
  613. if (rctx->ps_shader->current->shader.gs_prim_id_input && !rctx->gs_shader) {
  614. key.vs.as_gs_a = true;
  615. key.vs.prim_id_out = rctx->ps_shader->current->shader.input[rctx->ps_shader->current->shader.ps_prim_id_input].spi_sid;
  616. }
  617. break;
  618. }
  619. case PIPE_SHADER_GEOMETRY:
  620. break;
  621. case PIPE_SHADER_FRAGMENT: {
  622. key.ps.color_two_side = rctx->rasterizer && rctx->rasterizer->two_side;
  623. key.ps.alpha_to_one = rctx->alpha_to_one &&
  624. rctx->rasterizer && rctx->rasterizer->multisample_enable &&
  625. !rctx->framebuffer.cb0_is_integer;
  626. key.ps.nr_cbufs = rctx->framebuffer.state.nr_cbufs;
  627. /* Dual-source blending only makes sense with nr_cbufs == 1. */
  628. if (key.ps.nr_cbufs == 1 && rctx->dual_src_blend)
  629. key.ps.nr_cbufs = 2;
  630. break;
  631. }
  632. case PIPE_SHADER_TESS_EVAL:
  633. key.tes.as_es = (rctx->gs_shader != NULL);
  634. break;
  635. case PIPE_SHADER_TESS_CTRL:
  636. key.tcs.prim_mode = rctx->tes_shader->info.properties[TGSI_PROPERTY_TES_PRIM_MODE];
  637. break;
  638. default:
  639. assert(0);
  640. }
  641. return key;
  642. }
  643. /* Select the hw shader variant depending on the current state.
  644. * (*dirty) is set to 1 if current variant was changed */
  645. static int r600_shader_select(struct pipe_context *ctx,
  646. struct r600_pipe_shader_selector* sel,
  647. bool *dirty)
  648. {
  649. union r600_shader_key key;
  650. struct r600_pipe_shader * shader = NULL;
  651. int r;
  652. memset(&key, 0, sizeof(key));
  653. key = r600_shader_selector_key(ctx, sel);
  654. /* Check if we don't need to change anything.
  655. * This path is also used for most shaders that don't need multiple
  656. * variants, it will cost just a computation of the key and this
  657. * test. */
  658. if (likely(sel->current && memcmp(&sel->current->key, &key, sizeof(key)) == 0)) {
  659. return 0;
  660. }
  661. /* lookup if we have other variants in the list */
  662. if (sel->num_shaders > 1) {
  663. struct r600_pipe_shader *p = sel->current, *c = p->next_variant;
  664. while (c && memcmp(&c->key, &key, sizeof(key)) != 0) {
  665. p = c;
  666. c = c->next_variant;
  667. }
  668. if (c) {
  669. p->next_variant = c->next_variant;
  670. shader = c;
  671. }
  672. }
  673. if (unlikely(!shader)) {
  674. shader = CALLOC(1, sizeof(struct r600_pipe_shader));
  675. shader->selector = sel;
  676. r = r600_pipe_shader_create(ctx, shader, key);
  677. if (unlikely(r)) {
  678. R600_ERR("Failed to build shader variant (type=%u) %d\n",
  679. sel->type, r);
  680. sel->current = NULL;
  681. FREE(shader);
  682. return r;
  683. }
  684. /* We don't know the value of nr_ps_max_color_exports until we built
  685. * at least one variant, so we may need to recompute the key after
  686. * building first variant. */
  687. if (sel->type == PIPE_SHADER_FRAGMENT &&
  688. sel->num_shaders == 0) {
  689. sel->nr_ps_max_color_exports = shader->shader.nr_ps_max_color_exports;
  690. key = r600_shader_selector_key(ctx, sel);
  691. }
  692. memcpy(&shader->key, &key, sizeof(key));
  693. sel->num_shaders++;
  694. }
  695. if (dirty)
  696. *dirty = true;
  697. shader->next_variant = sel->current;
  698. sel->current = shader;
  699. return 0;
  700. }
  701. static void *r600_create_shader_state(struct pipe_context *ctx,
  702. const struct pipe_shader_state *state,
  703. unsigned pipe_shader_type)
  704. {
  705. struct r600_pipe_shader_selector *sel = CALLOC_STRUCT(r600_pipe_shader_selector);
  706. int i;
  707. sel->type = pipe_shader_type;
  708. sel->tokens = tgsi_dup_tokens(state->tokens);
  709. sel->so = state->stream_output;
  710. tgsi_scan_shader(state->tokens, &sel->info);
  711. switch (pipe_shader_type) {
  712. case PIPE_SHADER_GEOMETRY:
  713. sel->gs_output_prim =
  714. sel->info.properties[TGSI_PROPERTY_GS_OUTPUT_PRIM];
  715. sel->gs_max_out_vertices =
  716. sel->info.properties[TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES];
  717. sel->gs_num_invocations =
  718. sel->info.properties[TGSI_PROPERTY_GS_INVOCATIONS];
  719. break;
  720. case PIPE_SHADER_VERTEX:
  721. case PIPE_SHADER_TESS_CTRL:
  722. sel->lds_patch_outputs_written_mask = 0;
  723. sel->lds_outputs_written_mask = 0;
  724. for (i = 0; i < sel->info.num_outputs; i++) {
  725. unsigned name = sel->info.output_semantic_name[i];
  726. unsigned index = sel->info.output_semantic_index[i];
  727. switch (name) {
  728. case TGSI_SEMANTIC_TESSINNER:
  729. case TGSI_SEMANTIC_TESSOUTER:
  730. case TGSI_SEMANTIC_PATCH:
  731. sel->lds_patch_outputs_written_mask |=
  732. 1llu << r600_get_lds_unique_index(name, index);
  733. break;
  734. default:
  735. sel->lds_outputs_written_mask |=
  736. 1llu << r600_get_lds_unique_index(name, index);
  737. }
  738. }
  739. break;
  740. default:
  741. break;
  742. }
  743. return sel;
  744. }
  745. static void *r600_create_ps_state(struct pipe_context *ctx,
  746. const struct pipe_shader_state *state)
  747. {
  748. return r600_create_shader_state(ctx, state, PIPE_SHADER_FRAGMENT);
  749. }
  750. static void *r600_create_vs_state(struct pipe_context *ctx,
  751. const struct pipe_shader_state *state)
  752. {
  753. return r600_create_shader_state(ctx, state, PIPE_SHADER_VERTEX);
  754. }
  755. static void *r600_create_gs_state(struct pipe_context *ctx,
  756. const struct pipe_shader_state *state)
  757. {
  758. return r600_create_shader_state(ctx, state, PIPE_SHADER_GEOMETRY);
  759. }
  760. static void *r600_create_tcs_state(struct pipe_context *ctx,
  761. const struct pipe_shader_state *state)
  762. {
  763. return r600_create_shader_state(ctx, state, PIPE_SHADER_TESS_CTRL);
  764. }
  765. static void *r600_create_tes_state(struct pipe_context *ctx,
  766. const struct pipe_shader_state *state)
  767. {
  768. return r600_create_shader_state(ctx, state, PIPE_SHADER_TESS_EVAL);
  769. }
  770. static void r600_bind_ps_state(struct pipe_context *ctx, void *state)
  771. {
  772. struct r600_context *rctx = (struct r600_context *)ctx;
  773. if (!state)
  774. state = rctx->dummy_pixel_shader;
  775. rctx->ps_shader = (struct r600_pipe_shader_selector *)state;
  776. }
  777. static struct tgsi_shader_info *r600_get_vs_info(struct r600_context *rctx)
  778. {
  779. if (rctx->gs_shader)
  780. return &rctx->gs_shader->info;
  781. else if (rctx->tes_shader)
  782. return &rctx->tes_shader->info;
  783. else if (rctx->vs_shader)
  784. return &rctx->vs_shader->info;
  785. else
  786. return NULL;
  787. }
  788. static void r600_bind_vs_state(struct pipe_context *ctx, void *state)
  789. {
  790. struct r600_context *rctx = (struct r600_context *)ctx;
  791. if (!state)
  792. return;
  793. rctx->vs_shader = (struct r600_pipe_shader_selector *)state;
  794. r600_update_vs_writes_viewport_index(&rctx->b, r600_get_vs_info(rctx));
  795. rctx->b.streamout.stride_in_dw = rctx->vs_shader->so.stride;
  796. }
  797. static void r600_bind_gs_state(struct pipe_context *ctx, void *state)
  798. {
  799. struct r600_context *rctx = (struct r600_context *)ctx;
  800. rctx->gs_shader = (struct r600_pipe_shader_selector *)state;
  801. r600_update_vs_writes_viewport_index(&rctx->b, r600_get_vs_info(rctx));
  802. if (!state)
  803. return;
  804. rctx->b.streamout.stride_in_dw = rctx->gs_shader->so.stride;
  805. }
  806. static void r600_bind_tcs_state(struct pipe_context *ctx, void *state)
  807. {
  808. struct r600_context *rctx = (struct r600_context *)ctx;
  809. rctx->tcs_shader = (struct r600_pipe_shader_selector *)state;
  810. }
  811. static void r600_bind_tes_state(struct pipe_context *ctx, void *state)
  812. {
  813. struct r600_context *rctx = (struct r600_context *)ctx;
  814. rctx->tes_shader = (struct r600_pipe_shader_selector *)state;
  815. r600_update_vs_writes_viewport_index(&rctx->b, r600_get_vs_info(rctx));
  816. if (!state)
  817. return;
  818. rctx->b.streamout.stride_in_dw = rctx->tes_shader->so.stride;
  819. }
  820. static void r600_delete_shader_selector(struct pipe_context *ctx,
  821. struct r600_pipe_shader_selector *sel)
  822. {
  823. struct r600_pipe_shader *p = sel->current, *c;
  824. while (p) {
  825. c = p->next_variant;
  826. r600_pipe_shader_destroy(ctx, p);
  827. free(p);
  828. p = c;
  829. }
  830. free(sel->tokens);
  831. free(sel);
  832. }
  833. static void r600_delete_ps_state(struct pipe_context *ctx, void *state)
  834. {
  835. struct r600_context *rctx = (struct r600_context *)ctx;
  836. struct r600_pipe_shader_selector *sel = (struct r600_pipe_shader_selector *)state;
  837. if (rctx->ps_shader == sel) {
  838. rctx->ps_shader = NULL;
  839. }
  840. r600_delete_shader_selector(ctx, sel);
  841. }
  842. static void r600_delete_vs_state(struct pipe_context *ctx, void *state)
  843. {
  844. struct r600_context *rctx = (struct r600_context *)ctx;
  845. struct r600_pipe_shader_selector *sel = (struct r600_pipe_shader_selector *)state;
  846. if (rctx->vs_shader == sel) {
  847. rctx->vs_shader = NULL;
  848. }
  849. r600_delete_shader_selector(ctx, sel);
  850. }
  851. static void r600_delete_gs_state(struct pipe_context *ctx, void *state)
  852. {
  853. struct r600_context *rctx = (struct r600_context *)ctx;
  854. struct r600_pipe_shader_selector *sel = (struct r600_pipe_shader_selector *)state;
  855. if (rctx->gs_shader == sel) {
  856. rctx->gs_shader = NULL;
  857. }
  858. r600_delete_shader_selector(ctx, sel);
  859. }
  860. static void r600_delete_tcs_state(struct pipe_context *ctx, void *state)
  861. {
  862. struct r600_context *rctx = (struct r600_context *)ctx;
  863. struct r600_pipe_shader_selector *sel = (struct r600_pipe_shader_selector *)state;
  864. if (rctx->tcs_shader == sel) {
  865. rctx->tcs_shader = NULL;
  866. }
  867. r600_delete_shader_selector(ctx, sel);
  868. }
  869. static void r600_delete_tes_state(struct pipe_context *ctx, void *state)
  870. {
  871. struct r600_context *rctx = (struct r600_context *)ctx;
  872. struct r600_pipe_shader_selector *sel = (struct r600_pipe_shader_selector *)state;
  873. if (rctx->tes_shader == sel) {
  874. rctx->tes_shader = NULL;
  875. }
  876. r600_delete_shader_selector(ctx, sel);
  877. }
  878. void r600_constant_buffers_dirty(struct r600_context *rctx, struct r600_constbuf_state *state)
  879. {
  880. if (state->dirty_mask) {
  881. state->atom.num_dw = rctx->b.chip_class >= EVERGREEN ? util_bitcount(state->dirty_mask)*20
  882. : util_bitcount(state->dirty_mask)*19;
  883. r600_mark_atom_dirty(rctx, &state->atom);
  884. }
  885. }
  886. static void r600_set_constant_buffer(struct pipe_context *ctx, uint shader, uint index,
  887. const struct pipe_constant_buffer *input)
  888. {
  889. struct r600_context *rctx = (struct r600_context *)ctx;
  890. struct r600_constbuf_state *state = &rctx->constbuf_state[shader];
  891. struct pipe_constant_buffer *cb;
  892. const uint8_t *ptr;
  893. /* Note that the state tracker can unbind constant buffers by
  894. * passing NULL here.
  895. */
  896. if (unlikely(!input || (!input->buffer && !input->user_buffer))) {
  897. state->enabled_mask &= ~(1 << index);
  898. state->dirty_mask &= ~(1 << index);
  899. pipe_resource_reference(&state->cb[index].buffer, NULL);
  900. return;
  901. }
  902. cb = &state->cb[index];
  903. cb->buffer_size = input->buffer_size;
  904. ptr = input->user_buffer;
  905. if (ptr) {
  906. /* Upload the user buffer. */
  907. if (R600_BIG_ENDIAN) {
  908. uint32_t *tmpPtr;
  909. unsigned i, size = input->buffer_size;
  910. if (!(tmpPtr = malloc(size))) {
  911. R600_ERR("Failed to allocate BE swap buffer.\n");
  912. return;
  913. }
  914. for (i = 0; i < size / 4; ++i) {
  915. tmpPtr[i] = util_cpu_to_le32(((uint32_t *)ptr)[i]);
  916. }
  917. u_upload_data(rctx->b.uploader, 0, size, 256, tmpPtr, &cb->buffer_offset, &cb->buffer);
  918. free(tmpPtr);
  919. } else {
  920. u_upload_data(rctx->b.uploader, 0, input->buffer_size, 256, ptr, &cb->buffer_offset, &cb->buffer);
  921. }
  922. /* account it in gtt */
  923. rctx->b.gtt += input->buffer_size;
  924. } else {
  925. /* Setup the hw buffer. */
  926. cb->buffer_offset = input->buffer_offset;
  927. pipe_resource_reference(&cb->buffer, input->buffer);
  928. r600_context_add_resource_size(ctx, input->buffer);
  929. }
  930. state->enabled_mask |= 1 << index;
  931. state->dirty_mask |= 1 << index;
  932. r600_constant_buffers_dirty(rctx, state);
  933. }
  934. static void r600_set_sample_mask(struct pipe_context *pipe, unsigned sample_mask)
  935. {
  936. struct r600_context *rctx = (struct r600_context*)pipe;
  937. if (rctx->sample_mask.sample_mask == (uint16_t)sample_mask)
  938. return;
  939. rctx->sample_mask.sample_mask = sample_mask;
  940. r600_mark_atom_dirty(rctx, &rctx->sample_mask.atom);
  941. }
  942. static void r600_update_driver_const_buffers(struct r600_context *rctx)
  943. {
  944. int sh, size;
  945. void *ptr;
  946. struct pipe_constant_buffer cb;
  947. for (sh = 0; sh < PIPE_SHADER_TYPES; sh++) {
  948. struct r600_shader_driver_constants_info *info = &rctx->driver_consts[sh];
  949. if (!info->vs_ucp_dirty &&
  950. !info->texture_const_dirty &&
  951. !info->ps_sample_pos_dirty)
  952. continue;
  953. ptr = info->constants;
  954. size = info->alloc_size;
  955. if (info->vs_ucp_dirty) {
  956. assert(sh == PIPE_SHADER_VERTEX);
  957. if (!size) {
  958. ptr = rctx->clip_state.state.ucp;
  959. size = R600_UCP_SIZE;
  960. } else {
  961. memcpy(ptr, rctx->clip_state.state.ucp, R600_UCP_SIZE);
  962. }
  963. info->vs_ucp_dirty = false;
  964. }
  965. if (info->ps_sample_pos_dirty) {
  966. assert(sh == PIPE_SHADER_FRAGMENT);
  967. if (!size) {
  968. ptr = rctx->sample_positions;
  969. size = R600_UCP_SIZE;
  970. } else {
  971. memcpy(ptr, rctx->sample_positions, R600_UCP_SIZE);
  972. }
  973. info->ps_sample_pos_dirty = false;
  974. }
  975. if (info->texture_const_dirty) {
  976. assert (ptr);
  977. assert (size);
  978. if (sh == PIPE_SHADER_VERTEX)
  979. memcpy(ptr, rctx->clip_state.state.ucp, R600_UCP_SIZE);
  980. if (sh == PIPE_SHADER_FRAGMENT)
  981. memcpy(ptr, rctx->sample_positions, R600_UCP_SIZE);
  982. }
  983. info->texture_const_dirty = false;
  984. cb.buffer = NULL;
  985. cb.user_buffer = ptr;
  986. cb.buffer_offset = 0;
  987. cb.buffer_size = size;
  988. rctx->b.b.set_constant_buffer(&rctx->b.b, sh, R600_BUFFER_INFO_CONST_BUFFER, &cb);
  989. pipe_resource_reference(&cb.buffer, NULL);
  990. }
  991. }
  992. static void *r600_alloc_buf_consts(struct r600_context *rctx, int shader_type,
  993. int array_size, uint32_t *base_offset)
  994. {
  995. struct r600_shader_driver_constants_info *info = &rctx->driver_consts[shader_type];
  996. if (array_size + R600_UCP_SIZE > info->alloc_size) {
  997. info->constants = realloc(info->constants, array_size + R600_UCP_SIZE);
  998. info->alloc_size = array_size + R600_UCP_SIZE;
  999. }
  1000. memset(info->constants + (R600_UCP_SIZE / 4), 0, array_size);
  1001. info->texture_const_dirty = true;
  1002. *base_offset = R600_UCP_SIZE;
  1003. return info->constants;
  1004. }
  1005. /*
  1006. * On r600/700 hw we don't have vertex fetch swizzle, though TBO
  1007. * doesn't require full swizzles it does need masking and setting alpha
  1008. * to one, so we setup a set of 5 constants with the masks + alpha value
  1009. * then in the shader, we AND the 4 components with 0xffffffff or 0,
  1010. * then OR the alpha with the value given here.
  1011. * We use a 6th constant to store the txq buffer size in
  1012. * we use 7th slot for number of cube layers in a cube map array.
  1013. */
  1014. static void r600_setup_buffer_constants(struct r600_context *rctx, int shader_type)
  1015. {
  1016. struct r600_textures_info *samplers = &rctx->samplers[shader_type];
  1017. int bits;
  1018. uint32_t array_size;
  1019. int i, j;
  1020. uint32_t *constants;
  1021. uint32_t base_offset;
  1022. if (!samplers->views.dirty_buffer_constants)
  1023. return;
  1024. samplers->views.dirty_buffer_constants = FALSE;
  1025. bits = util_last_bit(samplers->views.enabled_mask);
  1026. array_size = bits * 8 * sizeof(uint32_t) * 4;
  1027. constants = r600_alloc_buf_consts(rctx, shader_type, array_size, &base_offset);
  1028. for (i = 0; i < bits; i++) {
  1029. if (samplers->views.enabled_mask & (1 << i)) {
  1030. int offset = (base_offset / 4) + i * 8;
  1031. const struct util_format_description *desc;
  1032. desc = util_format_description(samplers->views.views[i]->base.format);
  1033. for (j = 0; j < 4; j++)
  1034. if (j < desc->nr_channels)
  1035. constants[offset+j] = 0xffffffff;
  1036. else
  1037. constants[offset+j] = 0x0;
  1038. if (desc->nr_channels < 4) {
  1039. if (desc->channel[0].pure_integer)
  1040. constants[offset+4] = 1;
  1041. else
  1042. constants[offset+4] = fui(1.0);
  1043. } else
  1044. constants[offset + 4] = 0;
  1045. constants[offset + 5] = samplers->views.views[i]->base.texture->width0 / util_format_get_blocksize(samplers->views.views[i]->base.format);
  1046. constants[offset + 6] = samplers->views.views[i]->base.texture->array_size / 6;
  1047. }
  1048. }
  1049. }
  1050. /* On evergreen we store two values
  1051. * 1. buffer size for TXQ
  1052. * 2. number of cube layers in a cube map array.
  1053. */
  1054. static void eg_setup_buffer_constants(struct r600_context *rctx, int shader_type)
  1055. {
  1056. struct r600_textures_info *samplers = &rctx->samplers[shader_type];
  1057. int bits;
  1058. uint32_t array_size;
  1059. int i;
  1060. uint32_t *constants;
  1061. uint32_t base_offset;
  1062. if (!samplers->views.dirty_buffer_constants)
  1063. return;
  1064. samplers->views.dirty_buffer_constants = FALSE;
  1065. bits = util_last_bit(samplers->views.enabled_mask);
  1066. array_size = bits * 2 * sizeof(uint32_t) * 4;
  1067. constants = r600_alloc_buf_consts(rctx, shader_type, array_size,
  1068. &base_offset);
  1069. for (i = 0; i < bits; i++) {
  1070. if (samplers->views.enabled_mask & (1 << i)) {
  1071. uint32_t offset = (base_offset / 4) + i * 2;
  1072. constants[offset] = samplers->views.views[i]->base.texture->width0 / util_format_get_blocksize(samplers->views.views[i]->base.format);
  1073. constants[offset + 1] = samplers->views.views[i]->base.texture->array_size / 6;
  1074. }
  1075. }
  1076. }
  1077. /* set sample xy locations as array of fragment shader constants */
  1078. void r600_set_sample_locations_constant_buffer(struct r600_context *rctx)
  1079. {
  1080. int i;
  1081. struct pipe_context *ctx = &rctx->b.b;
  1082. assert(rctx->framebuffer.nr_samples < R600_UCP_SIZE);
  1083. assert(rctx->framebuffer.nr_samples <= ARRAY_SIZE(rctx->sample_positions)/4);
  1084. memset(rctx->sample_positions, 0, 4 * 4 * 16);
  1085. for (i = 0; i < rctx->framebuffer.nr_samples; i++) {
  1086. ctx->get_sample_position(ctx, rctx->framebuffer.nr_samples, i, &rctx->sample_positions[4*i]);
  1087. /* Also fill in center-zeroed positions used for interpolateAtSample */
  1088. rctx->sample_positions[4*i + 2] = rctx->sample_positions[4*i + 0] - 0.5f;
  1089. rctx->sample_positions[4*i + 3] = rctx->sample_positions[4*i + 1] - 0.5f;
  1090. }
  1091. rctx->driver_consts[PIPE_SHADER_FRAGMENT].ps_sample_pos_dirty = true;
  1092. }
  1093. static void update_shader_atom(struct pipe_context *ctx,
  1094. struct r600_shader_state *state,
  1095. struct r600_pipe_shader *shader)
  1096. {
  1097. struct r600_context *rctx = (struct r600_context *)ctx;
  1098. state->shader = shader;
  1099. if (shader) {
  1100. state->atom.num_dw = shader->command_buffer.num_dw;
  1101. r600_context_add_resource_size(ctx, (struct pipe_resource *)shader->bo);
  1102. } else {
  1103. state->atom.num_dw = 0;
  1104. }
  1105. r600_mark_atom_dirty(rctx, &state->atom);
  1106. }
  1107. static void update_gs_block_state(struct r600_context *rctx, unsigned enable)
  1108. {
  1109. if (rctx->shader_stages.geom_enable != enable) {
  1110. rctx->shader_stages.geom_enable = enable;
  1111. r600_mark_atom_dirty(rctx, &rctx->shader_stages.atom);
  1112. }
  1113. if (rctx->gs_rings.enable != enable) {
  1114. rctx->gs_rings.enable = enable;
  1115. r600_mark_atom_dirty(rctx, &rctx->gs_rings.atom);
  1116. if (enable && !rctx->gs_rings.esgs_ring.buffer) {
  1117. unsigned size = 0x1C000;
  1118. rctx->gs_rings.esgs_ring.buffer =
  1119. pipe_buffer_create(rctx->b.b.screen, PIPE_BIND_CUSTOM,
  1120. PIPE_USAGE_DEFAULT, size);
  1121. rctx->gs_rings.esgs_ring.buffer_size = size;
  1122. size = 0x4000000;
  1123. rctx->gs_rings.gsvs_ring.buffer =
  1124. pipe_buffer_create(rctx->b.b.screen, PIPE_BIND_CUSTOM,
  1125. PIPE_USAGE_DEFAULT, size);
  1126. rctx->gs_rings.gsvs_ring.buffer_size = size;
  1127. }
  1128. if (enable) {
  1129. r600_set_constant_buffer(&rctx->b.b, PIPE_SHADER_GEOMETRY,
  1130. R600_GS_RING_CONST_BUFFER, &rctx->gs_rings.esgs_ring);
  1131. if (rctx->tes_shader) {
  1132. r600_set_constant_buffer(&rctx->b.b, PIPE_SHADER_TESS_EVAL,
  1133. R600_GS_RING_CONST_BUFFER, &rctx->gs_rings.gsvs_ring);
  1134. } else {
  1135. r600_set_constant_buffer(&rctx->b.b, PIPE_SHADER_VERTEX,
  1136. R600_GS_RING_CONST_BUFFER, &rctx->gs_rings.gsvs_ring);
  1137. }
  1138. } else {
  1139. r600_set_constant_buffer(&rctx->b.b, PIPE_SHADER_GEOMETRY,
  1140. R600_GS_RING_CONST_BUFFER, NULL);
  1141. r600_set_constant_buffer(&rctx->b.b, PIPE_SHADER_VERTEX,
  1142. R600_GS_RING_CONST_BUFFER, NULL);
  1143. r600_set_constant_buffer(&rctx->b.b, PIPE_SHADER_TESS_EVAL,
  1144. R600_GS_RING_CONST_BUFFER, NULL);
  1145. }
  1146. }
  1147. }
  1148. static void r600_update_clip_state(struct r600_context *rctx,
  1149. struct r600_pipe_shader *current)
  1150. {
  1151. if (current->pa_cl_vs_out_cntl != rctx->clip_misc_state.pa_cl_vs_out_cntl ||
  1152. current->shader.clip_dist_write != rctx->clip_misc_state.clip_dist_write ||
  1153. current->shader.vs_position_window_space != rctx->clip_misc_state.clip_disable ||
  1154. current->shader.vs_out_viewport != rctx->clip_misc_state.vs_out_viewport) {
  1155. rctx->clip_misc_state.pa_cl_vs_out_cntl = current->pa_cl_vs_out_cntl;
  1156. rctx->clip_misc_state.clip_dist_write = current->shader.clip_dist_write;
  1157. rctx->clip_misc_state.clip_disable = current->shader.vs_position_window_space;
  1158. rctx->clip_misc_state.vs_out_viewport = current->shader.vs_out_viewport;
  1159. r600_mark_atom_dirty(rctx, &rctx->clip_misc_state.atom);
  1160. }
  1161. }
  1162. static void r600_generate_fixed_func_tcs(struct r600_context *rctx)
  1163. {
  1164. struct ureg_src const0, const1;
  1165. struct ureg_dst tessouter, tessinner;
  1166. struct ureg_program *ureg = ureg_create(PIPE_SHADER_TESS_CTRL);
  1167. if (!ureg)
  1168. return; /* if we get here, we're screwed */
  1169. assert(!rctx->fixed_func_tcs_shader);
  1170. ureg_DECL_constant2D(ureg, 0, 3, R600_LDS_INFO_CONST_BUFFER);
  1171. const0 = ureg_src_dimension(ureg_src_register(TGSI_FILE_CONSTANT, 2),
  1172. R600_LDS_INFO_CONST_BUFFER);
  1173. const1 = ureg_src_dimension(ureg_src_register(TGSI_FILE_CONSTANT, 3),
  1174. R600_LDS_INFO_CONST_BUFFER);
  1175. tessouter = ureg_DECL_output(ureg, TGSI_SEMANTIC_TESSOUTER, 0);
  1176. tessinner = ureg_DECL_output(ureg, TGSI_SEMANTIC_TESSINNER, 0);
  1177. ureg_MOV(ureg, tessouter, const0);
  1178. ureg_MOV(ureg, tessinner, const1);
  1179. ureg_END(ureg);
  1180. rctx->fixed_func_tcs_shader =
  1181. ureg_create_shader_and_destroy(ureg, &rctx->b.b);
  1182. }
  1183. #define SELECT_SHADER_OR_FAIL(x) do { \
  1184. r600_shader_select(ctx, rctx->x##_shader, &x##_dirty); \
  1185. if (unlikely(!rctx->x##_shader->current)) \
  1186. return false; \
  1187. } while(0)
  1188. #define UPDATE_SHADER(hw, sw) do { \
  1189. if (sw##_dirty || (rctx->hw_shader_stages[(hw)].shader != rctx->sw##_shader->current)) \
  1190. update_shader_atom(ctx, &rctx->hw_shader_stages[(hw)], rctx->sw##_shader->current); \
  1191. } while(0)
  1192. #define UPDATE_SHADER_CLIP(hw, sw) do { \
  1193. if (sw##_dirty || (rctx->hw_shader_stages[(hw)].shader != rctx->sw##_shader->current)) { \
  1194. update_shader_atom(ctx, &rctx->hw_shader_stages[(hw)], rctx->sw##_shader->current); \
  1195. clip_so_current = rctx->sw##_shader->current; \
  1196. } \
  1197. } while(0)
  1198. #define UPDATE_SHADER_GS(hw, hw2, sw) do { \
  1199. if (sw##_dirty || (rctx->hw_shader_stages[(hw)].shader != rctx->sw##_shader->current)) { \
  1200. update_shader_atom(ctx, &rctx->hw_shader_stages[(hw)], rctx->sw##_shader->current); \
  1201. update_shader_atom(ctx, &rctx->hw_shader_stages[(hw2)], rctx->sw##_shader->current->gs_copy_shader); \
  1202. clip_so_current = rctx->sw##_shader->current->gs_copy_shader; \
  1203. } \
  1204. } while(0)
  1205. #define SET_NULL_SHADER(hw) do { \
  1206. if (rctx->hw_shader_stages[(hw)].shader) \
  1207. update_shader_atom(ctx, &rctx->hw_shader_stages[(hw)], NULL); \
  1208. } while (0)
  1209. static bool r600_update_derived_state(struct r600_context *rctx)
  1210. {
  1211. struct pipe_context * ctx = (struct pipe_context*)rctx;
  1212. bool ps_dirty = false, vs_dirty = false, gs_dirty = false;
  1213. bool tcs_dirty = false, tes_dirty = false, fixed_func_tcs_dirty = false;
  1214. bool blend_disable;
  1215. bool need_buf_const;
  1216. struct r600_pipe_shader *clip_so_current = NULL;
  1217. if (!rctx->blitter->running) {
  1218. unsigned i;
  1219. unsigned counter;
  1220. counter = p_atomic_read(&rctx->screen->b.compressed_colortex_counter);
  1221. if (counter != rctx->b.last_compressed_colortex_counter) {
  1222. rctx->b.last_compressed_colortex_counter = counter;
  1223. for (i = 0; i < PIPE_SHADER_TYPES; ++i) {
  1224. r600_update_compressed_colortex_mask(&rctx->samplers[i].views);
  1225. }
  1226. }
  1227. /* Decompress textures if needed. */
  1228. for (i = 0; i < PIPE_SHADER_TYPES; i++) {
  1229. struct r600_samplerview_state *views = &rctx->samplers[i].views;
  1230. if (views->compressed_depthtex_mask) {
  1231. r600_decompress_depth_textures(rctx, views);
  1232. }
  1233. if (views->compressed_colortex_mask) {
  1234. r600_decompress_color_textures(rctx, views);
  1235. }
  1236. }
  1237. }
  1238. SELECT_SHADER_OR_FAIL(ps);
  1239. r600_mark_atom_dirty(rctx, &rctx->shader_stages.atom);
  1240. update_gs_block_state(rctx, rctx->gs_shader != NULL);
  1241. if (rctx->gs_shader)
  1242. SELECT_SHADER_OR_FAIL(gs);
  1243. /* Hull Shader */
  1244. if (rctx->tcs_shader) {
  1245. SELECT_SHADER_OR_FAIL(tcs);
  1246. UPDATE_SHADER(EG_HW_STAGE_HS, tcs);
  1247. } else if (rctx->tes_shader) {
  1248. if (!rctx->fixed_func_tcs_shader) {
  1249. r600_generate_fixed_func_tcs(rctx);
  1250. if (!rctx->fixed_func_tcs_shader)
  1251. return false;
  1252. }
  1253. SELECT_SHADER_OR_FAIL(fixed_func_tcs);
  1254. UPDATE_SHADER(EG_HW_STAGE_HS, fixed_func_tcs);
  1255. } else
  1256. SET_NULL_SHADER(EG_HW_STAGE_HS);
  1257. if (rctx->tes_shader) {
  1258. SELECT_SHADER_OR_FAIL(tes);
  1259. }
  1260. SELECT_SHADER_OR_FAIL(vs);
  1261. if (rctx->gs_shader) {
  1262. if (!rctx->shader_stages.geom_enable) {
  1263. rctx->shader_stages.geom_enable = true;
  1264. r600_mark_atom_dirty(rctx, &rctx->shader_stages.atom);
  1265. }
  1266. /* gs_shader provides GS and VS (copy shader) */
  1267. UPDATE_SHADER_GS(R600_HW_STAGE_GS, R600_HW_STAGE_VS, gs);
  1268. /* vs_shader is used as ES */
  1269. if (rctx->tes_shader) {
  1270. /* VS goes to LS, TES goes to ES */
  1271. UPDATE_SHADER(R600_HW_STAGE_ES, tes);
  1272. UPDATE_SHADER(EG_HW_STAGE_LS, vs);
  1273. } else {
  1274. /* vs_shader is used as ES */
  1275. UPDATE_SHADER(R600_HW_STAGE_ES, vs);
  1276. SET_NULL_SHADER(EG_HW_STAGE_LS);
  1277. }
  1278. } else {
  1279. if (unlikely(rctx->hw_shader_stages[R600_HW_STAGE_GS].shader)) {
  1280. SET_NULL_SHADER(R600_HW_STAGE_GS);
  1281. SET_NULL_SHADER(R600_HW_STAGE_ES);
  1282. rctx->shader_stages.geom_enable = false;
  1283. r600_mark_atom_dirty(rctx, &rctx->shader_stages.atom);
  1284. }
  1285. if (rctx->tes_shader) {
  1286. /* if TES is loaded and no geometry, TES runs on hw VS, VS runs on hw LS */
  1287. UPDATE_SHADER_CLIP(R600_HW_STAGE_VS, tes);
  1288. UPDATE_SHADER(EG_HW_STAGE_LS, vs);
  1289. } else {
  1290. SET_NULL_SHADER(EG_HW_STAGE_LS);
  1291. UPDATE_SHADER_CLIP(R600_HW_STAGE_VS, vs);
  1292. }
  1293. }
  1294. /* Update clip misc state. */
  1295. if (clip_so_current) {
  1296. r600_update_clip_state(rctx, clip_so_current);
  1297. rctx->b.streamout.enabled_stream_buffers_mask = clip_so_current->enabled_stream_buffers_mask;
  1298. }
  1299. if (unlikely(ps_dirty || rctx->hw_shader_stages[R600_HW_STAGE_PS].shader != rctx->ps_shader->current ||
  1300. rctx->rasterizer->sprite_coord_enable != rctx->ps_shader->current->sprite_coord_enable ||
  1301. rctx->rasterizer->flatshade != rctx->ps_shader->current->flatshade)) {
  1302. if (rctx->cb_misc_state.nr_ps_color_outputs != rctx->ps_shader->current->nr_ps_color_outputs) {
  1303. rctx->cb_misc_state.nr_ps_color_outputs = rctx->ps_shader->current->nr_ps_color_outputs;
  1304. r600_mark_atom_dirty(rctx, &rctx->cb_misc_state.atom);
  1305. }
  1306. if (rctx->b.chip_class <= R700) {
  1307. bool multiwrite = rctx->ps_shader->current->shader.fs_write_all;
  1308. if (rctx->cb_misc_state.multiwrite != multiwrite) {
  1309. rctx->cb_misc_state.multiwrite = multiwrite;
  1310. r600_mark_atom_dirty(rctx, &rctx->cb_misc_state.atom);
  1311. }
  1312. }
  1313. if (unlikely(!ps_dirty && rctx->ps_shader && rctx->rasterizer &&
  1314. ((rctx->rasterizer->sprite_coord_enable != rctx->ps_shader->current->sprite_coord_enable) ||
  1315. (rctx->rasterizer->flatshade != rctx->ps_shader->current->flatshade)))) {
  1316. if (rctx->b.chip_class >= EVERGREEN)
  1317. evergreen_update_ps_state(ctx, rctx->ps_shader->current);
  1318. else
  1319. r600_update_ps_state(ctx, rctx->ps_shader->current);
  1320. }
  1321. r600_mark_atom_dirty(rctx, &rctx->shader_stages.atom);
  1322. }
  1323. UPDATE_SHADER(R600_HW_STAGE_PS, ps);
  1324. if (rctx->b.chip_class >= EVERGREEN) {
  1325. evergreen_update_db_shader_control(rctx);
  1326. } else {
  1327. r600_update_db_shader_control(rctx);
  1328. }
  1329. /* on R600 we stuff masks + txq info into one constant buffer */
  1330. /* on evergreen we only need a txq info one */
  1331. if (rctx->ps_shader) {
  1332. need_buf_const = rctx->ps_shader->current->shader.uses_tex_buffers || rctx->ps_shader->current->shader.has_txq_cube_array_z_comp;
  1333. if (need_buf_const) {
  1334. if (rctx->b.chip_class < EVERGREEN)
  1335. r600_setup_buffer_constants(rctx, PIPE_SHADER_FRAGMENT);
  1336. else
  1337. eg_setup_buffer_constants(rctx, PIPE_SHADER_FRAGMENT);
  1338. }
  1339. }
  1340. if (rctx->vs_shader) {
  1341. need_buf_const = rctx->vs_shader->current->shader.uses_tex_buffers || rctx->vs_shader->current->shader.has_txq_cube_array_z_comp;
  1342. if (need_buf_const) {
  1343. if (rctx->b.chip_class < EVERGREEN)
  1344. r600_setup_buffer_constants(rctx, PIPE_SHADER_VERTEX);
  1345. else
  1346. eg_setup_buffer_constants(rctx, PIPE_SHADER_VERTEX);
  1347. }
  1348. }
  1349. if (rctx->gs_shader) {
  1350. need_buf_const = rctx->gs_shader->current->shader.uses_tex_buffers || rctx->gs_shader->current->shader.has_txq_cube_array_z_comp;
  1351. if (need_buf_const) {
  1352. if (rctx->b.chip_class < EVERGREEN)
  1353. r600_setup_buffer_constants(rctx, PIPE_SHADER_GEOMETRY);
  1354. else
  1355. eg_setup_buffer_constants(rctx, PIPE_SHADER_GEOMETRY);
  1356. }
  1357. }
  1358. r600_update_driver_const_buffers(rctx);
  1359. if (rctx->b.chip_class < EVERGREEN && rctx->ps_shader && rctx->vs_shader) {
  1360. if (!r600_adjust_gprs(rctx)) {
  1361. /* discard rendering */
  1362. return false;
  1363. }
  1364. }
  1365. if (rctx->b.chip_class == EVERGREEN) {
  1366. if (!evergreen_adjust_gprs(rctx)) {
  1367. /* discard rendering */
  1368. return false;
  1369. }
  1370. }
  1371. blend_disable = (rctx->dual_src_blend &&
  1372. rctx->ps_shader->current->nr_ps_color_outputs < 2);
  1373. if (blend_disable != rctx->force_blend_disable) {
  1374. rctx->force_blend_disable = blend_disable;
  1375. r600_bind_blend_state_internal(rctx,
  1376. rctx->blend_state.cso,
  1377. blend_disable);
  1378. }
  1379. return true;
  1380. }
  1381. void r600_emit_clip_misc_state(struct r600_context *rctx, struct r600_atom *atom)
  1382. {
  1383. struct radeon_winsys_cs *cs = rctx->b.gfx.cs;
  1384. struct r600_clip_misc_state *state = &rctx->clip_misc_state;
  1385. radeon_set_context_reg(cs, R_028810_PA_CL_CLIP_CNTL,
  1386. state->pa_cl_clip_cntl |
  1387. (state->clip_dist_write ? 0 : state->clip_plane_enable & 0x3F) |
  1388. S_028810_CLIP_DISABLE(state->clip_disable));
  1389. radeon_set_context_reg(cs, R_02881C_PA_CL_VS_OUT_CNTL,
  1390. state->pa_cl_vs_out_cntl |
  1391. (state->clip_plane_enable & state->clip_dist_write));
  1392. /* reuse needs to be set off if we write oViewport */
  1393. if (rctx->b.chip_class >= EVERGREEN)
  1394. radeon_set_context_reg(cs, R_028AB4_VGT_REUSE_OFF,
  1395. S_028AB4_REUSE_OFF(state->vs_out_viewport));
  1396. }
  1397. static void r600_draw_vbo(struct pipe_context *ctx, const struct pipe_draw_info *dinfo)
  1398. {
  1399. struct r600_context *rctx = (struct r600_context *)ctx;
  1400. struct pipe_draw_info info = *dinfo;
  1401. struct pipe_index_buffer ib = {};
  1402. struct radeon_winsys_cs *cs = rctx->b.gfx.cs;
  1403. bool render_cond_bit = rctx->b.render_cond && !rctx->b.render_cond_force_off;
  1404. uint64_t mask;
  1405. unsigned num_patches, dirty_fb_counter;
  1406. if (!info.indirect && !info.count && (info.indexed || !info.count_from_stream_output)) {
  1407. return;
  1408. }
  1409. if (!rctx->vs_shader || !rctx->ps_shader) {
  1410. assert(0);
  1411. return;
  1412. }
  1413. /* make sure that the gfx ring is only one active */
  1414. if (radeon_emitted(rctx->b.dma.cs, 0)) {
  1415. rctx->b.dma.flush(rctx, RADEON_FLUSH_ASYNC, NULL);
  1416. }
  1417. /* Re-emit the framebuffer state if needed. */
  1418. dirty_fb_counter = p_atomic_read(&rctx->b.screen->dirty_fb_counter);
  1419. if (dirty_fb_counter != rctx->b.last_dirty_fb_counter) {
  1420. rctx->b.last_dirty_fb_counter = dirty_fb_counter;
  1421. r600_mark_atom_dirty(rctx, &rctx->framebuffer.atom);
  1422. }
  1423. if (!r600_update_derived_state(rctx)) {
  1424. /* useless to render because current rendering command
  1425. * can't be achieved
  1426. */
  1427. return;
  1428. }
  1429. if (info.indexed) {
  1430. /* Initialize the index buffer struct. */
  1431. pipe_resource_reference(&ib.buffer, rctx->index_buffer.buffer);
  1432. ib.user_buffer = rctx->index_buffer.user_buffer;
  1433. ib.index_size = rctx->index_buffer.index_size;
  1434. ib.offset = rctx->index_buffer.offset;
  1435. if (!info.indirect) {
  1436. ib.offset += info.start * ib.index_size;
  1437. }
  1438. /* Translate 8-bit indices to 16-bit. */
  1439. if (unlikely(ib.index_size == 1)) {
  1440. struct pipe_resource *out_buffer = NULL;
  1441. unsigned out_offset;
  1442. void *ptr;
  1443. unsigned start, count;
  1444. if (likely(!info.indirect)) {
  1445. start = 0;
  1446. count = info.count;
  1447. }
  1448. else {
  1449. /* Have to get start/count from indirect buffer, slow path ahead... */
  1450. struct r600_resource *indirect_resource = (struct r600_resource *)info.indirect;
  1451. unsigned *data = r600_buffer_map_sync_with_rings(&rctx->b, indirect_resource,
  1452. PIPE_TRANSFER_READ);
  1453. if (data) {
  1454. data += info.indirect_offset / sizeof(unsigned);
  1455. start = data[2] * ib.index_size;
  1456. count = data[0];
  1457. }
  1458. else {
  1459. start = 0;
  1460. count = 0;
  1461. }
  1462. }
  1463. u_upload_alloc(rctx->b.uploader, start, count * 2, 256,
  1464. &out_offset, &out_buffer, &ptr);
  1465. util_shorten_ubyte_elts_to_userptr(
  1466. &rctx->b.b, &ib, 0, ib.offset + start, count, ptr);
  1467. pipe_resource_reference(&ib.buffer, NULL);
  1468. ib.user_buffer = NULL;
  1469. ib.buffer = out_buffer;
  1470. ib.offset = out_offset;
  1471. ib.index_size = 2;
  1472. }
  1473. /* Upload the index buffer.
  1474. * The upload is skipped for small index counts on little-endian machines
  1475. * and the indices are emitted via PKT3_DRAW_INDEX_IMMD.
  1476. * Indirect draws never use immediate indices.
  1477. * Note: Instanced rendering in combination with immediate indices hangs. */
  1478. if (ib.user_buffer && (R600_BIG_ENDIAN || info.indirect ||
  1479. info.instance_count > 1 ||
  1480. info.count*ib.index_size > 20)) {
  1481. u_upload_data(rctx->b.uploader, 0, info.count * ib.index_size, 256,
  1482. ib.user_buffer, &ib.offset, &ib.buffer);
  1483. ib.user_buffer = NULL;
  1484. }
  1485. } else {
  1486. info.index_bias = info.start;
  1487. }
  1488. /* Set the index offset and primitive restart. */
  1489. if (rctx->vgt_state.vgt_multi_prim_ib_reset_en != info.primitive_restart ||
  1490. rctx->vgt_state.vgt_multi_prim_ib_reset_indx != info.restart_index ||
  1491. rctx->vgt_state.vgt_indx_offset != info.index_bias ||
  1492. (rctx->vgt_state.last_draw_was_indirect && !info.indirect)) {
  1493. rctx->vgt_state.vgt_multi_prim_ib_reset_en = info.primitive_restart;
  1494. rctx->vgt_state.vgt_multi_prim_ib_reset_indx = info.restart_index;
  1495. rctx->vgt_state.vgt_indx_offset = info.index_bias;
  1496. r600_mark_atom_dirty(rctx, &rctx->vgt_state.atom);
  1497. }
  1498. /* Workaround for hardware deadlock on certain R600 ASICs: write into a CB register. */
  1499. if (rctx->b.chip_class == R600) {
  1500. rctx->b.flags |= R600_CONTEXT_PS_PARTIAL_FLUSH;
  1501. r600_mark_atom_dirty(rctx, &rctx->cb_misc_state.atom);
  1502. }
  1503. if (rctx->b.chip_class >= EVERGREEN)
  1504. evergreen_setup_tess_constants(rctx, &info, &num_patches);
  1505. /* Emit states. */
  1506. r600_need_cs_space(rctx, ib.user_buffer ? 5 : 0, TRUE);
  1507. r600_flush_emit(rctx);
  1508. mask = rctx->dirty_atoms;
  1509. while (mask != 0) {
  1510. r600_emit_atom(rctx, rctx->atoms[u_bit_scan64(&mask)]);
  1511. }
  1512. if (rctx->b.chip_class == CAYMAN) {
  1513. /* Copied from radeonsi. */
  1514. unsigned primgroup_size = 128; /* recommended without a GS */
  1515. bool ia_switch_on_eop = false;
  1516. bool partial_vs_wave = false;
  1517. if (rctx->gs_shader)
  1518. primgroup_size = 64; /* recommended with a GS */
  1519. if ((rctx->rasterizer && rctx->rasterizer->pa_sc_line_stipple) ||
  1520. (rctx->b.screen->debug_flags & DBG_SWITCH_ON_EOP)) {
  1521. ia_switch_on_eop = true;
  1522. }
  1523. if (r600_get_strmout_en(&rctx->b))
  1524. partial_vs_wave = true;
  1525. radeon_set_context_reg(cs, CM_R_028AA8_IA_MULTI_VGT_PARAM,
  1526. S_028AA8_SWITCH_ON_EOP(ia_switch_on_eop) |
  1527. S_028AA8_PARTIAL_VS_WAVE_ON(partial_vs_wave) |
  1528. S_028AA8_PRIMGROUP_SIZE(primgroup_size - 1));
  1529. }
  1530. if (rctx->b.chip_class >= EVERGREEN) {
  1531. uint32_t ls_hs_config = evergreen_get_ls_hs_config(rctx, &info,
  1532. num_patches);
  1533. evergreen_set_ls_hs_config(rctx, cs, ls_hs_config);
  1534. evergreen_set_lds_alloc(rctx, cs, rctx->lds_alloc);
  1535. }
  1536. /* On R6xx, CULL_FRONT=1 culls all points, lines, and rectangles,
  1537. * even though it should have no effect on those. */
  1538. if (rctx->b.chip_class == R600 && rctx->rasterizer) {
  1539. unsigned su_sc_mode_cntl = rctx->rasterizer->pa_su_sc_mode_cntl;
  1540. unsigned prim = info.mode;
  1541. if (rctx->gs_shader) {
  1542. prim = rctx->gs_shader->gs_output_prim;
  1543. }
  1544. prim = r600_conv_prim_to_gs_out(prim); /* decrease the number of types to 3 */
  1545. if (prim == V_028A6C_OUTPRIM_TYPE_POINTLIST ||
  1546. prim == V_028A6C_OUTPRIM_TYPE_LINESTRIP ||
  1547. info.mode == R600_PRIM_RECTANGLE_LIST) {
  1548. su_sc_mode_cntl &= C_028814_CULL_FRONT;
  1549. }
  1550. radeon_set_context_reg(cs, R_028814_PA_SU_SC_MODE_CNTL, su_sc_mode_cntl);
  1551. }
  1552. /* Update start instance. */
  1553. if (!info.indirect && rctx->last_start_instance != info.start_instance) {
  1554. radeon_set_ctl_const(cs, R_03CFF4_SQ_VTX_START_INST_LOC, info.start_instance);
  1555. rctx->last_start_instance = info.start_instance;
  1556. }
  1557. /* Update the primitive type. */
  1558. if (rctx->last_primitive_type != info.mode) {
  1559. unsigned ls_mask = 0;
  1560. if (info.mode == PIPE_PRIM_LINES)
  1561. ls_mask = 1;
  1562. else if (info.mode == PIPE_PRIM_LINE_STRIP ||
  1563. info.mode == PIPE_PRIM_LINE_LOOP)
  1564. ls_mask = 2;
  1565. radeon_set_context_reg(cs, R_028A0C_PA_SC_LINE_STIPPLE,
  1566. S_028A0C_AUTO_RESET_CNTL(ls_mask) |
  1567. (rctx->rasterizer ? rctx->rasterizer->pa_sc_line_stipple : 0));
  1568. radeon_set_config_reg(cs, R_008958_VGT_PRIMITIVE_TYPE,
  1569. r600_conv_pipe_prim(info.mode));
  1570. rctx->last_primitive_type = info.mode;
  1571. }
  1572. /* Draw packets. */
  1573. if (!info.indirect) {
  1574. radeon_emit(cs, PKT3(PKT3_NUM_INSTANCES, 0, 0));
  1575. radeon_emit(cs, info.instance_count);
  1576. }
  1577. if (unlikely(info.indirect)) {
  1578. uint64_t va = r600_resource(info.indirect)->gpu_address;
  1579. assert(rctx->b.chip_class >= EVERGREEN);
  1580. // Invalidate so non-indirect draw calls reset this state
  1581. rctx->vgt_state.last_draw_was_indirect = true;
  1582. rctx->last_start_instance = -1;
  1583. radeon_emit(cs, PKT3(EG_PKT3_SET_BASE, 2, 0));
  1584. radeon_emit(cs, EG_DRAW_INDEX_INDIRECT_PATCH_TABLE_BASE);
  1585. radeon_emit(cs, va);
  1586. radeon_emit(cs, (va >> 32UL) & 0xFF);
  1587. radeon_emit(cs, PKT3(PKT3_NOP, 0, 0));
  1588. radeon_emit(cs, radeon_add_to_buffer_list(&rctx->b, &rctx->b.gfx,
  1589. (struct r600_resource*)info.indirect,
  1590. RADEON_USAGE_READ,
  1591. RADEON_PRIO_DRAW_INDIRECT));
  1592. }
  1593. if (info.indexed) {
  1594. radeon_emit(cs, PKT3(PKT3_INDEX_TYPE, 0, 0));
  1595. radeon_emit(cs, ib.index_size == 4 ?
  1596. (VGT_INDEX_32 | (R600_BIG_ENDIAN ? VGT_DMA_SWAP_32_BIT : 0)) :
  1597. (VGT_INDEX_16 | (R600_BIG_ENDIAN ? VGT_DMA_SWAP_16_BIT : 0)));
  1598. if (ib.user_buffer) {
  1599. unsigned size_bytes = info.count*ib.index_size;
  1600. unsigned size_dw = align(size_bytes, 4) / 4;
  1601. radeon_emit(cs, PKT3(PKT3_DRAW_INDEX_IMMD, 1 + size_dw, render_cond_bit));
  1602. radeon_emit(cs, info.count);
  1603. radeon_emit(cs, V_0287F0_DI_SRC_SEL_IMMEDIATE);
  1604. radeon_emit_array(cs, ib.user_buffer, size_dw);
  1605. } else {
  1606. uint64_t va = r600_resource(ib.buffer)->gpu_address + ib.offset;
  1607. if (likely(!info.indirect)) {
  1608. radeon_emit(cs, PKT3(PKT3_DRAW_INDEX, 3, render_cond_bit));
  1609. radeon_emit(cs, va);
  1610. radeon_emit(cs, (va >> 32UL) & 0xFF);
  1611. radeon_emit(cs, info.count);
  1612. radeon_emit(cs, V_0287F0_DI_SRC_SEL_DMA);
  1613. radeon_emit(cs, PKT3(PKT3_NOP, 0, 0));
  1614. radeon_emit(cs, radeon_add_to_buffer_list(&rctx->b, &rctx->b.gfx,
  1615. (struct r600_resource*)ib.buffer,
  1616. RADEON_USAGE_READ,
  1617. RADEON_PRIO_INDEX_BUFFER));
  1618. }
  1619. else {
  1620. uint32_t max_size = (ib.buffer->width0 - ib.offset) / ib.index_size;
  1621. radeon_emit(cs, PKT3(EG_PKT3_INDEX_BASE, 1, 0));
  1622. radeon_emit(cs, va);
  1623. radeon_emit(cs, (va >> 32UL) & 0xFF);
  1624. radeon_emit(cs, PKT3(PKT3_NOP, 0, 0));
  1625. radeon_emit(cs, radeon_add_to_buffer_list(&rctx->b, &rctx->b.gfx,
  1626. (struct r600_resource*)ib.buffer,
  1627. RADEON_USAGE_READ,
  1628. RADEON_PRIO_INDEX_BUFFER));
  1629. radeon_emit(cs, PKT3(EG_PKT3_INDEX_BUFFER_SIZE, 0, 0));
  1630. radeon_emit(cs, max_size);
  1631. radeon_emit(cs, PKT3(EG_PKT3_DRAW_INDEX_INDIRECT, 1, render_cond_bit));
  1632. radeon_emit(cs, info.indirect_offset);
  1633. radeon_emit(cs, V_0287F0_DI_SRC_SEL_DMA);
  1634. }
  1635. }
  1636. } else {
  1637. if (unlikely(info.count_from_stream_output)) {
  1638. struct r600_so_target *t = (struct r600_so_target*)info.count_from_stream_output;
  1639. uint64_t va = t->buf_filled_size->gpu_address + t->buf_filled_size_offset;
  1640. radeon_set_context_reg(cs, R_028B30_VGT_STRMOUT_DRAW_OPAQUE_VERTEX_STRIDE, t->stride_in_dw);
  1641. radeon_emit(cs, PKT3(PKT3_COPY_DW, 4, 0));
  1642. radeon_emit(cs, COPY_DW_SRC_IS_MEM | COPY_DW_DST_IS_REG);
  1643. radeon_emit(cs, va & 0xFFFFFFFFUL); /* src address lo */
  1644. radeon_emit(cs, (va >> 32UL) & 0xFFUL); /* src address hi */
  1645. radeon_emit(cs, R_028B2C_VGT_STRMOUT_DRAW_OPAQUE_BUFFER_FILLED_SIZE >> 2); /* dst register */
  1646. radeon_emit(cs, 0); /* unused */
  1647. radeon_emit(cs, PKT3(PKT3_NOP, 0, 0));
  1648. radeon_emit(cs, radeon_add_to_buffer_list(&rctx->b, &rctx->b.gfx,
  1649. t->buf_filled_size, RADEON_USAGE_READ,
  1650. RADEON_PRIO_SO_FILLED_SIZE));
  1651. }
  1652. if (likely(!info.indirect)) {
  1653. radeon_emit(cs, PKT3(PKT3_DRAW_INDEX_AUTO, 1, render_cond_bit));
  1654. radeon_emit(cs, info.count);
  1655. }
  1656. else {
  1657. radeon_emit(cs, PKT3(EG_PKT3_DRAW_INDIRECT, 1, render_cond_bit));
  1658. radeon_emit(cs, info.indirect_offset);
  1659. }
  1660. radeon_emit(cs, V_0287F0_DI_SRC_SEL_AUTO_INDEX |
  1661. (info.count_from_stream_output ? S_0287F0_USE_OPAQUE(1) : 0));
  1662. }
  1663. /* SMX returns CONTEXT_DONE too early workaround */
  1664. if (rctx->b.family == CHIP_R600 ||
  1665. rctx->b.family == CHIP_RV610 ||
  1666. rctx->b.family == CHIP_RV630 ||
  1667. rctx->b.family == CHIP_RV635) {
  1668. /* if we have gs shader or streamout
  1669. we need to do a wait idle after every draw */
  1670. if (rctx->gs_shader || r600_get_strmout_en(&rctx->b)) {
  1671. radeon_set_config_reg(cs, R_008040_WAIT_UNTIL, S_008040_WAIT_3D_IDLE(1));
  1672. }
  1673. }
  1674. /* ES ring rolling over at EOP - workaround */
  1675. if (rctx->b.chip_class == R600) {
  1676. radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
  1677. radeon_emit(cs, EVENT_TYPE(EVENT_TYPE_SQ_NON_EVENT));
  1678. }
  1679. /* Set the depth buffer as dirty. */
  1680. if (rctx->framebuffer.state.zsbuf) {
  1681. struct pipe_surface *surf = rctx->framebuffer.state.zsbuf;
  1682. struct r600_texture *rtex = (struct r600_texture *)surf->texture;
  1683. rtex->dirty_level_mask |= 1 << surf->u.tex.level;
  1684. if (rtex->surface.flags & RADEON_SURF_SBUFFER)
  1685. rtex->stencil_dirty_level_mask |= 1 << surf->u.tex.level;
  1686. }
  1687. if (rctx->framebuffer.compressed_cb_mask) {
  1688. struct pipe_surface *surf;
  1689. struct r600_texture *rtex;
  1690. unsigned mask = rctx->framebuffer.compressed_cb_mask;
  1691. do {
  1692. unsigned i = u_bit_scan(&mask);
  1693. surf = rctx->framebuffer.state.cbufs[i];
  1694. rtex = (struct r600_texture*)surf->texture;
  1695. rtex->dirty_level_mask |= 1 << surf->u.tex.level;
  1696. } while (mask);
  1697. }
  1698. pipe_resource_reference(&ib.buffer, NULL);
  1699. rctx->b.num_draw_calls++;
  1700. }
  1701. uint32_t r600_translate_stencil_op(int s_op)
  1702. {
  1703. switch (s_op) {
  1704. case PIPE_STENCIL_OP_KEEP:
  1705. return V_028800_STENCIL_KEEP;
  1706. case PIPE_STENCIL_OP_ZERO:
  1707. return V_028800_STENCIL_ZERO;
  1708. case PIPE_STENCIL_OP_REPLACE:
  1709. return V_028800_STENCIL_REPLACE;
  1710. case PIPE_STENCIL_OP_INCR:
  1711. return V_028800_STENCIL_INCR;
  1712. case PIPE_STENCIL_OP_DECR:
  1713. return V_028800_STENCIL_DECR;
  1714. case PIPE_STENCIL_OP_INCR_WRAP:
  1715. return V_028800_STENCIL_INCR_WRAP;
  1716. case PIPE_STENCIL_OP_DECR_WRAP:
  1717. return V_028800_STENCIL_DECR_WRAP;
  1718. case PIPE_STENCIL_OP_INVERT:
  1719. return V_028800_STENCIL_INVERT;
  1720. default:
  1721. R600_ERR("Unknown stencil op %d", s_op);
  1722. assert(0);
  1723. break;
  1724. }
  1725. return 0;
  1726. }
  1727. uint32_t r600_translate_fill(uint32_t func)
  1728. {
  1729. switch(func) {
  1730. case PIPE_POLYGON_MODE_FILL:
  1731. return 2;
  1732. case PIPE_POLYGON_MODE_LINE:
  1733. return 1;
  1734. case PIPE_POLYGON_MODE_POINT:
  1735. return 0;
  1736. default:
  1737. assert(0);
  1738. return 0;
  1739. }
  1740. }
  1741. unsigned r600_tex_wrap(unsigned wrap)
  1742. {
  1743. switch (wrap) {
  1744. default:
  1745. case PIPE_TEX_WRAP_REPEAT:
  1746. return V_03C000_SQ_TEX_WRAP;
  1747. case PIPE_TEX_WRAP_CLAMP:
  1748. return V_03C000_SQ_TEX_CLAMP_HALF_BORDER;
  1749. case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
  1750. return V_03C000_SQ_TEX_CLAMP_LAST_TEXEL;
  1751. case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
  1752. return V_03C000_SQ_TEX_CLAMP_BORDER;
  1753. case PIPE_TEX_WRAP_MIRROR_REPEAT:
  1754. return V_03C000_SQ_TEX_MIRROR;
  1755. case PIPE_TEX_WRAP_MIRROR_CLAMP:
  1756. return V_03C000_SQ_TEX_MIRROR_ONCE_HALF_BORDER;
  1757. case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
  1758. return V_03C000_SQ_TEX_MIRROR_ONCE_LAST_TEXEL;
  1759. case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
  1760. return V_03C000_SQ_TEX_MIRROR_ONCE_BORDER;
  1761. }
  1762. }
  1763. unsigned r600_tex_mipfilter(unsigned filter)
  1764. {
  1765. switch (filter) {
  1766. case PIPE_TEX_MIPFILTER_NEAREST:
  1767. return V_03C000_SQ_TEX_Z_FILTER_POINT;
  1768. case PIPE_TEX_MIPFILTER_LINEAR:
  1769. return V_03C000_SQ_TEX_Z_FILTER_LINEAR;
  1770. default:
  1771. case PIPE_TEX_MIPFILTER_NONE:
  1772. return V_03C000_SQ_TEX_Z_FILTER_NONE;
  1773. }
  1774. }
  1775. unsigned r600_tex_compare(unsigned compare)
  1776. {
  1777. switch (compare) {
  1778. default:
  1779. case PIPE_FUNC_NEVER:
  1780. return V_03C000_SQ_TEX_DEPTH_COMPARE_NEVER;
  1781. case PIPE_FUNC_LESS:
  1782. return V_03C000_SQ_TEX_DEPTH_COMPARE_LESS;
  1783. case PIPE_FUNC_EQUAL:
  1784. return V_03C000_SQ_TEX_DEPTH_COMPARE_EQUAL;
  1785. case PIPE_FUNC_LEQUAL:
  1786. return V_03C000_SQ_TEX_DEPTH_COMPARE_LESSEQUAL;
  1787. case PIPE_FUNC_GREATER:
  1788. return V_03C000_SQ_TEX_DEPTH_COMPARE_GREATER;
  1789. case PIPE_FUNC_NOTEQUAL:
  1790. return V_03C000_SQ_TEX_DEPTH_COMPARE_NOTEQUAL;
  1791. case PIPE_FUNC_GEQUAL:
  1792. return V_03C000_SQ_TEX_DEPTH_COMPARE_GREATEREQUAL;
  1793. case PIPE_FUNC_ALWAYS:
  1794. return V_03C000_SQ_TEX_DEPTH_COMPARE_ALWAYS;
  1795. }
  1796. }
  1797. static bool wrap_mode_uses_border_color(unsigned wrap, bool linear_filter)
  1798. {
  1799. return wrap == PIPE_TEX_WRAP_CLAMP_TO_BORDER ||
  1800. wrap == PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER ||
  1801. (linear_filter &&
  1802. (wrap == PIPE_TEX_WRAP_CLAMP ||
  1803. wrap == PIPE_TEX_WRAP_MIRROR_CLAMP));
  1804. }
  1805. bool sampler_state_needs_border_color(const struct pipe_sampler_state *state)
  1806. {
  1807. bool linear_filter = state->min_img_filter != PIPE_TEX_FILTER_NEAREST ||
  1808. state->mag_img_filter != PIPE_TEX_FILTER_NEAREST;
  1809. return (state->border_color.ui[0] || state->border_color.ui[1] ||
  1810. state->border_color.ui[2] || state->border_color.ui[3]) &&
  1811. (wrap_mode_uses_border_color(state->wrap_s, linear_filter) ||
  1812. wrap_mode_uses_border_color(state->wrap_t, linear_filter) ||
  1813. wrap_mode_uses_border_color(state->wrap_r, linear_filter));
  1814. }
  1815. void r600_emit_shader(struct r600_context *rctx, struct r600_atom *a)
  1816. {
  1817. struct radeon_winsys_cs *cs = rctx->b.gfx.cs;
  1818. struct r600_pipe_shader *shader = ((struct r600_shader_state*)a)->shader;
  1819. if (!shader)
  1820. return;
  1821. r600_emit_command_buffer(cs, &shader->command_buffer);
  1822. radeon_emit(cs, PKT3(PKT3_NOP, 0, 0));
  1823. radeon_emit(cs, radeon_add_to_buffer_list(&rctx->b, &rctx->b.gfx, shader->bo,
  1824. RADEON_USAGE_READ, RADEON_PRIO_SHADER_BINARY));
  1825. }
  1826. unsigned r600_get_swizzle_combined(const unsigned char *swizzle_format,
  1827. const unsigned char *swizzle_view,
  1828. boolean vtx)
  1829. {
  1830. unsigned i;
  1831. unsigned char swizzle[4];
  1832. unsigned result = 0;
  1833. const uint32_t tex_swizzle_shift[4] = {
  1834. 16, 19, 22, 25,
  1835. };
  1836. const uint32_t vtx_swizzle_shift[4] = {
  1837. 3, 6, 9, 12,
  1838. };
  1839. const uint32_t swizzle_bit[4] = {
  1840. 0, 1, 2, 3,
  1841. };
  1842. const uint32_t *swizzle_shift = tex_swizzle_shift;
  1843. if (vtx)
  1844. swizzle_shift = vtx_swizzle_shift;
  1845. if (swizzle_view) {
  1846. util_format_compose_swizzles(swizzle_format, swizzle_view, swizzle);
  1847. } else {
  1848. memcpy(swizzle, swizzle_format, 4);
  1849. }
  1850. /* Get swizzle. */
  1851. for (i = 0; i < 4; i++) {
  1852. switch (swizzle[i]) {
  1853. case PIPE_SWIZZLE_Y:
  1854. result |= swizzle_bit[1] << swizzle_shift[i];
  1855. break;
  1856. case PIPE_SWIZZLE_Z:
  1857. result |= swizzle_bit[2] << swizzle_shift[i];
  1858. break;
  1859. case PIPE_SWIZZLE_W:
  1860. result |= swizzle_bit[3] << swizzle_shift[i];
  1861. break;
  1862. case PIPE_SWIZZLE_0:
  1863. result |= V_038010_SQ_SEL_0 << swizzle_shift[i];
  1864. break;
  1865. case PIPE_SWIZZLE_1:
  1866. result |= V_038010_SQ_SEL_1 << swizzle_shift[i];
  1867. break;
  1868. default: /* PIPE_SWIZZLE_X */
  1869. result |= swizzle_bit[0] << swizzle_shift[i];
  1870. }
  1871. }
  1872. return result;
  1873. }
  1874. /* texture format translate */
  1875. uint32_t r600_translate_texformat(struct pipe_screen *screen,
  1876. enum pipe_format format,
  1877. const unsigned char *swizzle_view,
  1878. uint32_t *word4_p, uint32_t *yuv_format_p,
  1879. bool do_endian_swap)
  1880. {
  1881. struct r600_screen *rscreen = (struct r600_screen *)screen;
  1882. uint32_t result = 0, word4 = 0, yuv_format = 0;
  1883. const struct util_format_description *desc;
  1884. boolean uniform = TRUE;
  1885. bool is_srgb_valid = FALSE;
  1886. const unsigned char swizzle_xxxx[4] = {0, 0, 0, 0};
  1887. const unsigned char swizzle_yyyy[4] = {1, 1, 1, 1};
  1888. const unsigned char swizzle_xxxy[4] = {0, 0, 0, 1};
  1889. const unsigned char swizzle_zyx1[4] = {2, 1, 0, 5};
  1890. const unsigned char swizzle_zyxw[4] = {2, 1, 0, 3};
  1891. int i;
  1892. const uint32_t sign_bit[4] = {
  1893. S_038010_FORMAT_COMP_X(V_038010_SQ_FORMAT_COMP_SIGNED),
  1894. S_038010_FORMAT_COMP_Y(V_038010_SQ_FORMAT_COMP_SIGNED),
  1895. S_038010_FORMAT_COMP_Z(V_038010_SQ_FORMAT_COMP_SIGNED),
  1896. S_038010_FORMAT_COMP_W(V_038010_SQ_FORMAT_COMP_SIGNED)
  1897. };
  1898. /* Need to replace the specified texture formats in case of big-endian.
  1899. * These formats are formats that have channels with number of bits
  1900. * not divisible by 8.
  1901. * Mesa conversion functions don't swap bits for those formats, and because
  1902. * we transmit this over a serial bus to the GPU (PCIe), the
  1903. * bit-endianess is important!!!
  1904. * In case we have an "opposite" format, just use that for the swizzling
  1905. * information. If we don't have such an "opposite" format, we need
  1906. * to use a fixed swizzle info instead (see below)
  1907. */
  1908. if (format == PIPE_FORMAT_R4A4_UNORM && do_endian_swap)
  1909. format = PIPE_FORMAT_A4R4_UNORM;
  1910. desc = util_format_description(format);
  1911. /* Depth and stencil swizzling is handled separately. */
  1912. if (desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS) {
  1913. /* Need to check for specific texture formats that don't have
  1914. * an "opposite" format we can use. For those formats, we directly
  1915. * specify the swizzling, which is the LE swizzling as defined in
  1916. * u_format.csv
  1917. */
  1918. if (do_endian_swap) {
  1919. if (format == PIPE_FORMAT_L4A4_UNORM)
  1920. word4 |= r600_get_swizzle_combined(swizzle_xxxy, swizzle_view, FALSE);
  1921. else if (format == PIPE_FORMAT_B4G4R4A4_UNORM)
  1922. word4 |= r600_get_swizzle_combined(swizzle_zyxw, swizzle_view, FALSE);
  1923. else if (format == PIPE_FORMAT_B4G4R4X4_UNORM || format == PIPE_FORMAT_B5G6R5_UNORM)
  1924. word4 |= r600_get_swizzle_combined(swizzle_zyx1, swizzle_view, FALSE);
  1925. else
  1926. word4 |= r600_get_swizzle_combined(desc->swizzle, swizzle_view, FALSE);
  1927. } else {
  1928. word4 |= r600_get_swizzle_combined(desc->swizzle, swizzle_view, FALSE);
  1929. }
  1930. }
  1931. /* Colorspace (return non-RGB formats directly). */
  1932. switch (desc->colorspace) {
  1933. /* Depth stencil formats */
  1934. case UTIL_FORMAT_COLORSPACE_ZS:
  1935. switch (format) {
  1936. /* Depth sampler formats. */
  1937. case PIPE_FORMAT_Z16_UNORM:
  1938. word4 |= r600_get_swizzle_combined(swizzle_xxxx, swizzle_view, FALSE);
  1939. result = FMT_16;
  1940. goto out_word4;
  1941. case PIPE_FORMAT_Z24X8_UNORM:
  1942. case PIPE_FORMAT_Z24_UNORM_S8_UINT:
  1943. word4 |= r600_get_swizzle_combined(swizzle_xxxx, swizzle_view, FALSE);
  1944. result = FMT_8_24;
  1945. goto out_word4;
  1946. case PIPE_FORMAT_X8Z24_UNORM:
  1947. case PIPE_FORMAT_S8_UINT_Z24_UNORM:
  1948. if (rscreen->b.chip_class < EVERGREEN)
  1949. goto out_unknown;
  1950. word4 |= r600_get_swizzle_combined(swizzle_yyyy, swizzle_view, FALSE);
  1951. result = FMT_24_8;
  1952. goto out_word4;
  1953. case PIPE_FORMAT_Z32_FLOAT:
  1954. word4 |= r600_get_swizzle_combined(swizzle_xxxx, swizzle_view, FALSE);
  1955. result = FMT_32_FLOAT;
  1956. goto out_word4;
  1957. case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
  1958. word4 |= r600_get_swizzle_combined(swizzle_xxxx, swizzle_view, FALSE);
  1959. result = FMT_X24_8_32_FLOAT;
  1960. goto out_word4;
  1961. /* Stencil sampler formats. */
  1962. case PIPE_FORMAT_S8_UINT:
  1963. word4 |= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT);
  1964. word4 |= r600_get_swizzle_combined(swizzle_xxxx, swizzle_view, FALSE);
  1965. result = FMT_8;
  1966. goto out_word4;
  1967. case PIPE_FORMAT_X24S8_UINT:
  1968. word4 |= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT);
  1969. word4 |= r600_get_swizzle_combined(swizzle_yyyy, swizzle_view, FALSE);
  1970. result = FMT_8_24;
  1971. goto out_word4;
  1972. case PIPE_FORMAT_S8X24_UINT:
  1973. if (rscreen->b.chip_class < EVERGREEN)
  1974. goto out_unknown;
  1975. word4 |= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT);
  1976. word4 |= r600_get_swizzle_combined(swizzle_xxxx, swizzle_view, FALSE);
  1977. result = FMT_24_8;
  1978. goto out_word4;
  1979. case PIPE_FORMAT_X32_S8X24_UINT:
  1980. word4 |= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT);
  1981. word4 |= r600_get_swizzle_combined(swizzle_yyyy, swizzle_view, FALSE);
  1982. result = FMT_X24_8_32_FLOAT;
  1983. goto out_word4;
  1984. default:
  1985. goto out_unknown;
  1986. }
  1987. case UTIL_FORMAT_COLORSPACE_YUV:
  1988. yuv_format |= (1 << 30);
  1989. switch (format) {
  1990. case PIPE_FORMAT_UYVY:
  1991. case PIPE_FORMAT_YUYV:
  1992. default:
  1993. break;
  1994. }
  1995. goto out_unknown; /* XXX */
  1996. case UTIL_FORMAT_COLORSPACE_SRGB:
  1997. word4 |= S_038010_FORCE_DEGAMMA(1);
  1998. break;
  1999. default:
  2000. break;
  2001. }
  2002. if (desc->layout == UTIL_FORMAT_LAYOUT_RGTC) {
  2003. switch (format) {
  2004. case PIPE_FORMAT_RGTC1_SNORM:
  2005. case PIPE_FORMAT_LATC1_SNORM:
  2006. word4 |= sign_bit[0];
  2007. case PIPE_FORMAT_RGTC1_UNORM:
  2008. case PIPE_FORMAT_LATC1_UNORM:
  2009. result = FMT_BC4;
  2010. goto out_word4;
  2011. case PIPE_FORMAT_RGTC2_SNORM:
  2012. case PIPE_FORMAT_LATC2_SNORM:
  2013. word4 |= sign_bit[0] | sign_bit[1];
  2014. case PIPE_FORMAT_RGTC2_UNORM:
  2015. case PIPE_FORMAT_LATC2_UNORM:
  2016. result = FMT_BC5;
  2017. goto out_word4;
  2018. default:
  2019. goto out_unknown;
  2020. }
  2021. }
  2022. if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
  2023. if (!util_format_s3tc_enabled) {
  2024. goto out_unknown;
  2025. }
  2026. switch (format) {
  2027. case PIPE_FORMAT_DXT1_RGB:
  2028. case PIPE_FORMAT_DXT1_RGBA:
  2029. case PIPE_FORMAT_DXT1_SRGB:
  2030. case PIPE_FORMAT_DXT1_SRGBA:
  2031. result = FMT_BC1;
  2032. is_srgb_valid = TRUE;
  2033. goto out_word4;
  2034. case PIPE_FORMAT_DXT3_RGBA:
  2035. case PIPE_FORMAT_DXT3_SRGBA:
  2036. result = FMT_BC2;
  2037. is_srgb_valid = TRUE;
  2038. goto out_word4;
  2039. case PIPE_FORMAT_DXT5_RGBA:
  2040. case PIPE_FORMAT_DXT5_SRGBA:
  2041. result = FMT_BC3;
  2042. is_srgb_valid = TRUE;
  2043. goto out_word4;
  2044. default:
  2045. goto out_unknown;
  2046. }
  2047. }
  2048. if (desc->layout == UTIL_FORMAT_LAYOUT_BPTC) {
  2049. if (rscreen->b.chip_class < EVERGREEN)
  2050. goto out_unknown;
  2051. switch (format) {
  2052. case PIPE_FORMAT_BPTC_RGBA_UNORM:
  2053. case PIPE_FORMAT_BPTC_SRGBA:
  2054. result = FMT_BC7;
  2055. is_srgb_valid = TRUE;
  2056. goto out_word4;
  2057. case PIPE_FORMAT_BPTC_RGB_FLOAT:
  2058. word4 |= sign_bit[0] | sign_bit[1] | sign_bit[2];
  2059. /* fall through */
  2060. case PIPE_FORMAT_BPTC_RGB_UFLOAT:
  2061. result = FMT_BC6;
  2062. goto out_word4;
  2063. default:
  2064. goto out_unknown;
  2065. }
  2066. }
  2067. if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED) {
  2068. switch (format) {
  2069. case PIPE_FORMAT_R8G8_B8G8_UNORM:
  2070. case PIPE_FORMAT_G8R8_B8R8_UNORM:
  2071. result = FMT_GB_GR;
  2072. goto out_word4;
  2073. case PIPE_FORMAT_G8R8_G8B8_UNORM:
  2074. case PIPE_FORMAT_R8G8_R8B8_UNORM:
  2075. result = FMT_BG_RG;
  2076. goto out_word4;
  2077. default:
  2078. goto out_unknown;
  2079. }
  2080. }
  2081. if (format == PIPE_FORMAT_R9G9B9E5_FLOAT) {
  2082. result = FMT_5_9_9_9_SHAREDEXP;
  2083. goto out_word4;
  2084. } else if (format == PIPE_FORMAT_R11G11B10_FLOAT) {
  2085. result = FMT_10_11_11_FLOAT;
  2086. goto out_word4;
  2087. }
  2088. for (i = 0; i < desc->nr_channels; i++) {
  2089. if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
  2090. word4 |= sign_bit[i];
  2091. }
  2092. }
  2093. /* R8G8Bx_SNORM - XXX CxV8U8 */
  2094. /* See whether the components are of the same size. */
  2095. for (i = 1; i < desc->nr_channels; i++) {
  2096. uniform = uniform && desc->channel[0].size == desc->channel[i].size;
  2097. }
  2098. /* Non-uniform formats. */
  2099. if (!uniform) {
  2100. if (desc->colorspace != UTIL_FORMAT_COLORSPACE_SRGB &&
  2101. desc->channel[0].pure_integer)
  2102. word4 |= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT);
  2103. switch(desc->nr_channels) {
  2104. case 3:
  2105. if (desc->channel[0].size == 5 &&
  2106. desc->channel[1].size == 6 &&
  2107. desc->channel[2].size == 5) {
  2108. result = FMT_5_6_5;
  2109. goto out_word4;
  2110. }
  2111. goto out_unknown;
  2112. case 4:
  2113. if (desc->channel[0].size == 5 &&
  2114. desc->channel[1].size == 5 &&
  2115. desc->channel[2].size == 5 &&
  2116. desc->channel[3].size == 1) {
  2117. result = FMT_1_5_5_5;
  2118. goto out_word4;
  2119. }
  2120. if (desc->channel[0].size == 10 &&
  2121. desc->channel[1].size == 10 &&
  2122. desc->channel[2].size == 10 &&
  2123. desc->channel[3].size == 2) {
  2124. result = FMT_2_10_10_10;
  2125. goto out_word4;
  2126. }
  2127. goto out_unknown;
  2128. }
  2129. goto out_unknown;
  2130. }
  2131. /* Find the first non-VOID channel. */
  2132. for (i = 0; i < 4; i++) {
  2133. if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID) {
  2134. break;
  2135. }
  2136. }
  2137. if (i == 4)
  2138. goto out_unknown;
  2139. /* uniform formats */
  2140. switch (desc->channel[i].type) {
  2141. case UTIL_FORMAT_TYPE_UNSIGNED:
  2142. case UTIL_FORMAT_TYPE_SIGNED:
  2143. #if 0
  2144. if (!desc->channel[i].normalized &&
  2145. desc->colorspace != UTIL_FORMAT_COLORSPACE_SRGB) {
  2146. goto out_unknown;
  2147. }
  2148. #endif
  2149. if (desc->colorspace != UTIL_FORMAT_COLORSPACE_SRGB &&
  2150. desc->channel[i].pure_integer)
  2151. word4 |= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT);
  2152. switch (desc->channel[i].size) {
  2153. case 4:
  2154. switch (desc->nr_channels) {
  2155. case 2:
  2156. result = FMT_4_4;
  2157. goto out_word4;
  2158. case 4:
  2159. result = FMT_4_4_4_4;
  2160. goto out_word4;
  2161. }
  2162. goto out_unknown;
  2163. case 8:
  2164. switch (desc->nr_channels) {
  2165. case 1:
  2166. result = FMT_8;
  2167. goto out_word4;
  2168. case 2:
  2169. result = FMT_8_8;
  2170. goto out_word4;
  2171. case 4:
  2172. result = FMT_8_8_8_8;
  2173. is_srgb_valid = TRUE;
  2174. goto out_word4;
  2175. }
  2176. goto out_unknown;
  2177. case 16:
  2178. switch (desc->nr_channels) {
  2179. case 1:
  2180. result = FMT_16;
  2181. goto out_word4;
  2182. case 2:
  2183. result = FMT_16_16;
  2184. goto out_word4;
  2185. case 4:
  2186. result = FMT_16_16_16_16;
  2187. goto out_word4;
  2188. }
  2189. goto out_unknown;
  2190. case 32:
  2191. switch (desc->nr_channels) {
  2192. case 1:
  2193. result = FMT_32;
  2194. goto out_word4;
  2195. case 2:
  2196. result = FMT_32_32;
  2197. goto out_word4;
  2198. case 4:
  2199. result = FMT_32_32_32_32;
  2200. goto out_word4;
  2201. }
  2202. }
  2203. goto out_unknown;
  2204. case UTIL_FORMAT_TYPE_FLOAT:
  2205. switch (desc->channel[i].size) {
  2206. case 16:
  2207. switch (desc->nr_channels) {
  2208. case 1:
  2209. result = FMT_16_FLOAT;
  2210. goto out_word4;
  2211. case 2:
  2212. result = FMT_16_16_FLOAT;
  2213. goto out_word4;
  2214. case 4:
  2215. result = FMT_16_16_16_16_FLOAT;
  2216. goto out_word4;
  2217. }
  2218. goto out_unknown;
  2219. case 32:
  2220. switch (desc->nr_channels) {
  2221. case 1:
  2222. result = FMT_32_FLOAT;
  2223. goto out_word4;
  2224. case 2:
  2225. result = FMT_32_32_FLOAT;
  2226. goto out_word4;
  2227. case 4:
  2228. result = FMT_32_32_32_32_FLOAT;
  2229. goto out_word4;
  2230. }
  2231. }
  2232. goto out_unknown;
  2233. }
  2234. out_word4:
  2235. if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB && !is_srgb_valid)
  2236. return ~0;
  2237. if (word4_p)
  2238. *word4_p = word4;
  2239. if (yuv_format_p)
  2240. *yuv_format_p = yuv_format;
  2241. return result;
  2242. out_unknown:
  2243. /* R600_ERR("Unable to handle texformat %d %s\n", format, util_format_name(format)); */
  2244. return ~0;
  2245. }
  2246. uint32_t r600_translate_colorformat(enum chip_class chip, enum pipe_format format,
  2247. bool do_endian_swap)
  2248. {
  2249. const struct util_format_description *desc = util_format_description(format);
  2250. int channel = util_format_get_first_non_void_channel(format);
  2251. bool is_float;
  2252. #define HAS_SIZE(x,y,z,w) \
  2253. (desc->channel[0].size == (x) && desc->channel[1].size == (y) && \
  2254. desc->channel[2].size == (z) && desc->channel[3].size == (w))
  2255. if (format == PIPE_FORMAT_R11G11B10_FLOAT) /* isn't plain */
  2256. return V_0280A0_COLOR_10_11_11_FLOAT;
  2257. if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN ||
  2258. channel == -1)
  2259. return ~0U;
  2260. is_float = desc->channel[channel].type == UTIL_FORMAT_TYPE_FLOAT;
  2261. switch (desc->nr_channels) {
  2262. case 1:
  2263. switch (desc->channel[0].size) {
  2264. case 8:
  2265. return V_0280A0_COLOR_8;
  2266. case 16:
  2267. if (is_float)
  2268. return V_0280A0_COLOR_16_FLOAT;
  2269. else
  2270. return V_0280A0_COLOR_16;
  2271. case 32:
  2272. if (is_float)
  2273. return V_0280A0_COLOR_32_FLOAT;
  2274. else
  2275. return V_0280A0_COLOR_32;
  2276. }
  2277. break;
  2278. case 2:
  2279. if (desc->channel[0].size == desc->channel[1].size) {
  2280. switch (desc->channel[0].size) {
  2281. case 4:
  2282. if (chip <= R700)
  2283. return V_0280A0_COLOR_4_4;
  2284. else
  2285. return ~0U; /* removed on Evergreen */
  2286. case 8:
  2287. return V_0280A0_COLOR_8_8;
  2288. case 16:
  2289. if (is_float)
  2290. return V_0280A0_COLOR_16_16_FLOAT;
  2291. else
  2292. return V_0280A0_COLOR_16_16;
  2293. case 32:
  2294. if (is_float)
  2295. return V_0280A0_COLOR_32_32_FLOAT;
  2296. else
  2297. return V_0280A0_COLOR_32_32;
  2298. }
  2299. } else if (HAS_SIZE(8,24,0,0)) {
  2300. return (do_endian_swap ? V_0280A0_COLOR_8_24 : V_0280A0_COLOR_24_8);
  2301. } else if (HAS_SIZE(24,8,0,0)) {
  2302. return V_0280A0_COLOR_8_24;
  2303. }
  2304. break;
  2305. case 3:
  2306. if (HAS_SIZE(5,6,5,0)) {
  2307. return V_0280A0_COLOR_5_6_5;
  2308. } else if (HAS_SIZE(32,8,24,0)) {
  2309. return V_0280A0_COLOR_X24_8_32_FLOAT;
  2310. }
  2311. break;
  2312. case 4:
  2313. if (desc->channel[0].size == desc->channel[1].size &&
  2314. desc->channel[0].size == desc->channel[2].size &&
  2315. desc->channel[0].size == desc->channel[3].size) {
  2316. switch (desc->channel[0].size) {
  2317. case 4:
  2318. return V_0280A0_COLOR_4_4_4_4;
  2319. case 8:
  2320. return V_0280A0_COLOR_8_8_8_8;
  2321. case 16:
  2322. if (is_float)
  2323. return V_0280A0_COLOR_16_16_16_16_FLOAT;
  2324. else
  2325. return V_0280A0_COLOR_16_16_16_16;
  2326. case 32:
  2327. if (is_float)
  2328. return V_0280A0_COLOR_32_32_32_32_FLOAT;
  2329. else
  2330. return V_0280A0_COLOR_32_32_32_32;
  2331. }
  2332. } else if (HAS_SIZE(5,5,5,1)) {
  2333. return V_0280A0_COLOR_1_5_5_5;
  2334. } else if (HAS_SIZE(10,10,10,2)) {
  2335. return V_0280A0_COLOR_2_10_10_10;
  2336. }
  2337. break;
  2338. }
  2339. return ~0U;
  2340. }
  2341. uint32_t r600_colorformat_endian_swap(uint32_t colorformat, bool do_endian_swap)
  2342. {
  2343. if (R600_BIG_ENDIAN) {
  2344. switch(colorformat) {
  2345. /* 8-bit buffers. */
  2346. case V_0280A0_COLOR_4_4:
  2347. case V_0280A0_COLOR_8:
  2348. return ENDIAN_NONE;
  2349. /* 16-bit buffers. */
  2350. case V_0280A0_COLOR_8_8:
  2351. /*
  2352. * No need to do endian swaps on array formats,
  2353. * as mesa<-->pipe formats conversion take into account
  2354. * the endianess
  2355. */
  2356. return ENDIAN_NONE;
  2357. case V_0280A0_COLOR_5_6_5:
  2358. case V_0280A0_COLOR_1_5_5_5:
  2359. case V_0280A0_COLOR_4_4_4_4:
  2360. case V_0280A0_COLOR_16:
  2361. return (do_endian_swap ? ENDIAN_8IN16 : ENDIAN_NONE);
  2362. /* 32-bit buffers. */
  2363. case V_0280A0_COLOR_8_8_8_8:
  2364. /*
  2365. * No need to do endian swaps on array formats,
  2366. * as mesa<-->pipe formats conversion take into account
  2367. * the endianess
  2368. */
  2369. return ENDIAN_NONE;
  2370. case V_0280A0_COLOR_2_10_10_10:
  2371. case V_0280A0_COLOR_8_24:
  2372. case V_0280A0_COLOR_24_8:
  2373. case V_0280A0_COLOR_32_FLOAT:
  2374. return (do_endian_swap ? ENDIAN_8IN32 : ENDIAN_NONE);
  2375. case V_0280A0_COLOR_16_16_FLOAT:
  2376. case V_0280A0_COLOR_16_16:
  2377. return ENDIAN_8IN16;
  2378. /* 64-bit buffers. */
  2379. case V_0280A0_COLOR_16_16_16_16:
  2380. case V_0280A0_COLOR_16_16_16_16_FLOAT:
  2381. return ENDIAN_8IN16;
  2382. case V_0280A0_COLOR_32_32_FLOAT:
  2383. case V_0280A0_COLOR_32_32:
  2384. case V_0280A0_COLOR_X24_8_32_FLOAT:
  2385. return ENDIAN_8IN32;
  2386. /* 128-bit buffers. */
  2387. case V_0280A0_COLOR_32_32_32_32_FLOAT:
  2388. case V_0280A0_COLOR_32_32_32_32:
  2389. return ENDIAN_8IN32;
  2390. default:
  2391. return ENDIAN_NONE; /* Unsupported. */
  2392. }
  2393. } else {
  2394. return ENDIAN_NONE;
  2395. }
  2396. }
  2397. static void r600_invalidate_buffer(struct pipe_context *ctx, struct pipe_resource *buf)
  2398. {
  2399. struct r600_context *rctx = (struct r600_context*)ctx;
  2400. struct r600_resource *rbuffer = r600_resource(buf);
  2401. unsigned i, shader, mask, alignment = rbuffer->buf->alignment;
  2402. struct r600_pipe_sampler_view *view;
  2403. /* Reallocate the buffer in the same pipe_resource. */
  2404. r600_init_resource(&rctx->screen->b, rbuffer, rbuffer->b.b.width0,
  2405. alignment);
  2406. /* We changed the buffer, now we need to bind it where the old one was bound. */
  2407. /* Vertex buffers. */
  2408. mask = rctx->vertex_buffer_state.enabled_mask;
  2409. while (mask) {
  2410. i = u_bit_scan(&mask);
  2411. if (rctx->vertex_buffer_state.vb[i].buffer == &rbuffer->b.b) {
  2412. rctx->vertex_buffer_state.dirty_mask |= 1 << i;
  2413. r600_vertex_buffers_dirty(rctx);
  2414. }
  2415. }
  2416. /* Streamout buffers. */
  2417. for (i = 0; i < rctx->b.streamout.num_targets; i++) {
  2418. if (rctx->b.streamout.targets[i] &&
  2419. rctx->b.streamout.targets[i]->b.buffer == &rbuffer->b.b) {
  2420. if (rctx->b.streamout.begin_emitted) {
  2421. r600_emit_streamout_end(&rctx->b);
  2422. }
  2423. rctx->b.streamout.append_bitmask = rctx->b.streamout.enabled_mask;
  2424. r600_streamout_buffers_dirty(&rctx->b);
  2425. }
  2426. }
  2427. /* Constant buffers. */
  2428. for (shader = 0; shader < PIPE_SHADER_TYPES; shader++) {
  2429. struct r600_constbuf_state *state = &rctx->constbuf_state[shader];
  2430. bool found = false;
  2431. uint32_t mask = state->enabled_mask;
  2432. while (mask) {
  2433. unsigned i = u_bit_scan(&mask);
  2434. if (state->cb[i].buffer == &rbuffer->b.b) {
  2435. found = true;
  2436. state->dirty_mask |= 1 << i;
  2437. }
  2438. }
  2439. if (found) {
  2440. r600_constant_buffers_dirty(rctx, state);
  2441. }
  2442. }
  2443. /* Texture buffer objects - update the virtual addresses in descriptors. */
  2444. LIST_FOR_EACH_ENTRY(view, &rctx->b.texture_buffers, list) {
  2445. if (view->base.texture == &rbuffer->b.b) {
  2446. uint64_t offset = view->base.u.buf.offset;
  2447. uint64_t va = rbuffer->gpu_address + offset;
  2448. view->tex_resource_words[0] = va;
  2449. view->tex_resource_words[2] &= C_038008_BASE_ADDRESS_HI;
  2450. view->tex_resource_words[2] |= S_038008_BASE_ADDRESS_HI(va >> 32);
  2451. }
  2452. }
  2453. /* Texture buffer objects - make bindings dirty if needed. */
  2454. for (shader = 0; shader < PIPE_SHADER_TYPES; shader++) {
  2455. struct r600_samplerview_state *state = &rctx->samplers[shader].views;
  2456. bool found = false;
  2457. uint32_t mask = state->enabled_mask;
  2458. while (mask) {
  2459. unsigned i = u_bit_scan(&mask);
  2460. if (state->views[i]->base.texture == &rbuffer->b.b) {
  2461. found = true;
  2462. state->dirty_mask |= 1 << i;
  2463. }
  2464. }
  2465. if (found) {
  2466. r600_sampler_views_dirty(rctx, state);
  2467. }
  2468. }
  2469. }
  2470. static void r600_set_active_query_state(struct pipe_context *ctx, boolean enable)
  2471. {
  2472. struct r600_context *rctx = (struct r600_context*)ctx;
  2473. /* Pipeline stat & streamout queries. */
  2474. if (enable) {
  2475. rctx->b.flags &= ~R600_CONTEXT_STOP_PIPELINE_STATS;
  2476. rctx->b.flags |= R600_CONTEXT_START_PIPELINE_STATS;
  2477. } else {
  2478. rctx->b.flags &= ~R600_CONTEXT_START_PIPELINE_STATS;
  2479. rctx->b.flags |= R600_CONTEXT_STOP_PIPELINE_STATS;
  2480. }
  2481. /* Occlusion queries. */
  2482. if (rctx->db_misc_state.occlusion_queries_disabled != !enable) {
  2483. rctx->db_misc_state.occlusion_queries_disabled = !enable;
  2484. r600_mark_atom_dirty(rctx, &rctx->db_misc_state.atom);
  2485. }
  2486. }
  2487. static void r600_set_occlusion_query_state(struct pipe_context *ctx, bool enable)
  2488. {
  2489. struct r600_context *rctx = (struct r600_context*)ctx;
  2490. r600_mark_atom_dirty(rctx, &rctx->db_misc_state.atom);
  2491. }
  2492. static void r600_need_gfx_cs_space(struct pipe_context *ctx, unsigned num_dw,
  2493. bool include_draw_vbo)
  2494. {
  2495. r600_need_cs_space((struct r600_context*)ctx, num_dw, include_draw_vbo);
  2496. }
  2497. /* keep this at the end of this file, please */
  2498. void r600_init_common_state_functions(struct r600_context *rctx)
  2499. {
  2500. rctx->b.b.create_fs_state = r600_create_ps_state;
  2501. rctx->b.b.create_vs_state = r600_create_vs_state;
  2502. rctx->b.b.create_gs_state = r600_create_gs_state;
  2503. rctx->b.b.create_tcs_state = r600_create_tcs_state;
  2504. rctx->b.b.create_tes_state = r600_create_tes_state;
  2505. rctx->b.b.create_vertex_elements_state = r600_create_vertex_fetch_shader;
  2506. rctx->b.b.bind_blend_state = r600_bind_blend_state;
  2507. rctx->b.b.bind_depth_stencil_alpha_state = r600_bind_dsa_state;
  2508. rctx->b.b.bind_sampler_states = r600_bind_sampler_states;
  2509. rctx->b.b.bind_fs_state = r600_bind_ps_state;
  2510. rctx->b.b.bind_rasterizer_state = r600_bind_rs_state;
  2511. rctx->b.b.bind_vertex_elements_state = r600_bind_vertex_elements;
  2512. rctx->b.b.bind_vs_state = r600_bind_vs_state;
  2513. rctx->b.b.bind_gs_state = r600_bind_gs_state;
  2514. rctx->b.b.bind_tcs_state = r600_bind_tcs_state;
  2515. rctx->b.b.bind_tes_state = r600_bind_tes_state;
  2516. rctx->b.b.delete_blend_state = r600_delete_blend_state;
  2517. rctx->b.b.delete_depth_stencil_alpha_state = r600_delete_dsa_state;
  2518. rctx->b.b.delete_fs_state = r600_delete_ps_state;
  2519. rctx->b.b.delete_rasterizer_state = r600_delete_rs_state;
  2520. rctx->b.b.delete_sampler_state = r600_delete_sampler_state;
  2521. rctx->b.b.delete_vertex_elements_state = r600_delete_vertex_elements;
  2522. rctx->b.b.delete_vs_state = r600_delete_vs_state;
  2523. rctx->b.b.delete_gs_state = r600_delete_gs_state;
  2524. rctx->b.b.delete_tcs_state = r600_delete_tcs_state;
  2525. rctx->b.b.delete_tes_state = r600_delete_tes_state;
  2526. rctx->b.b.set_blend_color = r600_set_blend_color;
  2527. rctx->b.b.set_clip_state = r600_set_clip_state;
  2528. rctx->b.b.set_constant_buffer = r600_set_constant_buffer;
  2529. rctx->b.b.set_sample_mask = r600_set_sample_mask;
  2530. rctx->b.b.set_stencil_ref = r600_set_pipe_stencil_ref;
  2531. rctx->b.b.set_vertex_buffers = r600_set_vertex_buffers;
  2532. rctx->b.b.set_index_buffer = r600_set_index_buffer;
  2533. rctx->b.b.set_sampler_views = r600_set_sampler_views;
  2534. rctx->b.b.sampler_view_destroy = r600_sampler_view_destroy;
  2535. rctx->b.b.texture_barrier = r600_texture_barrier;
  2536. rctx->b.b.set_stream_output_targets = r600_set_streamout_targets;
  2537. rctx->b.b.set_active_query_state = r600_set_active_query_state;
  2538. rctx->b.b.draw_vbo = r600_draw_vbo;
  2539. rctx->b.invalidate_buffer = r600_invalidate_buffer;
  2540. rctx->b.set_occlusion_query_state = r600_set_occlusion_query_state;
  2541. rctx->b.need_gfx_cs_space = r600_need_gfx_cs_space;
  2542. }