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.

ilo_state.c 84KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629
  1. /*
  2. * Mesa 3-D graphics library
  3. *
  4. * Copyright (C) 2012-2013 LunarG, Inc.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included
  14. * in all copies or substantial portions of the 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 NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors:
  25. * Chia-I Wu <olv@lunarg.com>
  26. */
  27. #include "util/u_dual_blend.h"
  28. #include "util/u_dynarray.h"
  29. #include "util/u_framebuffer.h"
  30. #include "util/u_helpers.h"
  31. #include "util/u_resource.h"
  32. #include "util/u_upload_mgr.h"
  33. #include "ilo_context.h"
  34. #include "ilo_format.h"
  35. #include "ilo_resource.h"
  36. #include "ilo_shader.h"
  37. #include "ilo_state.h"
  38. /**
  39. * Translate a pipe primitive type to the matching hardware primitive type.
  40. */
  41. static enum gen_3dprim_type
  42. ilo_translate_draw_mode(unsigned mode)
  43. {
  44. static const enum gen_3dprim_type prim_mapping[PIPE_PRIM_MAX] = {
  45. [PIPE_PRIM_POINTS] = GEN6_3DPRIM_POINTLIST,
  46. [PIPE_PRIM_LINES] = GEN6_3DPRIM_LINELIST,
  47. [PIPE_PRIM_LINE_LOOP] = GEN6_3DPRIM_LINELOOP,
  48. [PIPE_PRIM_LINE_STRIP] = GEN6_3DPRIM_LINESTRIP,
  49. [PIPE_PRIM_TRIANGLES] = GEN6_3DPRIM_TRILIST,
  50. [PIPE_PRIM_TRIANGLE_STRIP] = GEN6_3DPRIM_TRISTRIP,
  51. [PIPE_PRIM_TRIANGLE_FAN] = GEN6_3DPRIM_TRIFAN,
  52. [PIPE_PRIM_QUADS] = GEN6_3DPRIM_QUADLIST,
  53. [PIPE_PRIM_QUAD_STRIP] = GEN6_3DPRIM_QUADSTRIP,
  54. [PIPE_PRIM_POLYGON] = GEN6_3DPRIM_POLYGON,
  55. [PIPE_PRIM_LINES_ADJACENCY] = GEN6_3DPRIM_LINELIST_ADJ,
  56. [PIPE_PRIM_LINE_STRIP_ADJACENCY] = GEN6_3DPRIM_LINESTRIP_ADJ,
  57. [PIPE_PRIM_TRIANGLES_ADJACENCY] = GEN6_3DPRIM_TRILIST_ADJ,
  58. [PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY] = GEN6_3DPRIM_TRISTRIP_ADJ,
  59. };
  60. assert(prim_mapping[mode]);
  61. return prim_mapping[mode];
  62. }
  63. static enum gen_index_format
  64. ilo_translate_index_size(unsigned index_size)
  65. {
  66. switch (index_size) {
  67. case 1: return GEN6_INDEX_BYTE;
  68. case 2: return GEN6_INDEX_WORD;
  69. case 4: return GEN6_INDEX_DWORD;
  70. default:
  71. assert(!"unknown index size");
  72. return GEN6_INDEX_BYTE;
  73. }
  74. }
  75. static enum gen_mip_filter
  76. ilo_translate_mip_filter(unsigned filter)
  77. {
  78. switch (filter) {
  79. case PIPE_TEX_MIPFILTER_NEAREST: return GEN6_MIPFILTER_NEAREST;
  80. case PIPE_TEX_MIPFILTER_LINEAR: return GEN6_MIPFILTER_LINEAR;
  81. case PIPE_TEX_MIPFILTER_NONE: return GEN6_MIPFILTER_NONE;
  82. default:
  83. assert(!"unknown mipfilter");
  84. return GEN6_MIPFILTER_NONE;
  85. }
  86. }
  87. static int
  88. ilo_translate_img_filter(unsigned filter)
  89. {
  90. switch (filter) {
  91. case PIPE_TEX_FILTER_NEAREST: return GEN6_MAPFILTER_NEAREST;
  92. case PIPE_TEX_FILTER_LINEAR: return GEN6_MAPFILTER_LINEAR;
  93. default:
  94. assert(!"unknown sampler filter");
  95. return GEN6_MAPFILTER_NEAREST;
  96. }
  97. }
  98. static enum gen_texcoord_mode
  99. ilo_translate_address_wrap(unsigned wrap)
  100. {
  101. switch (wrap) {
  102. case PIPE_TEX_WRAP_CLAMP: return GEN8_TEXCOORDMODE_HALF_BORDER;
  103. case PIPE_TEX_WRAP_REPEAT: return GEN6_TEXCOORDMODE_WRAP;
  104. case PIPE_TEX_WRAP_CLAMP_TO_EDGE: return GEN6_TEXCOORDMODE_CLAMP;
  105. case PIPE_TEX_WRAP_CLAMP_TO_BORDER: return GEN6_TEXCOORDMODE_CLAMP_BORDER;
  106. case PIPE_TEX_WRAP_MIRROR_REPEAT: return GEN6_TEXCOORDMODE_MIRROR;
  107. case PIPE_TEX_WRAP_MIRROR_CLAMP:
  108. case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
  109. case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
  110. default:
  111. assert(!"unknown sampler wrap mode");
  112. return GEN6_TEXCOORDMODE_WRAP;
  113. }
  114. }
  115. static enum gen_aniso_ratio
  116. ilo_translate_max_anisotropy(unsigned max_anisotropy)
  117. {
  118. switch (max_anisotropy) {
  119. case 0: case 1: case 2: return GEN6_ANISORATIO_2;
  120. case 3: case 4: return GEN6_ANISORATIO_4;
  121. case 5: case 6: return GEN6_ANISORATIO_6;
  122. case 7: case 8: return GEN6_ANISORATIO_8;
  123. case 9: case 10: return GEN6_ANISORATIO_10;
  124. case 11: case 12: return GEN6_ANISORATIO_12;
  125. case 13: case 14: return GEN6_ANISORATIO_14;
  126. default: return GEN6_ANISORATIO_16;
  127. }
  128. }
  129. static enum gen_prefilter_op
  130. ilo_translate_shadow_func(unsigned func)
  131. {
  132. /*
  133. * For PIPE_FUNC_x, the reference value is on the left-hand side of the
  134. * comparison, and 1.0 is returned when the comparison is true.
  135. *
  136. * For GEN6_PREFILTEROP_x, the reference value is on the right-hand side of
  137. * the comparison, and 0.0 is returned when the comparison is true.
  138. */
  139. switch (func) {
  140. case PIPE_FUNC_NEVER: return GEN6_PREFILTEROP_ALWAYS;
  141. case PIPE_FUNC_LESS: return GEN6_PREFILTEROP_LEQUAL;
  142. case PIPE_FUNC_EQUAL: return GEN6_PREFILTEROP_NOTEQUAL;
  143. case PIPE_FUNC_LEQUAL: return GEN6_PREFILTEROP_LESS;
  144. case PIPE_FUNC_GREATER: return GEN6_PREFILTEROP_GEQUAL;
  145. case PIPE_FUNC_NOTEQUAL: return GEN6_PREFILTEROP_EQUAL;
  146. case PIPE_FUNC_GEQUAL: return GEN6_PREFILTEROP_GREATER;
  147. case PIPE_FUNC_ALWAYS: return GEN6_PREFILTEROP_NEVER;
  148. default:
  149. assert(!"unknown shadow compare function");
  150. return GEN6_PREFILTEROP_NEVER;
  151. }
  152. }
  153. static enum gen_front_winding
  154. ilo_translate_front_ccw(unsigned front_ccw)
  155. {
  156. return (front_ccw) ? GEN6_FRONTWINDING_CCW : GEN6_FRONTWINDING_CW;
  157. }
  158. static enum gen_cull_mode
  159. ilo_translate_cull_face(unsigned cull_face)
  160. {
  161. switch (cull_face) {
  162. case PIPE_FACE_NONE: return GEN6_CULLMODE_NONE;
  163. case PIPE_FACE_FRONT: return GEN6_CULLMODE_FRONT;
  164. case PIPE_FACE_BACK: return GEN6_CULLMODE_BACK;
  165. case PIPE_FACE_FRONT_AND_BACK: return GEN6_CULLMODE_BOTH;
  166. default:
  167. assert(!"unknown face culling");
  168. return GEN6_CULLMODE_NONE;
  169. }
  170. }
  171. static enum gen_fill_mode
  172. ilo_translate_poly_mode(unsigned poly_mode)
  173. {
  174. switch (poly_mode) {
  175. case PIPE_POLYGON_MODE_FILL: return GEN6_FILLMODE_SOLID;
  176. case PIPE_POLYGON_MODE_LINE: return GEN6_FILLMODE_WIREFRAME;
  177. case PIPE_POLYGON_MODE_POINT: return GEN6_FILLMODE_POINT;
  178. default:
  179. assert(!"unknown polygon mode");
  180. return GEN6_FILLMODE_SOLID;
  181. }
  182. }
  183. static enum gen_pixel_location
  184. ilo_translate_half_pixel_center(bool half_pixel_center)
  185. {
  186. return (half_pixel_center) ? GEN6_PIXLOC_CENTER : GEN6_PIXLOC_UL_CORNER;
  187. }
  188. static enum gen_compare_function
  189. ilo_translate_compare_func(unsigned func)
  190. {
  191. switch (func) {
  192. case PIPE_FUNC_NEVER: return GEN6_COMPAREFUNCTION_NEVER;
  193. case PIPE_FUNC_LESS: return GEN6_COMPAREFUNCTION_LESS;
  194. case PIPE_FUNC_EQUAL: return GEN6_COMPAREFUNCTION_EQUAL;
  195. case PIPE_FUNC_LEQUAL: return GEN6_COMPAREFUNCTION_LEQUAL;
  196. case PIPE_FUNC_GREATER: return GEN6_COMPAREFUNCTION_GREATER;
  197. case PIPE_FUNC_NOTEQUAL: return GEN6_COMPAREFUNCTION_NOTEQUAL;
  198. case PIPE_FUNC_GEQUAL: return GEN6_COMPAREFUNCTION_GEQUAL;
  199. case PIPE_FUNC_ALWAYS: return GEN6_COMPAREFUNCTION_ALWAYS;
  200. default:
  201. assert(!"unknown compare function");
  202. return GEN6_COMPAREFUNCTION_NEVER;
  203. }
  204. }
  205. static enum gen_stencil_op
  206. ilo_translate_stencil_op(unsigned stencil_op)
  207. {
  208. switch (stencil_op) {
  209. case PIPE_STENCIL_OP_KEEP: return GEN6_STENCILOP_KEEP;
  210. case PIPE_STENCIL_OP_ZERO: return GEN6_STENCILOP_ZERO;
  211. case PIPE_STENCIL_OP_REPLACE: return GEN6_STENCILOP_REPLACE;
  212. case PIPE_STENCIL_OP_INCR: return GEN6_STENCILOP_INCRSAT;
  213. case PIPE_STENCIL_OP_DECR: return GEN6_STENCILOP_DECRSAT;
  214. case PIPE_STENCIL_OP_INCR_WRAP: return GEN6_STENCILOP_INCR;
  215. case PIPE_STENCIL_OP_DECR_WRAP: return GEN6_STENCILOP_DECR;
  216. case PIPE_STENCIL_OP_INVERT: return GEN6_STENCILOP_INVERT;
  217. default:
  218. assert(!"unknown stencil op");
  219. return GEN6_STENCILOP_KEEP;
  220. }
  221. }
  222. static enum gen_logic_op
  223. ilo_translate_logicop(unsigned logicop)
  224. {
  225. switch (logicop) {
  226. case PIPE_LOGICOP_CLEAR: return GEN6_LOGICOP_CLEAR;
  227. case PIPE_LOGICOP_NOR: return GEN6_LOGICOP_NOR;
  228. case PIPE_LOGICOP_AND_INVERTED: return GEN6_LOGICOP_AND_INVERTED;
  229. case PIPE_LOGICOP_COPY_INVERTED: return GEN6_LOGICOP_COPY_INVERTED;
  230. case PIPE_LOGICOP_AND_REVERSE: return GEN6_LOGICOP_AND_REVERSE;
  231. case PIPE_LOGICOP_INVERT: return GEN6_LOGICOP_INVERT;
  232. case PIPE_LOGICOP_XOR: return GEN6_LOGICOP_XOR;
  233. case PIPE_LOGICOP_NAND: return GEN6_LOGICOP_NAND;
  234. case PIPE_LOGICOP_AND: return GEN6_LOGICOP_AND;
  235. case PIPE_LOGICOP_EQUIV: return GEN6_LOGICOP_EQUIV;
  236. case PIPE_LOGICOP_NOOP: return GEN6_LOGICOP_NOOP;
  237. case PIPE_LOGICOP_OR_INVERTED: return GEN6_LOGICOP_OR_INVERTED;
  238. case PIPE_LOGICOP_COPY: return GEN6_LOGICOP_COPY;
  239. case PIPE_LOGICOP_OR_REVERSE: return GEN6_LOGICOP_OR_REVERSE;
  240. case PIPE_LOGICOP_OR: return GEN6_LOGICOP_OR;
  241. case PIPE_LOGICOP_SET: return GEN6_LOGICOP_SET;
  242. default:
  243. assert(!"unknown logicop function");
  244. return GEN6_LOGICOP_CLEAR;
  245. }
  246. }
  247. static int
  248. ilo_translate_blend_func(unsigned blend)
  249. {
  250. switch (blend) {
  251. case PIPE_BLEND_ADD: return GEN6_BLENDFUNCTION_ADD;
  252. case PIPE_BLEND_SUBTRACT: return GEN6_BLENDFUNCTION_SUBTRACT;
  253. case PIPE_BLEND_REVERSE_SUBTRACT: return GEN6_BLENDFUNCTION_REVERSE_SUBTRACT;
  254. case PIPE_BLEND_MIN: return GEN6_BLENDFUNCTION_MIN;
  255. case PIPE_BLEND_MAX: return GEN6_BLENDFUNCTION_MAX;
  256. default:
  257. assert(!"unknown blend function");
  258. return GEN6_BLENDFUNCTION_ADD;
  259. }
  260. }
  261. static int
  262. ilo_translate_blend_factor(unsigned factor)
  263. {
  264. switch (factor) {
  265. case PIPE_BLENDFACTOR_ONE: return GEN6_BLENDFACTOR_ONE;
  266. case PIPE_BLENDFACTOR_SRC_COLOR: return GEN6_BLENDFACTOR_SRC_COLOR;
  267. case PIPE_BLENDFACTOR_SRC_ALPHA: return GEN6_BLENDFACTOR_SRC_ALPHA;
  268. case PIPE_BLENDFACTOR_DST_ALPHA: return GEN6_BLENDFACTOR_DST_ALPHA;
  269. case PIPE_BLENDFACTOR_DST_COLOR: return GEN6_BLENDFACTOR_DST_COLOR;
  270. case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE: return GEN6_BLENDFACTOR_SRC_ALPHA_SATURATE;
  271. case PIPE_BLENDFACTOR_CONST_COLOR: return GEN6_BLENDFACTOR_CONST_COLOR;
  272. case PIPE_BLENDFACTOR_CONST_ALPHA: return GEN6_BLENDFACTOR_CONST_ALPHA;
  273. case PIPE_BLENDFACTOR_SRC1_COLOR: return GEN6_BLENDFACTOR_SRC1_COLOR;
  274. case PIPE_BLENDFACTOR_SRC1_ALPHA: return GEN6_BLENDFACTOR_SRC1_ALPHA;
  275. case PIPE_BLENDFACTOR_ZERO: return GEN6_BLENDFACTOR_ZERO;
  276. case PIPE_BLENDFACTOR_INV_SRC_COLOR: return GEN6_BLENDFACTOR_INV_SRC_COLOR;
  277. case PIPE_BLENDFACTOR_INV_SRC_ALPHA: return GEN6_BLENDFACTOR_INV_SRC_ALPHA;
  278. case PIPE_BLENDFACTOR_INV_DST_ALPHA: return GEN6_BLENDFACTOR_INV_DST_ALPHA;
  279. case PIPE_BLENDFACTOR_INV_DST_COLOR: return GEN6_BLENDFACTOR_INV_DST_COLOR;
  280. case PIPE_BLENDFACTOR_INV_CONST_COLOR: return GEN6_BLENDFACTOR_INV_CONST_COLOR;
  281. case PIPE_BLENDFACTOR_INV_CONST_ALPHA: return GEN6_BLENDFACTOR_INV_CONST_ALPHA;
  282. case PIPE_BLENDFACTOR_INV_SRC1_COLOR: return GEN6_BLENDFACTOR_INV_SRC1_COLOR;
  283. case PIPE_BLENDFACTOR_INV_SRC1_ALPHA: return GEN6_BLENDFACTOR_INV_SRC1_ALPHA;
  284. default:
  285. assert(!"unknown blend factor");
  286. return GEN6_BLENDFACTOR_ONE;
  287. }
  288. }
  289. static void
  290. finalize_shader_states(struct ilo_state_vector *vec)
  291. {
  292. unsigned type;
  293. for (type = 0; type < PIPE_SHADER_TYPES; type++) {
  294. struct ilo_shader_state *shader;
  295. uint32_t state;
  296. switch (type) {
  297. case PIPE_SHADER_VERTEX:
  298. shader = vec->vs;
  299. state = ILO_DIRTY_VS;
  300. break;
  301. case PIPE_SHADER_GEOMETRY:
  302. shader = vec->gs;
  303. state = ILO_DIRTY_GS;
  304. break;
  305. case PIPE_SHADER_FRAGMENT:
  306. shader = vec->fs;
  307. state = ILO_DIRTY_FS;
  308. break;
  309. default:
  310. shader = NULL;
  311. state = 0;
  312. break;
  313. }
  314. if (!shader)
  315. continue;
  316. /* compile if the shader or the states it depends on changed */
  317. if (vec->dirty & state) {
  318. ilo_shader_select_kernel(shader, vec, ILO_DIRTY_ALL);
  319. }
  320. else if (ilo_shader_select_kernel(shader, vec, vec->dirty)) {
  321. /* mark the state dirty if a new kernel is selected */
  322. vec->dirty |= state;
  323. }
  324. /* need to setup SBE for FS */
  325. if (type == PIPE_SHADER_FRAGMENT && vec->dirty &
  326. (state | ILO_DIRTY_GS | ILO_DIRTY_VS | ILO_DIRTY_RASTERIZER)) {
  327. if (ilo_shader_select_kernel_sbe(shader,
  328. (vec->gs) ? vec->gs : vec->vs, vec->rasterizer))
  329. vec->dirty |= state;
  330. }
  331. }
  332. }
  333. static void
  334. finalize_cbuf_state(struct ilo_context *ilo,
  335. struct ilo_cbuf_state *cbuf,
  336. const struct ilo_shader_state *sh)
  337. {
  338. uint32_t upload_mask = cbuf->enabled_mask;
  339. /* skip CBUF0 if the kernel does not need it */
  340. upload_mask &=
  341. ~ilo_shader_get_kernel_param(sh, ILO_KERNEL_SKIP_CBUF0_UPLOAD);
  342. while (upload_mask) {
  343. unsigned offset, i;
  344. i = u_bit_scan(&upload_mask);
  345. /* no need to upload */
  346. if (cbuf->cso[i].resource)
  347. continue;
  348. u_upload_data(ilo->uploader, 0, cbuf->cso[i].info.size, 16,
  349. cbuf->cso[i].user_buffer, &offset, &cbuf->cso[i].resource);
  350. cbuf->cso[i].info.vma = ilo_resource_get_vma(cbuf->cso[i].resource);
  351. cbuf->cso[i].info.offset = offset;
  352. memset(&cbuf->cso[i].surface, 0, sizeof(cbuf->cso[i].surface));
  353. ilo_state_surface_init_for_buffer(&cbuf->cso[i].surface,
  354. ilo->dev, &cbuf->cso[i].info);
  355. ilo->state_vector.dirty |= ILO_DIRTY_CBUF;
  356. }
  357. }
  358. static void
  359. finalize_constant_buffers(struct ilo_context *ilo)
  360. {
  361. struct ilo_state_vector *vec = &ilo->state_vector;
  362. if (vec->dirty & (ILO_DIRTY_CBUF | ILO_DIRTY_VS))
  363. finalize_cbuf_state(ilo, &vec->cbuf[PIPE_SHADER_VERTEX], vec->vs);
  364. if (ilo->state_vector.dirty & (ILO_DIRTY_CBUF | ILO_DIRTY_FS))
  365. finalize_cbuf_state(ilo, &vec->cbuf[PIPE_SHADER_FRAGMENT], vec->fs);
  366. }
  367. static void
  368. finalize_index_buffer(struct ilo_context *ilo)
  369. {
  370. const struct ilo_dev *dev = ilo->dev;
  371. struct ilo_state_vector *vec = &ilo->state_vector;
  372. const bool need_upload = (vec->draw->indexed &&
  373. (vec->ib.state.user_buffer ||
  374. vec->ib.state.offset % vec->ib.state.index_size));
  375. struct pipe_resource *current_hw_res = NULL;
  376. struct ilo_state_index_buffer_info info;
  377. int64_t vertex_start_bias = 0;
  378. if (!(vec->dirty & ILO_DIRTY_IB) && !need_upload)
  379. return;
  380. /* make sure vec->ib.hw_resource changes when reallocated */
  381. pipe_resource_reference(&current_hw_res, vec->ib.hw_resource);
  382. if (need_upload) {
  383. const unsigned offset = vec->ib.state.index_size * vec->draw->start;
  384. const unsigned size = vec->ib.state.index_size * vec->draw->count;
  385. unsigned hw_offset;
  386. if (vec->ib.state.user_buffer) {
  387. u_upload_data(ilo->uploader, 0, size, 16,
  388. vec->ib.state.user_buffer + offset,
  389. &hw_offset, &vec->ib.hw_resource);
  390. } else {
  391. u_upload_buffer(ilo->uploader, 0,
  392. vec->ib.state.offset + offset, size, 16, vec->ib.state.buffer,
  393. &hw_offset, &vec->ib.hw_resource);
  394. }
  395. /* the HW offset should be aligned */
  396. assert(hw_offset % vec->ib.state.index_size == 0);
  397. vertex_start_bias = hw_offset / vec->ib.state.index_size;
  398. /*
  399. * INDEX[vec->draw->start] in the original buffer is INDEX[0] in the HW
  400. * resource
  401. */
  402. vertex_start_bias -= vec->draw->start;
  403. } else {
  404. pipe_resource_reference(&vec->ib.hw_resource, vec->ib.state.buffer);
  405. /* note that index size may be zero when the draw is not indexed */
  406. if (vec->draw->indexed)
  407. vertex_start_bias = vec->ib.state.offset / vec->ib.state.index_size;
  408. }
  409. vec->draw_info.vertex_start += vertex_start_bias;
  410. /* treat the IB as clean if the HW states do not change */
  411. if (vec->ib.hw_resource == current_hw_res &&
  412. vec->ib.hw_index_size == vec->ib.state.index_size)
  413. vec->dirty &= ~ILO_DIRTY_IB;
  414. else
  415. vec->ib.hw_index_size = vec->ib.state.index_size;
  416. pipe_resource_reference(&current_hw_res, NULL);
  417. memset(&info, 0, sizeof(info));
  418. if (vec->ib.hw_resource) {
  419. info.vma = ilo_resource_get_vma(vec->ib.hw_resource);
  420. info.size = info.vma->vm_size;
  421. info.format = ilo_translate_index_size(vec->ib.hw_index_size);
  422. }
  423. ilo_state_index_buffer_set_info(&vec->ib.ib, dev, &info);
  424. }
  425. static void
  426. finalize_vertex_elements(struct ilo_context *ilo)
  427. {
  428. const struct ilo_dev *dev = ilo->dev;
  429. struct ilo_state_vector *vec = &ilo->state_vector;
  430. struct ilo_ve_state *ve = vec->ve;
  431. const bool last_element_edge_flag = (vec->vs &&
  432. ilo_shader_get_kernel_param(vec->vs, ILO_KERNEL_VS_INPUT_EDGEFLAG));
  433. const bool prepend_vertexid = (vec->vs &&
  434. ilo_shader_get_kernel_param(vec->vs, ILO_KERNEL_VS_INPUT_VERTEXID));
  435. const bool prepend_instanceid = (vec->vs &&
  436. ilo_shader_get_kernel_param(vec->vs,
  437. ILO_KERNEL_VS_INPUT_INSTANCEID));
  438. const enum gen_index_format index_format = (vec->draw->indexed) ?
  439. ilo_translate_index_size(vec->ib.state.index_size) : GEN6_INDEX_DWORD;
  440. /* check for non-orthogonal states */
  441. if (ve->vf_params.cv_topology != vec->draw_info.topology ||
  442. ve->vf_params.prepend_vertexid != prepend_vertexid ||
  443. ve->vf_params.prepend_instanceid != prepend_instanceid ||
  444. ve->vf_params.last_element_edge_flag != last_element_edge_flag ||
  445. ve->vf_params.cv_index_format != index_format ||
  446. ve->vf_params.cut_index_enable != vec->draw->primitive_restart ||
  447. ve->vf_params.cut_index != vec->draw->restart_index) {
  448. ve->vf_params.cv_topology = vec->draw_info.topology;
  449. ve->vf_params.prepend_vertexid = prepend_vertexid;
  450. ve->vf_params.prepend_instanceid = prepend_instanceid;
  451. ve->vf_params.last_element_edge_flag = last_element_edge_flag;
  452. ve->vf_params.cv_index_format = index_format;
  453. ve->vf_params.cut_index_enable = vec->draw->primitive_restart;
  454. ve->vf_params.cut_index = vec->draw->restart_index;
  455. ilo_state_vf_set_params(&ve->vf, dev, &ve->vf_params);
  456. vec->dirty |= ILO_DIRTY_VE;
  457. }
  458. }
  459. static void
  460. finalize_vertex_buffers(struct ilo_context *ilo)
  461. {
  462. const struct ilo_dev *dev = ilo->dev;
  463. struct ilo_state_vector *vec = &ilo->state_vector;
  464. struct ilo_state_vertex_buffer_info info;
  465. unsigned i;
  466. if (!(vec->dirty & (ILO_DIRTY_VE | ILO_DIRTY_VB)))
  467. return;
  468. memset(&info, 0, sizeof(info));
  469. for (i = 0; i < vec->ve->vb_count; i++) {
  470. const unsigned pipe_idx = vec->ve->vb_mapping[i];
  471. const struct pipe_vertex_buffer *cso = &vec->vb.states[pipe_idx];
  472. if (cso->buffer) {
  473. info.vma = ilo_resource_get_vma(cso->buffer);
  474. info.offset = cso->buffer_offset;
  475. info.size = info.vma->vm_size - cso->buffer_offset;
  476. info.stride = cso->stride;
  477. } else {
  478. memset(&info, 0, sizeof(info));
  479. }
  480. ilo_state_vertex_buffer_set_info(&vec->vb.vb[i], dev, &info);
  481. }
  482. }
  483. static void
  484. finalize_urb(struct ilo_context *ilo)
  485. {
  486. const uint16_t attr_size = sizeof(uint32_t) * 4;
  487. const struct ilo_dev *dev = ilo->dev;
  488. struct ilo_state_vector *vec = &ilo->state_vector;
  489. struct ilo_state_urb_info info;
  490. if (!(vec->dirty & (ILO_DIRTY_VE | ILO_DIRTY_VS |
  491. ILO_DIRTY_GS | ILO_DIRTY_FS)))
  492. return;
  493. memset(&info, 0, sizeof(info));
  494. info.ve_entry_size = attr_size * ilo_state_vf_get_attr_count(&vec->ve->vf);
  495. if (vec->vs) {
  496. info.vs_const_data = (bool)
  497. (ilo_shader_get_kernel_param(vec->vs, ILO_KERNEL_PCB_CBUF0_SIZE) +
  498. ilo_shader_get_kernel_param(vec->vs, ILO_KERNEL_VS_PCB_UCP_SIZE));
  499. info.vs_entry_size = attr_size *
  500. ilo_shader_get_kernel_param(vec->vs, ILO_KERNEL_OUTPUT_COUNT);
  501. }
  502. if (vec->gs) {
  503. info.gs_const_data = (bool)
  504. ilo_shader_get_kernel_param(vec->gs, ILO_KERNEL_PCB_CBUF0_SIZE);
  505. /*
  506. * From the Ivy Bridge PRM, volume 2 part 1, page 189:
  507. *
  508. * "All outputs of a GS thread will be stored in the single GS
  509. * thread output URB entry."
  510. *
  511. * TODO
  512. */
  513. info.gs_entry_size = attr_size *
  514. ilo_shader_get_kernel_param(vec->gs, ILO_KERNEL_OUTPUT_COUNT);
  515. }
  516. if (vec->fs) {
  517. info.ps_const_data = (bool)
  518. ilo_shader_get_kernel_param(vec->fs, ILO_KERNEL_PCB_CBUF0_SIZE);
  519. }
  520. ilo_state_urb_set_info(&vec->urb, dev, &info);
  521. }
  522. static void
  523. finalize_viewport(struct ilo_context *ilo)
  524. {
  525. const struct ilo_dev *dev = ilo->dev;
  526. struct ilo_state_vector *vec = &ilo->state_vector;
  527. if (vec->dirty & ILO_DIRTY_VIEWPORT) {
  528. ilo_state_viewport_set_params(&vec->viewport.vp,
  529. dev, &vec->viewport.params, false);
  530. } else if (vec->dirty & ILO_DIRTY_SCISSOR) {
  531. ilo_state_viewport_set_params(&vec->viewport.vp,
  532. dev, &vec->viewport.params, true);
  533. vec->dirty |= ILO_DIRTY_VIEWPORT;
  534. }
  535. }
  536. static bool
  537. can_enable_gb_test(const struct ilo_rasterizer_state *rasterizer,
  538. const struct ilo_viewport_state *viewport,
  539. const struct ilo_fb_state *fb)
  540. {
  541. unsigned i;
  542. /*
  543. * There are several reasons that guard band test should be disabled
  544. *
  545. * - GL wide points (to avoid partially visibie object)
  546. * - GL wide or AA lines (to avoid partially visibie object)
  547. * - missing 2D clipping
  548. */
  549. if (rasterizer->state.point_size_per_vertex ||
  550. rasterizer->state.point_size > 1.0f ||
  551. rasterizer->state.line_width > 1.0f ||
  552. rasterizer->state.line_smooth)
  553. return false;
  554. for (i = 0; i < viewport->params.count; i++) {
  555. const struct ilo_state_viewport_matrix_info *mat =
  556. &viewport->matrices[i];
  557. float min_x, max_x, min_y, max_y;
  558. min_x = -1.0f * fabsf(mat->scale[0]) + mat->translate[0];
  559. max_x = 1.0f * fabsf(mat->scale[0]) + mat->translate[0];
  560. min_y = -1.0f * fabsf(mat->scale[1]) + mat->translate[1];
  561. max_y = 1.0f * fabsf(mat->scale[1]) + mat->translate[1];
  562. if (min_x > 0.0f || max_x < fb->state.width ||
  563. min_y > 0.0f || max_y < fb->state.height)
  564. return false;
  565. }
  566. return true;
  567. }
  568. static void
  569. finalize_rasterizer(struct ilo_context *ilo)
  570. {
  571. const struct ilo_dev *dev = ilo->dev;
  572. struct ilo_state_vector *vec = &ilo->state_vector;
  573. struct ilo_rasterizer_state *rasterizer = vec->rasterizer;
  574. struct ilo_state_raster_info *info = &vec->rasterizer->info;
  575. const bool gb_test_enable =
  576. can_enable_gb_test(rasterizer, &vec->viewport, &vec->fb);
  577. const bool multisample =
  578. (rasterizer->state.multisample && vec->fb.num_samples > 1);
  579. const uint8_t barycentric_interps = ilo_shader_get_kernel_param(vec->fs,
  580. ILO_KERNEL_FS_BARYCENTRIC_INTERPOLATIONS);
  581. /* check for non-orthogonal states */
  582. if (info->clip.viewport_count != vec->viewport.params.count ||
  583. info->clip.gb_test_enable != gb_test_enable ||
  584. info->setup.msaa_enable != multisample ||
  585. info->setup.line_msaa_enable != multisample ||
  586. info->tri.depth_offset_format != vec->fb.depth_offset_format ||
  587. info->scan.sample_count != vec->fb.num_samples ||
  588. info->scan.sample_mask != vec->sample_mask ||
  589. info->scan.barycentric_interps != barycentric_interps ||
  590. info->params.any_integer_rt != vec->fb.has_integer_rt ||
  591. info->params.hiz_enable != vec->fb.has_hiz) {
  592. info->clip.viewport_count = vec->viewport.params.count;
  593. info->clip.gb_test_enable = gb_test_enable;
  594. info->setup.msaa_enable = multisample;
  595. info->setup.line_msaa_enable = multisample;
  596. info->tri.depth_offset_format = vec->fb.depth_offset_format;
  597. info->scan.sample_count = vec->fb.num_samples;
  598. info->scan.sample_mask = vec->sample_mask;
  599. info->scan.barycentric_interps = barycentric_interps;
  600. info->params.any_integer_rt = vec->fb.has_integer_rt;
  601. info->params.hiz_enable = vec->fb.has_hiz;
  602. ilo_state_raster_set_info(&rasterizer->rs, dev, &rasterizer->info);
  603. vec->dirty |= ILO_DIRTY_RASTERIZER;
  604. }
  605. }
  606. static bool
  607. finalize_blend_rt(struct ilo_context *ilo)
  608. {
  609. struct ilo_state_vector *vec = &ilo->state_vector;
  610. const struct ilo_fb_state *fb = &vec->fb;
  611. struct ilo_blend_state *blend = vec->blend;
  612. struct ilo_state_cc_blend_info *info = &vec->blend->info.blend;
  613. bool changed = false;
  614. unsigned i;
  615. if (!(vec->dirty & (ILO_DIRTY_FB | ILO_DIRTY_BLEND)))
  616. return false;
  617. /* set up one for dummy RT writes */
  618. if (!fb->state.nr_cbufs) {
  619. if (info->rt != &blend->dummy_rt) {
  620. info->rt = &blend->dummy_rt;
  621. info->rt_count = 1;
  622. changed = true;
  623. }
  624. return changed;
  625. }
  626. if (info->rt != blend->effective_rt ||
  627. info->rt_count != fb->state.nr_cbufs) {
  628. info->rt = blend->effective_rt;
  629. info->rt_count = fb->state.nr_cbufs;
  630. changed = true;
  631. }
  632. for (i = 0; i < fb->state.nr_cbufs; i++) {
  633. const struct ilo_fb_blend_caps *caps = &fb->blend_caps[i];
  634. struct ilo_state_cc_blend_rt_info *rt = &blend->effective_rt[i];
  635. /* ignore logicop when not UNORM */
  636. const bool logicop_enable =
  637. (blend->rt[i].logicop_enable && caps->is_unorm);
  638. if (rt->cv_is_unorm != caps->is_unorm ||
  639. rt->cv_is_integer != caps->is_integer ||
  640. rt->logicop_enable != logicop_enable ||
  641. rt->force_dst_alpha_one != caps->force_dst_alpha_one) {
  642. rt->cv_is_unorm = caps->is_unorm;
  643. rt->cv_is_integer = caps->is_integer;
  644. rt->logicop_enable = logicop_enable;
  645. rt->force_dst_alpha_one = caps->force_dst_alpha_one;
  646. changed = true;
  647. }
  648. }
  649. return changed;
  650. }
  651. static void
  652. finalize_blend(struct ilo_context *ilo)
  653. {
  654. const struct ilo_dev *dev = ilo->dev;
  655. struct ilo_state_vector *vec = &ilo->state_vector;
  656. struct ilo_blend_state *blend = vec->blend;
  657. struct ilo_state_cc_info *info = &blend->info;
  658. const bool sample_count_one = (vec->fb.num_samples <= 1);
  659. const bool float_source0_alpha =
  660. (!vec->fb.state.nr_cbufs || !vec->fb.state.cbufs[0] ||
  661. !util_format_is_pure_integer(vec->fb.state.cbufs[0]->format));
  662. /* check for non-orthogonal states */
  663. if (finalize_blend_rt(ilo) ||
  664. info->alpha.cv_sample_count_one != sample_count_one ||
  665. info->alpha.cv_float_source0_alpha != float_source0_alpha ||
  666. info->alpha.test_enable != vec->dsa->alpha_test ||
  667. info->alpha.test_func != vec->dsa->alpha_func ||
  668. memcmp(&info->stencil, &vec->dsa->stencil, sizeof(info->stencil)) ||
  669. memcmp(&info->depth, &vec->dsa->depth, sizeof(info->depth)) ||
  670. memcmp(&info->params, &vec->cc_params, sizeof(info->params))) {
  671. info->alpha.cv_sample_count_one = sample_count_one;
  672. info->alpha.cv_float_source0_alpha = float_source0_alpha;
  673. info->alpha.test_enable = vec->dsa->alpha_test;
  674. info->alpha.test_func = vec->dsa->alpha_func;
  675. info->stencil = vec->dsa->stencil;
  676. info->depth = vec->dsa->depth;
  677. info->params = vec->cc_params;
  678. ilo_state_cc_set_info(&blend->cc, dev, info);
  679. blend->alpha_may_kill = (info->alpha.alpha_to_coverage ||
  680. info->alpha.test_enable);
  681. vec->dirty |= ILO_DIRTY_BLEND;
  682. }
  683. }
  684. /**
  685. * Finalize states. Some states depend on other states and are
  686. * incomplete/invalid until finalized.
  687. */
  688. void
  689. ilo_finalize_3d_states(struct ilo_context *ilo,
  690. const struct pipe_draw_info *draw)
  691. {
  692. ilo->state_vector.draw = draw;
  693. ilo->state_vector.draw_info.topology = ilo_translate_draw_mode(draw->mode);
  694. ilo->state_vector.draw_info.indexed = draw->indexed;
  695. ilo->state_vector.draw_info.vertex_count = draw->count;
  696. ilo->state_vector.draw_info.vertex_start = draw->start;
  697. ilo->state_vector.draw_info.instance_count = draw->instance_count;
  698. ilo->state_vector.draw_info.instance_start = draw->start_instance;
  699. ilo->state_vector.draw_info.vertex_base = draw->index_bias;
  700. finalize_blend(ilo);
  701. finalize_shader_states(&ilo->state_vector);
  702. finalize_constant_buffers(ilo);
  703. finalize_index_buffer(ilo);
  704. finalize_vertex_elements(ilo);
  705. finalize_vertex_buffers(ilo);
  706. finalize_urb(ilo);
  707. finalize_rasterizer(ilo);
  708. finalize_viewport(ilo);
  709. u_upload_unmap(ilo->uploader);
  710. }
  711. static void
  712. finalize_global_binding(struct ilo_state_vector *vec)
  713. {
  714. struct ilo_shader_state *cs = vec->cs;
  715. int base, count, shift;
  716. int i;
  717. count = ilo_shader_get_kernel_param(cs,
  718. ILO_KERNEL_CS_SURFACE_GLOBAL_COUNT);
  719. if (!count)
  720. return;
  721. base = ilo_shader_get_kernel_param(cs, ILO_KERNEL_CS_SURFACE_GLOBAL_BASE);
  722. shift = 32 - util_last_bit(base + count - 1);
  723. if (count > vec->global_binding.count)
  724. count = vec->global_binding.count;
  725. for (i = 0; i < count; i++) {
  726. struct ilo_global_binding_cso *cso =
  727. util_dynarray_element(&vec->global_binding.bindings,
  728. struct ilo_global_binding_cso, i);
  729. const uint32_t offset = *cso->handle & ((1 << shift) - 1);
  730. *cso->handle = ((base + i) << shift) | offset;
  731. }
  732. }
  733. void
  734. ilo_finalize_compute_states(struct ilo_context *ilo)
  735. {
  736. finalize_global_binding(&ilo->state_vector);
  737. }
  738. static void *
  739. ilo_create_blend_state(struct pipe_context *pipe,
  740. const struct pipe_blend_state *state)
  741. {
  742. const struct ilo_dev *dev = ilo_context(pipe)->dev;
  743. struct ilo_state_cc_info *info;
  744. struct ilo_blend_state *blend;
  745. int i;
  746. blend = CALLOC_STRUCT(ilo_blend_state);
  747. assert(blend);
  748. info = &blend->info;
  749. info->alpha.cv_float_source0_alpha = true;
  750. info->alpha.cv_sample_count_one = true;
  751. info->alpha.alpha_to_one = state->alpha_to_one;
  752. info->alpha.alpha_to_coverage = state->alpha_to_coverage;
  753. info->alpha.test_enable = false;
  754. info->alpha.test_func = GEN6_COMPAREFUNCTION_ALWAYS;
  755. info->stencil.cv_has_buffer = true;
  756. info->depth.cv_has_buffer= true;
  757. info->blend.rt = blend->effective_rt;
  758. info->blend.rt_count = 1;
  759. info->blend.dither_enable = state->dither;
  760. for (i = 0; i < ARRAY_SIZE(blend->rt); i++) {
  761. const struct pipe_rt_blend_state *rt = &state->rt[i];
  762. struct ilo_state_cc_blend_rt_info *rt_info = &blend->rt[i];
  763. rt_info->cv_has_buffer = true;
  764. rt_info->cv_is_unorm = true;
  765. rt_info->cv_is_integer = false;
  766. /* logic op takes precedence over blending */
  767. if (state->logicop_enable) {
  768. rt_info->logicop_enable = true;
  769. rt_info->logicop_func = ilo_translate_logicop(state->logicop_func);
  770. } else if (rt->blend_enable) {
  771. rt_info->blend_enable = true;
  772. rt_info->rgb_src = ilo_translate_blend_factor(rt->rgb_src_factor);
  773. rt_info->rgb_dst = ilo_translate_blend_factor(rt->rgb_dst_factor);
  774. rt_info->rgb_func = ilo_translate_blend_func(rt->rgb_func);
  775. rt_info->a_src = ilo_translate_blend_factor(rt->alpha_src_factor);
  776. rt_info->a_dst = ilo_translate_blend_factor(rt->alpha_dst_factor);
  777. rt_info->a_func = ilo_translate_blend_func(rt->alpha_func);
  778. }
  779. if (!(rt->colormask & PIPE_MASK_A))
  780. rt_info->argb_write_disables |= (1 << 3);
  781. if (!(rt->colormask & PIPE_MASK_R))
  782. rt_info->argb_write_disables |= (1 << 2);
  783. if (!(rt->colormask & PIPE_MASK_G))
  784. rt_info->argb_write_disables |= (1 << 1);
  785. if (!(rt->colormask & PIPE_MASK_B))
  786. rt_info->argb_write_disables |= (1 << 0);
  787. if (!state->independent_blend_enable) {
  788. for (i = 1; i < ARRAY_SIZE(blend->rt); i++)
  789. blend->rt[i] = *rt_info;
  790. break;
  791. }
  792. }
  793. memcpy(blend->effective_rt, blend->rt, sizeof(blend->rt));
  794. blend->dummy_rt.argb_write_disables = 0xf;
  795. if (!ilo_state_cc_init(&blend->cc, dev, &blend->info)) {
  796. FREE(blend);
  797. return NULL;
  798. }
  799. blend->dual_blend = util_blend_state_is_dual(state, 0);
  800. return blend;
  801. }
  802. static void
  803. ilo_bind_blend_state(struct pipe_context *pipe, void *state)
  804. {
  805. struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;
  806. vec->blend = state;
  807. vec->dirty |= ILO_DIRTY_BLEND;
  808. }
  809. static void
  810. ilo_delete_blend_state(struct pipe_context *pipe, void *state)
  811. {
  812. FREE(state);
  813. }
  814. static void *
  815. ilo_create_sampler_state(struct pipe_context *pipe,
  816. const struct pipe_sampler_state *state)
  817. {
  818. const struct ilo_dev *dev = ilo_context(pipe)->dev;
  819. struct ilo_sampler_cso *sampler;
  820. struct ilo_state_sampler_info info;
  821. struct ilo_state_sampler_border_info border;
  822. sampler = CALLOC_STRUCT(ilo_sampler_cso);
  823. assert(sampler);
  824. memset(&info, 0, sizeof(info));
  825. info.non_normalized = !state->normalized_coords;
  826. if (state->normalized_coords) {
  827. info.lod_bias = state->lod_bias;
  828. info.min_lod = state->min_lod;
  829. info.max_lod = state->max_lod;
  830. info.mip_filter = ilo_translate_mip_filter(state->min_mip_filter);
  831. } else {
  832. /* work around a bug in util_blitter */
  833. info.mip_filter = GEN6_MIPFILTER_NONE;
  834. }
  835. if (state->max_anisotropy) {
  836. info.min_filter = GEN6_MAPFILTER_ANISOTROPIC;
  837. info.mag_filter = GEN6_MAPFILTER_ANISOTROPIC;
  838. } else {
  839. info.min_filter = ilo_translate_img_filter(state->min_img_filter);
  840. info.mag_filter = ilo_translate_img_filter(state->mag_img_filter);
  841. }
  842. info.max_anisotropy = ilo_translate_max_anisotropy(state->max_anisotropy);
  843. /* use LOD 0 when no mipmapping (see sampler_set_gen6_SAMPLER_STATE()) */
  844. if (info.mip_filter == GEN6_MIPFILTER_NONE && info.min_lod > 0.0f) {
  845. info.min_lod = 0.0f;
  846. info.mag_filter = info.min_filter;
  847. }
  848. if (state->seamless_cube_map) {
  849. if (state->min_img_filter == PIPE_TEX_FILTER_NEAREST ||
  850. state->mag_img_filter == PIPE_TEX_FILTER_NEAREST) {
  851. info.tcx_ctrl = GEN6_TEXCOORDMODE_CLAMP;
  852. info.tcy_ctrl = GEN6_TEXCOORDMODE_CLAMP;
  853. info.tcz_ctrl = GEN6_TEXCOORDMODE_CLAMP;
  854. } else {
  855. info.tcx_ctrl = GEN6_TEXCOORDMODE_CUBE;
  856. info.tcy_ctrl = GEN6_TEXCOORDMODE_CUBE;
  857. info.tcz_ctrl = GEN6_TEXCOORDMODE_CUBE;
  858. }
  859. } else {
  860. info.tcx_ctrl = ilo_translate_address_wrap(state->wrap_s);
  861. info.tcy_ctrl = ilo_translate_address_wrap(state->wrap_t);
  862. info.tcz_ctrl = ilo_translate_address_wrap(state->wrap_r);
  863. if (ilo_dev_gen(dev) < ILO_GEN(8)) {
  864. /*
  865. * For nearest filtering, PIPE_TEX_WRAP_CLAMP means
  866. * PIPE_TEX_WRAP_CLAMP_TO_EDGE; for linear filtering,
  867. * PIPE_TEX_WRAP_CLAMP means PIPE_TEX_WRAP_CLAMP_TO_BORDER while
  868. * additionally clamping the texture coordinates to [0.0, 1.0].
  869. *
  870. * PIPE_TEX_WRAP_CLAMP is not supported natively until Gen8. The
  871. * clamping has to be taken care of in the shaders. There are two
  872. * filters here, but let the minification one has a say.
  873. */
  874. const bool clamp_is_to_edge =
  875. (state->min_img_filter == PIPE_TEX_FILTER_NEAREST);
  876. if (clamp_is_to_edge) {
  877. if (info.tcx_ctrl == GEN8_TEXCOORDMODE_HALF_BORDER)
  878. info.tcx_ctrl = GEN6_TEXCOORDMODE_CLAMP;
  879. if (info.tcy_ctrl == GEN8_TEXCOORDMODE_HALF_BORDER)
  880. info.tcy_ctrl = GEN6_TEXCOORDMODE_CLAMP;
  881. if (info.tcz_ctrl == GEN8_TEXCOORDMODE_HALF_BORDER)
  882. info.tcz_ctrl = GEN6_TEXCOORDMODE_CLAMP;
  883. } else {
  884. if (info.tcx_ctrl == GEN8_TEXCOORDMODE_HALF_BORDER) {
  885. info.tcx_ctrl = GEN6_TEXCOORDMODE_CLAMP_BORDER;
  886. sampler->saturate_s = true;
  887. }
  888. if (info.tcy_ctrl == GEN8_TEXCOORDMODE_HALF_BORDER) {
  889. info.tcy_ctrl = GEN6_TEXCOORDMODE_CLAMP_BORDER;
  890. sampler->saturate_t = true;
  891. }
  892. if (info.tcz_ctrl == GEN8_TEXCOORDMODE_HALF_BORDER) {
  893. info.tcz_ctrl = GEN6_TEXCOORDMODE_CLAMP_BORDER;
  894. sampler->saturate_r = true;
  895. }
  896. }
  897. }
  898. }
  899. if (state->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE)
  900. info.shadow_func = ilo_translate_shadow_func(state->compare_func);
  901. ilo_state_sampler_init(&sampler->sampler, dev, &info);
  902. memset(&border, 0, sizeof(border));
  903. memcpy(border.rgba.f, state->border_color.f, sizeof(border.rgba.f));
  904. ilo_state_sampler_border_init(&sampler->border, dev, &border);
  905. return sampler;
  906. }
  907. static void
  908. ilo_bind_sampler_states(struct pipe_context *pipe,
  909. enum pipe_shader_type shader,
  910. unsigned start, unsigned count, void **samplers)
  911. {
  912. struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;
  913. struct ilo_sampler_state *dst = &vec->sampler[shader];
  914. bool changed = false;
  915. unsigned i;
  916. assert(start + count <= ARRAY_SIZE(dst->cso));
  917. if (samplers) {
  918. for (i = 0; i < count; i++) {
  919. if (dst->cso[start + i] != samplers[i]) {
  920. dst->cso[start + i] = samplers[i];
  921. /*
  922. * This function is sometimes called to reduce the number of bound
  923. * samplers. Do not consider that as a state change (and create a
  924. * new array of SAMPLER_STATE).
  925. */
  926. if (samplers[i])
  927. changed = true;
  928. }
  929. }
  930. }
  931. else {
  932. for (i = 0; i < count; i++)
  933. dst->cso[start + i] = NULL;
  934. }
  935. if (changed) {
  936. switch (shader) {
  937. case PIPE_SHADER_VERTEX:
  938. vec->dirty |= ILO_DIRTY_SAMPLER_VS;
  939. break;
  940. case PIPE_SHADER_GEOMETRY:
  941. vec->dirty |= ILO_DIRTY_SAMPLER_GS;
  942. break;
  943. case PIPE_SHADER_FRAGMENT:
  944. vec->dirty |= ILO_DIRTY_SAMPLER_FS;
  945. break;
  946. case PIPE_SHADER_COMPUTE:
  947. vec->dirty |= ILO_DIRTY_SAMPLER_CS;
  948. break;
  949. }
  950. }
  951. }
  952. static void
  953. ilo_delete_sampler_state(struct pipe_context *pipe, void *state)
  954. {
  955. FREE(state);
  956. }
  957. static void *
  958. ilo_create_rasterizer_state(struct pipe_context *pipe,
  959. const struct pipe_rasterizer_state *state)
  960. {
  961. const struct ilo_dev *dev = ilo_context(pipe)->dev;
  962. struct ilo_rasterizer_state *rast;
  963. struct ilo_state_raster_info *info;
  964. rast = CALLOC_STRUCT(ilo_rasterizer_state);
  965. assert(rast);
  966. rast->state = *state;
  967. info = &rast->info;
  968. info->clip.clip_enable = true;
  969. info->clip.stats_enable = true;
  970. info->clip.viewport_count = 1;
  971. info->clip.force_rtaindex_zero = true;
  972. info->clip.user_clip_enables = state->clip_plane_enable;
  973. info->clip.gb_test_enable = true;
  974. info->clip.xy_test_enable = true;
  975. info->clip.z_far_enable = state->depth_clip;
  976. info->clip.z_near_enable = state->depth_clip;
  977. info->clip.z_near_zero = state->clip_halfz;
  978. info->setup.first_vertex_provoking = state->flatshade_first;
  979. info->setup.viewport_transform = true;
  980. info->setup.scissor_enable = state->scissor;
  981. info->setup.msaa_enable = false;
  982. info->setup.line_msaa_enable = false;
  983. info->point.aa_enable = state->point_smooth;
  984. info->point.programmable_width = state->point_size_per_vertex;
  985. info->line.aa_enable = state->line_smooth;
  986. info->line.stipple_enable = state->line_stipple_enable;
  987. info->line.giq_enable = true;
  988. info->line.giq_last_pixel = state->line_last_pixel;
  989. info->tri.front_winding = ilo_translate_front_ccw(state->front_ccw);
  990. info->tri.cull_mode = ilo_translate_cull_face(state->cull_face);
  991. info->tri.fill_mode_front = ilo_translate_poly_mode(state->fill_front);
  992. info->tri.fill_mode_back = ilo_translate_poly_mode(state->fill_back);
  993. info->tri.depth_offset_format = GEN6_ZFORMAT_D24_UNORM_X8_UINT;
  994. info->tri.depth_offset_solid = state->offset_tri;
  995. info->tri.depth_offset_wireframe = state->offset_line;
  996. info->tri.depth_offset_point = state->offset_point;
  997. info->tri.poly_stipple_enable = state->poly_stipple_enable;
  998. info->scan.stats_enable = true;
  999. info->scan.sample_count = 1;
  1000. info->scan.pixloc =
  1001. ilo_translate_half_pixel_center(state->half_pixel_center);
  1002. info->scan.sample_mask = ~0u;
  1003. info->scan.zw_interp = GEN6_ZW_INTERP_PIXEL;
  1004. info->scan.barycentric_interps = GEN6_INTERP_PERSPECTIVE_PIXEL;
  1005. info->scan.earlyz_control = GEN7_EDSC_NORMAL;
  1006. info->scan.earlyz_op = ILO_STATE_RASTER_EARLYZ_NORMAL;
  1007. info->scan.earlyz_stencil_clear = false;
  1008. info->params.any_integer_rt = false;
  1009. info->params.hiz_enable = true;
  1010. info->params.point_width =
  1011. (state->point_size == 0.0f) ? 1.0f : state->point_size;
  1012. info->params.line_width =
  1013. (state->line_width == 0.0f) ? 1.0f : state->line_width;
  1014. info->params.depth_offset_scale = state->offset_scale;
  1015. /*
  1016. * Scale the constant term. The minimum representable value used by the HW
  1017. * is not large enouch to be the minimum resolvable difference.
  1018. */
  1019. info->params.depth_offset_const = state->offset_units * 2.0f;
  1020. info->params.depth_offset_clamp = state->offset_clamp;
  1021. ilo_state_raster_init(&rast->rs, dev, info);
  1022. return rast;
  1023. }
  1024. static void
  1025. ilo_bind_rasterizer_state(struct pipe_context *pipe, void *state)
  1026. {
  1027. const struct ilo_dev *dev = ilo_context(pipe)->dev;
  1028. struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;
  1029. vec->rasterizer = state;
  1030. if (vec->rasterizer) {
  1031. struct ilo_state_line_stipple_info info;
  1032. info.pattern = vec->rasterizer->state.line_stipple_pattern;
  1033. info.repeat_count = vec->rasterizer->state.line_stipple_factor + 1;
  1034. ilo_state_line_stipple_set_info(&vec->line_stipple, dev, &info);
  1035. }
  1036. vec->dirty |= ILO_DIRTY_RASTERIZER;
  1037. }
  1038. static void
  1039. ilo_delete_rasterizer_state(struct pipe_context *pipe, void *state)
  1040. {
  1041. FREE(state);
  1042. }
  1043. static void *
  1044. ilo_create_depth_stencil_alpha_state(struct pipe_context *pipe,
  1045. const struct pipe_depth_stencil_alpha_state *state)
  1046. {
  1047. struct ilo_dsa_state *dsa;
  1048. int i;
  1049. dsa = CALLOC_STRUCT(ilo_dsa_state);
  1050. assert(dsa);
  1051. dsa->depth.cv_has_buffer = true;
  1052. dsa->depth.test_enable = state->depth.enabled;
  1053. dsa->depth.write_enable = state->depth.writemask;
  1054. dsa->depth.test_func = ilo_translate_compare_func(state->depth.func);
  1055. dsa->stencil.cv_has_buffer = true;
  1056. for (i = 0; i < ARRAY_SIZE(state->stencil); i++) {
  1057. const struct pipe_stencil_state *stencil = &state->stencil[i];
  1058. struct ilo_state_cc_stencil_op_info *op;
  1059. if (!stencil->enabled)
  1060. break;
  1061. if (i == 0) {
  1062. dsa->stencil.test_enable = true;
  1063. dsa->stencil_front.test_mask = stencil->valuemask;
  1064. dsa->stencil_front.write_mask = stencil->writemask;
  1065. op = &dsa->stencil.front;
  1066. } else {
  1067. dsa->stencil.twosided_enable = true;
  1068. dsa->stencil_back.test_mask = stencil->valuemask;
  1069. dsa->stencil_back.write_mask = stencil->writemask;
  1070. op = &dsa->stencil.back;
  1071. }
  1072. op->test_func = ilo_translate_compare_func(stencil->func);
  1073. op->fail_op = ilo_translate_stencil_op(stencil->fail_op);
  1074. op->zfail_op = ilo_translate_stencil_op(stencil->zfail_op);
  1075. op->zpass_op = ilo_translate_stencil_op(stencil->zpass_op);
  1076. }
  1077. dsa->alpha_test = state->alpha.enabled;
  1078. dsa->alpha_ref = state->alpha.ref_value;
  1079. dsa->alpha_func = ilo_translate_compare_func(state->alpha.func);
  1080. return dsa;
  1081. }
  1082. static void
  1083. ilo_bind_depth_stencil_alpha_state(struct pipe_context *pipe, void *state)
  1084. {
  1085. struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;
  1086. vec->dsa = state;
  1087. if (vec->dsa) {
  1088. vec->cc_params.alpha_ref = vec->dsa->alpha_ref;
  1089. vec->cc_params.stencil_front.test_mask =
  1090. vec->dsa->stencil_front.test_mask;
  1091. vec->cc_params.stencil_front.write_mask =
  1092. vec->dsa->stencil_front.write_mask;
  1093. vec->cc_params.stencil_back.test_mask =
  1094. vec->dsa->stencil_back.test_mask;
  1095. vec->cc_params.stencil_back.write_mask =
  1096. vec->dsa->stencil_back.write_mask;
  1097. }
  1098. vec->dirty |= ILO_DIRTY_DSA;
  1099. }
  1100. static void
  1101. ilo_delete_depth_stencil_alpha_state(struct pipe_context *pipe, void *state)
  1102. {
  1103. FREE(state);
  1104. }
  1105. static void *
  1106. ilo_create_fs_state(struct pipe_context *pipe,
  1107. const struct pipe_shader_state *state)
  1108. {
  1109. struct ilo_context *ilo = ilo_context(pipe);
  1110. struct ilo_shader_state *shader;
  1111. shader = ilo_shader_create_fs(ilo->dev, state, &ilo->state_vector);
  1112. assert(shader);
  1113. ilo_shader_cache_add(ilo->shader_cache, shader);
  1114. return shader;
  1115. }
  1116. static void
  1117. ilo_bind_fs_state(struct pipe_context *pipe, void *state)
  1118. {
  1119. struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;
  1120. vec->fs = state;
  1121. vec->dirty |= ILO_DIRTY_FS;
  1122. }
  1123. static void
  1124. ilo_delete_fs_state(struct pipe_context *pipe, void *state)
  1125. {
  1126. struct ilo_context *ilo = ilo_context(pipe);
  1127. struct ilo_shader_state *fs = (struct ilo_shader_state *) state;
  1128. ilo_shader_cache_remove(ilo->shader_cache, fs);
  1129. ilo_shader_destroy(fs);
  1130. }
  1131. static void *
  1132. ilo_create_vs_state(struct pipe_context *pipe,
  1133. const struct pipe_shader_state *state)
  1134. {
  1135. struct ilo_context *ilo = ilo_context(pipe);
  1136. struct ilo_shader_state *shader;
  1137. shader = ilo_shader_create_vs(ilo->dev, state, &ilo->state_vector);
  1138. assert(shader);
  1139. ilo_shader_cache_add(ilo->shader_cache, shader);
  1140. return shader;
  1141. }
  1142. static void
  1143. ilo_bind_vs_state(struct pipe_context *pipe, void *state)
  1144. {
  1145. struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;
  1146. vec->vs = state;
  1147. vec->dirty |= ILO_DIRTY_VS;
  1148. }
  1149. static void
  1150. ilo_delete_vs_state(struct pipe_context *pipe, void *state)
  1151. {
  1152. struct ilo_context *ilo = ilo_context(pipe);
  1153. struct ilo_shader_state *vs = (struct ilo_shader_state *) state;
  1154. ilo_shader_cache_remove(ilo->shader_cache, vs);
  1155. ilo_shader_destroy(vs);
  1156. }
  1157. static void *
  1158. ilo_create_gs_state(struct pipe_context *pipe,
  1159. const struct pipe_shader_state *state)
  1160. {
  1161. struct ilo_context *ilo = ilo_context(pipe);
  1162. struct ilo_shader_state *shader;
  1163. shader = ilo_shader_create_gs(ilo->dev, state, &ilo->state_vector);
  1164. assert(shader);
  1165. ilo_shader_cache_add(ilo->shader_cache, shader);
  1166. return shader;
  1167. }
  1168. static void
  1169. ilo_bind_gs_state(struct pipe_context *pipe, void *state)
  1170. {
  1171. struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;
  1172. /* util_blitter may set this unnecessarily */
  1173. if (vec->gs == state)
  1174. return;
  1175. vec->gs = state;
  1176. vec->dirty |= ILO_DIRTY_GS;
  1177. }
  1178. static void
  1179. ilo_delete_gs_state(struct pipe_context *pipe, void *state)
  1180. {
  1181. struct ilo_context *ilo = ilo_context(pipe);
  1182. struct ilo_shader_state *gs = (struct ilo_shader_state *) state;
  1183. ilo_shader_cache_remove(ilo->shader_cache, gs);
  1184. ilo_shader_destroy(gs);
  1185. }
  1186. static void *
  1187. ilo_create_vertex_elements_state(struct pipe_context *pipe,
  1188. unsigned num_elements,
  1189. const struct pipe_vertex_element *elements)
  1190. {
  1191. const struct ilo_dev *dev = ilo_context(pipe)->dev;
  1192. struct ilo_state_vf_element_info vf_elements[PIPE_MAX_ATTRIBS];
  1193. unsigned instance_divisors[PIPE_MAX_ATTRIBS];
  1194. struct ilo_state_vf_info vf_info;
  1195. struct ilo_ve_state *ve;
  1196. unsigned i;
  1197. ve = CALLOC_STRUCT(ilo_ve_state);
  1198. assert(ve);
  1199. for (i = 0; i < num_elements; i++) {
  1200. const struct pipe_vertex_element *elem = &elements[i];
  1201. struct ilo_state_vf_element_info *attr = &vf_elements[i];
  1202. unsigned hw_idx;
  1203. /*
  1204. * map the pipe vb to the hardware vb, which has a fixed instance
  1205. * divisor
  1206. */
  1207. for (hw_idx = 0; hw_idx < ve->vb_count; hw_idx++) {
  1208. if (ve->vb_mapping[hw_idx] == elem->vertex_buffer_index &&
  1209. instance_divisors[hw_idx] == elem->instance_divisor)
  1210. break;
  1211. }
  1212. /* create one if there is no matching hardware vb */
  1213. if (hw_idx >= ve->vb_count) {
  1214. hw_idx = ve->vb_count++;
  1215. ve->vb_mapping[hw_idx] = elem->vertex_buffer_index;
  1216. instance_divisors[hw_idx] = elem->instance_divisor;
  1217. }
  1218. attr->buffer = hw_idx;
  1219. attr->vertex_offset = elem->src_offset;
  1220. attr->format = ilo_format_translate_vertex(dev, elem->src_format);
  1221. attr->format_size = util_format_get_blocksize(elem->src_format);
  1222. attr->component_count = util_format_get_nr_components(elem->src_format);
  1223. attr->is_integer = util_format_is_pure_integer(elem->src_format);
  1224. attr->instancing_enable = (elem->instance_divisor != 0);
  1225. attr->instancing_step_rate = elem->instance_divisor;
  1226. }
  1227. memset(&vf_info, 0, sizeof(vf_info));
  1228. vf_info.data = ve->vf_data;
  1229. vf_info.data_size = sizeof(ve->vf_data);
  1230. vf_info.elements = vf_elements;
  1231. vf_info.element_count = num_elements;
  1232. /* vf_info.params and ve->vf_params are both zeroed */
  1233. if (!ilo_state_vf_init(&ve->vf, dev, &vf_info)) {
  1234. FREE(ve);
  1235. return NULL;
  1236. }
  1237. return ve;
  1238. }
  1239. static void
  1240. ilo_bind_vertex_elements_state(struct pipe_context *pipe, void *state)
  1241. {
  1242. struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;
  1243. vec->ve = state;
  1244. vec->dirty |= ILO_DIRTY_VE;
  1245. }
  1246. static void
  1247. ilo_delete_vertex_elements_state(struct pipe_context *pipe, void *state)
  1248. {
  1249. struct ilo_ve_state *ve = state;
  1250. FREE(ve);
  1251. }
  1252. static void
  1253. ilo_set_blend_color(struct pipe_context *pipe,
  1254. const struct pipe_blend_color *state)
  1255. {
  1256. struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;
  1257. memcpy(vec->cc_params.blend_rgba, state->color, sizeof(state->color));
  1258. vec->dirty |= ILO_DIRTY_BLEND_COLOR;
  1259. }
  1260. static void
  1261. ilo_set_stencil_ref(struct pipe_context *pipe,
  1262. const struct pipe_stencil_ref *state)
  1263. {
  1264. struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;
  1265. /* util_blitter may set this unnecessarily */
  1266. if (!memcmp(&vec->stencil_ref, state, sizeof(*state)))
  1267. return;
  1268. vec->stencil_ref = *state;
  1269. vec->cc_params.stencil_front.test_ref = state->ref_value[0];
  1270. vec->cc_params.stencil_back.test_ref = state->ref_value[1];
  1271. vec->dirty |= ILO_DIRTY_STENCIL_REF;
  1272. }
  1273. static void
  1274. ilo_set_sample_mask(struct pipe_context *pipe,
  1275. unsigned sample_mask)
  1276. {
  1277. struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;
  1278. /* util_blitter may set this unnecessarily */
  1279. if (vec->sample_mask == sample_mask)
  1280. return;
  1281. vec->sample_mask = sample_mask;
  1282. vec->dirty |= ILO_DIRTY_SAMPLE_MASK;
  1283. }
  1284. static void
  1285. ilo_set_clip_state(struct pipe_context *pipe,
  1286. const struct pipe_clip_state *state)
  1287. {
  1288. struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;
  1289. vec->clip = *state;
  1290. vec->dirty |= ILO_DIRTY_CLIP;
  1291. }
  1292. static void
  1293. ilo_set_constant_buffer(struct pipe_context *pipe,
  1294. uint shader, uint index,
  1295. const struct pipe_constant_buffer *buf)
  1296. {
  1297. const struct ilo_dev *dev = ilo_context(pipe)->dev;
  1298. struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;
  1299. struct ilo_cbuf_state *cbuf = &vec->cbuf[shader];
  1300. const unsigned count = 1;
  1301. unsigned i;
  1302. assert(shader < ARRAY_SIZE(vec->cbuf));
  1303. assert(index + count <= ARRAY_SIZE(vec->cbuf[shader].cso));
  1304. if (buf) {
  1305. for (i = 0; i < count; i++) {
  1306. struct ilo_cbuf_cso *cso = &cbuf->cso[index + i];
  1307. pipe_resource_reference(&cso->resource, buf[i].buffer);
  1308. cso->info.access = ILO_STATE_SURFACE_ACCESS_DP_DATA;
  1309. cso->info.format = GEN6_FORMAT_R32G32B32A32_FLOAT;
  1310. cso->info.format_size = 16;
  1311. cso->info.struct_size = 16;
  1312. cso->info.readonly = true;
  1313. cso->info.size = buf[i].buffer_size;
  1314. if (buf[i].buffer) {
  1315. cso->info.vma = ilo_resource_get_vma(buf[i].buffer);
  1316. cso->info.offset = buf[i].buffer_offset;
  1317. memset(&cso->surface, 0, sizeof(cso->surface));
  1318. ilo_state_surface_init_for_buffer(&cso->surface, dev, &cso->info);
  1319. cso->user_buffer = NULL;
  1320. cbuf->enabled_mask |= 1 << (index + i);
  1321. } else if (buf[i].user_buffer) {
  1322. cso->info.vma = NULL;
  1323. /* buffer_offset does not apply for user buffer */
  1324. cso->user_buffer = buf[i].user_buffer;
  1325. cbuf->enabled_mask |= 1 << (index + i);
  1326. } else {
  1327. cso->info.vma = NULL;
  1328. cso->info.size = 0;
  1329. cso->user_buffer = NULL;
  1330. cbuf->enabled_mask &= ~(1 << (index + i));
  1331. }
  1332. }
  1333. } else {
  1334. for (i = 0; i < count; i++) {
  1335. struct ilo_cbuf_cso *cso = &cbuf->cso[index + i];
  1336. pipe_resource_reference(&cso->resource, NULL);
  1337. cso->info.vma = NULL;
  1338. cso->info.size = 0;
  1339. cso->user_buffer = NULL;
  1340. cbuf->enabled_mask &= ~(1 << (index + i));
  1341. }
  1342. }
  1343. vec->dirty |= ILO_DIRTY_CBUF;
  1344. }
  1345. static void
  1346. fb_set_blend_caps(const struct ilo_dev *dev,
  1347. enum pipe_format format,
  1348. struct ilo_fb_blend_caps *caps)
  1349. {
  1350. const struct util_format_description *desc =
  1351. util_format_description(format);
  1352. const int ch = util_format_get_first_non_void_channel(format);
  1353. memset(caps, 0, sizeof(*caps));
  1354. if (format == PIPE_FORMAT_NONE || desc->is_mixed)
  1355. return;
  1356. caps->is_unorm = (ch >= 0 && desc->channel[ch].normalized &&
  1357. desc->channel[ch].type == UTIL_FORMAT_TYPE_UNSIGNED &&
  1358. desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB);
  1359. caps->is_integer = util_format_is_pure_integer(format);
  1360. /*
  1361. * From the Sandy Bridge PRM, volume 2 part 1, page 365:
  1362. *
  1363. * "Logic Ops are only supported on *_UNORM surfaces (excluding _SRGB
  1364. * variants), otherwise Logic Ops must be DISABLED."
  1365. *
  1366. * According to the classic driver, this is lifted on Gen8+.
  1367. */
  1368. caps->can_logicop = (ilo_dev_gen(dev) >= ILO_GEN(8) || caps->is_unorm);
  1369. /* no blending for pure integer formats */
  1370. caps->can_blend = !caps->is_integer;
  1371. /*
  1372. * From the Sandy Bridge PRM, volume 2 part 1, page 382:
  1373. *
  1374. * "Alpha Test can only be enabled if Pixel Shader outputs a float
  1375. * alpha value."
  1376. */
  1377. caps->can_alpha_test = !caps->is_integer;
  1378. caps->force_dst_alpha_one =
  1379. (ilo_format_translate_render(dev, format) !=
  1380. ilo_format_translate_color(dev, format));
  1381. /* sanity check */
  1382. if (caps->force_dst_alpha_one) {
  1383. enum pipe_format render_format;
  1384. switch (format) {
  1385. case PIPE_FORMAT_B8G8R8X8_UNORM:
  1386. render_format = PIPE_FORMAT_B8G8R8A8_UNORM;
  1387. break;
  1388. default:
  1389. render_format = PIPE_FORMAT_NONE;
  1390. break;
  1391. }
  1392. assert(ilo_format_translate_render(dev, format) ==
  1393. ilo_format_translate_color(dev, render_format));
  1394. }
  1395. }
  1396. static void
  1397. ilo_set_framebuffer_state(struct pipe_context *pipe,
  1398. const struct pipe_framebuffer_state *state)
  1399. {
  1400. const struct ilo_dev *dev = ilo_context(pipe)->dev;
  1401. struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;
  1402. struct ilo_fb_state *fb = &vec->fb;
  1403. const struct pipe_surface *first_surf = NULL;
  1404. int i;
  1405. util_copy_framebuffer_state(&fb->state, state);
  1406. fb->has_integer_rt = false;
  1407. for (i = 0; i < state->nr_cbufs; i++) {
  1408. if (state->cbufs[i]) {
  1409. fb_set_blend_caps(dev, state->cbufs[i]->format, &fb->blend_caps[i]);
  1410. fb->has_integer_rt |= fb->blend_caps[i].is_integer;
  1411. if (!first_surf)
  1412. first_surf = state->cbufs[i];
  1413. } else {
  1414. fb_set_blend_caps(dev, PIPE_FORMAT_NONE, &fb->blend_caps[i]);
  1415. }
  1416. }
  1417. if (!first_surf && state->zsbuf)
  1418. first_surf = state->zsbuf;
  1419. fb->num_samples = (first_surf) ? first_surf->texture->nr_samples : 1;
  1420. if (!fb->num_samples)
  1421. fb->num_samples = 1;
  1422. if (state->zsbuf) {
  1423. const struct ilo_surface_cso *cso =
  1424. (const struct ilo_surface_cso *) state->zsbuf;
  1425. const struct ilo_texture *tex = ilo_texture(cso->base.texture);
  1426. fb->has_hiz = cso->u.zs.hiz_vma;
  1427. fb->depth_offset_format =
  1428. ilo_format_translate_depth(dev, tex->image_format);
  1429. } else {
  1430. fb->has_hiz = false;
  1431. fb->depth_offset_format = GEN6_ZFORMAT_D32_FLOAT;
  1432. }
  1433. /*
  1434. * The PRMs list several restrictions when the framebuffer has more than
  1435. * one surface. It seems they are actually lifted on GEN6+.
  1436. */
  1437. vec->dirty |= ILO_DIRTY_FB;
  1438. }
  1439. static void
  1440. ilo_set_polygon_stipple(struct pipe_context *pipe,
  1441. const struct pipe_poly_stipple *state)
  1442. {
  1443. const struct ilo_dev *dev = ilo_context(pipe)->dev;
  1444. struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;
  1445. struct ilo_state_poly_stipple_info info;
  1446. int i;
  1447. for (i = 0; i < 32; i++)
  1448. info.pattern[i] = state->stipple[i];
  1449. ilo_state_poly_stipple_set_info(&vec->poly_stipple, dev, &info);
  1450. vec->dirty |= ILO_DIRTY_POLY_STIPPLE;
  1451. }
  1452. static void
  1453. ilo_set_scissor_states(struct pipe_context *pipe,
  1454. unsigned start_slot,
  1455. unsigned num_scissors,
  1456. const struct pipe_scissor_state *scissors)
  1457. {
  1458. struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;
  1459. unsigned i;
  1460. for (i = 0; i < num_scissors; i++) {
  1461. struct ilo_state_viewport_scissor_info *info =
  1462. &vec->viewport.scissors[start_slot + i];
  1463. if (scissors[i].minx < scissors[i].maxx &&
  1464. scissors[i].miny < scissors[i].maxy) {
  1465. info->min_x = scissors[i].minx;
  1466. info->min_y = scissors[i].miny;
  1467. info->max_x = scissors[i].maxx - 1;
  1468. info->max_y = scissors[i].maxy - 1;
  1469. } else {
  1470. info->min_x = 1;
  1471. info->min_y = 1;
  1472. info->max_x = 0;
  1473. info->max_y = 0;
  1474. }
  1475. }
  1476. vec->dirty |= ILO_DIRTY_SCISSOR;
  1477. }
  1478. static void
  1479. ilo_set_viewport_states(struct pipe_context *pipe,
  1480. unsigned start_slot,
  1481. unsigned num_viewports,
  1482. const struct pipe_viewport_state *viewports)
  1483. {
  1484. struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;
  1485. if (viewports) {
  1486. unsigned i;
  1487. for (i = 0; i < num_viewports; i++) {
  1488. struct ilo_state_viewport_matrix_info *info =
  1489. &vec->viewport.matrices[start_slot + i];
  1490. memcpy(info->scale, viewports[i].scale, sizeof(info->scale));
  1491. memcpy(info->translate, viewports[i].translate,
  1492. sizeof(info->translate));
  1493. }
  1494. if (vec->viewport.params.count < start_slot + num_viewports)
  1495. vec->viewport.params.count = start_slot + num_viewports;
  1496. /* need to save viewport 0 for util_blitter */
  1497. if (!start_slot && num_viewports)
  1498. vec->viewport.viewport0 = viewports[0];
  1499. }
  1500. else {
  1501. if (vec->viewport.params.count <= start_slot + num_viewports &&
  1502. vec->viewport.params.count > start_slot)
  1503. vec->viewport.params.count = start_slot;
  1504. }
  1505. vec->dirty |= ILO_DIRTY_VIEWPORT;
  1506. }
  1507. static void
  1508. ilo_set_sampler_views(struct pipe_context *pipe, enum pipe_shader_type shader,
  1509. unsigned start, unsigned count,
  1510. struct pipe_sampler_view **views)
  1511. {
  1512. struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;
  1513. struct ilo_view_state *dst = &vec->view[shader];
  1514. unsigned i;
  1515. assert(start + count <= ARRAY_SIZE(dst->states));
  1516. if (views) {
  1517. for (i = 0; i < count; i++)
  1518. pipe_sampler_view_reference(&dst->states[start + i], views[i]);
  1519. }
  1520. else {
  1521. for (i = 0; i < count; i++)
  1522. pipe_sampler_view_reference(&dst->states[start + i], NULL);
  1523. }
  1524. if (dst->count <= start + count) {
  1525. if (views)
  1526. count += start;
  1527. else
  1528. count = start;
  1529. while (count > 0 && !dst->states[count - 1])
  1530. count--;
  1531. dst->count = count;
  1532. }
  1533. switch (shader) {
  1534. case PIPE_SHADER_VERTEX:
  1535. vec->dirty |= ILO_DIRTY_VIEW_VS;
  1536. break;
  1537. case PIPE_SHADER_GEOMETRY:
  1538. vec->dirty |= ILO_DIRTY_VIEW_GS;
  1539. break;
  1540. case PIPE_SHADER_FRAGMENT:
  1541. vec->dirty |= ILO_DIRTY_VIEW_FS;
  1542. break;
  1543. case PIPE_SHADER_COMPUTE:
  1544. vec->dirty |= ILO_DIRTY_VIEW_CS;
  1545. break;
  1546. default:
  1547. assert(!"unexpected shader type");
  1548. break;
  1549. }
  1550. }
  1551. static void
  1552. ilo_set_shader_images(struct pipe_context *pipe, unsigned shader,
  1553. unsigned start, unsigned count,
  1554. const struct pipe_image_view *views)
  1555. {
  1556. #if 0
  1557. struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;
  1558. struct ilo_resource_state *dst = &vec->resource;
  1559. unsigned i;
  1560. assert(start + count <= ARRAY_SIZE(dst->states));
  1561. if (surfaces) {
  1562. for (i = 0; i < count; i++)
  1563. pipe_surface_reference(&dst->states[start + i], surfaces[i]);
  1564. }
  1565. else {
  1566. for (i = 0; i < count; i++)
  1567. pipe_surface_reference(&dst->states[start + i], NULL);
  1568. }
  1569. if (dst->count <= start + count) {
  1570. if (surfaces)
  1571. count += start;
  1572. else
  1573. count = start;
  1574. while (count > 0 && !dst->states[count - 1])
  1575. count--;
  1576. dst->count = count;
  1577. }
  1578. vec->dirty |= ILO_DIRTY_RESOURCE;
  1579. #endif
  1580. }
  1581. static void
  1582. ilo_set_vertex_buffers(struct pipe_context *pipe,
  1583. unsigned start_slot, unsigned num_buffers,
  1584. const struct pipe_vertex_buffer *buffers)
  1585. {
  1586. struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;
  1587. unsigned i;
  1588. /* no PIPE_CAP_USER_VERTEX_BUFFERS */
  1589. if (buffers) {
  1590. for (i = 0; i < num_buffers; i++)
  1591. assert(!buffers[i].user_buffer);
  1592. }
  1593. util_set_vertex_buffers_mask(vec->vb.states,
  1594. &vec->vb.enabled_mask, buffers, start_slot, num_buffers);
  1595. vec->dirty |= ILO_DIRTY_VB;
  1596. }
  1597. static void
  1598. ilo_set_index_buffer(struct pipe_context *pipe,
  1599. const struct pipe_index_buffer *state)
  1600. {
  1601. struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;
  1602. if (state) {
  1603. pipe_resource_reference(&vec->ib.state.buffer, state->buffer);
  1604. vec->ib.state = *state;
  1605. } else {
  1606. pipe_resource_reference(&vec->ib.state.buffer, NULL);
  1607. memset(&vec->ib.state, 0, sizeof(vec->ib.state));
  1608. }
  1609. vec->dirty |= ILO_DIRTY_IB;
  1610. }
  1611. static struct pipe_stream_output_target *
  1612. ilo_create_stream_output_target(struct pipe_context *pipe,
  1613. struct pipe_resource *res,
  1614. unsigned buffer_offset,
  1615. unsigned buffer_size)
  1616. {
  1617. const struct ilo_dev *dev = ilo_context(pipe)->dev;
  1618. struct ilo_stream_output_target *target;
  1619. struct ilo_state_sol_buffer_info info;
  1620. target = CALLOC_STRUCT(ilo_stream_output_target);
  1621. assert(target);
  1622. pipe_reference_init(&target->base.reference, 1);
  1623. pipe_resource_reference(&target->base.buffer, res);
  1624. target->base.context = pipe;
  1625. target->base.buffer_offset = buffer_offset;
  1626. target->base.buffer_size = buffer_size;
  1627. memset(&info, 0, sizeof(info));
  1628. info.vma = ilo_resource_get_vma(res);
  1629. info.offset = buffer_offset;
  1630. info.size = buffer_size;
  1631. ilo_state_sol_buffer_init(&target->sb, dev, &info);
  1632. return &target->base;
  1633. }
  1634. static void
  1635. ilo_set_stream_output_targets(struct pipe_context *pipe,
  1636. unsigned num_targets,
  1637. struct pipe_stream_output_target **targets,
  1638. const unsigned *offset)
  1639. {
  1640. struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;
  1641. unsigned i;
  1642. unsigned append_bitmask = 0;
  1643. if (!targets)
  1644. num_targets = 0;
  1645. /* util_blitter may set this unnecessarily */
  1646. if (!vec->so.count && !num_targets)
  1647. return;
  1648. for (i = 0; i < num_targets; i++) {
  1649. pipe_so_target_reference(&vec->so.states[i], targets[i]);
  1650. if (offset[i] == (unsigned)-1)
  1651. append_bitmask |= 1 << i;
  1652. }
  1653. for (; i < vec->so.count; i++)
  1654. pipe_so_target_reference(&vec->so.states[i], NULL);
  1655. vec->so.count = num_targets;
  1656. vec->so.append_bitmask = append_bitmask;
  1657. vec->so.enabled = (vec->so.count > 0);
  1658. vec->dirty |= ILO_DIRTY_SO;
  1659. }
  1660. static void
  1661. ilo_stream_output_target_destroy(struct pipe_context *pipe,
  1662. struct pipe_stream_output_target *target)
  1663. {
  1664. pipe_resource_reference(&target->buffer, NULL);
  1665. FREE(target);
  1666. }
  1667. static struct pipe_sampler_view *
  1668. ilo_create_sampler_view(struct pipe_context *pipe,
  1669. struct pipe_resource *res,
  1670. const struct pipe_sampler_view *templ)
  1671. {
  1672. const struct ilo_dev *dev = ilo_context(pipe)->dev;
  1673. struct ilo_view_cso *view;
  1674. view = CALLOC_STRUCT(ilo_view_cso);
  1675. assert(view);
  1676. view->base = *templ;
  1677. pipe_reference_init(&view->base.reference, 1);
  1678. view->base.texture = NULL;
  1679. pipe_resource_reference(&view->base.texture, res);
  1680. view->base.context = pipe;
  1681. if (res->target == PIPE_BUFFER) {
  1682. struct ilo_state_surface_buffer_info info;
  1683. memset(&info, 0, sizeof(info));
  1684. info.vma = ilo_resource_get_vma(res);
  1685. info.offset = templ->u.buf.offset;
  1686. info.size = templ->u.buf.size;
  1687. info.access = ILO_STATE_SURFACE_ACCESS_SAMPLER;
  1688. info.format = ilo_format_translate_color(dev, templ->format);
  1689. info.format_size = util_format_get_blocksize(templ->format);
  1690. info.struct_size = info.format_size;
  1691. info.readonly = true;
  1692. ilo_state_surface_init_for_buffer(&view->surface, dev, &info);
  1693. } else {
  1694. struct ilo_texture *tex = ilo_texture(res);
  1695. struct ilo_state_surface_image_info info;
  1696. /* warn about degraded performance because of a missing binding flag */
  1697. if (tex->image.tiling == GEN6_TILING_NONE &&
  1698. !(tex->base.bind & PIPE_BIND_SAMPLER_VIEW)) {
  1699. ilo_warn("creating sampler view for a resource "
  1700. "not created for sampling\n");
  1701. }
  1702. memset(&info, 0, sizeof(info));
  1703. info.img = &tex->image;
  1704. info.level_base = templ->u.tex.first_level;
  1705. info.level_count = templ->u.tex.last_level -
  1706. templ->u.tex.first_level + 1;
  1707. info.slice_base = templ->u.tex.first_layer;
  1708. info.slice_count = templ->u.tex.last_layer -
  1709. templ->u.tex.first_layer + 1;
  1710. info.vma = &tex->vma;
  1711. info.access = ILO_STATE_SURFACE_ACCESS_SAMPLER;
  1712. info.type = tex->image.type;
  1713. if (templ->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT &&
  1714. tex->separate_s8) {
  1715. info.format = ilo_format_translate_texture(dev,
  1716. PIPE_FORMAT_Z32_FLOAT);
  1717. } else {
  1718. info.format = ilo_format_translate_texture(dev, templ->format);
  1719. }
  1720. info.is_array = util_resource_is_array_texture(&tex->base);
  1721. info.readonly = true;
  1722. ilo_state_surface_init_for_image(&view->surface, dev, &info);
  1723. }
  1724. return &view->base;
  1725. }
  1726. static void
  1727. ilo_sampler_view_destroy(struct pipe_context *pipe,
  1728. struct pipe_sampler_view *view)
  1729. {
  1730. pipe_resource_reference(&view->texture, NULL);
  1731. FREE(view);
  1732. }
  1733. static struct pipe_surface *
  1734. ilo_create_surface(struct pipe_context *pipe,
  1735. struct pipe_resource *res,
  1736. const struct pipe_surface *templ)
  1737. {
  1738. const struct ilo_dev *dev = ilo_context(pipe)->dev;
  1739. struct ilo_texture *tex = ilo_texture(res);
  1740. struct ilo_surface_cso *surf;
  1741. surf = CALLOC_STRUCT(ilo_surface_cso);
  1742. assert(surf);
  1743. surf->base = *templ;
  1744. pipe_reference_init(&surf->base.reference, 1);
  1745. surf->base.texture = NULL;
  1746. pipe_resource_reference(&surf->base.texture, &tex->base);
  1747. surf->base.context = pipe;
  1748. surf->base.width = u_minify(tex->base.width0, templ->u.tex.level);
  1749. surf->base.height = u_minify(tex->base.height0, templ->u.tex.level);
  1750. surf->is_rt = !util_format_is_depth_or_stencil(templ->format);
  1751. if (surf->is_rt) {
  1752. struct ilo_state_surface_image_info info;
  1753. /* relax this? */
  1754. assert(tex->base.target != PIPE_BUFFER);
  1755. memset(&info, 0, sizeof(info));
  1756. info.img = &tex->image;
  1757. info.level_base = templ->u.tex.level;
  1758. info.level_count = 1;
  1759. info.slice_base = templ->u.tex.first_layer;
  1760. info.slice_count = templ->u.tex.last_layer -
  1761. templ->u.tex.first_layer + 1;
  1762. info.vma = &tex->vma;
  1763. if (ilo_image_can_enable_aux(&tex->image, templ->u.tex.level))
  1764. info.aux_vma = &tex->aux_vma;
  1765. info.access = ILO_STATE_SURFACE_ACCESS_DP_RENDER;
  1766. info.type = (tex->image.type == GEN6_SURFTYPE_CUBE) ?
  1767. GEN6_SURFTYPE_2D : tex->image.type;
  1768. info.format = ilo_format_translate_render(dev, templ->format);
  1769. info.is_array = util_resource_is_array_texture(&tex->base);
  1770. ilo_state_surface_init_for_image(&surf->u.rt, dev, &info);
  1771. } else {
  1772. struct ilo_state_zs_info info;
  1773. assert(res->target != PIPE_BUFFER);
  1774. memset(&info, 0, sizeof(info));
  1775. if (templ->format == PIPE_FORMAT_S8_UINT) {
  1776. info.s_vma = &tex->vma;
  1777. info.s_img = &tex->image;
  1778. } else {
  1779. info.z_vma = &tex->vma;
  1780. info.z_img = &tex->image;
  1781. if (tex->separate_s8) {
  1782. info.s_vma = &tex->separate_s8->vma;
  1783. info.s_img = &tex->separate_s8->image;
  1784. }
  1785. if (ilo_image_can_enable_aux(&tex->image, templ->u.tex.level))
  1786. info.hiz_vma = &tex->aux_vma;
  1787. }
  1788. info.level = templ->u.tex.level;
  1789. info.slice_base = templ->u.tex.first_layer;
  1790. info.slice_count = templ->u.tex.last_layer -
  1791. templ->u.tex.first_layer + 1;
  1792. info.type = (tex->image.type == GEN6_SURFTYPE_CUBE) ?
  1793. GEN6_SURFTYPE_2D : tex->image.type;
  1794. info.format = ilo_format_translate_depth(dev, tex->image_format);
  1795. if (ilo_dev_gen(dev) == ILO_GEN(6) && !info.hiz_vma &&
  1796. tex->image_format == PIPE_FORMAT_Z24X8_UNORM)
  1797. info.format = GEN6_ZFORMAT_D24_UNORM_S8_UINT;
  1798. ilo_state_zs_init(&surf->u.zs, dev, &info);
  1799. }
  1800. return &surf->base;
  1801. }
  1802. static void
  1803. ilo_surface_destroy(struct pipe_context *pipe,
  1804. struct pipe_surface *surface)
  1805. {
  1806. pipe_resource_reference(&surface->texture, NULL);
  1807. FREE(surface);
  1808. }
  1809. static void *
  1810. ilo_create_compute_state(struct pipe_context *pipe,
  1811. const struct pipe_compute_state *state)
  1812. {
  1813. struct ilo_context *ilo = ilo_context(pipe);
  1814. struct ilo_shader_state *shader;
  1815. shader = ilo_shader_create_cs(ilo->dev, state, &ilo->state_vector);
  1816. assert(shader);
  1817. ilo_shader_cache_add(ilo->shader_cache, shader);
  1818. return shader;
  1819. }
  1820. static void
  1821. ilo_bind_compute_state(struct pipe_context *pipe, void *state)
  1822. {
  1823. struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;
  1824. vec->cs = state;
  1825. vec->dirty |= ILO_DIRTY_CS;
  1826. }
  1827. static void
  1828. ilo_delete_compute_state(struct pipe_context *pipe, void *state)
  1829. {
  1830. struct ilo_context *ilo = ilo_context(pipe);
  1831. struct ilo_shader_state *cs = (struct ilo_shader_state *) state;
  1832. ilo_shader_cache_remove(ilo->shader_cache, cs);
  1833. ilo_shader_destroy(cs);
  1834. }
  1835. static void
  1836. ilo_set_compute_resources(struct pipe_context *pipe,
  1837. unsigned start, unsigned count,
  1838. struct pipe_surface **surfaces)
  1839. {
  1840. struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;
  1841. struct ilo_resource_state *dst = &vec->cs_resource;
  1842. unsigned i;
  1843. assert(start + count <= ARRAY_SIZE(dst->states));
  1844. if (surfaces) {
  1845. for (i = 0; i < count; i++)
  1846. pipe_surface_reference(&dst->states[start + i], surfaces[i]);
  1847. }
  1848. else {
  1849. for (i = 0; i < count; i++)
  1850. pipe_surface_reference(&dst->states[start + i], NULL);
  1851. }
  1852. if (dst->count <= start + count) {
  1853. if (surfaces)
  1854. count += start;
  1855. else
  1856. count = start;
  1857. while (count > 0 && !dst->states[count - 1])
  1858. count--;
  1859. dst->count = count;
  1860. }
  1861. vec->dirty |= ILO_DIRTY_CS_RESOURCE;
  1862. }
  1863. static void
  1864. ilo_set_global_binding(struct pipe_context *pipe,
  1865. unsigned start, unsigned count,
  1866. struct pipe_resource **resources,
  1867. uint32_t **handles)
  1868. {
  1869. struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;
  1870. struct ilo_global_binding_cso *dst;
  1871. unsigned i;
  1872. /* make room */
  1873. if (vec->global_binding.count < start + count) {
  1874. if (resources) {
  1875. const unsigned old_size = vec->global_binding.bindings.size;
  1876. const unsigned new_size = sizeof(*dst) * (start + count);
  1877. if (old_size < new_size) {
  1878. util_dynarray_resize(&vec->global_binding.bindings, new_size);
  1879. memset(vec->global_binding.bindings.data + old_size, 0,
  1880. new_size - old_size);
  1881. }
  1882. } else {
  1883. count = vec->global_binding.count - start;
  1884. }
  1885. }
  1886. dst = util_dynarray_element(&vec->global_binding.bindings,
  1887. struct ilo_global_binding_cso, start);
  1888. if (resources) {
  1889. for (i = 0; i < count; i++) {
  1890. pipe_resource_reference(&dst[i].resource, resources[i]);
  1891. dst[i].handle = handles[i];
  1892. }
  1893. } else {
  1894. for (i = 0; i < count; i++) {
  1895. pipe_resource_reference(&dst[i].resource, NULL);
  1896. dst[i].handle = NULL;
  1897. }
  1898. }
  1899. if (vec->global_binding.count <= start + count) {
  1900. dst = util_dynarray_begin(&vec->global_binding.bindings);
  1901. if (resources)
  1902. count += start;
  1903. else
  1904. count = start;
  1905. while (count > 0 && !dst[count - 1].resource)
  1906. count--;
  1907. vec->global_binding.count = count;
  1908. }
  1909. vec->dirty |= ILO_DIRTY_GLOBAL_BINDING;
  1910. }
  1911. /**
  1912. * Initialize state-related functions.
  1913. */
  1914. void
  1915. ilo_init_state_functions(struct ilo_context *ilo)
  1916. {
  1917. STATIC_ASSERT(ILO_STATE_COUNT <= 32);
  1918. ilo->base.create_blend_state = ilo_create_blend_state;
  1919. ilo->base.bind_blend_state = ilo_bind_blend_state;
  1920. ilo->base.delete_blend_state = ilo_delete_blend_state;
  1921. ilo->base.create_sampler_state = ilo_create_sampler_state;
  1922. ilo->base.bind_sampler_states = ilo_bind_sampler_states;
  1923. ilo->base.delete_sampler_state = ilo_delete_sampler_state;
  1924. ilo->base.create_rasterizer_state = ilo_create_rasterizer_state;
  1925. ilo->base.bind_rasterizer_state = ilo_bind_rasterizer_state;
  1926. ilo->base.delete_rasterizer_state = ilo_delete_rasterizer_state;
  1927. ilo->base.create_depth_stencil_alpha_state = ilo_create_depth_stencil_alpha_state;
  1928. ilo->base.bind_depth_stencil_alpha_state = ilo_bind_depth_stencil_alpha_state;
  1929. ilo->base.delete_depth_stencil_alpha_state = ilo_delete_depth_stencil_alpha_state;
  1930. ilo->base.create_fs_state = ilo_create_fs_state;
  1931. ilo->base.bind_fs_state = ilo_bind_fs_state;
  1932. ilo->base.delete_fs_state = ilo_delete_fs_state;
  1933. ilo->base.create_vs_state = ilo_create_vs_state;
  1934. ilo->base.bind_vs_state = ilo_bind_vs_state;
  1935. ilo->base.delete_vs_state = ilo_delete_vs_state;
  1936. ilo->base.create_gs_state = ilo_create_gs_state;
  1937. ilo->base.bind_gs_state = ilo_bind_gs_state;
  1938. ilo->base.delete_gs_state = ilo_delete_gs_state;
  1939. ilo->base.create_vertex_elements_state = ilo_create_vertex_elements_state;
  1940. ilo->base.bind_vertex_elements_state = ilo_bind_vertex_elements_state;
  1941. ilo->base.delete_vertex_elements_state = ilo_delete_vertex_elements_state;
  1942. ilo->base.set_blend_color = ilo_set_blend_color;
  1943. ilo->base.set_stencil_ref = ilo_set_stencil_ref;
  1944. ilo->base.set_sample_mask = ilo_set_sample_mask;
  1945. ilo->base.set_clip_state = ilo_set_clip_state;
  1946. ilo->base.set_constant_buffer = ilo_set_constant_buffer;
  1947. ilo->base.set_framebuffer_state = ilo_set_framebuffer_state;
  1948. ilo->base.set_polygon_stipple = ilo_set_polygon_stipple;
  1949. ilo->base.set_scissor_states = ilo_set_scissor_states;
  1950. ilo->base.set_viewport_states = ilo_set_viewport_states;
  1951. ilo->base.set_sampler_views = ilo_set_sampler_views;
  1952. ilo->base.set_shader_images = ilo_set_shader_images;
  1953. ilo->base.set_vertex_buffers = ilo_set_vertex_buffers;
  1954. ilo->base.set_index_buffer = ilo_set_index_buffer;
  1955. ilo->base.create_stream_output_target = ilo_create_stream_output_target;
  1956. ilo->base.stream_output_target_destroy = ilo_stream_output_target_destroy;
  1957. ilo->base.set_stream_output_targets = ilo_set_stream_output_targets;
  1958. ilo->base.create_sampler_view = ilo_create_sampler_view;
  1959. ilo->base.sampler_view_destroy = ilo_sampler_view_destroy;
  1960. ilo->base.create_surface = ilo_create_surface;
  1961. ilo->base.surface_destroy = ilo_surface_destroy;
  1962. ilo->base.create_compute_state = ilo_create_compute_state;
  1963. ilo->base.bind_compute_state = ilo_bind_compute_state;
  1964. ilo->base.delete_compute_state = ilo_delete_compute_state;
  1965. ilo->base.set_compute_resources = ilo_set_compute_resources;
  1966. ilo->base.set_global_binding = ilo_set_global_binding;
  1967. }
  1968. void
  1969. ilo_state_vector_init(const struct ilo_dev *dev,
  1970. struct ilo_state_vector *vec)
  1971. {
  1972. struct ilo_state_urb_info urb_info;
  1973. vec->sample_mask = ~0u;
  1974. ilo_state_viewport_init_data_only(&vec->viewport.vp, dev,
  1975. vec->viewport.vp_data, sizeof(vec->viewport.vp_data));
  1976. assert(vec->viewport.vp.array_size >= ILO_MAX_VIEWPORTS);
  1977. vec->viewport.params.matrices = vec->viewport.matrices;
  1978. vec->viewport.params.scissors = vec->viewport.scissors;
  1979. ilo_state_hs_init_disabled(&vec->disabled_hs, dev);
  1980. ilo_state_ds_init_disabled(&vec->disabled_ds, dev);
  1981. ilo_state_gs_init_disabled(&vec->disabled_gs, dev);
  1982. ilo_state_sol_buffer_init_disabled(&vec->so.dummy_sb, dev);
  1983. ilo_state_surface_init_for_null(&vec->fb.null_rt, dev);
  1984. ilo_state_zs_init_for_null(&vec->fb.null_zs, dev);
  1985. ilo_state_sampler_init_disabled(&vec->disabled_sampler, dev);
  1986. memset(&urb_info, 0, sizeof(urb_info));
  1987. ilo_state_urb_init(&vec->urb, dev, &urb_info);
  1988. util_dynarray_init(&vec->global_binding.bindings);
  1989. vec->dirty = ILO_DIRTY_ALL;
  1990. }
  1991. void
  1992. ilo_state_vector_cleanup(struct ilo_state_vector *vec)
  1993. {
  1994. unsigned i, sh;
  1995. for (i = 0; i < ARRAY_SIZE(vec->vb.states); i++) {
  1996. if (vec->vb.enabled_mask & (1 << i))
  1997. pipe_resource_reference(&vec->vb.states[i].buffer, NULL);
  1998. }
  1999. pipe_resource_reference(&vec->ib.state.buffer, NULL);
  2000. pipe_resource_reference(&vec->ib.hw_resource, NULL);
  2001. for (i = 0; i < vec->so.count; i++)
  2002. pipe_so_target_reference(&vec->so.states[i], NULL);
  2003. for (sh = 0; sh < PIPE_SHADER_TYPES; sh++) {
  2004. for (i = 0; i < vec->view[sh].count; i++) {
  2005. struct pipe_sampler_view *view = vec->view[sh].states[i];
  2006. pipe_sampler_view_reference(&view, NULL);
  2007. }
  2008. for (i = 0; i < ARRAY_SIZE(vec->cbuf[sh].cso); i++) {
  2009. struct ilo_cbuf_cso *cbuf = &vec->cbuf[sh].cso[i];
  2010. pipe_resource_reference(&cbuf->resource, NULL);
  2011. }
  2012. }
  2013. for (i = 0; i < vec->resource.count; i++)
  2014. pipe_surface_reference(&vec->resource.states[i], NULL);
  2015. for (i = 0; i < vec->fb.state.nr_cbufs; i++)
  2016. pipe_surface_reference(&vec->fb.state.cbufs[i], NULL);
  2017. if (vec->fb.state.zsbuf)
  2018. pipe_surface_reference(&vec->fb.state.zsbuf, NULL);
  2019. for (i = 0; i < vec->cs_resource.count; i++)
  2020. pipe_surface_reference(&vec->cs_resource.states[i], NULL);
  2021. for (i = 0; i < vec->global_binding.count; i++) {
  2022. struct ilo_global_binding_cso *cso =
  2023. util_dynarray_element(&vec->global_binding.bindings,
  2024. struct ilo_global_binding_cso, i);
  2025. pipe_resource_reference(&cso->resource, NULL);
  2026. }
  2027. util_dynarray_fini(&vec->global_binding.bindings);
  2028. }
  2029. /**
  2030. * Mark all states that have the resource dirty.
  2031. */
  2032. void
  2033. ilo_state_vector_resource_renamed(struct ilo_state_vector *vec,
  2034. struct pipe_resource *res)
  2035. {
  2036. uint32_t states = 0;
  2037. unsigned sh, i;
  2038. if (res->target == PIPE_BUFFER) {
  2039. uint32_t vb_mask = vec->vb.enabled_mask;
  2040. while (vb_mask) {
  2041. const unsigned idx = u_bit_scan(&vb_mask);
  2042. if (vec->vb.states[idx].buffer == res) {
  2043. states |= ILO_DIRTY_VB;
  2044. break;
  2045. }
  2046. }
  2047. if (vec->ib.state.buffer == res) {
  2048. states |= ILO_DIRTY_IB;
  2049. /*
  2050. * finalize_index_buffer() has an optimization that clears
  2051. * ILO_DIRTY_IB when the HW states do not change. However, it fails
  2052. * to flush the VF cache when the HW states do not change, but the
  2053. * contents of the IB has changed. Here, we set the index size to an
  2054. * invalid value to avoid the optimization.
  2055. */
  2056. vec->ib.hw_index_size = 0;
  2057. }
  2058. for (i = 0; i < vec->so.count; i++) {
  2059. if (vec->so.states[i]->buffer == res) {
  2060. states |= ILO_DIRTY_SO;
  2061. break;
  2062. }
  2063. }
  2064. }
  2065. for (sh = 0; sh < PIPE_SHADER_TYPES; sh++) {
  2066. for (i = 0; i < vec->view[sh].count; i++) {
  2067. struct ilo_view_cso *cso = (struct ilo_view_cso *) vec->view[sh].states[i];
  2068. if (cso->base.texture == res) {
  2069. static const unsigned view_dirty_bits[PIPE_SHADER_TYPES] = {
  2070. [PIPE_SHADER_VERTEX] = ILO_DIRTY_VIEW_VS,
  2071. [PIPE_SHADER_FRAGMENT] = ILO_DIRTY_VIEW_FS,
  2072. [PIPE_SHADER_GEOMETRY] = ILO_DIRTY_VIEW_GS,
  2073. [PIPE_SHADER_COMPUTE] = ILO_DIRTY_VIEW_CS,
  2074. };
  2075. states |= view_dirty_bits[sh];
  2076. break;
  2077. }
  2078. }
  2079. if (res->target == PIPE_BUFFER) {
  2080. for (i = 0; i < ARRAY_SIZE(vec->cbuf[sh].cso); i++) {
  2081. struct ilo_cbuf_cso *cbuf = &vec->cbuf[sh].cso[i];
  2082. if (cbuf->resource == res) {
  2083. states |= ILO_DIRTY_CBUF;
  2084. break;
  2085. }
  2086. }
  2087. }
  2088. }
  2089. for (i = 0; i < vec->resource.count; i++) {
  2090. struct ilo_surface_cso *cso =
  2091. (struct ilo_surface_cso *) vec->resource.states[i];
  2092. if (cso->base.texture == res) {
  2093. states |= ILO_DIRTY_RESOURCE;
  2094. break;
  2095. }
  2096. }
  2097. /* for now? */
  2098. if (res->target != PIPE_BUFFER) {
  2099. for (i = 0; i < vec->fb.state.nr_cbufs; i++) {
  2100. struct ilo_surface_cso *cso =
  2101. (struct ilo_surface_cso *) vec->fb.state.cbufs[i];
  2102. if (cso && cso->base.texture == res) {
  2103. states |= ILO_DIRTY_FB;
  2104. break;
  2105. }
  2106. }
  2107. if (vec->fb.state.zsbuf && vec->fb.state.zsbuf->texture == res)
  2108. states |= ILO_DIRTY_FB;
  2109. }
  2110. for (i = 0; i < vec->cs_resource.count; i++) {
  2111. struct ilo_surface_cso *cso =
  2112. (struct ilo_surface_cso *) vec->cs_resource.states[i];
  2113. if (cso->base.texture == res) {
  2114. states |= ILO_DIRTY_CS_RESOURCE;
  2115. break;
  2116. }
  2117. }
  2118. for (i = 0; i < vec->global_binding.count; i++) {
  2119. struct ilo_global_binding_cso *cso =
  2120. util_dynarray_element(&vec->global_binding.bindings,
  2121. struct ilo_global_binding_cso, i);
  2122. if (cso->resource == res) {
  2123. states |= ILO_DIRTY_GLOBAL_BINDING;
  2124. break;
  2125. }
  2126. }
  2127. vec->dirty |= states;
  2128. }
  2129. void
  2130. ilo_state_vector_dump_dirty(const struct ilo_state_vector *vec)
  2131. {
  2132. static const char *state_names[ILO_STATE_COUNT] = {
  2133. [ILO_STATE_VB] = "VB",
  2134. [ILO_STATE_VE] = "VE",
  2135. [ILO_STATE_IB] = "IB",
  2136. [ILO_STATE_VS] = "VS",
  2137. [ILO_STATE_GS] = "GS",
  2138. [ILO_STATE_SO] = "SO",
  2139. [ILO_STATE_CLIP] = "CLIP",
  2140. [ILO_STATE_VIEWPORT] = "VIEWPORT",
  2141. [ILO_STATE_SCISSOR] = "SCISSOR",
  2142. [ILO_STATE_RASTERIZER] = "RASTERIZER",
  2143. [ILO_STATE_POLY_STIPPLE] = "POLY_STIPPLE",
  2144. [ILO_STATE_SAMPLE_MASK] = "SAMPLE_MASK",
  2145. [ILO_STATE_FS] = "FS",
  2146. [ILO_STATE_DSA] = "DSA",
  2147. [ILO_STATE_STENCIL_REF] = "STENCIL_REF",
  2148. [ILO_STATE_BLEND] = "BLEND",
  2149. [ILO_STATE_BLEND_COLOR] = "BLEND_COLOR",
  2150. [ILO_STATE_FB] = "FB",
  2151. [ILO_STATE_SAMPLER_VS] = "SAMPLER_VS",
  2152. [ILO_STATE_SAMPLER_GS] = "SAMPLER_GS",
  2153. [ILO_STATE_SAMPLER_FS] = "SAMPLER_FS",
  2154. [ILO_STATE_SAMPLER_CS] = "SAMPLER_CS",
  2155. [ILO_STATE_VIEW_VS] = "VIEW_VS",
  2156. [ILO_STATE_VIEW_GS] = "VIEW_GS",
  2157. [ILO_STATE_VIEW_FS] = "VIEW_FS",
  2158. [ILO_STATE_VIEW_CS] = "VIEW_CS",
  2159. [ILO_STATE_CBUF] = "CBUF",
  2160. [ILO_STATE_RESOURCE] = "RESOURCE",
  2161. [ILO_STATE_CS] = "CS",
  2162. [ILO_STATE_CS_RESOURCE] = "CS_RESOURCE",
  2163. [ILO_STATE_GLOBAL_BINDING] = "GLOBAL_BINDING",
  2164. };
  2165. uint32_t dirty = vec->dirty;
  2166. if (!dirty) {
  2167. ilo_printf("no state is dirty\n");
  2168. return;
  2169. }
  2170. dirty &= (1U << ILO_STATE_COUNT) - 1;
  2171. ilo_printf("%2d states are dirty:", util_bitcount(dirty));
  2172. while (dirty) {
  2173. const enum ilo_state state = u_bit_scan(&dirty);
  2174. ilo_printf(" %s", state_names[state]);
  2175. }
  2176. ilo_printf("\n");
  2177. }