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.

si_state_draw.c 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. /*
  2. * Copyright 2012 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * on the rights to use, copy, modify, merge, publish, distribute, sub
  8. * license, and/or sell copies of the Software, and to permit persons to whom
  9. * the Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
  19. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  20. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  21. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Christian König <christian.koenig@amd.com>
  25. */
  26. #include "util/u_memory.h"
  27. #include "util/u_framebuffer.h"
  28. #include "util/u_blitter.h"
  29. #include "tgsi/tgsi_parse.h"
  30. #include "radeonsi_pipe.h"
  31. #include "radeonsi_shader.h"
  32. #include "si_state.h"
  33. #include "../radeon/r600_cs.h"
  34. #include "sid.h"
  35. /*
  36. * Shaders
  37. */
  38. static void si_pipe_shader_vs(struct pipe_context *ctx, struct si_pipe_shader *shader)
  39. {
  40. struct r600_context *rctx = (struct r600_context *)ctx;
  41. struct si_pm4_state *pm4;
  42. unsigned num_sgprs, num_user_sgprs;
  43. unsigned nparams, i, vgpr_comp_cnt;
  44. uint64_t va;
  45. si_pm4_delete_state(rctx, vs, shader->pm4);
  46. pm4 = shader->pm4 = si_pm4_alloc_state(rctx);
  47. if (pm4 == NULL)
  48. return;
  49. /* Certain attributes (position, psize, etc.) don't count as params.
  50. * VS is required to export at least one param and r600_shader_from_tgsi()
  51. * takes care of adding a dummy export.
  52. */
  53. for (nparams = 0, i = 0 ; i < shader->shader.noutput; i++) {
  54. switch (shader->shader.output[i].name) {
  55. case TGSI_SEMANTIC_CLIPVERTEX:
  56. case TGSI_SEMANTIC_POSITION:
  57. case TGSI_SEMANTIC_PSIZE:
  58. break;
  59. default:
  60. nparams++;
  61. }
  62. }
  63. if (nparams < 1)
  64. nparams = 1;
  65. si_pm4_set_reg(pm4, R_0286C4_SPI_VS_OUT_CONFIG,
  66. S_0286C4_VS_EXPORT_COUNT(nparams - 1));
  67. si_pm4_set_reg(pm4, R_02870C_SPI_SHADER_POS_FORMAT,
  68. S_02870C_POS0_EXPORT_FORMAT(V_02870C_SPI_SHADER_4COMP) |
  69. S_02870C_POS1_EXPORT_FORMAT(shader->shader.nr_pos_exports > 1 ?
  70. V_02870C_SPI_SHADER_4COMP :
  71. V_02870C_SPI_SHADER_NONE) |
  72. S_02870C_POS2_EXPORT_FORMAT(shader->shader.nr_pos_exports > 2 ?
  73. V_02870C_SPI_SHADER_4COMP :
  74. V_02870C_SPI_SHADER_NONE) |
  75. S_02870C_POS3_EXPORT_FORMAT(shader->shader.nr_pos_exports > 3 ?
  76. V_02870C_SPI_SHADER_4COMP :
  77. V_02870C_SPI_SHADER_NONE));
  78. va = r600_resource_va(ctx->screen, (void *)shader->bo);
  79. si_pm4_add_bo(pm4, shader->bo, RADEON_USAGE_READ);
  80. si_pm4_set_reg(pm4, R_00B120_SPI_SHADER_PGM_LO_VS, va >> 8);
  81. si_pm4_set_reg(pm4, R_00B124_SPI_SHADER_PGM_HI_VS, va >> 40);
  82. num_user_sgprs = SI_VS_NUM_USER_SGPR;
  83. num_sgprs = shader->num_sgprs;
  84. if (num_user_sgprs > num_sgprs) {
  85. /* Last 2 reserved SGPRs are used for VCC */
  86. num_sgprs = num_user_sgprs + 2;
  87. }
  88. assert(num_sgprs <= 104);
  89. vgpr_comp_cnt = shader->shader.uses_instanceid ? 3 : 0;
  90. si_pm4_set_reg(pm4, R_00B128_SPI_SHADER_PGM_RSRC1_VS,
  91. S_00B128_VGPRS((shader->num_vgprs - 1) / 4) |
  92. S_00B128_SGPRS((num_sgprs - 1) / 8) |
  93. S_00B128_VGPR_COMP_CNT(vgpr_comp_cnt));
  94. si_pm4_set_reg(pm4, R_00B12C_SPI_SHADER_PGM_RSRC2_VS,
  95. S_00B12C_USER_SGPR(num_user_sgprs));
  96. if (rctx->b.chip_class >= CIK) {
  97. si_pm4_set_reg(pm4, R_00B118_SPI_SHADER_PGM_RSRC3_VS,
  98. S_00B118_CU_EN(0xffff));
  99. si_pm4_set_reg(pm4, R_00B11C_SPI_SHADER_LATE_ALLOC_VS,
  100. S_00B11C_LIMIT(0));
  101. }
  102. si_pm4_bind_state(rctx, vs, shader->pm4);
  103. rctx->b.flags |= R600_CONTEXT_INV_SHADER_CACHE;
  104. }
  105. static void si_pipe_shader_ps(struct pipe_context *ctx, struct si_pipe_shader *shader)
  106. {
  107. struct r600_context *rctx = (struct r600_context *)ctx;
  108. struct si_pm4_state *pm4;
  109. unsigned i, exports_ps, num_cout, spi_ps_in_control, db_shader_control;
  110. unsigned num_sgprs, num_user_sgprs;
  111. unsigned spi_baryc_cntl = 0, spi_ps_input_ena, spi_shader_z_format;
  112. uint64_t va;
  113. si_pm4_delete_state(rctx, ps, shader->pm4);
  114. pm4 = shader->pm4 = si_pm4_alloc_state(rctx);
  115. if (pm4 == NULL)
  116. return;
  117. db_shader_control = S_02880C_Z_ORDER(V_02880C_EARLY_Z_THEN_LATE_Z) |
  118. S_02880C_ALPHA_TO_MASK_DISABLE(rctx->fb_cb0_is_integer);
  119. for (i = 0; i < shader->shader.ninput; i++) {
  120. switch (shader->shader.input[i].name) {
  121. case TGSI_SEMANTIC_POSITION:
  122. if (shader->shader.input[i].centroid) {
  123. /* SPI_BARYC_CNTL.POS_FLOAT_LOCATION
  124. * Possible vaules:
  125. * 0 -> Position = pixel center (default)
  126. * 1 -> Position = pixel centroid
  127. * 2 -> Position = iterated sample number XXX:
  128. * What does this mean?
  129. */
  130. spi_baryc_cntl |= S_0286E0_POS_FLOAT_LOCATION(1);
  131. }
  132. /* Fall through */
  133. case TGSI_SEMANTIC_FACE:
  134. continue;
  135. }
  136. }
  137. for (i = 0; i < shader->shader.noutput; i++) {
  138. if (shader->shader.output[i].name == TGSI_SEMANTIC_POSITION)
  139. db_shader_control |= S_02880C_Z_EXPORT_ENABLE(1);
  140. if (shader->shader.output[i].name == TGSI_SEMANTIC_STENCIL)
  141. db_shader_control |= S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(1);
  142. }
  143. if (shader->shader.uses_kill || shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
  144. db_shader_control |= S_02880C_KILL_ENABLE(1);
  145. exports_ps = 0;
  146. num_cout = 0;
  147. for (i = 0; i < shader->shader.noutput; i++) {
  148. if (shader->shader.output[i].name == TGSI_SEMANTIC_POSITION ||
  149. shader->shader.output[i].name == TGSI_SEMANTIC_STENCIL)
  150. exports_ps |= 1;
  151. else if (shader->shader.output[i].name == TGSI_SEMANTIC_COLOR) {
  152. if (shader->shader.fs_write_all)
  153. num_cout = shader->shader.nr_cbufs;
  154. else
  155. num_cout++;
  156. }
  157. }
  158. if (!exports_ps) {
  159. /* always at least export 1 component per pixel */
  160. exports_ps = 2;
  161. }
  162. spi_ps_in_control = S_0286D8_NUM_INTERP(shader->shader.ninterp) |
  163. S_0286D8_BC_OPTIMIZE_DISABLE(1);
  164. si_pm4_set_reg(pm4, R_0286E0_SPI_BARYC_CNTL, spi_baryc_cntl);
  165. spi_ps_input_ena = shader->spi_ps_input_ena;
  166. /* we need to enable at least one of them, otherwise we hang the GPU */
  167. assert(G_0286CC_PERSP_SAMPLE_ENA(spi_ps_input_ena) ||
  168. G_0286CC_PERSP_CENTER_ENA(spi_ps_input_ena) ||
  169. G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena) ||
  170. G_0286CC_PERSP_PULL_MODEL_ENA(spi_ps_input_ena) ||
  171. G_0286CC_LINEAR_SAMPLE_ENA(spi_ps_input_ena) ||
  172. G_0286CC_LINEAR_CENTER_ENA(spi_ps_input_ena) ||
  173. G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena) ||
  174. G_0286CC_LINE_STIPPLE_TEX_ENA(spi_ps_input_ena));
  175. si_pm4_set_reg(pm4, R_0286CC_SPI_PS_INPUT_ENA, spi_ps_input_ena);
  176. si_pm4_set_reg(pm4, R_0286D0_SPI_PS_INPUT_ADDR, spi_ps_input_ena);
  177. si_pm4_set_reg(pm4, R_0286D8_SPI_PS_IN_CONTROL, spi_ps_in_control);
  178. if (G_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(db_shader_control))
  179. spi_shader_z_format = V_028710_SPI_SHADER_32_GR;
  180. else if (G_02880C_Z_EXPORT_ENABLE(db_shader_control))
  181. spi_shader_z_format = V_028710_SPI_SHADER_32_R;
  182. else
  183. spi_shader_z_format = 0;
  184. si_pm4_set_reg(pm4, R_028710_SPI_SHADER_Z_FORMAT, spi_shader_z_format);
  185. si_pm4_set_reg(pm4, R_028714_SPI_SHADER_COL_FORMAT,
  186. shader->spi_shader_col_format);
  187. si_pm4_set_reg(pm4, R_02823C_CB_SHADER_MASK, shader->cb_shader_mask);
  188. va = r600_resource_va(ctx->screen, (void *)shader->bo);
  189. si_pm4_add_bo(pm4, shader->bo, RADEON_USAGE_READ);
  190. si_pm4_set_reg(pm4, R_00B020_SPI_SHADER_PGM_LO_PS, va >> 8);
  191. si_pm4_set_reg(pm4, R_00B024_SPI_SHADER_PGM_HI_PS, va >> 40);
  192. num_user_sgprs = SI_PS_NUM_USER_SGPR;
  193. num_sgprs = shader->num_sgprs;
  194. /* One SGPR after user SGPRs is pre-loaded with {prim_mask, lds_offset} */
  195. if ((num_user_sgprs + 1) > num_sgprs) {
  196. /* Last 2 reserved SGPRs are used for VCC */
  197. num_sgprs = num_user_sgprs + 1 + 2;
  198. }
  199. assert(num_sgprs <= 104);
  200. si_pm4_set_reg(pm4, R_00B028_SPI_SHADER_PGM_RSRC1_PS,
  201. S_00B028_VGPRS((shader->num_vgprs - 1) / 4) |
  202. S_00B028_SGPRS((num_sgprs - 1) / 8));
  203. si_pm4_set_reg(pm4, R_00B02C_SPI_SHADER_PGM_RSRC2_PS,
  204. S_00B02C_EXTRA_LDS_SIZE(shader->lds_size) |
  205. S_00B02C_USER_SGPR(num_user_sgprs));
  206. if (rctx->b.chip_class >= CIK) {
  207. si_pm4_set_reg(pm4, R_00B01C_SPI_SHADER_PGM_RSRC3_PS,
  208. S_00B01C_CU_EN(0xffff));
  209. }
  210. si_pm4_set_reg(pm4, R_02880C_DB_SHADER_CONTROL, db_shader_control);
  211. shader->cb0_is_integer = rctx->fb_cb0_is_integer;
  212. shader->sprite_coord_enable = rctx->sprite_coord_enable;
  213. si_pm4_bind_state(rctx, ps, shader->pm4);
  214. rctx->b.flags |= R600_CONTEXT_INV_SHADER_CACHE;
  215. }
  216. /*
  217. * Drawing
  218. */
  219. static unsigned si_conv_pipe_prim(unsigned pprim)
  220. {
  221. static const unsigned prim_conv[] = {
  222. [PIPE_PRIM_POINTS] = V_008958_DI_PT_POINTLIST,
  223. [PIPE_PRIM_LINES] = V_008958_DI_PT_LINELIST,
  224. [PIPE_PRIM_LINE_LOOP] = V_008958_DI_PT_LINELOOP,
  225. [PIPE_PRIM_LINE_STRIP] = V_008958_DI_PT_LINESTRIP,
  226. [PIPE_PRIM_TRIANGLES] = V_008958_DI_PT_TRILIST,
  227. [PIPE_PRIM_TRIANGLE_STRIP] = V_008958_DI_PT_TRISTRIP,
  228. [PIPE_PRIM_TRIANGLE_FAN] = V_008958_DI_PT_TRIFAN,
  229. [PIPE_PRIM_QUADS] = V_008958_DI_PT_QUADLIST,
  230. [PIPE_PRIM_QUAD_STRIP] = V_008958_DI_PT_QUADSTRIP,
  231. [PIPE_PRIM_POLYGON] = V_008958_DI_PT_POLYGON,
  232. [PIPE_PRIM_LINES_ADJACENCY] = ~0,
  233. [PIPE_PRIM_LINE_STRIP_ADJACENCY] = ~0,
  234. [PIPE_PRIM_TRIANGLES_ADJACENCY] = ~0,
  235. [PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY] = ~0
  236. };
  237. unsigned result = prim_conv[pprim];
  238. if (result == ~0) {
  239. R600_ERR("unsupported primitive type %d\n", pprim);
  240. }
  241. return result;
  242. }
  243. static unsigned r600_conv_prim_to_gs_out(unsigned mode)
  244. {
  245. static const int prim_conv[] = {
  246. [PIPE_PRIM_POINTS] = V_028A6C_OUTPRIM_TYPE_POINTLIST,
  247. [PIPE_PRIM_LINES] = V_028A6C_OUTPRIM_TYPE_LINESTRIP,
  248. [PIPE_PRIM_LINE_LOOP] = V_028A6C_OUTPRIM_TYPE_LINESTRIP,
  249. [PIPE_PRIM_LINE_STRIP] = V_028A6C_OUTPRIM_TYPE_LINESTRIP,
  250. [PIPE_PRIM_TRIANGLES] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
  251. [PIPE_PRIM_TRIANGLE_STRIP] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
  252. [PIPE_PRIM_TRIANGLE_FAN] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
  253. [PIPE_PRIM_QUADS] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
  254. [PIPE_PRIM_QUAD_STRIP] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
  255. [PIPE_PRIM_POLYGON] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
  256. [PIPE_PRIM_LINES_ADJACENCY] = V_028A6C_OUTPRIM_TYPE_LINESTRIP,
  257. [PIPE_PRIM_LINE_STRIP_ADJACENCY] = V_028A6C_OUTPRIM_TYPE_LINESTRIP,
  258. [PIPE_PRIM_TRIANGLES_ADJACENCY] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
  259. [PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY] = V_028A6C_OUTPRIM_TYPE_TRISTRIP
  260. };
  261. assert(mode < Elements(prim_conv));
  262. return prim_conv[mode];
  263. }
  264. static bool si_update_draw_info_state(struct r600_context *rctx,
  265. const struct pipe_draw_info *info)
  266. {
  267. struct si_pm4_state *pm4 = si_pm4_alloc_state(rctx);
  268. struct si_shader *vs = &rctx->vs_shader->current->shader;
  269. unsigned prim = si_conv_pipe_prim(info->mode);
  270. unsigned gs_out_prim = r600_conv_prim_to_gs_out(info->mode);
  271. unsigned ls_mask = 0;
  272. if (pm4 == NULL)
  273. return false;
  274. if (prim == ~0) {
  275. FREE(pm4);
  276. return false;
  277. }
  278. if (rctx->b.chip_class >= CIK)
  279. si_pm4_set_reg(pm4, R_030908_VGT_PRIMITIVE_TYPE, prim);
  280. else {
  281. si_pm4_set_reg(pm4, R_008958_VGT_PRIMITIVE_TYPE, prim);
  282. si_pm4_set_reg(pm4, R_028A6C_VGT_GS_OUT_PRIM_TYPE, gs_out_prim);
  283. }
  284. si_pm4_set_reg(pm4, R_028400_VGT_MAX_VTX_INDX, ~0);
  285. si_pm4_set_reg(pm4, R_028404_VGT_MIN_VTX_INDX, 0);
  286. si_pm4_set_reg(pm4, R_028408_VGT_INDX_OFFSET,
  287. info->indexed ? info->index_bias : info->start);
  288. si_pm4_set_reg(pm4, R_02840C_VGT_MULTI_PRIM_IB_RESET_INDX, info->restart_index);
  289. si_pm4_set_reg(pm4, R_028A94_VGT_MULTI_PRIM_IB_RESET_EN, info->primitive_restart);
  290. si_pm4_set_reg(pm4, R_00B130_SPI_SHADER_USER_DATA_VS_0 + SI_SGPR_START_INSTANCE * 4,
  291. info->start_instance);
  292. if (prim == V_008958_DI_PT_LINELIST)
  293. ls_mask = 1;
  294. else if (prim == V_008958_DI_PT_LINESTRIP)
  295. ls_mask = 2;
  296. si_pm4_set_reg(pm4, R_028A0C_PA_SC_LINE_STIPPLE,
  297. S_028A0C_AUTO_RESET_CNTL(ls_mask) |
  298. rctx->pa_sc_line_stipple);
  299. if (info->mode == PIPE_PRIM_QUADS || info->mode == PIPE_PRIM_QUAD_STRIP || info->mode == PIPE_PRIM_POLYGON) {
  300. si_pm4_set_reg(pm4, R_028814_PA_SU_SC_MODE_CNTL,
  301. S_028814_PROVOKING_VTX_LAST(1) | rctx->pa_su_sc_mode_cntl);
  302. } else {
  303. si_pm4_set_reg(pm4, R_028814_PA_SU_SC_MODE_CNTL, rctx->pa_su_sc_mode_cntl);
  304. }
  305. si_pm4_set_reg(pm4, R_02881C_PA_CL_VS_OUT_CNTL,
  306. S_02881C_USE_VTX_POINT_SIZE(vs->vs_out_point_size) |
  307. S_02881C_VS_OUT_CCDIST0_VEC_ENA((vs->clip_dist_write & 0x0F) != 0) |
  308. S_02881C_VS_OUT_CCDIST1_VEC_ENA((vs->clip_dist_write & 0xF0) != 0) |
  309. S_02881C_VS_OUT_MISC_VEC_ENA(vs->vs_out_misc_write) |
  310. (rctx->queued.named.rasterizer->clip_plane_enable &
  311. vs->clip_dist_write));
  312. si_pm4_set_reg(pm4, R_028810_PA_CL_CLIP_CNTL,
  313. rctx->queued.named.rasterizer->pa_cl_clip_cntl |
  314. (vs->clip_dist_write ? 0 :
  315. rctx->queued.named.rasterizer->clip_plane_enable & 0x3F));
  316. si_pm4_set_state(rctx, draw_info, pm4);
  317. return true;
  318. }
  319. static void si_update_spi_map(struct r600_context *rctx)
  320. {
  321. struct si_shader *ps = &rctx->ps_shader->current->shader;
  322. struct si_shader *vs = &rctx->vs_shader->current->shader;
  323. struct si_pm4_state *pm4 = si_pm4_alloc_state(rctx);
  324. unsigned i, j, tmp;
  325. for (i = 0; i < ps->ninput; i++) {
  326. unsigned name = ps->input[i].name;
  327. unsigned param_offset = ps->input[i].param_offset;
  328. if (name == TGSI_SEMANTIC_POSITION)
  329. /* Read from preloaded VGPRs, not parameters */
  330. continue;
  331. bcolor:
  332. tmp = 0;
  333. if (ps->input[i].interpolate == TGSI_INTERPOLATE_CONSTANT ||
  334. (ps->input[i].interpolate == TGSI_INTERPOLATE_COLOR &&
  335. rctx->ps_shader->current->key.ps.flatshade)) {
  336. tmp |= S_028644_FLAT_SHADE(1);
  337. }
  338. if (name == TGSI_SEMANTIC_GENERIC &&
  339. rctx->sprite_coord_enable & (1 << ps->input[i].sid)) {
  340. tmp |= S_028644_PT_SPRITE_TEX(1);
  341. }
  342. for (j = 0; j < vs->noutput; j++) {
  343. if (name == vs->output[j].name &&
  344. ps->input[i].sid == vs->output[j].sid) {
  345. tmp |= S_028644_OFFSET(vs->output[j].param_offset);
  346. break;
  347. }
  348. }
  349. if (j == vs->noutput) {
  350. /* No corresponding output found, load defaults into input */
  351. tmp |= S_028644_OFFSET(0x20);
  352. }
  353. si_pm4_set_reg(pm4,
  354. R_028644_SPI_PS_INPUT_CNTL_0 + param_offset * 4,
  355. tmp);
  356. if (name == TGSI_SEMANTIC_COLOR &&
  357. rctx->ps_shader->current->key.ps.color_two_side) {
  358. name = TGSI_SEMANTIC_BCOLOR;
  359. param_offset++;
  360. goto bcolor;
  361. }
  362. }
  363. si_pm4_set_state(rctx, spi, pm4);
  364. }
  365. static void si_update_derived_state(struct r600_context *rctx)
  366. {
  367. struct pipe_context * ctx = (struct pipe_context*)rctx;
  368. unsigned vs_dirty = 0, ps_dirty = 0;
  369. if (!rctx->blitter->running) {
  370. /* Flush depth textures which need to be flushed. */
  371. for (int i = 0; i < SI_NUM_SHADERS; i++) {
  372. if (rctx->samplers[i].depth_texture_mask) {
  373. si_flush_depth_textures(rctx, &rctx->samplers[i]);
  374. }
  375. if (rctx->samplers[i].compressed_colortex_mask) {
  376. r600_decompress_color_textures(rctx, &rctx->samplers[i]);
  377. }
  378. }
  379. }
  380. si_shader_select(ctx, rctx->vs_shader, &vs_dirty);
  381. if (!rctx->vs_shader->current->pm4) {
  382. si_pipe_shader_vs(ctx, rctx->vs_shader->current);
  383. vs_dirty = 0;
  384. }
  385. if (vs_dirty) {
  386. si_pm4_bind_state(rctx, vs, rctx->vs_shader->current->pm4);
  387. }
  388. si_shader_select(ctx, rctx->ps_shader, &ps_dirty);
  389. if (!rctx->ps_shader->current->pm4) {
  390. si_pipe_shader_ps(ctx, rctx->ps_shader->current);
  391. ps_dirty = 0;
  392. }
  393. if (!rctx->ps_shader->current->bo) {
  394. if (!rctx->dummy_pixel_shader->pm4)
  395. si_pipe_shader_ps(ctx, rctx->dummy_pixel_shader);
  396. else
  397. si_pm4_bind_state(rctx, vs, rctx->dummy_pixel_shader->pm4);
  398. ps_dirty = 0;
  399. }
  400. if (rctx->ps_shader->current->cb0_is_integer != rctx->fb_cb0_is_integer) {
  401. si_pipe_shader_ps(ctx, rctx->ps_shader->current);
  402. ps_dirty = 1;
  403. }
  404. if (ps_dirty) {
  405. si_pm4_bind_state(rctx, ps, rctx->ps_shader->current->pm4);
  406. }
  407. if (si_pm4_state_changed(rctx, ps) || si_pm4_state_changed(rctx, vs)) {
  408. /* XXX: Emitting the PS state even when only the VS changed
  409. * fixes random failures with piglit glsl-max-varyings.
  410. * Not sure why...
  411. */
  412. rctx->emitted.named.ps = NULL;
  413. si_update_spi_map(rctx);
  414. }
  415. }
  416. static void si_vertex_buffer_update(struct r600_context *rctx)
  417. {
  418. struct pipe_context *ctx = &rctx->b.b;
  419. struct si_pm4_state *pm4 = si_pm4_alloc_state(rctx);
  420. bool bound[PIPE_MAX_ATTRIBS] = {};
  421. unsigned i, count;
  422. uint64_t va;
  423. rctx->b.flags |= R600_CONTEXT_INV_TEX_CACHE;
  424. count = rctx->vertex_elements->count;
  425. assert(count <= 256 / 4);
  426. si_pm4_sh_data_begin(pm4);
  427. for (i = 0 ; i < count; i++) {
  428. struct pipe_vertex_element *ve = &rctx->vertex_elements->elements[i];
  429. struct pipe_vertex_buffer *vb;
  430. struct r600_resource *rbuffer;
  431. unsigned offset;
  432. if (ve->vertex_buffer_index >= rctx->nr_vertex_buffers)
  433. continue;
  434. vb = &rctx->vertex_buffer[ve->vertex_buffer_index];
  435. rbuffer = (struct r600_resource*)vb->buffer;
  436. if (rbuffer == NULL)
  437. continue;
  438. offset = 0;
  439. offset += vb->buffer_offset;
  440. offset += ve->src_offset;
  441. va = r600_resource_va(ctx->screen, (void*)rbuffer);
  442. va += offset;
  443. /* Fill in T# buffer resource description */
  444. si_pm4_sh_data_add(pm4, va & 0xFFFFFFFF);
  445. si_pm4_sh_data_add(pm4, (S_008F04_BASE_ADDRESS_HI(va >> 32) |
  446. S_008F04_STRIDE(vb->stride)));
  447. if (vb->stride)
  448. /* Round up by rounding down and adding 1 */
  449. si_pm4_sh_data_add(pm4,
  450. (vb->buffer->width0 - offset -
  451. util_format_get_blocksize(ve->src_format)) /
  452. vb->stride + 1);
  453. else
  454. si_pm4_sh_data_add(pm4, vb->buffer->width0 - offset);
  455. si_pm4_sh_data_add(pm4, rctx->vertex_elements->rsrc_word3[i]);
  456. if (!bound[ve->vertex_buffer_index]) {
  457. si_pm4_add_bo(pm4, rbuffer, RADEON_USAGE_READ);
  458. bound[ve->vertex_buffer_index] = true;
  459. }
  460. }
  461. si_pm4_sh_data_end(pm4, R_00B130_SPI_SHADER_USER_DATA_VS_0, SI_SGPR_VERTEX_BUFFER);
  462. si_pm4_set_state(rctx, vertex_buffers, pm4);
  463. }
  464. static void si_state_draw(struct r600_context *rctx,
  465. const struct pipe_draw_info *info,
  466. const struct pipe_index_buffer *ib)
  467. {
  468. struct si_pm4_state *pm4 = si_pm4_alloc_state(rctx);
  469. if (pm4 == NULL)
  470. return;
  471. /* queries need some special values
  472. * (this is non-zero if any query is active) */
  473. if (rctx->num_cs_dw_nontimer_queries_suspend) {
  474. struct si_state_dsa *dsa = rctx->queued.named.dsa;
  475. si_pm4_set_reg(pm4, R_028004_DB_COUNT_CONTROL,
  476. S_028004_PERFECT_ZPASS_COUNTS(1) |
  477. S_028004_SAMPLE_RATE(rctx->fb_log_samples));
  478. si_pm4_set_reg(pm4, R_02800C_DB_RENDER_OVERRIDE,
  479. dsa->db_render_override |
  480. S_02800C_NOOP_CULL_DISABLE(1));
  481. }
  482. if (info->count_from_stream_output) {
  483. struct r600_so_target *t =
  484. (struct r600_so_target*)info->count_from_stream_output;
  485. uint64_t va = r600_resource_va(&rctx->screen->b.b,
  486. &t->buf_filled_size->b.b);
  487. va += t->buf_filled_size_offset;
  488. si_pm4_set_reg(pm4, R_028B30_VGT_STRMOUT_DRAW_OPAQUE_VERTEX_STRIDE,
  489. t->stride_in_dw);
  490. si_pm4_cmd_begin(pm4, PKT3_COPY_DATA);
  491. si_pm4_cmd_add(pm4,
  492. COPY_DATA_SRC_SEL(COPY_DATA_MEM) |
  493. COPY_DATA_DST_SEL(COPY_DATA_REG) |
  494. COPY_DATA_WR_CONFIRM);
  495. si_pm4_cmd_add(pm4, va); /* src address lo */
  496. si_pm4_cmd_add(pm4, va >> 32UL); /* src address hi */
  497. si_pm4_cmd_add(pm4, R_028B2C_VGT_STRMOUT_DRAW_OPAQUE_BUFFER_FILLED_SIZE >> 2);
  498. si_pm4_cmd_add(pm4, 0); /* unused */
  499. si_pm4_add_bo(pm4, t->buf_filled_size, RADEON_USAGE_READ);
  500. si_pm4_cmd_end(pm4, true);
  501. }
  502. /* draw packet */
  503. si_pm4_cmd_begin(pm4, PKT3_INDEX_TYPE);
  504. if (ib->index_size == 4) {
  505. si_pm4_cmd_add(pm4, V_028A7C_VGT_INDEX_32 | (R600_BIG_ENDIAN ?
  506. V_028A7C_VGT_DMA_SWAP_32_BIT : 0));
  507. } else {
  508. si_pm4_cmd_add(pm4, V_028A7C_VGT_INDEX_16 | (R600_BIG_ENDIAN ?
  509. V_028A7C_VGT_DMA_SWAP_16_BIT : 0));
  510. }
  511. si_pm4_cmd_end(pm4, rctx->predicate_drawing);
  512. si_pm4_cmd_begin(pm4, PKT3_NUM_INSTANCES);
  513. si_pm4_cmd_add(pm4, info->instance_count);
  514. si_pm4_cmd_end(pm4, rctx->predicate_drawing);
  515. if (info->indexed) {
  516. uint32_t max_size = (ib->buffer->width0 - ib->offset) /
  517. rctx->index_buffer.index_size;
  518. uint64_t va;
  519. va = r600_resource_va(&rctx->screen->b.b, ib->buffer);
  520. va += ib->offset;
  521. si_pm4_add_bo(pm4, (struct r600_resource *)ib->buffer, RADEON_USAGE_READ);
  522. si_cmd_draw_index_2(pm4, max_size, va, info->count,
  523. V_0287F0_DI_SRC_SEL_DMA,
  524. rctx->predicate_drawing);
  525. } else {
  526. uint32_t initiator = V_0287F0_DI_SRC_SEL_AUTO_INDEX;
  527. initiator |= S_0287F0_USE_OPAQUE(!!info->count_from_stream_output);
  528. si_cmd_draw_index_auto(pm4, info->count, initiator, rctx->predicate_drawing);
  529. }
  530. si_pm4_set_state(rctx, draw, pm4);
  531. }
  532. void si_emit_cache_flush(struct r600_common_context *rctx, struct r600_atom *atom)
  533. {
  534. struct radeon_winsys_cs *cs = rctx->rings.gfx.cs;
  535. uint32_t cp_coher_cntl = 0;
  536. /* XXX SI flushes both ICACHE and KCACHE if either flag is set.
  537. * XXX CIK shouldn't have this issue. Test CIK before separating the flags
  538. * XXX to ensure there is no regression. Also find out if there is another
  539. * XXX way to flush either ICACHE or KCACHE but not both for SI. */
  540. if (rctx->flags & (R600_CONTEXT_INV_SHADER_CACHE |
  541. R600_CONTEXT_INV_CONST_CACHE)) {
  542. cp_coher_cntl |= S_0085F0_SH_ICACHE_ACTION_ENA(1) |
  543. S_0085F0_SH_KCACHE_ACTION_ENA(1);
  544. }
  545. if (rctx->flags & (R600_CONTEXT_INV_TEX_CACHE |
  546. R600_CONTEXT_STREAMOUT_FLUSH)) {
  547. cp_coher_cntl |= S_0085F0_TC_ACTION_ENA(1) |
  548. S_0085F0_TCL1_ACTION_ENA(1);
  549. }
  550. if (rctx->flags & R600_CONTEXT_FLUSH_AND_INV_CB) {
  551. cp_coher_cntl |= S_0085F0_CB_ACTION_ENA(1) |
  552. S_0085F0_CB0_DEST_BASE_ENA(1) |
  553. S_0085F0_CB1_DEST_BASE_ENA(1) |
  554. S_0085F0_CB2_DEST_BASE_ENA(1) |
  555. S_0085F0_CB3_DEST_BASE_ENA(1) |
  556. S_0085F0_CB4_DEST_BASE_ENA(1) |
  557. S_0085F0_CB5_DEST_BASE_ENA(1) |
  558. S_0085F0_CB6_DEST_BASE_ENA(1) |
  559. S_0085F0_CB7_DEST_BASE_ENA(1);
  560. }
  561. if (rctx->flags & R600_CONTEXT_FLUSH_AND_INV_DB) {
  562. cp_coher_cntl |= S_0085F0_DB_ACTION_ENA(1) |
  563. S_0085F0_DB_DEST_BASE_ENA(1);
  564. }
  565. if (cp_coher_cntl) {
  566. if (rctx->chip_class >= CIK) {
  567. radeon_emit(cs, PKT3(PKT3_ACQUIRE_MEM, 5, 0));
  568. radeon_emit(cs, cp_coher_cntl); /* CP_COHER_CNTL */
  569. radeon_emit(cs, 0xffffffff); /* CP_COHER_SIZE */
  570. radeon_emit(cs, 0xff); /* CP_COHER_SIZE_HI */
  571. radeon_emit(cs, 0); /* CP_COHER_BASE */
  572. radeon_emit(cs, 0); /* CP_COHER_BASE_HI */
  573. radeon_emit(cs, 0x0000000A); /* POLL_INTERVAL */
  574. } else {
  575. radeon_emit(cs, PKT3(PKT3_SURFACE_SYNC, 3, 0));
  576. radeon_emit(cs, cp_coher_cntl); /* CP_COHER_CNTL */
  577. radeon_emit(cs, 0xffffffff); /* CP_COHER_SIZE */
  578. radeon_emit(cs, 0); /* CP_COHER_BASE */
  579. radeon_emit(cs, 0x0000000A); /* POLL_INTERVAL */
  580. }
  581. }
  582. if (rctx->flags & R600_CONTEXT_FLUSH_AND_INV_CB_META) {
  583. radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
  584. radeon_emit(cs, EVENT_TYPE(V_028A90_FLUSH_AND_INV_CB_META) | EVENT_INDEX(0));
  585. }
  586. if (rctx->flags & R600_CONTEXT_STREAMOUT_FLUSH) {
  587. /* Needed if streamout buffers are going to be used as a source. */
  588. radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
  589. radeon_emit(cs, EVENT_TYPE(V_028A90_VS_PARTIAL_FLUSH) | EVENT_INDEX(4));
  590. }
  591. rctx->flags = 0;
  592. }
  593. const struct r600_atom si_atom_cache_flush = { si_emit_cache_flush, 11 }; /* number of CS dwords */
  594. void si_draw_vbo(struct pipe_context *ctx, const struct pipe_draw_info *info)
  595. {
  596. struct r600_context *rctx = (struct r600_context *)ctx;
  597. struct pipe_index_buffer ib = {};
  598. uint32_t i;
  599. if (!info->count && (info->indexed || !info->count_from_stream_output))
  600. return;
  601. if (!rctx->ps_shader || !rctx->vs_shader)
  602. return;
  603. si_update_derived_state(rctx);
  604. si_vertex_buffer_update(rctx);
  605. if (info->indexed) {
  606. /* Initialize the index buffer struct. */
  607. pipe_resource_reference(&ib.buffer, rctx->index_buffer.buffer);
  608. ib.user_buffer = rctx->index_buffer.user_buffer;
  609. ib.index_size = rctx->index_buffer.index_size;
  610. ib.offset = rctx->index_buffer.offset + info->start * ib.index_size;
  611. /* Translate or upload, if needed. */
  612. r600_translate_index_buffer(rctx, &ib, info->count);
  613. if (ib.user_buffer && !ib.buffer) {
  614. r600_upload_index_buffer(rctx, &ib, info->count);
  615. }
  616. }
  617. if (!si_update_draw_info_state(rctx, info))
  618. return;
  619. si_state_draw(rctx, info, &ib);
  620. rctx->pm4_dirty_cdwords += si_pm4_dirty_dw(rctx);
  621. /* Check flush flags. */
  622. if (rctx->b.flags)
  623. rctx->atoms.cache_flush->dirty = true;
  624. si_need_cs_space(rctx, 0, TRUE);
  625. /* Emit states. */
  626. for (i = 0; i < SI_NUM_ATOMS(rctx); i++) {
  627. if (rctx->atoms.array[i]->dirty) {
  628. rctx->atoms.array[i]->emit(&rctx->b, rctx->atoms.array[i]);
  629. rctx->atoms.array[i]->dirty = false;
  630. }
  631. }
  632. si_pm4_emit_dirty(rctx);
  633. rctx->pm4_dirty_cdwords = 0;
  634. #if R600_TRACE_CS
  635. if (rctx->screen->trace_bo) {
  636. r600_trace_emit(rctx);
  637. }
  638. #endif
  639. /* Set the depth buffer as dirty. */
  640. if (rctx->framebuffer.zsbuf) {
  641. struct pipe_surface *surf = rctx->framebuffer.zsbuf;
  642. struct r600_texture *rtex = (struct r600_texture *)surf->texture;
  643. rtex->dirty_level_mask |= 1 << surf->u.tex.level;
  644. }
  645. if (rctx->fb_compressed_cb_mask) {
  646. struct pipe_surface *surf;
  647. struct r600_texture *rtex;
  648. unsigned mask = rctx->fb_compressed_cb_mask;
  649. do {
  650. unsigned i = u_bit_scan(&mask);
  651. surf = rctx->framebuffer.cbufs[i];
  652. rtex = (struct r600_texture*)surf->texture;
  653. rtex->dirty_level_mask |= 1 << surf->u.tex.level;
  654. } while (mask);
  655. }
  656. pipe_resource_reference(&ib.buffer, NULL);
  657. }