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.

i915_state.c 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. /**************************************************************************
  2. *
  3. * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  21. * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
  22. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  23. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  24. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. /* Authors: Keith Whitwell <keith@tungstengraphics.com>
  28. */
  29. #include "draw/draw_context.h"
  30. #include "pipe/internal/p_winsys_screen.h"
  31. #include "pipe/p_inlines.h"
  32. #include "util/u_math.h"
  33. #include "util/u_memory.h"
  34. #include "tgsi/tgsi_parse.h"
  35. #include "i915_context.h"
  36. #include "i915_reg.h"
  37. #include "i915_state.h"
  38. #include "i915_state_inlines.h"
  39. #include "i915_fpc.h"
  40. /* The i915 (and related graphics cores) do not support GL_CLAMP. The
  41. * Intel drivers for "other operating systems" implement GL_CLAMP as
  42. * GL_CLAMP_TO_EDGE, so the same is done here.
  43. */
  44. static unsigned
  45. translate_wrap_mode(unsigned wrap)
  46. {
  47. switch (wrap) {
  48. case PIPE_TEX_WRAP_REPEAT:
  49. return TEXCOORDMODE_WRAP;
  50. case PIPE_TEX_WRAP_CLAMP:
  51. return TEXCOORDMODE_CLAMP_EDGE; /* not quite correct */
  52. case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
  53. return TEXCOORDMODE_CLAMP_EDGE;
  54. case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
  55. return TEXCOORDMODE_CLAMP_BORDER;
  56. // case PIPE_TEX_WRAP_MIRRORED_REPEAT:
  57. // return TEXCOORDMODE_MIRROR;
  58. default:
  59. return TEXCOORDMODE_WRAP;
  60. }
  61. }
  62. static unsigned translate_img_filter( unsigned filter )
  63. {
  64. switch (filter) {
  65. case PIPE_TEX_FILTER_NEAREST:
  66. return FILTER_NEAREST;
  67. case PIPE_TEX_FILTER_LINEAR:
  68. return FILTER_LINEAR;
  69. case PIPE_TEX_FILTER_ANISO:
  70. return FILTER_ANISOTROPIC;
  71. default:
  72. assert(0);
  73. return FILTER_NEAREST;
  74. }
  75. }
  76. static unsigned translate_mip_filter( unsigned filter )
  77. {
  78. switch (filter) {
  79. case PIPE_TEX_MIPFILTER_NONE:
  80. return MIPFILTER_NONE;
  81. case PIPE_TEX_MIPFILTER_NEAREST:
  82. return MIPFILTER_NEAREST;
  83. case PIPE_TEX_MIPFILTER_LINEAR:
  84. return MIPFILTER_LINEAR;
  85. default:
  86. assert(0);
  87. return MIPFILTER_NONE;
  88. }
  89. }
  90. /* None of this state is actually used for anything yet.
  91. */
  92. static void *
  93. i915_create_blend_state(struct pipe_context *pipe,
  94. const struct pipe_blend_state *blend)
  95. {
  96. struct i915_blend_state *cso_data = CALLOC_STRUCT( i915_blend_state );
  97. {
  98. unsigned eqRGB = blend->rgb_func;
  99. unsigned srcRGB = blend->rgb_src_factor;
  100. unsigned dstRGB = blend->rgb_dst_factor;
  101. unsigned eqA = blend->alpha_func;
  102. unsigned srcA = blend->alpha_src_factor;
  103. unsigned dstA = blend->alpha_dst_factor;
  104. /* Special handling for MIN/MAX filter modes handled at
  105. * state_tracker level.
  106. */
  107. if (srcA != srcRGB ||
  108. dstA != dstRGB ||
  109. eqA != eqRGB) {
  110. cso_data->iab = (_3DSTATE_INDEPENDENT_ALPHA_BLEND_CMD |
  111. IAB_MODIFY_ENABLE |
  112. IAB_ENABLE |
  113. IAB_MODIFY_FUNC |
  114. IAB_MODIFY_SRC_FACTOR |
  115. IAB_MODIFY_DST_FACTOR |
  116. SRC_ABLND_FACT(i915_translate_blend_factor(srcA)) |
  117. DST_ABLND_FACT(i915_translate_blend_factor(dstA)) |
  118. (i915_translate_blend_func(eqA) << IAB_FUNC_SHIFT));
  119. }
  120. else {
  121. cso_data->iab = (_3DSTATE_INDEPENDENT_ALPHA_BLEND_CMD |
  122. IAB_MODIFY_ENABLE |
  123. 0);
  124. }
  125. }
  126. cso_data->modes4 |= (_3DSTATE_MODES_4_CMD |
  127. ENABLE_LOGIC_OP_FUNC |
  128. LOGIC_OP_FUNC(i915_translate_logic_op(blend->logicop_func)));
  129. if (blend->logicop_enable)
  130. cso_data->LIS5 |= S5_LOGICOP_ENABLE;
  131. if (blend->dither)
  132. cso_data->LIS5 |= S5_COLOR_DITHER_ENABLE;
  133. if ((blend->colormask & PIPE_MASK_R) == 0)
  134. cso_data->LIS5 |= S5_WRITEDISABLE_RED;
  135. if ((blend->colormask & PIPE_MASK_G) == 0)
  136. cso_data->LIS5 |= S5_WRITEDISABLE_GREEN;
  137. if ((blend->colormask & PIPE_MASK_B) == 0)
  138. cso_data->LIS5 |= S5_WRITEDISABLE_BLUE;
  139. if ((blend->colormask & PIPE_MASK_A) == 0)
  140. cso_data->LIS5 |= S5_WRITEDISABLE_ALPHA;
  141. if (blend->blend_enable) {
  142. unsigned funcRGB = blend->rgb_func;
  143. unsigned srcRGB = blend->rgb_src_factor;
  144. unsigned dstRGB = blend->rgb_dst_factor;
  145. cso_data->LIS6 |= (S6_CBUF_BLEND_ENABLE |
  146. SRC_BLND_FACT(i915_translate_blend_factor(srcRGB)) |
  147. DST_BLND_FACT(i915_translate_blend_factor(dstRGB)) |
  148. (i915_translate_blend_func(funcRGB) << S6_CBUF_BLEND_FUNC_SHIFT));
  149. }
  150. return cso_data;
  151. }
  152. static void i915_bind_blend_state(struct pipe_context *pipe,
  153. void *blend)
  154. {
  155. struct i915_context *i915 = i915_context(pipe);
  156. draw_flush(i915->draw);
  157. i915->blend = (struct i915_blend_state*)blend;
  158. i915->dirty |= I915_NEW_BLEND;
  159. }
  160. static void i915_delete_blend_state(struct pipe_context *pipe, void *blend)
  161. {
  162. FREE(blend);
  163. }
  164. static void i915_set_blend_color( struct pipe_context *pipe,
  165. const struct pipe_blend_color *blend_color )
  166. {
  167. struct i915_context *i915 = i915_context(pipe);
  168. draw_flush(i915->draw);
  169. i915->blend_color = *blend_color;
  170. i915->dirty |= I915_NEW_BLEND;
  171. }
  172. static void *
  173. i915_create_sampler_state(struct pipe_context *pipe,
  174. const struct pipe_sampler_state *sampler)
  175. {
  176. struct i915_sampler_state *cso = CALLOC_STRUCT( i915_sampler_state );
  177. const unsigned ws = sampler->wrap_s;
  178. const unsigned wt = sampler->wrap_t;
  179. const unsigned wr = sampler->wrap_r;
  180. unsigned minFilt, magFilt;
  181. unsigned mipFilt;
  182. cso->templ = sampler;
  183. mipFilt = translate_mip_filter(sampler->min_mip_filter);
  184. minFilt = translate_img_filter( sampler->min_img_filter );
  185. magFilt = translate_img_filter( sampler->mag_img_filter );
  186. if (sampler->max_anisotropy > 2.0) {
  187. cso->state[0] |= SS2_MAX_ANISO_4;
  188. }
  189. {
  190. int b = (int) (sampler->lod_bias * 16.0);
  191. b = CLAMP(b, -256, 255);
  192. cso->state[0] |= ((b << SS2_LOD_BIAS_SHIFT) & SS2_LOD_BIAS_MASK);
  193. }
  194. /* Shadow:
  195. */
  196. if (sampler->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE)
  197. {
  198. cso->state[0] |= (SS2_SHADOW_ENABLE |
  199. i915_translate_compare_func(sampler->compare_func));
  200. minFilt = FILTER_4X4_FLAT;
  201. magFilt = FILTER_4X4_FLAT;
  202. }
  203. cso->state[0] |= ((minFilt << SS2_MIN_FILTER_SHIFT) |
  204. (mipFilt << SS2_MIP_FILTER_SHIFT) |
  205. (magFilt << SS2_MAG_FILTER_SHIFT));
  206. cso->state[1] |=
  207. ((translate_wrap_mode(ws) << SS3_TCX_ADDR_MODE_SHIFT) |
  208. (translate_wrap_mode(wt) << SS3_TCY_ADDR_MODE_SHIFT) |
  209. (translate_wrap_mode(wr) << SS3_TCZ_ADDR_MODE_SHIFT));
  210. if (sampler->normalized_coords)
  211. cso->state[1] |= SS3_NORMALIZED_COORDS;
  212. {
  213. int minlod = (int) (16.0 * sampler->min_lod);
  214. int maxlod = (int) (16.0 * sampler->max_lod);
  215. minlod = CLAMP(minlod, 0, 16 * 11);
  216. maxlod = CLAMP(maxlod, 0, 16 * 11);
  217. if (minlod > maxlod)
  218. maxlod = minlod;
  219. cso->minlod = minlod;
  220. cso->maxlod = maxlod;
  221. }
  222. {
  223. ubyte r = float_to_ubyte(sampler->border_color[0]);
  224. ubyte g = float_to_ubyte(sampler->border_color[1]);
  225. ubyte b = float_to_ubyte(sampler->border_color[2]);
  226. ubyte a = float_to_ubyte(sampler->border_color[3]);
  227. cso->state[2] = I915PACKCOLOR8888(r, g, b, a);
  228. }
  229. return cso;
  230. }
  231. static void i915_bind_sampler_states(struct pipe_context *pipe,
  232. unsigned num, void **sampler)
  233. {
  234. struct i915_context *i915 = i915_context(pipe);
  235. unsigned i;
  236. assert(num <= PIPE_MAX_SAMPLERS);
  237. /* Check for no-op */
  238. if (num == i915->num_samplers &&
  239. !memcmp(i915->sampler, sampler, num * sizeof(void *)))
  240. return;
  241. draw_flush(i915->draw);
  242. for (i = 0; i < num; ++i)
  243. i915->sampler[i] = sampler[i];
  244. for (i = num; i < PIPE_MAX_SAMPLERS; ++i)
  245. i915->sampler[i] = NULL;
  246. i915->num_samplers = num;
  247. i915->dirty |= I915_NEW_SAMPLER;
  248. }
  249. static void i915_delete_sampler_state(struct pipe_context *pipe,
  250. void *sampler)
  251. {
  252. FREE(sampler);
  253. }
  254. /** XXX move someday? Or consolidate all these simple state setters
  255. * into one file.
  256. */
  257. static void *
  258. i915_create_depth_stencil_state(struct pipe_context *pipe,
  259. const struct pipe_depth_stencil_alpha_state *depth_stencil)
  260. {
  261. struct i915_depth_stencil_state *cso = CALLOC_STRUCT( i915_depth_stencil_state );
  262. {
  263. int testmask = depth_stencil->stencil[0].valuemask & 0xff;
  264. int writemask = depth_stencil->stencil[0].writemask & 0xff;
  265. cso->stencil_modes4 |= (_3DSTATE_MODES_4_CMD |
  266. ENABLE_STENCIL_TEST_MASK |
  267. STENCIL_TEST_MASK(testmask) |
  268. ENABLE_STENCIL_WRITE_MASK |
  269. STENCIL_WRITE_MASK(writemask));
  270. }
  271. if (depth_stencil->stencil[0].enabled) {
  272. int test = i915_translate_compare_func(depth_stencil->stencil[0].func);
  273. int fop = i915_translate_stencil_op(depth_stencil->stencil[0].fail_op);
  274. int dfop = i915_translate_stencil_op(depth_stencil->stencil[0].zfail_op);
  275. int dpop = i915_translate_stencil_op(depth_stencil->stencil[0].zpass_op);
  276. int ref = depth_stencil->stencil[0].ref_value & 0xff;
  277. cso->stencil_LIS5 |= (S5_STENCIL_TEST_ENABLE |
  278. S5_STENCIL_WRITE_ENABLE |
  279. (ref << S5_STENCIL_REF_SHIFT) |
  280. (test << S5_STENCIL_TEST_FUNC_SHIFT) |
  281. (fop << S5_STENCIL_FAIL_SHIFT) |
  282. (dfop << S5_STENCIL_PASS_Z_FAIL_SHIFT) |
  283. (dpop << S5_STENCIL_PASS_Z_PASS_SHIFT));
  284. }
  285. if (depth_stencil->stencil[1].enabled) {
  286. int test = i915_translate_compare_func(depth_stencil->stencil[1].func);
  287. int fop = i915_translate_stencil_op(depth_stencil->stencil[1].fail_op);
  288. int dfop = i915_translate_stencil_op(depth_stencil->stencil[1].zfail_op);
  289. int dpop = i915_translate_stencil_op(depth_stencil->stencil[1].zpass_op);
  290. int ref = depth_stencil->stencil[1].ref_value & 0xff;
  291. int tmask = depth_stencil->stencil[1].valuemask & 0xff;
  292. int wmask = depth_stencil->stencil[1].writemask & 0xff;
  293. cso->bfo[0] = (_3DSTATE_BACKFACE_STENCIL_OPS |
  294. BFO_ENABLE_STENCIL_FUNCS |
  295. BFO_ENABLE_STENCIL_TWO_SIDE |
  296. BFO_ENABLE_STENCIL_REF |
  297. BFO_STENCIL_TWO_SIDE |
  298. (ref << BFO_STENCIL_REF_SHIFT) |
  299. (test << BFO_STENCIL_TEST_SHIFT) |
  300. (fop << BFO_STENCIL_FAIL_SHIFT) |
  301. (dfop << BFO_STENCIL_PASS_Z_FAIL_SHIFT) |
  302. (dpop << BFO_STENCIL_PASS_Z_PASS_SHIFT));
  303. cso->bfo[1] = (_3DSTATE_BACKFACE_STENCIL_MASKS |
  304. BFM_ENABLE_STENCIL_TEST_MASK |
  305. BFM_ENABLE_STENCIL_WRITE_MASK |
  306. (tmask << BFM_STENCIL_TEST_MASK_SHIFT) |
  307. (wmask << BFM_STENCIL_WRITE_MASK_SHIFT));
  308. }
  309. else {
  310. /* This actually disables two-side stencil: The bit set is a
  311. * modify-enable bit to indicate we are changing the two-side
  312. * setting. Then there is a symbolic zero to show that we are
  313. * setting the flag to zero/off.
  314. */
  315. cso->bfo[0] = (_3DSTATE_BACKFACE_STENCIL_OPS |
  316. BFO_ENABLE_STENCIL_TWO_SIDE |
  317. 0);
  318. cso->bfo[1] = 0;
  319. }
  320. if (depth_stencil->depth.enabled) {
  321. int func = i915_translate_compare_func(depth_stencil->depth.func);
  322. cso->depth_LIS6 |= (S6_DEPTH_TEST_ENABLE |
  323. (func << S6_DEPTH_TEST_FUNC_SHIFT));
  324. if (depth_stencil->depth.writemask)
  325. cso->depth_LIS6 |= S6_DEPTH_WRITE_ENABLE;
  326. }
  327. if (depth_stencil->alpha.enabled) {
  328. int test = i915_translate_compare_func(depth_stencil->alpha.func);
  329. ubyte refByte = float_to_ubyte(depth_stencil->alpha.ref_value);
  330. cso->depth_LIS6 |= (S6_ALPHA_TEST_ENABLE |
  331. (test << S6_ALPHA_TEST_FUNC_SHIFT) |
  332. (((unsigned) refByte) << S6_ALPHA_REF_SHIFT));
  333. }
  334. return cso;
  335. }
  336. static void i915_bind_depth_stencil_state(struct pipe_context *pipe,
  337. void *depth_stencil)
  338. {
  339. struct i915_context *i915 = i915_context(pipe);
  340. draw_flush(i915->draw);
  341. i915->depth_stencil = (const struct i915_depth_stencil_state *)depth_stencil;
  342. i915->dirty |= I915_NEW_DEPTH_STENCIL;
  343. }
  344. static void i915_delete_depth_stencil_state(struct pipe_context *pipe,
  345. void *depth_stencil)
  346. {
  347. FREE(depth_stencil);
  348. }
  349. static void i915_set_scissor_state( struct pipe_context *pipe,
  350. const struct pipe_scissor_state *scissor )
  351. {
  352. struct i915_context *i915 = i915_context(pipe);
  353. draw_flush(i915->draw);
  354. memcpy( &i915->scissor, scissor, sizeof(*scissor) );
  355. i915->dirty |= I915_NEW_SCISSOR;
  356. }
  357. static void i915_set_polygon_stipple( struct pipe_context *pipe,
  358. const struct pipe_poly_stipple *stipple )
  359. {
  360. }
  361. static void *
  362. i915_create_fs_state(struct pipe_context *pipe,
  363. const struct pipe_shader_state *templ)
  364. {
  365. struct i915_context *i915 = i915_context(pipe);
  366. struct i915_fragment_shader *ifs = CALLOC_STRUCT(i915_fragment_shader);
  367. if (!ifs)
  368. return NULL;
  369. ifs->state.tokens = tgsi_dup_tokens(templ->tokens);
  370. tgsi_scan_shader(templ->tokens, &ifs->info);
  371. /* The shader's compiled to i915 instructions here */
  372. i915_translate_fragment_program(i915, ifs);
  373. return ifs;
  374. }
  375. static void
  376. i915_bind_fs_state(struct pipe_context *pipe, void *shader)
  377. {
  378. struct i915_context *i915 = i915_context(pipe);
  379. draw_flush(i915->draw);
  380. i915->fs = (struct i915_fragment_shader*) shader;
  381. i915->dirty |= I915_NEW_FS;
  382. }
  383. static
  384. void i915_delete_fs_state(struct pipe_context *pipe, void *shader)
  385. {
  386. struct i915_fragment_shader *ifs = (struct i915_fragment_shader *) shader;
  387. if (ifs->program)
  388. FREE(ifs->program);
  389. ifs->program_len = 0;
  390. FREE((struct tgsi_token *)ifs->state.tokens);
  391. FREE(ifs);
  392. }
  393. static void *
  394. i915_create_vs_state(struct pipe_context *pipe,
  395. const struct pipe_shader_state *templ)
  396. {
  397. struct i915_context *i915 = i915_context(pipe);
  398. /* just pass-through to draw module */
  399. return draw_create_vertex_shader(i915->draw, templ);
  400. }
  401. static void i915_bind_vs_state(struct pipe_context *pipe, void *shader)
  402. {
  403. struct i915_context *i915 = i915_context(pipe);
  404. /* just pass-through to draw module */
  405. draw_bind_vertex_shader(i915->draw, (struct draw_vertex_shader *) shader);
  406. i915->dirty |= I915_NEW_VS;
  407. }
  408. static void i915_delete_vs_state(struct pipe_context *pipe, void *shader)
  409. {
  410. struct i915_context *i915 = i915_context(pipe);
  411. /* just pass-through to draw module */
  412. draw_delete_vertex_shader(i915->draw, (struct draw_vertex_shader *) shader);
  413. }
  414. static void i915_set_constant_buffer(struct pipe_context *pipe,
  415. uint shader, uint index,
  416. const struct pipe_constant_buffer *buf)
  417. {
  418. struct i915_context *i915 = i915_context(pipe);
  419. struct pipe_screen *screen = pipe->screen;
  420. draw_flush(i915->draw);
  421. assert(shader < PIPE_SHADER_TYPES);
  422. assert(index == 0);
  423. /* Make a copy of shader constants.
  424. * During fragment program translation we may add additional
  425. * constants to the array.
  426. *
  427. * We want to consider the situation where some user constants
  428. * (ex: a material color) may change frequently but the shader program
  429. * stays the same. In that case we should only be updating the first
  430. * N constants, leaving any extras from shader translation alone.
  431. */
  432. if (buf) {
  433. void *mapped;
  434. if (buf->buffer && buf->buffer->size &&
  435. (mapped = pipe_buffer_map(screen, buf->buffer,
  436. PIPE_BUFFER_USAGE_CPU_READ))) {
  437. memcpy(i915->current.constants[shader], mapped, buf->buffer->size);
  438. pipe_buffer_unmap(screen, buf->buffer);
  439. i915->current.num_user_constants[shader]
  440. = buf->buffer->size / (4 * sizeof(float));
  441. }
  442. else {
  443. i915->current.num_user_constants[shader] = 0;
  444. }
  445. }
  446. i915->dirty |= I915_NEW_CONSTANTS;
  447. }
  448. static void i915_set_sampler_textures(struct pipe_context *pipe,
  449. unsigned num,
  450. struct pipe_texture **texture)
  451. {
  452. struct i915_context *i915 = i915_context(pipe);
  453. uint i;
  454. assert(num <= PIPE_MAX_SAMPLERS);
  455. /* Check for no-op */
  456. if (num == i915->num_textures &&
  457. !memcmp(i915->texture, texture, num * sizeof(struct pipe_texture *)))
  458. return;
  459. /* Fixes wrong texture in texobj with VBUF */
  460. draw_flush(i915->draw);
  461. for (i = 0; i < num; i++)
  462. pipe_texture_reference((struct pipe_texture **) &i915->texture[i],
  463. texture[i]);
  464. for (i = num; i < i915->num_textures; i++)
  465. pipe_texture_reference((struct pipe_texture **) &i915->texture[i],
  466. NULL);
  467. i915->num_textures = num;
  468. i915->dirty |= I915_NEW_TEXTURE;
  469. }
  470. static void i915_set_framebuffer_state(struct pipe_context *pipe,
  471. const struct pipe_framebuffer_state *fb)
  472. {
  473. struct i915_context *i915 = i915_context(pipe);
  474. draw_flush(i915->draw);
  475. i915->framebuffer = *fb; /* struct copy */
  476. i915->dirty |= I915_NEW_FRAMEBUFFER;
  477. }
  478. static void i915_set_clip_state( struct pipe_context *pipe,
  479. const struct pipe_clip_state *clip )
  480. {
  481. struct i915_context *i915 = i915_context(pipe);
  482. draw_flush(i915->draw);
  483. draw_set_clip_state(i915->draw, clip);
  484. i915->dirty |= I915_NEW_CLIP;
  485. }
  486. /* Called when driver state tracker notices changes to the viewport
  487. * matrix:
  488. */
  489. static void i915_set_viewport_state( struct pipe_context *pipe,
  490. const struct pipe_viewport_state *viewport )
  491. {
  492. struct i915_context *i915 = i915_context(pipe);
  493. i915->viewport = *viewport; /* struct copy */
  494. /* pass the viewport info to the draw module */
  495. draw_set_viewport_state(i915->draw, &i915->viewport);
  496. i915->dirty |= I915_NEW_VIEWPORT;
  497. }
  498. static void *
  499. i915_create_rasterizer_state(struct pipe_context *pipe,
  500. const struct pipe_rasterizer_state *rasterizer)
  501. {
  502. struct i915_rasterizer_state *cso = CALLOC_STRUCT( i915_rasterizer_state );
  503. cso->templ = rasterizer;
  504. cso->color_interp = rasterizer->flatshade ? INTERP_CONSTANT : INTERP_LINEAR;
  505. cso->light_twoside = rasterizer->light_twoside;
  506. cso->ds[0].u = _3DSTATE_DEPTH_OFFSET_SCALE;
  507. cso->ds[1].f = rasterizer->offset_scale;
  508. if (rasterizer->poly_stipple_enable) {
  509. cso->st |= ST1_ENABLE;
  510. }
  511. if (rasterizer->scissor)
  512. cso->sc[0] = _3DSTATE_SCISSOR_ENABLE_CMD | ENABLE_SCISSOR_RECT;
  513. else
  514. cso->sc[0] = _3DSTATE_SCISSOR_ENABLE_CMD | DISABLE_SCISSOR_RECT;
  515. switch (rasterizer->cull_mode) {
  516. case PIPE_WINDING_NONE:
  517. cso->LIS4 |= S4_CULLMODE_NONE;
  518. break;
  519. case PIPE_WINDING_CW:
  520. cso->LIS4 |= S4_CULLMODE_CW;
  521. break;
  522. case PIPE_WINDING_CCW:
  523. cso->LIS4 |= S4_CULLMODE_CCW;
  524. break;
  525. case PIPE_WINDING_BOTH:
  526. cso->LIS4 |= S4_CULLMODE_BOTH;
  527. break;
  528. }
  529. {
  530. int line_width = CLAMP((int)(rasterizer->line_width * 2), 1, 0xf);
  531. cso->LIS4 |= line_width << S4_LINE_WIDTH_SHIFT;
  532. if (rasterizer->line_smooth)
  533. cso->LIS4 |= S4_LINE_ANTIALIAS_ENABLE;
  534. }
  535. {
  536. int point_size = CLAMP((int) rasterizer->point_size, 1, 0xff);
  537. cso->LIS4 |= point_size << S4_POINT_WIDTH_SHIFT;
  538. }
  539. if (rasterizer->flatshade) {
  540. cso->LIS4 |= (S4_FLATSHADE_ALPHA |
  541. S4_FLATSHADE_COLOR |
  542. S4_FLATSHADE_SPECULAR);
  543. }
  544. cso->LIS7 = fui( rasterizer->offset_units );
  545. return cso;
  546. }
  547. static void i915_bind_rasterizer_state( struct pipe_context *pipe,
  548. void *raster )
  549. {
  550. struct i915_context *i915 = i915_context(pipe);
  551. i915->rasterizer = (struct i915_rasterizer_state *)raster;
  552. /* pass-through to draw module */
  553. draw_set_rasterizer_state(i915->draw,
  554. (i915->rasterizer ? i915->rasterizer->templ : NULL));
  555. i915->dirty |= I915_NEW_RASTERIZER;
  556. }
  557. static void i915_delete_rasterizer_state(struct pipe_context *pipe,
  558. void *raster)
  559. {
  560. FREE(raster);
  561. }
  562. static void i915_set_vertex_buffers(struct pipe_context *pipe,
  563. unsigned count,
  564. const struct pipe_vertex_buffer *buffers)
  565. {
  566. struct i915_context *i915 = i915_context(pipe);
  567. /* Because we change state before the draw_set_vertex_buffers call
  568. * we need a flush here, just to be sure.
  569. */
  570. draw_flush(i915->draw);
  571. memcpy(i915->vertex_buffer, buffers, count * sizeof(buffers[0]));
  572. i915->num_vertex_buffers = count;
  573. /* pass-through to draw module */
  574. draw_set_vertex_buffers(i915->draw, count, buffers);
  575. }
  576. static void i915_set_vertex_elements(struct pipe_context *pipe,
  577. unsigned count,
  578. const struct pipe_vertex_element *elements)
  579. {
  580. struct i915_context *i915 = i915_context(pipe);
  581. /* Because we change state before the draw_set_vertex_buffers call
  582. * we need a flush here, just to be sure.
  583. */
  584. draw_flush(i915->draw);
  585. i915->num_vertex_elements = count;
  586. /* pass-through to draw module */
  587. draw_set_vertex_elements(i915->draw, count, elements);
  588. }
  589. static void i915_set_edgeflags(struct pipe_context *pipe,
  590. const unsigned *bitfield)
  591. {
  592. /* TODO do something here */
  593. }
  594. void
  595. i915_init_state_functions( struct i915_context *i915 )
  596. {
  597. i915->base.set_edgeflags = i915_set_edgeflags;
  598. i915->base.create_blend_state = i915_create_blend_state;
  599. i915->base.bind_blend_state = i915_bind_blend_state;
  600. i915->base.delete_blend_state = i915_delete_blend_state;
  601. i915->base.create_sampler_state = i915_create_sampler_state;
  602. i915->base.bind_sampler_states = i915_bind_sampler_states;
  603. i915->base.delete_sampler_state = i915_delete_sampler_state;
  604. i915->base.create_depth_stencil_alpha_state = i915_create_depth_stencil_state;
  605. i915->base.bind_depth_stencil_alpha_state = i915_bind_depth_stencil_state;
  606. i915->base.delete_depth_stencil_alpha_state = i915_delete_depth_stencil_state;
  607. i915->base.create_rasterizer_state = i915_create_rasterizer_state;
  608. i915->base.bind_rasterizer_state = i915_bind_rasterizer_state;
  609. i915->base.delete_rasterizer_state = i915_delete_rasterizer_state;
  610. i915->base.create_fs_state = i915_create_fs_state;
  611. i915->base.bind_fs_state = i915_bind_fs_state;
  612. i915->base.delete_fs_state = i915_delete_fs_state;
  613. i915->base.create_vs_state = i915_create_vs_state;
  614. i915->base.bind_vs_state = i915_bind_vs_state;
  615. i915->base.delete_vs_state = i915_delete_vs_state;
  616. i915->base.set_blend_color = i915_set_blend_color;
  617. i915->base.set_clip_state = i915_set_clip_state;
  618. i915->base.set_constant_buffer = i915_set_constant_buffer;
  619. i915->base.set_framebuffer_state = i915_set_framebuffer_state;
  620. i915->base.set_polygon_stipple = i915_set_polygon_stipple;
  621. i915->base.set_scissor_state = i915_set_scissor_state;
  622. i915->base.set_sampler_textures = i915_set_sampler_textures;
  623. i915->base.set_viewport_state = i915_set_viewport_state;
  624. i915->base.set_vertex_buffers = i915_set_vertex_buffers;
  625. i915->base.set_vertex_elements = i915_set_vertex_elements;
  626. }