Clone of mesa.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

r300_state.c 54KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524
  1. /*
  2. * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
  3. * Copyright 2009 Marek Olšák <maraeo@gmail.com>
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * on the rights to use, copy, modify, merge, publish, distribute, sub
  9. * license, and/or sell copies of the Software, and to permit persons to whom
  10. * the Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
  20. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  21. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  22. * USE OR OTHER DEALINGS IN THE SOFTWARE. */
  23. #include "draw/draw_context.h"
  24. #include "util/u_math.h"
  25. #include "util/u_memory.h"
  26. #include "util/u_pack_color.h"
  27. #include "tgsi/tgsi_parse.h"
  28. #include "pipe/p_config.h"
  29. #include "r300_context.h"
  30. #include "r300_reg.h"
  31. #include "r300_screen.h"
  32. #include "r300_screen_buffer.h"
  33. #include "r300_state_inlines.h"
  34. #include "r300_fs.h"
  35. #include "r300_vs.h"
  36. #include "radeon_winsys.h"
  37. /* r300_state: Functions used to intialize state context by translating
  38. * Gallium state objects into semi-native r300 state objects. */
  39. #define UPDATE_STATE(cso, atom) \
  40. if (cso != atom.state) { \
  41. atom.state = cso; \
  42. atom.dirty = TRUE; \
  43. }
  44. static boolean blend_discard_if_src_alpha_0(unsigned srcRGB, unsigned srcA,
  45. unsigned dstRGB, unsigned dstA)
  46. {
  47. /* If the blend equation is ADD or REVERSE_SUBTRACT,
  48. * SRC_ALPHA == 0, and the following state is set, the colorbuffer
  49. * will not be changed.
  50. * Notice that the dst factors are the src factors inverted. */
  51. return (srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
  52. srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
  53. srcRGB == PIPE_BLENDFACTOR_ZERO) &&
  54. (srcA == PIPE_BLENDFACTOR_SRC_COLOR ||
  55. srcA == PIPE_BLENDFACTOR_SRC_ALPHA ||
  56. srcA == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
  57. srcA == PIPE_BLENDFACTOR_ZERO) &&
  58. (dstRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
  59. dstRGB == PIPE_BLENDFACTOR_ONE) &&
  60. (dstA == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
  61. dstA == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
  62. dstA == PIPE_BLENDFACTOR_ONE);
  63. }
  64. static boolean blend_discard_if_src_alpha_1(unsigned srcRGB, unsigned srcA,
  65. unsigned dstRGB, unsigned dstA)
  66. {
  67. /* If the blend equation is ADD or REVERSE_SUBTRACT,
  68. * SRC_ALPHA == 1, and the following state is set, the colorbuffer
  69. * will not be changed.
  70. * Notice that the dst factors are the src factors inverted. */
  71. return (srcRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
  72. srcRGB == PIPE_BLENDFACTOR_ZERO) &&
  73. (srcA == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
  74. srcA == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
  75. srcA == PIPE_BLENDFACTOR_ZERO) &&
  76. (dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
  77. dstRGB == PIPE_BLENDFACTOR_ONE) &&
  78. (dstA == PIPE_BLENDFACTOR_SRC_COLOR ||
  79. dstA == PIPE_BLENDFACTOR_SRC_ALPHA ||
  80. dstA == PIPE_BLENDFACTOR_ONE);
  81. }
  82. static boolean blend_discard_if_src_color_0(unsigned srcRGB, unsigned srcA,
  83. unsigned dstRGB, unsigned dstA)
  84. {
  85. /* If the blend equation is ADD or REVERSE_SUBTRACT,
  86. * SRC_COLOR == (0,0,0), and the following state is set, the colorbuffer
  87. * will not be changed.
  88. * Notice that the dst factors are the src factors inverted. */
  89. return (srcRGB == PIPE_BLENDFACTOR_SRC_COLOR ||
  90. srcRGB == PIPE_BLENDFACTOR_ZERO) &&
  91. (srcA == PIPE_BLENDFACTOR_ZERO) &&
  92. (dstRGB == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
  93. dstRGB == PIPE_BLENDFACTOR_ONE) &&
  94. (dstA == PIPE_BLENDFACTOR_ONE);
  95. }
  96. static boolean blend_discard_if_src_color_1(unsigned srcRGB, unsigned srcA,
  97. unsigned dstRGB, unsigned dstA)
  98. {
  99. /* If the blend equation is ADD or REVERSE_SUBTRACT,
  100. * SRC_COLOR == (1,1,1), and the following state is set, the colorbuffer
  101. * will not be changed.
  102. * Notice that the dst factors are the src factors inverted. */
  103. return (srcRGB == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
  104. srcRGB == PIPE_BLENDFACTOR_ZERO) &&
  105. (srcA == PIPE_BLENDFACTOR_ZERO) &&
  106. (dstRGB == PIPE_BLENDFACTOR_SRC_COLOR ||
  107. dstRGB == PIPE_BLENDFACTOR_ONE) &&
  108. (dstA == PIPE_BLENDFACTOR_ONE);
  109. }
  110. static boolean blend_discard_if_src_alpha_color_0(unsigned srcRGB, unsigned srcA,
  111. unsigned dstRGB, unsigned dstA)
  112. {
  113. /* If the blend equation is ADD or REVERSE_SUBTRACT,
  114. * SRC_ALPHA_COLOR == (0,0,0,0), and the following state is set,
  115. * the colorbuffer will not be changed.
  116. * Notice that the dst factors are the src factors inverted. */
  117. return (srcRGB == PIPE_BLENDFACTOR_SRC_COLOR ||
  118. srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
  119. srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
  120. srcRGB == PIPE_BLENDFACTOR_ZERO) &&
  121. (srcA == PIPE_BLENDFACTOR_SRC_COLOR ||
  122. srcA == PIPE_BLENDFACTOR_SRC_ALPHA ||
  123. srcA == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
  124. srcA == PIPE_BLENDFACTOR_ZERO) &&
  125. (dstRGB == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
  126. dstRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
  127. dstRGB == PIPE_BLENDFACTOR_ONE) &&
  128. (dstA == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
  129. dstA == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
  130. dstA == PIPE_BLENDFACTOR_ONE);
  131. }
  132. static boolean blend_discard_if_src_alpha_color_1(unsigned srcRGB, unsigned srcA,
  133. unsigned dstRGB, unsigned dstA)
  134. {
  135. /* If the blend equation is ADD or REVERSE_SUBTRACT,
  136. * SRC_ALPHA_COLOR == (1,1,1,1), and the following state is set,
  137. * the colorbuffer will not be changed.
  138. * Notice that the dst factors are the src factors inverted. */
  139. return (srcRGB == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
  140. srcRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
  141. srcRGB == PIPE_BLENDFACTOR_ZERO) &&
  142. (srcA == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
  143. srcA == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
  144. srcA == PIPE_BLENDFACTOR_ZERO) &&
  145. (dstRGB == PIPE_BLENDFACTOR_SRC_COLOR ||
  146. dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
  147. dstRGB == PIPE_BLENDFACTOR_ONE) &&
  148. (dstA == PIPE_BLENDFACTOR_SRC_COLOR ||
  149. dstA == PIPE_BLENDFACTOR_SRC_ALPHA ||
  150. dstA == PIPE_BLENDFACTOR_ONE);
  151. }
  152. static unsigned bgra_cmask(unsigned mask)
  153. {
  154. /* Gallium uses RGBA color ordering while R300 expects BGRA. */
  155. return ((mask & PIPE_MASK_R) << 2) |
  156. ((mask & PIPE_MASK_B) >> 2) |
  157. (mask & (PIPE_MASK_G | PIPE_MASK_A));
  158. }
  159. /* Create a new blend state based on the CSO blend state.
  160. *
  161. * This encompasses alpha blending, logic/raster ops, and blend dithering. */
  162. static void* r300_create_blend_state(struct pipe_context* pipe,
  163. const struct pipe_blend_state* state)
  164. {
  165. struct r300_screen* r300screen = r300_screen(pipe->screen);
  166. struct r300_blend_state* blend = CALLOC_STRUCT(r300_blend_state);
  167. if (state->rt[0].blend_enable)
  168. {
  169. unsigned eqRGB = state->rt[0].rgb_func;
  170. unsigned srcRGB = state->rt[0].rgb_src_factor;
  171. unsigned dstRGB = state->rt[0].rgb_dst_factor;
  172. unsigned eqA = state->rt[0].alpha_func;
  173. unsigned srcA = state->rt[0].alpha_src_factor;
  174. unsigned dstA = state->rt[0].alpha_dst_factor;
  175. /* despite the name, ALPHA_BLEND_ENABLE has nothing to do with alpha,
  176. * this is just the crappy D3D naming */
  177. blend->blend_control = R300_ALPHA_BLEND_ENABLE |
  178. r300_translate_blend_function(eqRGB) |
  179. ( r300_translate_blend_factor(srcRGB) << R300_SRC_BLEND_SHIFT) |
  180. ( r300_translate_blend_factor(dstRGB) << R300_DST_BLEND_SHIFT);
  181. /* Optimization: some operations do not require the destination color.
  182. *
  183. * When SRC_ALPHA_SATURATE is used, colorbuffer reads must be enabled,
  184. * otherwise blending gives incorrect results. It seems to be
  185. * a hardware bug. */
  186. if (eqRGB == PIPE_BLEND_MIN || eqA == PIPE_BLEND_MIN ||
  187. eqRGB == PIPE_BLEND_MAX || eqA == PIPE_BLEND_MAX ||
  188. dstRGB != PIPE_BLENDFACTOR_ZERO ||
  189. dstA != PIPE_BLENDFACTOR_ZERO ||
  190. srcRGB == PIPE_BLENDFACTOR_DST_COLOR ||
  191. srcRGB == PIPE_BLENDFACTOR_DST_ALPHA ||
  192. srcRGB == PIPE_BLENDFACTOR_INV_DST_COLOR ||
  193. srcRGB == PIPE_BLENDFACTOR_INV_DST_ALPHA ||
  194. srcA == PIPE_BLENDFACTOR_DST_COLOR ||
  195. srcA == PIPE_BLENDFACTOR_DST_ALPHA ||
  196. srcA == PIPE_BLENDFACTOR_INV_DST_COLOR ||
  197. srcA == PIPE_BLENDFACTOR_INV_DST_ALPHA ||
  198. srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE) {
  199. /* Enable reading from the colorbuffer. */
  200. blend->blend_control |= R300_READ_ENABLE;
  201. if (r300_screen(r300_context(pipe)->context.screen)->caps->is_r500) {
  202. /* Optimization: Depending on incoming pixels, we can
  203. * conditionally disable the reading in hardware... */
  204. if (eqRGB != PIPE_BLEND_MIN && eqA != PIPE_BLEND_MIN &&
  205. eqRGB != PIPE_BLEND_MAX && eqA != PIPE_BLEND_MAX) {
  206. /* Disable reading if SRC_ALPHA == 0. */
  207. if ((dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
  208. dstRGB == PIPE_BLENDFACTOR_ZERO) &&
  209. (dstA == PIPE_BLENDFACTOR_SRC_COLOR ||
  210. dstA == PIPE_BLENDFACTOR_SRC_ALPHA ||
  211. dstA == PIPE_BLENDFACTOR_ZERO)) {
  212. blend->blend_control |= R500_SRC_ALPHA_0_NO_READ;
  213. }
  214. /* Disable reading if SRC_ALPHA == 1. */
  215. if ((dstRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
  216. dstRGB == PIPE_BLENDFACTOR_ZERO) &&
  217. (dstA == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
  218. dstA == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
  219. dstA == PIPE_BLENDFACTOR_ZERO)) {
  220. blend->blend_control |= R500_SRC_ALPHA_1_NO_READ;
  221. }
  222. }
  223. }
  224. }
  225. /* Optimization: discard pixels which don't change the colorbuffer.
  226. *
  227. * The code below is non-trivial and some math is involved.
  228. *
  229. * Discarding pixels must be disabled when FP16 AA is enabled.
  230. * This is a hardware bug. Also, this implementation wouldn't work
  231. * with FP blending enabled and equation clamping disabled.
  232. *
  233. * Equations other than ADD are rarely used and therefore won't be
  234. * optimized. */
  235. if ((eqRGB == PIPE_BLEND_ADD || eqRGB == PIPE_BLEND_REVERSE_SUBTRACT) &&
  236. (eqA == PIPE_BLEND_ADD || eqA == PIPE_BLEND_REVERSE_SUBTRACT)) {
  237. /* ADD: X+Y
  238. * REVERSE_SUBTRACT: Y-X
  239. *
  240. * The idea is:
  241. * If X = src*srcFactor = 0 and Y = dst*dstFactor = 1,
  242. * then CB will not be changed.
  243. *
  244. * Given the srcFactor and dstFactor variables, we can derive
  245. * what src and dst should be equal to and discard appropriate
  246. * pixels.
  247. */
  248. if (blend_discard_if_src_alpha_0(srcRGB, srcA, dstRGB, dstA)) {
  249. blend->blend_control |= R300_DISCARD_SRC_PIXELS_SRC_ALPHA_0;
  250. } else if (blend_discard_if_src_alpha_1(srcRGB, srcA,
  251. dstRGB, dstA)) {
  252. blend->blend_control |= R300_DISCARD_SRC_PIXELS_SRC_ALPHA_1;
  253. } else if (blend_discard_if_src_color_0(srcRGB, srcA,
  254. dstRGB, dstA)) {
  255. blend->blend_control |= R300_DISCARD_SRC_PIXELS_SRC_COLOR_0;
  256. } else if (blend_discard_if_src_color_1(srcRGB, srcA,
  257. dstRGB, dstA)) {
  258. blend->blend_control |= R300_DISCARD_SRC_PIXELS_SRC_COLOR_1;
  259. } else if (blend_discard_if_src_alpha_color_0(srcRGB, srcA,
  260. dstRGB, dstA)) {
  261. blend->blend_control |=
  262. R300_DISCARD_SRC_PIXELS_SRC_ALPHA_COLOR_0;
  263. } else if (blend_discard_if_src_alpha_color_1(srcRGB, srcA,
  264. dstRGB, dstA)) {
  265. blend->blend_control |=
  266. R300_DISCARD_SRC_PIXELS_SRC_ALPHA_COLOR_1;
  267. }
  268. }
  269. /* separate alpha */
  270. if (srcA != srcRGB || dstA != dstRGB || eqA != eqRGB) {
  271. blend->blend_control |= R300_SEPARATE_ALPHA_ENABLE;
  272. blend->alpha_blend_control =
  273. r300_translate_blend_function(eqA) |
  274. (r300_translate_blend_factor(srcA) << R300_SRC_BLEND_SHIFT) |
  275. (r300_translate_blend_factor(dstA) << R300_DST_BLEND_SHIFT);
  276. }
  277. }
  278. /* PIPE_LOGICOP_* don't need to be translated, fortunately. */
  279. if (state->logicop_enable) {
  280. blend->rop = R300_RB3D_ROPCNTL_ROP_ENABLE |
  281. (state->logicop_func) << R300_RB3D_ROPCNTL_ROP_SHIFT;
  282. }
  283. /* Color channel masks for all MRTs. */
  284. blend->color_channel_mask = bgra_cmask(state->rt[0].colormask);
  285. if (r300screen->caps->is_r500 && state->independent_blend_enable) {
  286. if (state->rt[1].blend_enable) {
  287. blend->color_channel_mask |= bgra_cmask(state->rt[1].colormask) << 4;
  288. }
  289. if (state->rt[2].blend_enable) {
  290. blend->color_channel_mask |= bgra_cmask(state->rt[2].colormask) << 8;
  291. }
  292. if (state->rt[3].blend_enable) {
  293. blend->color_channel_mask |= bgra_cmask(state->rt[3].colormask) << 12;
  294. }
  295. }
  296. if (state->dither) {
  297. blend->dither = R300_RB3D_DITHER_CTL_DITHER_MODE_LUT |
  298. R300_RB3D_DITHER_CTL_ALPHA_DITHER_MODE_LUT;
  299. }
  300. return (void*)blend;
  301. }
  302. /* Bind blend state. */
  303. static void r300_bind_blend_state(struct pipe_context* pipe,
  304. void* state)
  305. {
  306. struct r300_context* r300 = r300_context(pipe);
  307. UPDATE_STATE(state, r300->blend_state);
  308. }
  309. /* Free blend state. */
  310. static void r300_delete_blend_state(struct pipe_context* pipe,
  311. void* state)
  312. {
  313. FREE(state);
  314. }
  315. /* Convert float to 10bit integer */
  316. static unsigned float_to_fixed10(float f)
  317. {
  318. return CLAMP((unsigned)(f * 1023.9f), 0, 1023);
  319. }
  320. /* Set blend color.
  321. * Setup both R300 and R500 registers, figure out later which one to write. */
  322. static void r300_set_blend_color(struct pipe_context* pipe,
  323. const struct pipe_blend_color* color)
  324. {
  325. struct r300_context* r300 = r300_context(pipe);
  326. struct r300_screen* r300screen = r300_screen(pipe->screen);
  327. struct r300_blend_color_state* state =
  328. (struct r300_blend_color_state*)r300->blend_color_state.state;
  329. union util_color uc;
  330. util_pack_color(color->color, PIPE_FORMAT_B8G8R8A8_UNORM, &uc);
  331. state->blend_color = uc.ui;
  332. /* XXX if FP16 blending is enabled, we should use the FP16 format */
  333. state->blend_color_red_alpha =
  334. float_to_fixed10(color->color[0]) |
  335. (float_to_fixed10(color->color[3]) << 16);
  336. state->blend_color_green_blue =
  337. float_to_fixed10(color->color[2]) |
  338. (float_to_fixed10(color->color[1]) << 16);
  339. r300->blend_color_state.size = r300screen->caps->is_r500 ? 3 : 2;
  340. r300->blend_color_state.dirty = TRUE;
  341. }
  342. static void r300_set_clip_state(struct pipe_context* pipe,
  343. const struct pipe_clip_state* state)
  344. {
  345. struct r300_context* r300 = r300_context(pipe);
  346. r300->clip = *state;
  347. if (r300_screen(pipe->screen)->caps->has_tcl) {
  348. memcpy(r300->clip_state.state, state, sizeof(struct pipe_clip_state));
  349. r300->clip_state.size = 29;
  350. } else {
  351. draw_flush(r300->draw);
  352. draw_set_clip_state(r300->draw, state);
  353. r300->clip_state.size = 2;
  354. }
  355. r300->clip_state.dirty = TRUE;
  356. }
  357. /* Create a new depth, stencil, and alpha state based on the CSO dsa state.
  358. *
  359. * This contains the depth buffer, stencil buffer, alpha test, and such.
  360. * On the Radeon, depth and stencil buffer setup are intertwined, which is
  361. * the reason for some of the strange-looking assignments across registers. */
  362. static void*
  363. r300_create_dsa_state(struct pipe_context* pipe,
  364. const struct pipe_depth_stencil_alpha_state* state)
  365. {
  366. struct r300_capabilities *caps =
  367. r300_screen(r300_context(pipe)->context.screen)->caps;
  368. struct r300_dsa_state* dsa = CALLOC_STRUCT(r300_dsa_state);
  369. /* Depth test setup. */
  370. if (state->depth.enabled) {
  371. dsa->z_buffer_control |= R300_Z_ENABLE;
  372. if (state->depth.writemask) {
  373. dsa->z_buffer_control |= R300_Z_WRITE_ENABLE;
  374. }
  375. dsa->z_stencil_control |=
  376. (r300_translate_depth_stencil_function(state->depth.func) <<
  377. R300_Z_FUNC_SHIFT);
  378. }
  379. /* Stencil buffer setup. */
  380. if (state->stencil[0].enabled) {
  381. dsa->z_buffer_control |= R300_STENCIL_ENABLE;
  382. dsa->z_stencil_control |=
  383. (r300_translate_depth_stencil_function(state->stencil[0].func) <<
  384. R300_S_FRONT_FUNC_SHIFT) |
  385. (r300_translate_stencil_op(state->stencil[0].fail_op) <<
  386. R300_S_FRONT_SFAIL_OP_SHIFT) |
  387. (r300_translate_stencil_op(state->stencil[0].zpass_op) <<
  388. R300_S_FRONT_ZPASS_OP_SHIFT) |
  389. (r300_translate_stencil_op(state->stencil[0].zfail_op) <<
  390. R300_S_FRONT_ZFAIL_OP_SHIFT);
  391. dsa->stencil_ref_mask =
  392. (state->stencil[0].valuemask << R300_STENCILMASK_SHIFT) |
  393. (state->stencil[0].writemask << R300_STENCILWRITEMASK_SHIFT);
  394. if (state->stencil[1].enabled) {
  395. dsa->z_buffer_control |= R300_STENCIL_FRONT_BACK;
  396. dsa->z_stencil_control |=
  397. (r300_translate_depth_stencil_function(state->stencil[1].func) <<
  398. R300_S_BACK_FUNC_SHIFT) |
  399. (r300_translate_stencil_op(state->stencil[1].fail_op) <<
  400. R300_S_BACK_SFAIL_OP_SHIFT) |
  401. (r300_translate_stencil_op(state->stencil[1].zpass_op) <<
  402. R300_S_BACK_ZPASS_OP_SHIFT) |
  403. (r300_translate_stencil_op(state->stencil[1].zfail_op) <<
  404. R300_S_BACK_ZFAIL_OP_SHIFT);
  405. if (caps->is_r500)
  406. {
  407. dsa->z_buffer_control |= R500_STENCIL_REFMASK_FRONT_BACK;
  408. dsa->stencil_ref_bf =
  409. (state->stencil[1].valuemask <<
  410. R300_STENCILMASK_SHIFT) |
  411. (state->stencil[1].writemask <<
  412. R300_STENCILWRITEMASK_SHIFT);
  413. }
  414. }
  415. }
  416. /* Alpha test setup. */
  417. if (state->alpha.enabled) {
  418. dsa->alpha_function =
  419. r300_translate_alpha_function(state->alpha.func) |
  420. R300_FG_ALPHA_FUNC_ENABLE;
  421. /* We could use 10bit alpha ref but who needs that? */
  422. dsa->alpha_function |= float_to_ubyte(state->alpha.ref_value);
  423. if (caps->is_r500)
  424. dsa->alpha_function |= R500_FG_ALPHA_FUNC_8BIT;
  425. }
  426. return (void*)dsa;
  427. }
  428. /* Bind DSA state. */
  429. static void r300_bind_dsa_state(struct pipe_context* pipe,
  430. void* state)
  431. {
  432. struct r300_context* r300 = r300_context(pipe);
  433. UPDATE_STATE(state, r300->dsa_state);
  434. }
  435. /* Free DSA state. */
  436. static void r300_delete_dsa_state(struct pipe_context* pipe,
  437. void* state)
  438. {
  439. FREE(state);
  440. }
  441. static void r300_set_stencil_ref(struct pipe_context* pipe,
  442. const struct pipe_stencil_ref* sr)
  443. {
  444. struct r300_context* r300 = r300_context(pipe);
  445. r300->stencil_ref = *sr;
  446. r300->dsa_state.dirty = TRUE;
  447. }
  448. /* This switcheroo is needed just because of goddamned MACRO_SWITCH. */
  449. static void r300_fb_update_tiling_flags(struct r300_context *r300,
  450. const struct pipe_framebuffer_state *old_state,
  451. const struct pipe_framebuffer_state *new_state)
  452. {
  453. struct r300_texture *tex;
  454. unsigned i, j, level;
  455. /* Reset tiling flags for old surfaces to default values. */
  456. for (i = 0; i < old_state->nr_cbufs; i++) {
  457. for (j = 0; j < new_state->nr_cbufs; j++) {
  458. if (old_state->cbufs[i]->texture == new_state->cbufs[j]->texture) {
  459. break;
  460. }
  461. }
  462. /* If not binding the surface again... */
  463. if (j != new_state->nr_cbufs) {
  464. continue;
  465. }
  466. tex = (struct r300_texture*)old_state->cbufs[i]->texture;
  467. if (tex) {
  468. r300->rws->buffer_set_tiling(r300->rws, tex->buffer,
  469. tex->pitch[0],
  470. tex->microtile != 0,
  471. tex->macrotile != 0);
  472. }
  473. }
  474. if (old_state->zsbuf &&
  475. (!new_state->zsbuf ||
  476. old_state->zsbuf->texture != new_state->zsbuf->texture)) {
  477. tex = (struct r300_texture*)old_state->zsbuf->texture;
  478. if (tex) {
  479. r300->rws->buffer_set_tiling(r300->rws, tex->buffer,
  480. tex->pitch[0],
  481. tex->microtile != 0,
  482. tex->macrotile != 0);
  483. }
  484. }
  485. /* Set tiling flags for new surfaces. */
  486. for (i = 0; i < new_state->nr_cbufs; i++) {
  487. tex = (struct r300_texture*)new_state->cbufs[i]->texture;
  488. level = new_state->cbufs[i]->level;
  489. r300->rws->buffer_set_tiling(r300->rws, tex->buffer,
  490. tex->pitch[level],
  491. tex->microtile != 0,
  492. tex->mip_macrotile[level] != 0);
  493. }
  494. if (new_state->zsbuf) {
  495. tex = (struct r300_texture*)new_state->zsbuf->texture;
  496. level = new_state->zsbuf->level;
  497. r300->rws->buffer_set_tiling(r300->rws, tex->buffer,
  498. tex->pitch[level],
  499. tex->microtile != 0,
  500. tex->mip_macrotile[level] != 0);
  501. }
  502. }
  503. static void
  504. r300_set_framebuffer_state(struct pipe_context* pipe,
  505. const struct pipe_framebuffer_state* state)
  506. {
  507. struct r300_context* r300 = r300_context(pipe);
  508. struct r300_screen* r300screen = r300_screen(pipe->screen);
  509. struct pipe_framebuffer_state *old_state = r300->fb_state.state;
  510. unsigned max_width, max_height;
  511. uint32_t zbuffer_bpp = 0;
  512. if (state->nr_cbufs > 4) {
  513. debug_printf("r300: Implementation error: Too many MRTs in %s, "
  514. "refusing to bind framebuffer state!\n", __FUNCTION__);
  515. return;
  516. }
  517. if (r300screen->caps->is_r500) {
  518. max_width = max_height = 4096;
  519. } else if (r300screen->caps->is_r400) {
  520. max_width = max_height = 4021;
  521. } else {
  522. max_width = max_height = 2560;
  523. }
  524. if (state->width > max_width || state->height > max_height) {
  525. debug_printf("r300: Implementation error: Render targets are too "
  526. "big in %s, refusing to bind framebuffer state!\n", __FUNCTION__);
  527. return;
  528. }
  529. if (r300->draw) {
  530. draw_flush(r300->draw);
  531. }
  532. r300->fb_state.dirty = TRUE;
  533. /* If nr_cbufs is changed from zero to non-zero or vice versa... */
  534. if (!!old_state->nr_cbufs != !!state->nr_cbufs) {
  535. r300->blend_state.dirty = TRUE;
  536. }
  537. /* If zsbuf is set from NULL to non-NULL or vice versa.. */
  538. if (!!old_state->zsbuf != !!state->zsbuf) {
  539. r300->dsa_state.dirty = TRUE;
  540. }
  541. if (!r300->scissor_enabled) {
  542. r300->scissor_state.dirty = TRUE;
  543. }
  544. r300_fb_update_tiling_flags(r300, r300->fb_state.state, state);
  545. memcpy(r300->fb_state.state, state, sizeof(struct pipe_framebuffer_state));
  546. r300->fb_state.size = (10 * state->nr_cbufs) + (2 * (4 - state->nr_cbufs)) +
  547. (state->zsbuf ? 10 : 0) + 8;
  548. /* Polygon offset depends on the zbuffer bit depth. */
  549. if (state->zsbuf && r300->polygon_offset_enabled) {
  550. switch (util_format_get_blocksize(state->zsbuf->texture->format)) {
  551. case 2:
  552. zbuffer_bpp = 16;
  553. break;
  554. case 4:
  555. zbuffer_bpp = 24;
  556. break;
  557. }
  558. if (r300->zbuffer_bpp != zbuffer_bpp) {
  559. r300->zbuffer_bpp = zbuffer_bpp;
  560. r300->rs_state.dirty = TRUE;
  561. }
  562. }
  563. }
  564. /* Create fragment shader state. */
  565. static void* r300_create_fs_state(struct pipe_context* pipe,
  566. const struct pipe_shader_state* shader)
  567. {
  568. struct r300_fragment_shader* fs = NULL;
  569. fs = (struct r300_fragment_shader*)CALLOC_STRUCT(r300_fragment_shader);
  570. /* Copy state directly into shader. */
  571. fs->state = *shader;
  572. fs->state.tokens = tgsi_dup_tokens(shader->tokens);
  573. tgsi_scan_shader(shader->tokens, &fs->info);
  574. r300_shader_read_fs_inputs(&fs->info, &fs->inputs);
  575. return (void*)fs;
  576. }
  577. /* Bind fragment shader state. */
  578. static void r300_bind_fs_state(struct pipe_context* pipe, void* shader)
  579. {
  580. struct r300_context* r300 = r300_context(pipe);
  581. struct r300_fragment_shader* fs = (struct r300_fragment_shader*)shader;
  582. if (fs == NULL) {
  583. r300->fs = NULL;
  584. return;
  585. }
  586. r300->fs = fs;
  587. r300_pick_fragment_shader(r300);
  588. r300->rs_block_state.dirty = TRUE; /* Will be updated before the emission. */
  589. if (r300->vs_state.state && r300_vertex_shader_setup_wpos(r300)) {
  590. r300->vap_output_state.dirty = TRUE;
  591. }
  592. r300->dirty_state |= R300_NEW_FRAGMENT_SHADER | R300_NEW_FRAGMENT_SHADER_CONSTANTS;
  593. }
  594. /* Delete fragment shader state. */
  595. static void r300_delete_fs_state(struct pipe_context* pipe, void* shader)
  596. {
  597. struct r300_fragment_shader* fs = (struct r300_fragment_shader*)shader;
  598. struct r300_fragment_shader_code *tmp, *ptr = fs->first;
  599. while (ptr) {
  600. tmp = ptr;
  601. ptr = ptr->next;
  602. rc_constants_destroy(&tmp->code.constants);
  603. FREE(tmp);
  604. }
  605. FREE((void*)fs->state.tokens);
  606. FREE(shader);
  607. }
  608. static void r300_set_polygon_stipple(struct pipe_context* pipe,
  609. const struct pipe_poly_stipple* state)
  610. {
  611. /* XXX no idea how to set this up, but not terribly important */
  612. }
  613. /* Create a new rasterizer state based on the CSO rasterizer state.
  614. *
  615. * This is a very large chunk of state, and covers most of the graphics
  616. * backend (GB), geometry assembly (GA), and setup unit (SU) blocks.
  617. *
  618. * In a not entirely unironic sidenote, this state has nearly nothing to do
  619. * with the actual block on the Radeon called the rasterizer (RS). */
  620. static void* r300_create_rs_state(struct pipe_context* pipe,
  621. const struct pipe_rasterizer_state* state)
  622. {
  623. struct r300_screen* r300screen = r300_screen(pipe->screen);
  624. struct r300_rs_state* rs = CALLOC_STRUCT(r300_rs_state);
  625. /* Copy rasterizer state for Draw. */
  626. rs->rs = *state;
  627. #ifdef PIPE_ARCH_LITTLE_ENDIAN
  628. rs->vap_control_status = R300_VC_NO_SWAP;
  629. #else
  630. rs->vap_control_status = R300_VC_32BIT_SWAP;
  631. #endif
  632. /* If no TCL engine is present, turn off the HW TCL. */
  633. if (!r300screen->caps->has_tcl) {
  634. rs->vap_control_status |= R300_VAP_TCL_BYPASS;
  635. }
  636. rs->point_size = pack_float_16_6x(state->point_size) |
  637. (pack_float_16_6x(state->point_size) << R300_POINTSIZE_X_SHIFT);
  638. rs->line_control = pack_float_16_6x(state->line_width) |
  639. R300_GA_LINE_CNTL_END_TYPE_COMP;
  640. /* Enable polygon mode */
  641. if (state->fill_cw != PIPE_POLYGON_MODE_FILL ||
  642. state->fill_ccw != PIPE_POLYGON_MODE_FILL) {
  643. rs->polygon_mode = R300_GA_POLY_MODE_DUAL;
  644. }
  645. /* Radeons don't think in "CW/CCW", they think in "front/back". */
  646. if (state->front_winding == PIPE_WINDING_CW) {
  647. rs->cull_mode = R300_FRONT_FACE_CW;
  648. /* Polygon offset */
  649. if (state->offset_cw) {
  650. rs->polygon_offset_enable |= R300_FRONT_ENABLE;
  651. }
  652. if (state->offset_ccw) {
  653. rs->polygon_offset_enable |= R300_BACK_ENABLE;
  654. }
  655. /* Polygon mode */
  656. if (rs->polygon_mode) {
  657. rs->polygon_mode |=
  658. r300_translate_polygon_mode_front(state->fill_cw);
  659. rs->polygon_mode |=
  660. r300_translate_polygon_mode_back(state->fill_ccw);
  661. }
  662. } else {
  663. rs->cull_mode = R300_FRONT_FACE_CCW;
  664. /* Polygon offset */
  665. if (state->offset_ccw) {
  666. rs->polygon_offset_enable |= R300_FRONT_ENABLE;
  667. }
  668. if (state->offset_cw) {
  669. rs->polygon_offset_enable |= R300_BACK_ENABLE;
  670. }
  671. /* Polygon mode */
  672. if (rs->polygon_mode) {
  673. rs->polygon_mode |=
  674. r300_translate_polygon_mode_front(state->fill_ccw);
  675. rs->polygon_mode |=
  676. r300_translate_polygon_mode_back(state->fill_cw);
  677. }
  678. }
  679. if (state->front_winding & state->cull_mode) {
  680. rs->cull_mode |= R300_CULL_FRONT;
  681. }
  682. if (~(state->front_winding) & state->cull_mode) {
  683. rs->cull_mode |= R300_CULL_BACK;
  684. }
  685. if (rs->polygon_offset_enable) {
  686. rs->depth_offset = state->offset_units;
  687. rs->depth_scale = state->offset_scale;
  688. }
  689. if (state->line_stipple_enable) {
  690. rs->line_stipple_config =
  691. R300_GA_LINE_STIPPLE_CONFIG_LINE_RESET_LINE |
  692. (fui((float)state->line_stipple_factor) &
  693. R300_GA_LINE_STIPPLE_CONFIG_STIPPLE_SCALE_MASK);
  694. /* XXX this might need to be scaled up */
  695. rs->line_stipple_value = state->line_stipple_pattern;
  696. }
  697. if (state->flatshade) {
  698. rs->color_control = R300_SHADE_MODEL_FLAT;
  699. } else {
  700. rs->color_control = R300_SHADE_MODEL_SMOOTH;
  701. }
  702. return (void*)rs;
  703. }
  704. /* Bind rasterizer state. */
  705. static void r300_bind_rs_state(struct pipe_context* pipe, void* state)
  706. {
  707. struct r300_context* r300 = r300_context(pipe);
  708. struct r300_rs_state* rs = (struct r300_rs_state*)state;
  709. boolean scissor_was_enabled = r300->scissor_enabled;
  710. if (r300->draw) {
  711. draw_flush(r300->draw);
  712. draw_set_rasterizer_state(r300->draw, &rs->rs);
  713. }
  714. if (rs) {
  715. r300->polygon_offset_enabled = rs->rs.offset_cw || rs->rs.offset_ccw;
  716. r300->scissor_enabled = rs->rs.scissor;
  717. } else {
  718. r300->polygon_offset_enabled = FALSE;
  719. r300->scissor_enabled = FALSE;
  720. }
  721. UPDATE_STATE(state, r300->rs_state);
  722. r300->rs_state.size = 17 + (r300->polygon_offset_enabled ? 5 : 0);
  723. if (scissor_was_enabled != r300->scissor_enabled) {
  724. r300->scissor_state.dirty = TRUE;
  725. }
  726. }
  727. /* Free rasterizer state. */
  728. static void r300_delete_rs_state(struct pipe_context* pipe, void* state)
  729. {
  730. FREE(state);
  731. }
  732. static void*
  733. r300_create_sampler_state(struct pipe_context* pipe,
  734. const struct pipe_sampler_state* state)
  735. {
  736. struct r300_context* r300 = r300_context(pipe);
  737. struct r300_sampler_state* sampler = CALLOC_STRUCT(r300_sampler_state);
  738. boolean is_r500 = r300_screen(pipe->screen)->caps->is_r500;
  739. int lod_bias;
  740. union util_color uc;
  741. sampler->state = *state;
  742. sampler->filter0 |=
  743. (r300_translate_wrap(state->wrap_s) << R300_TX_WRAP_S_SHIFT) |
  744. (r300_translate_wrap(state->wrap_t) << R300_TX_WRAP_T_SHIFT) |
  745. (r300_translate_wrap(state->wrap_r) << R300_TX_WRAP_R_SHIFT);
  746. sampler->filter0 |= r300_translate_tex_filters(state->min_img_filter,
  747. state->mag_img_filter,
  748. state->min_mip_filter,
  749. state->max_anisotropy > 0);
  750. sampler->filter0 |= r300_anisotropy(state->max_anisotropy);
  751. /* Unfortunately, r300-r500 don't support floating-point mipmap lods. */
  752. /* We must pass these to the merge function to clamp them properly. */
  753. sampler->min_lod = MAX2((unsigned)state->min_lod, 0);
  754. sampler->max_lod = MAX2((unsigned)ceilf(state->max_lod), 0);
  755. lod_bias = CLAMP((int)(state->lod_bias * 32), -(1 << 9), (1 << 9) - 1);
  756. sampler->filter1 |= lod_bias << R300_LOD_BIAS_SHIFT;
  757. /* This is very high quality anisotropic filtering for R5xx.
  758. * It's good for benchmarking the performance of texturing but
  759. * in practice we don't want to slow down the driver because it's
  760. * a pretty good performance killer. Feel free to play with it. */
  761. if (DBG_ON(r300, DBG_ANISOHQ) && is_r500) {
  762. sampler->filter1 |= r500_anisotropy(state->max_anisotropy);
  763. }
  764. util_pack_color(state->border_color, PIPE_FORMAT_B8G8R8A8_UNORM, &uc);
  765. sampler->border_color = uc.ui;
  766. /* R500-specific fixups and optimizations */
  767. if (r300_screen(r300->context.screen)->caps->is_r500) {
  768. sampler->filter1 |= R500_BORDER_FIX;
  769. }
  770. return (void*)sampler;
  771. }
  772. static void r300_bind_sampler_states(struct pipe_context* pipe,
  773. unsigned count,
  774. void** states)
  775. {
  776. struct r300_context* r300 = r300_context(pipe);
  777. struct r300_textures_state* state =
  778. (struct r300_textures_state*)r300->textures_state.state;
  779. if (count > 8) {
  780. return;
  781. }
  782. memcpy(state->sampler_states, states, sizeof(void*) * count);
  783. state->sampler_count = count;
  784. r300->textures_state.dirty = TRUE;
  785. /* Pick a fragment shader based on the texture compare state. */
  786. if (r300->fs && count) {
  787. if (r300_pick_fragment_shader(r300)) {
  788. r300->dirty_state |= R300_NEW_FRAGMENT_SHADER |
  789. R300_NEW_FRAGMENT_SHADER_CONSTANTS;
  790. }
  791. }
  792. }
  793. static void r300_lacks_vertex_textures(struct pipe_context* pipe,
  794. unsigned count,
  795. void** states)
  796. {
  797. }
  798. static void r300_delete_sampler_state(struct pipe_context* pipe, void* state)
  799. {
  800. FREE(state);
  801. }
  802. static void r300_set_fragment_sampler_views(struct pipe_context* pipe,
  803. unsigned count,
  804. struct pipe_sampler_view** views)
  805. {
  806. struct r300_context* r300 = r300_context(pipe);
  807. struct r300_textures_state* state =
  808. (struct r300_textures_state*)r300->textures_state.state;
  809. unsigned i;
  810. boolean is_r500 = r300_screen(r300->context.screen)->caps->is_r500;
  811. boolean dirty_tex = FALSE;
  812. /* XXX magic num */
  813. if (count > 8) {
  814. return;
  815. }
  816. for (i = 0; i < count; i++) {
  817. if (state->fragment_sampler_views[i] != views[i]) {
  818. struct r300_texture *texture;
  819. pipe_sampler_view_reference(&state->fragment_sampler_views[i],
  820. views[i]);
  821. dirty_tex = TRUE;
  822. texture = (struct r300_texture *)views[i]->texture;
  823. /* R300-specific - set the texrect factor in the fragment shader */
  824. if (!is_r500 && texture->is_npot) {
  825. /* XXX It would be nice to re-emit just 1 constant,
  826. * XXX not all of them */
  827. r300->dirty_state |= R300_NEW_FRAGMENT_SHADER_CONSTANTS;
  828. }
  829. }
  830. }
  831. for (i = count; i < 8; i++) {
  832. if (state->fragment_sampler_views[i]) {
  833. pipe_sampler_view_reference(&state->fragment_sampler_views[i],
  834. NULL);
  835. }
  836. }
  837. state->texture_count = count;
  838. r300->textures_state.dirty = TRUE;
  839. if (dirty_tex) {
  840. r300->texture_cache_inval.dirty = TRUE;
  841. }
  842. }
  843. static struct pipe_sampler_view *
  844. r300_create_sampler_view(struct pipe_context *pipe,
  845. struct pipe_texture *texture,
  846. const struct pipe_sampler_view *templ)
  847. {
  848. struct pipe_sampler_view *view = CALLOC_STRUCT(pipe_sampler_view);
  849. if (view) {
  850. *view = *templ;
  851. view->reference.count = 1;
  852. view->texture = NULL;
  853. pipe_texture_reference(&view->texture, texture);
  854. view->context = pipe;
  855. }
  856. return view;
  857. }
  858. static void
  859. r300_sampler_view_destroy(struct pipe_context *pipe,
  860. struct pipe_sampler_view *view)
  861. {
  862. pipe_texture_reference(&view->texture, NULL);
  863. FREE(view);
  864. }
  865. static void r300_set_scissor_state(struct pipe_context* pipe,
  866. const struct pipe_scissor_state* state)
  867. {
  868. struct r300_context* r300 = r300_context(pipe);
  869. memcpy(r300->scissor_state.state, state,
  870. sizeof(struct pipe_scissor_state));
  871. if (r300->scissor_enabled) {
  872. r300->scissor_state.dirty = TRUE;
  873. }
  874. }
  875. static void r300_set_viewport_state(struct pipe_context* pipe,
  876. const struct pipe_viewport_state* state)
  877. {
  878. struct r300_context* r300 = r300_context(pipe);
  879. struct r300_viewport_state* viewport =
  880. (struct r300_viewport_state*)r300->viewport_state.state;
  881. r300->viewport = *state;
  882. /* Do the transform in HW. */
  883. viewport->vte_control = R300_VTX_W0_FMT;
  884. if (state->scale[0] != 1.0f) {
  885. viewport->xscale = state->scale[0];
  886. viewport->vte_control |= R300_VPORT_X_SCALE_ENA;
  887. }
  888. if (state->scale[1] != 1.0f) {
  889. viewport->yscale = state->scale[1];
  890. viewport->vte_control |= R300_VPORT_Y_SCALE_ENA;
  891. }
  892. if (state->scale[2] != 1.0f) {
  893. viewport->zscale = state->scale[2];
  894. viewport->vte_control |= R300_VPORT_Z_SCALE_ENA;
  895. }
  896. if (state->translate[0] != 0.0f) {
  897. viewport->xoffset = state->translate[0];
  898. viewport->vte_control |= R300_VPORT_X_OFFSET_ENA;
  899. }
  900. if (state->translate[1] != 0.0f) {
  901. viewport->yoffset = state->translate[1];
  902. viewport->vte_control |= R300_VPORT_Y_OFFSET_ENA;
  903. }
  904. if (state->translate[2] != 0.0f) {
  905. viewport->zoffset = state->translate[2];
  906. viewport->vte_control |= R300_VPORT_Z_OFFSET_ENA;
  907. }
  908. r300->viewport_state.dirty = TRUE;
  909. if (r300->fs && r300->fs->inputs.wpos != ATTR_UNUSED) {
  910. r300->dirty_state |= R300_NEW_FRAGMENT_SHADER_CONSTANTS;
  911. }
  912. }
  913. static void r300_set_vertex_buffers(struct pipe_context* pipe,
  914. unsigned count,
  915. const struct pipe_vertex_buffer* buffers)
  916. {
  917. struct r300_context* r300 = r300_context(pipe);
  918. int i;
  919. unsigned max_index = (1 << 24) - 1;
  920. boolean any_user_buffer = false;
  921. if (count == r300->vertex_buffer_count &&
  922. memcmp(r300->vertex_buffer, buffers, count * sizeof(buffers[0])) == 0)
  923. return;
  924. for (i = 0; i < count; i++) {
  925. pipe_buffer_reference(&r300->vertex_buffer[i].buffer, buffers[i].buffer);
  926. if (r300_buffer_is_user_buffer(buffers[i].buffer))
  927. any_user_buffer = true;
  928. max_index = MIN2(buffers[i].max_index, max_index);
  929. }
  930. for ( ; i < r300->vertex_buffer_count; i++)
  931. pipe_buffer_reference(&r300->vertex_buffer[i].buffer, NULL);
  932. memcpy(r300->vertex_buffer, buffers,
  933. sizeof(struct pipe_vertex_buffer) * count);
  934. r300->vertex_buffer_count = count;
  935. r300->vertex_buffer_max_index = max_index;
  936. r300->any_user_vbs = any_user_buffer;
  937. if (r300->draw) {
  938. draw_flush(r300->draw);
  939. draw_set_vertex_buffers(r300->draw, count, buffers);
  940. }
  941. }
  942. static boolean r300_validate_aos(struct r300_context *r300)
  943. {
  944. struct pipe_vertex_buffer *vbuf = r300->vertex_buffer;
  945. struct pipe_vertex_element *velem = r300->velems->velem;
  946. int i;
  947. /* Check if formats and strides are aligned to the size of DWORD. */
  948. for (i = 0; i < r300->velems->count; i++) {
  949. if (vbuf[velem[i].vertex_buffer_index].stride % 4 != 0 ||
  950. util_format_get_blocksize(velem[i].src_format) % 4 != 0) {
  951. return FALSE;
  952. }
  953. }
  954. return TRUE;
  955. }
  956. static void r300_draw_emit_attrib(struct r300_context* r300,
  957. enum attrib_emit emit,
  958. enum interp_mode interp,
  959. int index)
  960. {
  961. struct r300_vertex_shader* vs = r300->vs_state.state;
  962. struct tgsi_shader_info* info = &vs->info;
  963. int output;
  964. output = draw_find_shader_output(r300->draw,
  965. info->output_semantic_name[index],
  966. info->output_semantic_index[index]);
  967. draw_emit_vertex_attr(&r300->vertex_info, emit, interp, output);
  968. }
  969. static void r300_draw_emit_all_attribs(struct r300_context* r300)
  970. {
  971. struct r300_vertex_shader* vs = r300->vs_state.state;
  972. struct r300_shader_semantics* vs_outputs = &vs->outputs;
  973. int i, gen_count;
  974. /* Position. */
  975. if (vs_outputs->pos != ATTR_UNUSED) {
  976. r300_draw_emit_attrib(r300, EMIT_4F, INTERP_PERSPECTIVE,
  977. vs_outputs->pos);
  978. } else {
  979. assert(0);
  980. }
  981. /* Point size. */
  982. if (vs_outputs->psize != ATTR_UNUSED) {
  983. r300_draw_emit_attrib(r300, EMIT_1F_PSIZE, INTERP_POS,
  984. vs_outputs->psize);
  985. }
  986. /* Colors. */
  987. for (i = 0; i < ATTR_COLOR_COUNT; i++) {
  988. if (vs_outputs->color[i] != ATTR_UNUSED) {
  989. r300_draw_emit_attrib(r300, EMIT_4F, INTERP_LINEAR,
  990. vs_outputs->color[i]);
  991. }
  992. }
  993. /* XXX Back-face colors. */
  994. /* Texture coordinates. */
  995. gen_count = 0;
  996. for (i = 0; i < ATTR_GENERIC_COUNT; i++) {
  997. if (vs_outputs->generic[i] != ATTR_UNUSED) {
  998. r300_draw_emit_attrib(r300, EMIT_4F, INTERP_PERSPECTIVE,
  999. vs_outputs->generic[i]);
  1000. gen_count++;
  1001. }
  1002. }
  1003. /* Fog coordinates. */
  1004. if (vs_outputs->fog != ATTR_UNUSED) {
  1005. r300_draw_emit_attrib(r300, EMIT_4F, INTERP_PERSPECTIVE,
  1006. vs_outputs->fog);
  1007. gen_count++;
  1008. }
  1009. /* XXX magic */
  1010. assert(gen_count <= 8);
  1011. }
  1012. /* Update the PSC tables. */
  1013. static void r300_vertex_psc(struct r300_vertex_element_state *velems)
  1014. {
  1015. struct r300_vertex_stream_state *vstream = &velems->vertex_stream;
  1016. uint16_t type, swizzle;
  1017. enum pipe_format format;
  1018. unsigned i;
  1019. assert(velems->count <= 16);
  1020. /* Vertex shaders have no semantics on their inputs,
  1021. * so PSC should just route stuff based on the vertex elements,
  1022. * and not on attrib information. */
  1023. for (i = 0; i < velems->count; i++) {
  1024. format = velems->velem[i].src_format;
  1025. type = r300_translate_vertex_data_type(format) |
  1026. (i << R300_DST_VEC_LOC_SHIFT);
  1027. swizzle = r300_translate_vertex_data_swizzle(format);
  1028. if (i & 1) {
  1029. vstream->vap_prog_stream_cntl[i >> 1] |= type << 16;
  1030. vstream->vap_prog_stream_cntl_ext[i >> 1] |= swizzle << 16;
  1031. } else {
  1032. vstream->vap_prog_stream_cntl[i >> 1] |= type;
  1033. vstream->vap_prog_stream_cntl_ext[i >> 1] |= swizzle;
  1034. }
  1035. }
  1036. /* Set the last vector in the PSC. */
  1037. if (i) {
  1038. i -= 1;
  1039. }
  1040. vstream->vap_prog_stream_cntl[i >> 1] |=
  1041. (R300_LAST_VEC << (i & 1 ? 16 : 0));
  1042. vstream->count = (i >> 1) + 1;
  1043. }
  1044. /* Update the PSC tables for SW TCL, using Draw. */
  1045. static void r300_swtcl_vertex_psc(struct r300_context *r300,
  1046. struct r300_vertex_element_state *velems)
  1047. {
  1048. struct r300_vertex_stream_state *vstream = &velems->vertex_stream;
  1049. struct r300_vertex_shader* vs = r300->vs_state.state;
  1050. struct vertex_info* vinfo = &r300->vertex_info;
  1051. uint16_t type, swizzle;
  1052. enum pipe_format format;
  1053. unsigned i, attrib_count;
  1054. int* vs_output_tab = vs->stream_loc_notcl;
  1055. /* For each Draw attribute, route it to the fragment shader according
  1056. * to the vs_output_tab. */
  1057. attrib_count = vinfo->num_attribs;
  1058. DBG(r300, DBG_DRAW, "r300: attrib count: %d\n", attrib_count);
  1059. for (i = 0; i < attrib_count; i++) {
  1060. DBG(r300, DBG_DRAW, "r300: attrib: offset %d, interp %d, size %d,"
  1061. " vs_output_tab %d\n", vinfo->attrib[i].src_index,
  1062. vinfo->attrib[i].interp_mode, vinfo->attrib[i].emit,
  1063. vs_output_tab[i]);
  1064. }
  1065. for (i = 0; i < attrib_count; i++) {
  1066. /* Make sure we have a proper destination for our attribute. */
  1067. assert(vs_output_tab[i] != -1);
  1068. format = draw_translate_vinfo_format(vinfo->attrib[i].emit);
  1069. /* Obtain the type of data in this attribute. */
  1070. type = r300_translate_vertex_data_type(format) |
  1071. vs_output_tab[i] << R300_DST_VEC_LOC_SHIFT;
  1072. /* Obtain the swizzle for this attribute. Note that the default
  1073. * swizzle in the hardware is not XYZW! */
  1074. swizzle = r300_translate_vertex_data_swizzle(format);
  1075. /* Add the attribute to the PSC table. */
  1076. if (i & 1) {
  1077. vstream->vap_prog_stream_cntl[i >> 1] |= type << 16;
  1078. vstream->vap_prog_stream_cntl_ext[i >> 1] |= swizzle << 16;
  1079. } else {
  1080. vstream->vap_prog_stream_cntl[i >> 1] |= type;
  1081. vstream->vap_prog_stream_cntl_ext[i >> 1] |= swizzle;
  1082. }
  1083. }
  1084. /* Set the last vector in the PSC. */
  1085. if (i) {
  1086. i -= 1;
  1087. }
  1088. vstream->vap_prog_stream_cntl[i >> 1] |=
  1089. (R300_LAST_VEC << (i & 1 ? 16 : 0));
  1090. vstream->count = (i >> 1) + 1;
  1091. }
  1092. static void* r300_create_vertex_elements_state(struct pipe_context* pipe,
  1093. unsigned count,
  1094. const struct pipe_vertex_element* attribs)
  1095. {
  1096. struct r300_context *r300 = r300_context(pipe);
  1097. struct r300_screen* r300screen = r300_screen(pipe->screen);
  1098. struct r300_vertex_element_state *velems;
  1099. assert(count <= PIPE_MAX_ATTRIBS);
  1100. velems = CALLOC_STRUCT(r300_vertex_element_state);
  1101. if (velems != NULL) {
  1102. velems->count = count;
  1103. memcpy(velems->velem, attribs, sizeof(struct pipe_vertex_element) * count);
  1104. if (r300screen->caps->has_tcl) {
  1105. r300_vertex_psc(velems);
  1106. } else {
  1107. memset(&r300->vertex_info, 0, sizeof(struct vertex_info));
  1108. r300_draw_emit_all_attribs(r300);
  1109. draw_compute_vertex_size(&r300->vertex_info);
  1110. r300_swtcl_vertex_psc(r300, velems);
  1111. }
  1112. }
  1113. return velems;
  1114. }
  1115. static void r300_bind_vertex_elements_state(struct pipe_context *pipe,
  1116. void *state)
  1117. {
  1118. struct r300_context *r300 = r300_context(pipe);
  1119. struct r300_vertex_element_state *velems = state;
  1120. if (velems == NULL) {
  1121. return;
  1122. }
  1123. r300->velems = velems;
  1124. if (r300->draw) {
  1125. draw_flush(r300->draw);
  1126. draw_set_vertex_elements(r300->draw, velems->count, velems->velem);
  1127. }
  1128. if (!r300_validate_aos(r300)) {
  1129. /* XXX We should fallback using draw. */
  1130. assert(0);
  1131. abort();
  1132. }
  1133. UPDATE_STATE(&velems->vertex_stream, r300->vertex_stream_state);
  1134. r300->vertex_stream_state.size = (1 + velems->vertex_stream.count) * 2;
  1135. }
  1136. static void r300_delete_vertex_elements_state(struct pipe_context *pipe, void *state)
  1137. {
  1138. FREE(state);
  1139. }
  1140. static void* r300_create_vs_state(struct pipe_context* pipe,
  1141. const struct pipe_shader_state* shader)
  1142. {
  1143. struct r300_context* r300 = r300_context(pipe);
  1144. struct r300_vertex_shader* vs = CALLOC_STRUCT(r300_vertex_shader);
  1145. r300_vertex_shader_common_init(vs, shader);
  1146. if (r300_screen(pipe->screen)->caps->has_tcl) {
  1147. r300_translate_vertex_shader(r300, vs);
  1148. } else {
  1149. vs->draw_vs = draw_create_vertex_shader(r300->draw, shader);
  1150. }
  1151. return vs;
  1152. }
  1153. static void r300_bind_vs_state(struct pipe_context* pipe, void* shader)
  1154. {
  1155. struct r300_context* r300 = r300_context(pipe);
  1156. struct r300_vertex_shader* vs = (struct r300_vertex_shader*)shader;
  1157. if (vs == NULL) {
  1158. r300->vs_state.state = NULL;
  1159. return;
  1160. }
  1161. if (vs == r300->vs_state.state) {
  1162. return;
  1163. }
  1164. r300->vs_state.state = vs;
  1165. // VS output mapping for HWTCL or stream mapping for SWTCL to the RS block
  1166. if (r300->fs) {
  1167. r300_vertex_shader_setup_wpos(r300);
  1168. }
  1169. memcpy(r300->vap_output_state.state, &vs->vap_out,
  1170. sizeof(struct r300_vap_output_state));
  1171. r300->vap_output_state.dirty = TRUE;
  1172. /* The majority of the RS block bits is dependent on the vertex shader. */
  1173. r300->rs_block_state.dirty = TRUE; /* Will be updated before the emission. */
  1174. if (r300_screen(pipe->screen)->caps->has_tcl) {
  1175. r300->vs_state.dirty = TRUE;
  1176. r300->vs_state.size = vs->code.length + 9;
  1177. r300->pvs_flush.dirty = TRUE;
  1178. r300->dirty_state |= R300_NEW_VERTEX_SHADER_CONSTANTS;
  1179. } else {
  1180. draw_flush(r300->draw);
  1181. draw_bind_vertex_shader(r300->draw,
  1182. (struct draw_vertex_shader*)vs->draw_vs);
  1183. }
  1184. }
  1185. static void r300_delete_vs_state(struct pipe_context* pipe, void* shader)
  1186. {
  1187. struct r300_context* r300 = r300_context(pipe);
  1188. struct r300_vertex_shader* vs = (struct r300_vertex_shader*)shader;
  1189. if (r300_screen(pipe->screen)->caps->has_tcl) {
  1190. rc_constants_destroy(&vs->code.constants);
  1191. } else {
  1192. draw_delete_vertex_shader(r300->draw,
  1193. (struct draw_vertex_shader*)vs->draw_vs);
  1194. }
  1195. FREE((void*)vs->state.tokens);
  1196. FREE(shader);
  1197. }
  1198. static void r300_set_constant_buffer(struct pipe_context *pipe,
  1199. uint shader, uint index,
  1200. struct pipe_buffer *buf)
  1201. {
  1202. struct r300_context* r300 = r300_context(pipe);
  1203. struct r300_screen *r300screen = r300_screen(pipe->screen);
  1204. void *mapped;
  1205. int max_size = 0;
  1206. if (buf == NULL || buf->size == 0 ||
  1207. (mapped = pipe_buffer_map(pipe->screen, buf, PIPE_BUFFER_USAGE_CPU_READ)) == NULL)
  1208. {
  1209. r300->shader_constants[shader].count = 0;
  1210. return;
  1211. }
  1212. assert((buf->size % 4 * sizeof(float)) == 0);
  1213. /* Check the size of the constant buffer. */
  1214. switch (shader) {
  1215. case PIPE_SHADER_VERTEX:
  1216. max_size = 256;
  1217. break;
  1218. case PIPE_SHADER_FRAGMENT:
  1219. if (r300screen->caps->is_r500) {
  1220. max_size = 256;
  1221. /* XXX Implement emission of r400's extended constant buffer. */
  1222. /*} else if (r300screen->caps->is_r400) {
  1223. max_size = 64;*/
  1224. } else {
  1225. max_size = 32;
  1226. }
  1227. break;
  1228. default:
  1229. assert(0);
  1230. }
  1231. /* XXX Subtract immediates and RC_STATE_* variables. */
  1232. if (buf->size > (sizeof(float) * 4 * max_size)) {
  1233. debug_printf("r300: Max size of the constant buffer is "
  1234. "%i*4 floats.\n", max_size);
  1235. abort();
  1236. }
  1237. memcpy(r300->shader_constants[shader].constants, mapped, buf->size);
  1238. r300->shader_constants[shader].count = buf->size / (4 * sizeof(float));
  1239. pipe_buffer_unmap(pipe->screen, buf);
  1240. if (shader == PIPE_SHADER_VERTEX) {
  1241. if (r300screen->caps->has_tcl) {
  1242. r300->dirty_state |= R300_NEW_VERTEX_SHADER_CONSTANTS;
  1243. r300->pvs_flush.dirty = TRUE;
  1244. }
  1245. }
  1246. else if (shader == PIPE_SHADER_FRAGMENT)
  1247. r300->dirty_state |= R300_NEW_FRAGMENT_SHADER_CONSTANTS;
  1248. }
  1249. void r300_init_state_functions(struct r300_context* r300)
  1250. {
  1251. r300->context.create_blend_state = r300_create_blend_state;
  1252. r300->context.bind_blend_state = r300_bind_blend_state;
  1253. r300->context.delete_blend_state = r300_delete_blend_state;
  1254. r300->context.set_blend_color = r300_set_blend_color;
  1255. r300->context.set_clip_state = r300_set_clip_state;
  1256. r300->context.set_constant_buffer = r300_set_constant_buffer;
  1257. r300->context.create_depth_stencil_alpha_state = r300_create_dsa_state;
  1258. r300->context.bind_depth_stencil_alpha_state = r300_bind_dsa_state;
  1259. r300->context.delete_depth_stencil_alpha_state = r300_delete_dsa_state;
  1260. r300->context.set_stencil_ref = r300_set_stencil_ref;
  1261. r300->context.set_framebuffer_state = r300_set_framebuffer_state;
  1262. r300->context.create_fs_state = r300_create_fs_state;
  1263. r300->context.bind_fs_state = r300_bind_fs_state;
  1264. r300->context.delete_fs_state = r300_delete_fs_state;
  1265. r300->context.set_polygon_stipple = r300_set_polygon_stipple;
  1266. r300->context.create_rasterizer_state = r300_create_rs_state;
  1267. r300->context.bind_rasterizer_state = r300_bind_rs_state;
  1268. r300->context.delete_rasterizer_state = r300_delete_rs_state;
  1269. r300->context.create_sampler_state = r300_create_sampler_state;
  1270. r300->context.bind_fragment_sampler_states = r300_bind_sampler_states;
  1271. r300->context.bind_vertex_sampler_states = r300_lacks_vertex_textures;
  1272. r300->context.delete_sampler_state = r300_delete_sampler_state;
  1273. r300->context.set_fragment_sampler_views = r300_set_fragment_sampler_views;
  1274. r300->context.create_sampler_view = r300_create_sampler_view;
  1275. r300->context.sampler_view_destroy = r300_sampler_view_destroy;
  1276. r300->context.set_scissor_state = r300_set_scissor_state;
  1277. r300->context.set_viewport_state = r300_set_viewport_state;
  1278. r300->context.set_vertex_buffers = r300_set_vertex_buffers;
  1279. r300->context.create_vertex_elements_state = r300_create_vertex_elements_state;
  1280. r300->context.bind_vertex_elements_state = r300_bind_vertex_elements_state;
  1281. r300->context.delete_vertex_elements_state = r300_delete_vertex_elements_state;
  1282. r300->context.create_vs_state = r300_create_vs_state;
  1283. r300->context.bind_vs_state = r300_bind_vs_state;
  1284. r300->context.delete_vs_state = r300_delete_vs_state;
  1285. }