1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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
28 /* Authors: Keith Whitwell <keithw@vmware.com>
29 */
30
31
32 #include "draw/draw_context.h"
33 #include "util/u_helpers.h"
34 #include "util/u_inlines.h"
35 #include "util/u_math.h"
36 #include "util/u_memory.h"
37 #include "util/u_transfer.h"
38 #include "tgsi/tgsi_parse.h"
39
40 #include "i915_context.h"
41 #include "i915_reg.h"
42 #include "i915_state_inlines.h"
43 #include "i915_fpc.h"
44 #include "i915_resource.h"
45 #include "i915_state.h"
46
47 /* The i915 (and related graphics cores) do not support GL_CLAMP. The
48 * Intel drivers for "other operating systems" implement GL_CLAMP as
49 * GL_CLAMP_TO_EDGE, so the same is done here.
50 */
51 static unsigned
translate_wrap_mode(unsigned wrap)52 translate_wrap_mode(unsigned wrap)
53 {
54 switch (wrap) {
55 case PIPE_TEX_WRAP_REPEAT:
56 return TEXCOORDMODE_WRAP;
57 case PIPE_TEX_WRAP_CLAMP:
58 return TEXCOORDMODE_CLAMP_EDGE; /* not quite correct */
59 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
60 return TEXCOORDMODE_CLAMP_EDGE;
61 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
62 return TEXCOORDMODE_CLAMP_BORDER;
63 case PIPE_TEX_WRAP_MIRROR_REPEAT:
64 return TEXCOORDMODE_MIRROR;
65 default:
66 return TEXCOORDMODE_WRAP;
67 }
68 }
69
translate_img_filter(unsigned filter)70 static unsigned translate_img_filter( unsigned filter )
71 {
72 switch (filter) {
73 case PIPE_TEX_FILTER_NEAREST:
74 return FILTER_NEAREST;
75 case PIPE_TEX_FILTER_LINEAR:
76 return FILTER_LINEAR;
77 default:
78 assert(0);
79 return FILTER_NEAREST;
80 }
81 }
82
translate_mip_filter(unsigned filter)83 static unsigned translate_mip_filter( unsigned filter )
84 {
85 switch (filter) {
86 case PIPE_TEX_MIPFILTER_NONE:
87 return MIPFILTER_NONE;
88 case PIPE_TEX_MIPFILTER_NEAREST:
89 return MIPFILTER_NEAREST;
90 case PIPE_TEX_MIPFILTER_LINEAR:
91 return MIPFILTER_LINEAR;
92 default:
93 assert(0);
94 return MIPFILTER_NONE;
95 }
96 }
97
98 /* None of this state is actually used for anything yet.
99 */
100 static void *
i915_create_blend_state(struct pipe_context * pipe,const struct pipe_blend_state * blend)101 i915_create_blend_state(struct pipe_context *pipe,
102 const struct pipe_blend_state *blend)
103 {
104 struct i915_blend_state *cso_data = CALLOC_STRUCT( i915_blend_state );
105
106 {
107 unsigned eqRGB = blend->rt[0].rgb_func;
108 unsigned srcRGB = blend->rt[0].rgb_src_factor;
109 unsigned dstRGB = blend->rt[0].rgb_dst_factor;
110
111 unsigned eqA = blend->rt[0].alpha_func;
112 unsigned srcA = blend->rt[0].alpha_src_factor;
113 unsigned dstA = blend->rt[0].alpha_dst_factor;
114
115 /* Special handling for MIN/MAX filter modes handled at
116 * frontend level.
117 */
118
119 if (srcA != srcRGB ||
120 dstA != dstRGB ||
121 eqA != eqRGB) {
122
123 cso_data->iab = (_3DSTATE_INDEPENDENT_ALPHA_BLEND_CMD |
124 IAB_MODIFY_ENABLE |
125 IAB_ENABLE |
126 IAB_MODIFY_FUNC |
127 IAB_MODIFY_SRC_FACTOR |
128 IAB_MODIFY_DST_FACTOR |
129 SRC_ABLND_FACT(i915_translate_blend_factor(srcA)) |
130 DST_ABLND_FACT(i915_translate_blend_factor(dstA)) |
131 (i915_translate_blend_func(eqA) << IAB_FUNC_SHIFT));
132 }
133 else {
134 cso_data->iab = (_3DSTATE_INDEPENDENT_ALPHA_BLEND_CMD |
135 IAB_MODIFY_ENABLE |
136 0);
137 }
138 }
139
140 cso_data->modes4 |= (_3DSTATE_MODES_4_CMD |
141 ENABLE_LOGIC_OP_FUNC |
142 LOGIC_OP_FUNC(i915_translate_logic_op(blend->logicop_func)));
143
144 if (blend->logicop_enable)
145 cso_data->LIS5 |= S5_LOGICOP_ENABLE;
146
147 if (blend->dither)
148 cso_data->LIS5 |= S5_COLOR_DITHER_ENABLE;
149
150 /* We potentially do some fixup at emission for non-BGRA targets */
151 if ((blend->rt[0].colormask & PIPE_MASK_R) == 0)
152 cso_data->LIS5 |= S5_WRITEDISABLE_RED;
153
154 if ((blend->rt[0].colormask & PIPE_MASK_G) == 0)
155 cso_data->LIS5 |= S5_WRITEDISABLE_GREEN;
156
157 if ((blend->rt[0].colormask & PIPE_MASK_B) == 0)
158 cso_data->LIS5 |= S5_WRITEDISABLE_BLUE;
159
160 if ((blend->rt[0].colormask & PIPE_MASK_A) == 0)
161 cso_data->LIS5 |= S5_WRITEDISABLE_ALPHA;
162
163 if (blend->rt[0].blend_enable) {
164 unsigned funcRGB = blend->rt[0].rgb_func;
165 unsigned srcRGB = blend->rt[0].rgb_src_factor;
166 unsigned dstRGB = blend->rt[0].rgb_dst_factor;
167
168 cso_data->LIS6 |= (S6_CBUF_BLEND_ENABLE |
169 SRC_BLND_FACT(i915_translate_blend_factor(srcRGB)) |
170 DST_BLND_FACT(i915_translate_blend_factor(dstRGB)) |
171 (i915_translate_blend_func(funcRGB) << S6_CBUF_BLEND_FUNC_SHIFT));
172 }
173
174 return cso_data;
175 }
176
i915_bind_blend_state(struct pipe_context * pipe,void * blend)177 static void i915_bind_blend_state(struct pipe_context *pipe,
178 void *blend)
179 {
180 struct i915_context *i915 = i915_context(pipe);
181
182 if (i915->blend == blend)
183 return;
184
185 i915->blend = (struct i915_blend_state*)blend;
186
187 i915->dirty |= I915_NEW_BLEND;
188 }
189
190
i915_delete_blend_state(struct pipe_context * pipe,void * blend)191 static void i915_delete_blend_state(struct pipe_context *pipe, void *blend)
192 {
193 FREE(blend);
194 }
195
i915_set_blend_color(struct pipe_context * pipe,const struct pipe_blend_color * blend_color)196 static void i915_set_blend_color( struct pipe_context *pipe,
197 const struct pipe_blend_color *blend_color )
198 {
199 struct i915_context *i915 = i915_context(pipe);
200
201 if (!blend_color)
202 return;
203
204 i915->blend_color = *blend_color;
205
206 i915->dirty |= I915_NEW_BLEND;
207 }
208
i915_set_stencil_ref(struct pipe_context * pipe,const struct pipe_stencil_ref * stencil_ref)209 static void i915_set_stencil_ref( struct pipe_context *pipe,
210 const struct pipe_stencil_ref *stencil_ref )
211 {
212 struct i915_context *i915 = i915_context(pipe);
213
214 i915->stencil_ref = *stencil_ref;
215
216 i915->dirty |= I915_NEW_DEPTH_STENCIL;
217 }
218
219 static void *
i915_create_sampler_state(struct pipe_context * pipe,const struct pipe_sampler_state * sampler)220 i915_create_sampler_state(struct pipe_context *pipe,
221 const struct pipe_sampler_state *sampler)
222 {
223 struct i915_sampler_state *cso = CALLOC_STRUCT( i915_sampler_state );
224 const unsigned ws = sampler->wrap_s;
225 const unsigned wt = sampler->wrap_t;
226 const unsigned wr = sampler->wrap_r;
227 unsigned minFilt, magFilt;
228 unsigned mipFilt;
229
230 cso->templ = *sampler;
231
232 mipFilt = translate_mip_filter(sampler->min_mip_filter);
233 minFilt = translate_img_filter( sampler->min_img_filter );
234 magFilt = translate_img_filter( sampler->mag_img_filter );
235
236 if (sampler->max_anisotropy > 1)
237 minFilt = magFilt = FILTER_ANISOTROPIC;
238
239 if (sampler->max_anisotropy > 2) {
240 cso->state[0] |= SS2_MAX_ANISO_4;
241 }
242
243 {
244 int b = (int) (sampler->lod_bias * 16.0);
245 b = CLAMP(b, -256, 255);
246 cso->state[0] |= ((b << SS2_LOD_BIAS_SHIFT) & SS2_LOD_BIAS_MASK);
247 }
248
249 /* Shadow:
250 */
251 if (sampler->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE)
252 {
253 cso->state[0] |= (SS2_SHADOW_ENABLE |
254 i915_translate_shadow_compare_func(sampler->compare_func));
255
256 minFilt = FILTER_4X4_FLAT;
257 magFilt = FILTER_4X4_FLAT;
258 }
259
260 cso->state[0] |= ((minFilt << SS2_MIN_FILTER_SHIFT) |
261 (mipFilt << SS2_MIP_FILTER_SHIFT) |
262 (magFilt << SS2_MAG_FILTER_SHIFT));
263
264 cso->state[1] |=
265 ((translate_wrap_mode(ws) << SS3_TCX_ADDR_MODE_SHIFT) |
266 (translate_wrap_mode(wt) << SS3_TCY_ADDR_MODE_SHIFT) |
267 (translate_wrap_mode(wr) << SS3_TCZ_ADDR_MODE_SHIFT));
268
269 if (sampler->normalized_coords)
270 cso->state[1] |= SS3_NORMALIZED_COORDS;
271
272 {
273 int minlod = (int) (16.0 * sampler->min_lod);
274 int maxlod = (int) (16.0 * sampler->max_lod);
275 minlod = CLAMP(minlod, 0, 16 * 11);
276 maxlod = CLAMP(maxlod, 0, 16 * 11);
277
278 if (minlod > maxlod)
279 maxlod = minlod;
280
281 cso->minlod = minlod;
282 cso->maxlod = maxlod;
283 }
284
285 {
286 ubyte r = float_to_ubyte(sampler->border_color.f[0]);
287 ubyte g = float_to_ubyte(sampler->border_color.f[1]);
288 ubyte b = float_to_ubyte(sampler->border_color.f[2]);
289 ubyte a = float_to_ubyte(sampler->border_color.f[3]);
290 cso->state[2] = I915PACKCOLOR8888(r, g, b, a);
291 }
292 return cso;
293 }
294
295 static void
i915_bind_vertex_sampler_states(struct pipe_context * pipe,unsigned start,unsigned num,void ** samplers)296 i915_bind_vertex_sampler_states(struct pipe_context *pipe,
297 unsigned start,
298 unsigned num,
299 void **samplers)
300 {
301 struct i915_context *i915 = i915_context(pipe);
302 unsigned i;
303
304 assert(start + num <= ARRAY_SIZE(i915->vertex_samplers));
305
306 /* Check for no-op */
307 if (num == i915->num_vertex_samplers &&
308 !memcmp(i915->vertex_samplers + start, samplers,
309 num * sizeof(void *)))
310 return;
311
312 for (i = 0; i < num; ++i)
313 i915->vertex_samplers[i + start] = samplers[i];
314
315 /* find highest non-null samplers[] entry */
316 {
317 unsigned j = MAX2(i915->num_vertex_samplers, start + num);
318 while (j > 0 && i915->vertex_samplers[j - 1] == NULL)
319 j--;
320 i915->num_vertex_samplers = j;
321 }
322
323 draw_set_samplers(i915->draw,
324 PIPE_SHADER_VERTEX,
325 i915->vertex_samplers,
326 i915->num_vertex_samplers);
327 }
328
329
330
i915_bind_fragment_sampler_states(struct pipe_context * pipe,unsigned start,unsigned num,void ** samplers)331 static void i915_bind_fragment_sampler_states(struct pipe_context *pipe,
332 unsigned start,
333 unsigned num,
334 void **samplers)
335 {
336 struct i915_context *i915 = i915_context(pipe);
337 unsigned i;
338
339 /* Check for no-op */
340 if (num == i915->num_samplers &&
341 !memcmp(i915->fragment_sampler + start, samplers,
342 num * sizeof(void *)))
343 return;
344
345 for (i = 0; i < num; ++i)
346 i915->fragment_sampler[i + start] = samplers[i];
347
348 /* find highest non-null samplers[] entry */
349 {
350 unsigned j = MAX2(i915->num_samplers, start + num);
351 while (j > 0 && i915->fragment_sampler[j - 1] == NULL)
352 j--;
353 i915->num_samplers = j;
354 }
355
356 i915->dirty |= I915_NEW_SAMPLER;
357 }
358
359
360 static void
i915_bind_sampler_states(struct pipe_context * pipe,enum pipe_shader_type shader,unsigned start,unsigned num_samplers,void ** samplers)361 i915_bind_sampler_states(struct pipe_context *pipe,
362 enum pipe_shader_type shader,
363 unsigned start, unsigned num_samplers,
364 void **samplers)
365 {
366 switch (shader) {
367 case PIPE_SHADER_VERTEX:
368 i915_bind_vertex_sampler_states(pipe, start, num_samplers, samplers);
369 break;
370 case PIPE_SHADER_FRAGMENT:
371 i915_bind_fragment_sampler_states(pipe, start, num_samplers, samplers);
372 break;
373 default:
374 ;
375 }
376 }
377
378
i915_delete_sampler_state(struct pipe_context * pipe,void * sampler)379 static void i915_delete_sampler_state(struct pipe_context *pipe,
380 void *sampler)
381 {
382 FREE(sampler);
383 }
384
385
386 /**
387 * Called before drawing VBO to map vertex samplers and hand them to draw
388 */
389 void
i915_prepare_vertex_sampling(struct i915_context * i915)390 i915_prepare_vertex_sampling(struct i915_context *i915)
391 {
392 struct i915_winsys *iws = i915->iws;
393 unsigned i,j;
394 uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS];
395 uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS];
396 uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS];
397 unsigned num = i915->num_vertex_sampler_views;
398 struct pipe_sampler_view **views = i915->vertex_sampler_views;
399
400 assert(num <= PIPE_MAX_SAMPLERS);
401 if (!num)
402 return;
403
404 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
405 struct pipe_sampler_view *view = i < num ? views[i] : NULL;
406
407 if (view) {
408 struct pipe_resource *tex = view->texture;
409 struct i915_texture *i915_tex = i915_texture(tex);
410 ubyte *addr;
411
412 /* We're referencing the texture's internal data, so save a
413 * reference to it.
414 */
415 pipe_resource_reference(&i915->mapped_vs_tex[i], tex);
416
417 i915->mapped_vs_tex_buffer[i] = i915_tex->buffer;
418 addr = iws->buffer_map(iws,
419 i915_tex->buffer,
420 FALSE /* read only */);
421
422 /* Setup array of mipmap level pointers */
423 /* FIXME: handle 3D textures? */
424 for (j = view->u.tex.first_level; j <= tex->last_level; j++) {
425 mip_offsets[j] = i915_texture_offset(i915_tex, j , 0 /* FIXME depth */);
426 row_stride[j] = i915_tex->stride;
427 img_stride[j] = 0; /* FIXME */
428 }
429
430 draw_set_mapped_texture(i915->draw,
431 PIPE_SHADER_VERTEX,
432 i,
433 tex->width0, tex->height0, tex->depth0,
434 view->u.tex.first_level, tex->last_level,
435 0, 0, addr,
436 row_stride, img_stride, mip_offsets);
437 } else
438 i915->mapped_vs_tex[i] = NULL;
439 }
440 }
441
442 void
i915_cleanup_vertex_sampling(struct i915_context * i915)443 i915_cleanup_vertex_sampling(struct i915_context *i915)
444 {
445 struct i915_winsys *iws = i915->iws;
446 unsigned i;
447 for (i = 0; i < ARRAY_SIZE(i915->mapped_vs_tex); i++) {
448 if (i915->mapped_vs_tex_buffer[i]) {
449 iws->buffer_unmap(iws, i915->mapped_vs_tex_buffer[i]);
450 pipe_resource_reference(&i915->mapped_vs_tex[i], NULL);
451 }
452 }
453 }
454
455
456
457 /** XXX move someday? Or consolidate all these simple state setters
458 * into one file.
459 */
460
461 static void *
i915_create_depth_stencil_state(struct pipe_context * pipe,const struct pipe_depth_stencil_alpha_state * depth_stencil)462 i915_create_depth_stencil_state(struct pipe_context *pipe,
463 const struct pipe_depth_stencil_alpha_state *depth_stencil)
464 {
465 struct i915_depth_stencil_state *cso = CALLOC_STRUCT( i915_depth_stencil_state );
466
467 {
468 int testmask = depth_stencil->stencil[0].valuemask & 0xff;
469 int writemask = depth_stencil->stencil[0].writemask & 0xff;
470
471 cso->stencil_modes4 |= (_3DSTATE_MODES_4_CMD |
472 ENABLE_STENCIL_TEST_MASK |
473 STENCIL_TEST_MASK(testmask) |
474 ENABLE_STENCIL_WRITE_MASK |
475 STENCIL_WRITE_MASK(writemask));
476 }
477
478 if (depth_stencil->stencil[0].enabled) {
479 int test = i915_translate_compare_func(depth_stencil->stencil[0].func);
480 int fop = i915_translate_stencil_op(depth_stencil->stencil[0].fail_op);
481 int dfop = i915_translate_stencil_op(depth_stencil->stencil[0].zfail_op);
482 int dpop = i915_translate_stencil_op(depth_stencil->stencil[0].zpass_op);
483
484 cso->stencil_LIS5 |= (S5_STENCIL_TEST_ENABLE |
485 S5_STENCIL_WRITE_ENABLE |
486 (test << S5_STENCIL_TEST_FUNC_SHIFT) |
487 (fop << S5_STENCIL_FAIL_SHIFT) |
488 (dfop << S5_STENCIL_PASS_Z_FAIL_SHIFT) |
489 (dpop << S5_STENCIL_PASS_Z_PASS_SHIFT));
490 }
491
492 if (depth_stencil->stencil[1].enabled) {
493 int test = i915_translate_compare_func(depth_stencil->stencil[1].func);
494 int fop = i915_translate_stencil_op(depth_stencil->stencil[1].fail_op);
495 int dfop = i915_translate_stencil_op(depth_stencil->stencil[1].zfail_op);
496 int dpop = i915_translate_stencil_op(depth_stencil->stencil[1].zpass_op);
497 int tmask = depth_stencil->stencil[1].valuemask & 0xff;
498 int wmask = depth_stencil->stencil[1].writemask & 0xff;
499
500 cso->bfo[0] = (_3DSTATE_BACKFACE_STENCIL_OPS |
501 BFO_ENABLE_STENCIL_FUNCS |
502 BFO_ENABLE_STENCIL_TWO_SIDE |
503 BFO_ENABLE_STENCIL_REF |
504 BFO_STENCIL_TWO_SIDE |
505 (test << BFO_STENCIL_TEST_SHIFT) |
506 (fop << BFO_STENCIL_FAIL_SHIFT) |
507 (dfop << BFO_STENCIL_PASS_Z_FAIL_SHIFT) |
508 (dpop << BFO_STENCIL_PASS_Z_PASS_SHIFT));
509
510 cso->bfo[1] = (_3DSTATE_BACKFACE_STENCIL_MASKS |
511 BFM_ENABLE_STENCIL_TEST_MASK |
512 BFM_ENABLE_STENCIL_WRITE_MASK |
513 (tmask << BFM_STENCIL_TEST_MASK_SHIFT) |
514 (wmask << BFM_STENCIL_WRITE_MASK_SHIFT));
515 }
516 else {
517 /* This actually disables two-side stencil: The bit set is a
518 * modify-enable bit to indicate we are changing the two-side
519 * setting. Then there is a symbolic zero to show that we are
520 * setting the flag to zero/off.
521 */
522 cso->bfo[0] = (_3DSTATE_BACKFACE_STENCIL_OPS |
523 BFO_ENABLE_STENCIL_TWO_SIDE |
524 0);
525 cso->bfo[1] = 0;
526 }
527
528 if (depth_stencil->depth.enabled) {
529 int func = i915_translate_compare_func(depth_stencil->depth.func);
530
531 cso->depth_LIS6 |= (S6_DEPTH_TEST_ENABLE |
532 (func << S6_DEPTH_TEST_FUNC_SHIFT));
533
534 if (depth_stencil->depth.writemask)
535 cso->depth_LIS6 |= S6_DEPTH_WRITE_ENABLE;
536 }
537
538 if (depth_stencil->alpha.enabled) {
539 int test = i915_translate_compare_func(depth_stencil->alpha.func);
540 ubyte refByte = float_to_ubyte(depth_stencil->alpha.ref_value);
541
542 cso->depth_LIS6 |= (S6_ALPHA_TEST_ENABLE |
543 (test << S6_ALPHA_TEST_FUNC_SHIFT) |
544 (((unsigned) refByte) << S6_ALPHA_REF_SHIFT));
545 }
546
547 return cso;
548 }
549
i915_bind_depth_stencil_state(struct pipe_context * pipe,void * depth_stencil)550 static void i915_bind_depth_stencil_state(struct pipe_context *pipe,
551 void *depth_stencil)
552 {
553 struct i915_context *i915 = i915_context(pipe);
554
555 if (i915->depth_stencil == depth_stencil)
556 return;
557
558 i915->depth_stencil = (const struct i915_depth_stencil_state *)depth_stencil;
559
560 i915->dirty |= I915_NEW_DEPTH_STENCIL;
561 }
562
i915_delete_depth_stencil_state(struct pipe_context * pipe,void * depth_stencil)563 static void i915_delete_depth_stencil_state(struct pipe_context *pipe,
564 void *depth_stencil)
565 {
566 FREE(depth_stencil);
567 }
568
569
i915_set_scissor_states(struct pipe_context * pipe,unsigned start_slot,unsigned num_scissors,const struct pipe_scissor_state * scissor)570 static void i915_set_scissor_states( struct pipe_context *pipe,
571 unsigned start_slot,
572 unsigned num_scissors,
573 const struct pipe_scissor_state *scissor )
574 {
575 struct i915_context *i915 = i915_context(pipe);
576
577 memcpy( &i915->scissor, scissor, sizeof(*scissor) );
578 i915->dirty |= I915_NEW_SCISSOR;
579 }
580
581
i915_set_polygon_stipple(struct pipe_context * pipe,const struct pipe_poly_stipple * stipple)582 static void i915_set_polygon_stipple( struct pipe_context *pipe,
583 const struct pipe_poly_stipple *stipple )
584 {
585 }
586
587
588
589 static void *
i915_create_fs_state(struct pipe_context * pipe,const struct pipe_shader_state * templ)590 i915_create_fs_state(struct pipe_context *pipe,
591 const struct pipe_shader_state *templ)
592 {
593 struct i915_context *i915 = i915_context(pipe);
594 struct i915_fragment_shader *ifs = CALLOC_STRUCT(i915_fragment_shader);
595 if (!ifs)
596 return NULL;
597
598 ifs->draw_data = draw_create_fragment_shader(i915->draw, templ);
599 ifs->state.tokens = tgsi_dup_tokens(templ->tokens);
600
601 tgsi_scan_shader(templ->tokens, &ifs->info);
602
603 /* The shader's compiled to i915 instructions here */
604 i915_translate_fragment_program(i915, ifs);
605
606 return ifs;
607 }
608
609 static void
i915_bind_fs_state(struct pipe_context * pipe,void * shader)610 i915_bind_fs_state(struct pipe_context *pipe, void *shader)
611 {
612 struct i915_context *i915 = i915_context(pipe);
613
614 if (i915->fs == shader)
615 return;
616
617 i915->fs = (struct i915_fragment_shader*) shader;
618
619 draw_bind_fragment_shader(i915->draw, (i915->fs ? i915->fs->draw_data : NULL));
620
621 i915->dirty |= I915_NEW_FS;
622 }
623
624 static
i915_delete_fs_state(struct pipe_context * pipe,void * shader)625 void i915_delete_fs_state(struct pipe_context *pipe, void *shader)
626 {
627 struct i915_fragment_shader *ifs = (struct i915_fragment_shader *) shader;
628
629 FREE(ifs->decl);
630 ifs->decl = NULL;
631
632 FREE(ifs->program);
633 ifs->program = NULL;
634 FREE((struct tgsi_token *)ifs->state.tokens);
635 ifs->state.tokens = NULL;
636
637 ifs->program_len = 0;
638 ifs->decl_len = 0;
639
640 FREE(ifs);
641 }
642
643
644 static void *
i915_create_vs_state(struct pipe_context * pipe,const struct pipe_shader_state * templ)645 i915_create_vs_state(struct pipe_context *pipe,
646 const struct pipe_shader_state *templ)
647 {
648 struct i915_context *i915 = i915_context(pipe);
649
650 /* just pass-through to draw module */
651 return draw_create_vertex_shader(i915->draw, templ);
652 }
653
i915_bind_vs_state(struct pipe_context * pipe,void * shader)654 static void i915_bind_vs_state(struct pipe_context *pipe, void *shader)
655 {
656 struct i915_context *i915 = i915_context(pipe);
657
658 if (i915->vs == shader)
659 return;
660
661 i915->vs = shader;
662
663 /* just pass-through to draw module */
664 draw_bind_vertex_shader(i915->draw, (struct draw_vertex_shader *) shader);
665
666 i915->dirty |= I915_NEW_VS;
667 }
668
i915_delete_vs_state(struct pipe_context * pipe,void * shader)669 static void i915_delete_vs_state(struct pipe_context *pipe, void *shader)
670 {
671 struct i915_context *i915 = i915_context(pipe);
672
673 /* just pass-through to draw module */
674 draw_delete_vertex_shader(i915->draw, (struct draw_vertex_shader *) shader);
675 }
676
i915_set_constant_buffer(struct pipe_context * pipe,enum pipe_shader_type shader,uint index,const struct pipe_constant_buffer * cb)677 static void i915_set_constant_buffer(struct pipe_context *pipe,
678 enum pipe_shader_type shader, uint index,
679 const struct pipe_constant_buffer *cb)
680 {
681 struct i915_context *i915 = i915_context(pipe);
682 struct pipe_resource *buf = cb ? cb->buffer : NULL;
683 unsigned new_num = 0;
684 boolean diff = TRUE;
685
686 /* XXX don't support geom shaders now */
687 if (shader == PIPE_SHADER_GEOMETRY)
688 return;
689
690 if (cb && cb->user_buffer) {
691 buf = i915_user_buffer_create(pipe->screen, (void *) cb->user_buffer,
692 cb->buffer_size,
693 PIPE_BIND_CONSTANT_BUFFER);
694 }
695
696 /* if we have a new buffer compare it with the old one */
697 if (buf) {
698 struct i915_buffer *ibuf = i915_buffer(buf);
699 struct pipe_resource *old_buf = i915->constants[shader];
700 struct i915_buffer *old = old_buf ? i915_buffer(old_buf) : NULL;
701 unsigned old_num = i915->current.num_user_constants[shader];
702
703 new_num = ibuf->b.b.width0 / 4 * sizeof(float);
704
705 if (old_num == new_num) {
706 if (old_num == 0)
707 diff = FALSE;
708 #if 0
709 /* XXX no point in running this code since st/mesa only uses user buffers */
710 /* Can't compare the buffer data since they are userbuffers */
711 else if (old && old->free_on_destroy)
712 diff = memcmp(old->data, ibuf->data, ibuf->b.b.width0);
713 #else
714 (void)old;
715 #endif
716 }
717 } else {
718 diff = i915->current.num_user_constants[shader] != 0;
719 }
720
721 pipe_resource_reference(&i915->constants[shader], buf);
722 i915->current.num_user_constants[shader] = new_num;
723
724 if (diff)
725 i915->dirty |= shader == PIPE_SHADER_VERTEX ? I915_NEW_VS_CONSTANTS : I915_NEW_FS_CONSTANTS;
726
727 if (cb && cb->user_buffer) {
728 pipe_resource_reference(&buf, NULL);
729 }
730 }
731
732
i915_set_fragment_sampler_views(struct pipe_context * pipe,unsigned num,struct pipe_sampler_view ** views)733 static void i915_set_fragment_sampler_views(struct pipe_context *pipe,
734 unsigned num,
735 struct pipe_sampler_view **views)
736 {
737 struct i915_context *i915 = i915_context(pipe);
738 uint i;
739
740 assert(num <= PIPE_MAX_SAMPLERS);
741
742 /* Check for no-op */
743 if (num == i915->num_fragment_sampler_views &&
744 !memcmp(i915->fragment_sampler_views, views, num * sizeof(struct pipe_sampler_view *)))
745 return;
746
747 for (i = 0; i < num; i++) {
748 pipe_sampler_view_reference(&i915->fragment_sampler_views[i], views[i]);
749 }
750
751 for (i = num; i < i915->num_fragment_sampler_views; i++)
752 pipe_sampler_view_reference(&i915->fragment_sampler_views[i], NULL);
753
754 i915->num_fragment_sampler_views = num;
755
756 i915->dirty |= I915_NEW_SAMPLER_VIEW;
757 }
758
759 static void
i915_set_vertex_sampler_views(struct pipe_context * pipe,unsigned num,struct pipe_sampler_view ** views)760 i915_set_vertex_sampler_views(struct pipe_context *pipe,
761 unsigned num,
762 struct pipe_sampler_view **views)
763 {
764 struct i915_context *i915 = i915_context(pipe);
765 uint i;
766
767 assert(num <= ARRAY_SIZE(i915->vertex_sampler_views));
768
769 /* Check for no-op */
770 if (num == i915->num_vertex_sampler_views &&
771 !memcmp(i915->vertex_sampler_views, views, num * sizeof(struct pipe_sampler_view *))) {
772 return;
773 }
774
775 for (i = 0; i < ARRAY_SIZE(i915->vertex_sampler_views); i++) {
776 struct pipe_sampler_view *view = i < num ? views[i] : NULL;
777
778 pipe_sampler_view_reference(&i915->vertex_sampler_views[i], view);
779 }
780
781 i915->num_vertex_sampler_views = num;
782
783 draw_set_sampler_views(i915->draw,
784 PIPE_SHADER_VERTEX,
785 i915->vertex_sampler_views,
786 i915->num_vertex_sampler_views);
787 }
788
789
790 static void
i915_set_sampler_views(struct pipe_context * pipe,enum pipe_shader_type shader,unsigned start,unsigned num,struct pipe_sampler_view ** views)791 i915_set_sampler_views(struct pipe_context *pipe, enum pipe_shader_type shader,
792 unsigned start, unsigned num,
793 struct pipe_sampler_view **views)
794 {
795 assert(start == 0);
796 switch (shader) {
797 case PIPE_SHADER_FRAGMENT:
798 i915_set_fragment_sampler_views(pipe, num, views);
799 break;
800 case PIPE_SHADER_VERTEX:
801 i915_set_vertex_sampler_views(pipe, num, views);
802 break;
803 default:
804 ;
805 }
806 }
807
808
809 struct pipe_sampler_view *
i915_create_sampler_view_custom(struct pipe_context * pipe,struct pipe_resource * texture,const struct pipe_sampler_view * templ,unsigned width0,unsigned height0)810 i915_create_sampler_view_custom(struct pipe_context *pipe,
811 struct pipe_resource *texture,
812 const struct pipe_sampler_view *templ,
813 unsigned width0,
814 unsigned height0)
815 {
816 struct pipe_sampler_view *view = CALLOC_STRUCT(pipe_sampler_view);
817
818 if (view) {
819 *view = *templ;
820 view->reference.count = 1;
821 view->texture = NULL;
822 pipe_resource_reference(&view->texture, texture);
823 view->context = pipe;
824 }
825
826 return view;
827 }
828
829 static struct pipe_sampler_view *
i915_create_sampler_view(struct pipe_context * pipe,struct pipe_resource * texture,const struct pipe_sampler_view * templ)830 i915_create_sampler_view(struct pipe_context *pipe,
831 struct pipe_resource *texture,
832 const struct pipe_sampler_view *templ)
833 {
834 struct pipe_sampler_view *view = CALLOC_STRUCT(pipe_sampler_view);
835
836 if (view) {
837 *view = *templ;
838 view->reference.count = 1;
839 view->texture = NULL;
840 pipe_resource_reference(&view->texture, texture);
841 view->context = pipe;
842 }
843
844 return view;
845 }
846
847
848 static void
i915_sampler_view_destroy(struct pipe_context * pipe,struct pipe_sampler_view * view)849 i915_sampler_view_destroy(struct pipe_context *pipe,
850 struct pipe_sampler_view *view)
851 {
852 pipe_resource_reference(&view->texture, NULL);
853 FREE(view);
854 }
855
856
i915_set_framebuffer_state(struct pipe_context * pipe,const struct pipe_framebuffer_state * fb)857 static void i915_set_framebuffer_state(struct pipe_context *pipe,
858 const struct pipe_framebuffer_state *fb)
859 {
860 struct i915_context *i915 = i915_context(pipe);
861 int i;
862
863 i915->framebuffer.width = fb->width;
864 i915->framebuffer.height = fb->height;
865 i915->framebuffer.nr_cbufs = fb->nr_cbufs;
866 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
867 pipe_surface_reference(&i915->framebuffer.cbufs[i],
868 i < fb->nr_cbufs ? fb->cbufs[i] : NULL);
869 }
870 pipe_surface_reference(&i915->framebuffer.zsbuf, fb->zsbuf);
871
872 i915->dirty |= I915_NEW_FRAMEBUFFER;
873 }
874
875
876
i915_set_clip_state(struct pipe_context * pipe,const struct pipe_clip_state * clip)877 static void i915_set_clip_state( struct pipe_context *pipe,
878 const struct pipe_clip_state *clip )
879 {
880 struct i915_context *i915 = i915_context(pipe);
881
882 i915->clip = *clip;
883
884 draw_set_clip_state(i915->draw, clip);
885
886 i915->dirty |= I915_NEW_CLIP;
887 }
888
889
890
891 /* Called when gallium frontends notice changes to the viewport
892 * matrix:
893 */
i915_set_viewport_states(struct pipe_context * pipe,unsigned start_slot,unsigned num_viewports,const struct pipe_viewport_state * viewport)894 static void i915_set_viewport_states( struct pipe_context *pipe,
895 unsigned start_slot,
896 unsigned num_viewports,
897 const struct pipe_viewport_state *viewport )
898 {
899 struct i915_context *i915 = i915_context(pipe);
900
901 i915->viewport = *viewport; /* struct copy */
902
903 /* pass the viewport info to the draw module */
904 draw_set_viewport_states(i915->draw, start_slot, num_viewports,
905 &i915->viewport);
906
907 i915->dirty |= I915_NEW_VIEWPORT;
908 }
909
910
911 static void *
i915_create_rasterizer_state(struct pipe_context * pipe,const struct pipe_rasterizer_state * rasterizer)912 i915_create_rasterizer_state(struct pipe_context *pipe,
913 const struct pipe_rasterizer_state *rasterizer)
914 {
915 struct i915_rasterizer_state *cso = CALLOC_STRUCT( i915_rasterizer_state );
916
917 cso->templ = *rasterizer;
918 cso->light_twoside = rasterizer->light_twoside;
919 cso->ds[0].u = _3DSTATE_DEPTH_OFFSET_SCALE;
920 cso->ds[1].f = rasterizer->offset_scale;
921 if (rasterizer->poly_stipple_enable) {
922 cso->st |= ST1_ENABLE;
923 }
924
925 if (rasterizer->scissor)
926 cso->sc[0] = _3DSTATE_SCISSOR_ENABLE_CMD | ENABLE_SCISSOR_RECT;
927 else
928 cso->sc[0] = _3DSTATE_SCISSOR_ENABLE_CMD | DISABLE_SCISSOR_RECT;
929
930 switch (rasterizer->cull_face) {
931 case PIPE_FACE_NONE:
932 cso->LIS4 |= S4_CULLMODE_NONE;
933 break;
934 case PIPE_FACE_FRONT:
935 if (rasterizer->front_ccw)
936 cso->LIS4 |= S4_CULLMODE_CCW;
937 else
938 cso->LIS4 |= S4_CULLMODE_CW;
939 break;
940 case PIPE_FACE_BACK:
941 if (rasterizer->front_ccw)
942 cso->LIS4 |= S4_CULLMODE_CW;
943 else
944 cso->LIS4 |= S4_CULLMODE_CCW;
945 break;
946 case PIPE_FACE_FRONT_AND_BACK:
947 cso->LIS4 |= S4_CULLMODE_BOTH;
948 break;
949 }
950
951 {
952 int line_width = CLAMP((int)(rasterizer->line_width * 2), 1, 0xf);
953
954 cso->LIS4 |= line_width << S4_LINE_WIDTH_SHIFT;
955
956 if (rasterizer->line_smooth)
957 cso->LIS4 |= S4_LINE_ANTIALIAS_ENABLE;
958 }
959
960 {
961 int point_size = CLAMP((int) rasterizer->point_size, 1, 0xff);
962
963 cso->LIS4 |= point_size << S4_POINT_WIDTH_SHIFT;
964 }
965
966 if (rasterizer->flatshade) {
967 cso->LIS4 |= (S4_FLATSHADE_ALPHA |
968 S4_FLATSHADE_COLOR |
969 S4_FLATSHADE_SPECULAR);
970 }
971
972 cso->LIS7 = fui( rasterizer->offset_units );
973
974
975 return cso;
976 }
977
i915_bind_rasterizer_state(struct pipe_context * pipe,void * raster)978 static void i915_bind_rasterizer_state( struct pipe_context *pipe,
979 void *raster )
980 {
981 struct i915_context *i915 = i915_context(pipe);
982
983 if (i915->rasterizer == raster)
984 return;
985
986 i915->rasterizer = (struct i915_rasterizer_state *)raster;
987
988 /* pass-through to draw module */
989 draw_set_rasterizer_state(i915->draw,
990 (i915->rasterizer ? &(i915->rasterizer->templ) : NULL),
991 raster);
992
993 i915->dirty |= I915_NEW_RASTERIZER;
994 }
995
i915_delete_rasterizer_state(struct pipe_context * pipe,void * raster)996 static void i915_delete_rasterizer_state(struct pipe_context *pipe,
997 void *raster)
998 {
999 FREE(raster);
1000 }
1001
i915_set_vertex_buffers(struct pipe_context * pipe,unsigned start_slot,unsigned count,const struct pipe_vertex_buffer * buffers)1002 static void i915_set_vertex_buffers(struct pipe_context *pipe,
1003 unsigned start_slot, unsigned count,
1004 const struct pipe_vertex_buffer *buffers)
1005 {
1006 struct i915_context *i915 = i915_context(pipe);
1007 struct draw_context *draw = i915->draw;
1008
1009 util_set_vertex_buffers_count(i915->vertex_buffers,
1010 &i915->nr_vertex_buffers,
1011 buffers, start_slot, count);
1012
1013 /* pass-through to draw module */
1014 draw_set_vertex_buffers(draw, start_slot, count, buffers);
1015 }
1016
1017 static void *
i915_create_vertex_elements_state(struct pipe_context * pipe,unsigned count,const struct pipe_vertex_element * attribs)1018 i915_create_vertex_elements_state(struct pipe_context *pipe,
1019 unsigned count,
1020 const struct pipe_vertex_element *attribs)
1021 {
1022 struct i915_velems_state *velems;
1023 assert(count <= PIPE_MAX_ATTRIBS);
1024 velems = (struct i915_velems_state *) MALLOC(sizeof(struct i915_velems_state));
1025 if (velems) {
1026 velems->count = count;
1027 memcpy(velems->velem, attribs, sizeof(*attribs) * count);
1028 }
1029 return velems;
1030 }
1031
1032 static void
i915_bind_vertex_elements_state(struct pipe_context * pipe,void * velems)1033 i915_bind_vertex_elements_state(struct pipe_context *pipe,
1034 void *velems)
1035 {
1036 struct i915_context *i915 = i915_context(pipe);
1037 struct i915_velems_state *i915_velems = (struct i915_velems_state *) velems;
1038
1039 if (i915->velems == velems)
1040 return;
1041
1042 i915->velems = velems;
1043
1044 /* pass-through to draw module */
1045 if (i915_velems) {
1046 draw_set_vertex_elements(i915->draw,
1047 i915_velems->count, i915_velems->velem);
1048 }
1049 }
1050
1051 static void
i915_delete_vertex_elements_state(struct pipe_context * pipe,void * velems)1052 i915_delete_vertex_elements_state(struct pipe_context *pipe, void *velems)
1053 {
1054 FREE( velems );
1055 }
1056
1057 static void
i915_set_sample_mask(struct pipe_context * pipe,unsigned sample_mask)1058 i915_set_sample_mask(struct pipe_context *pipe,
1059 unsigned sample_mask)
1060 {
1061 }
1062
1063 void
i915_init_state_functions(struct i915_context * i915)1064 i915_init_state_functions( struct i915_context *i915 )
1065 {
1066 i915->base.create_blend_state = i915_create_blend_state;
1067 i915->base.bind_blend_state = i915_bind_blend_state;
1068 i915->base.delete_blend_state = i915_delete_blend_state;
1069
1070 i915->base.create_sampler_state = i915_create_sampler_state;
1071 i915->base.bind_sampler_states = i915_bind_sampler_states;
1072 i915->base.delete_sampler_state = i915_delete_sampler_state;
1073
1074 i915->base.create_depth_stencil_alpha_state = i915_create_depth_stencil_state;
1075 i915->base.bind_depth_stencil_alpha_state = i915_bind_depth_stencil_state;
1076 i915->base.delete_depth_stencil_alpha_state = i915_delete_depth_stencil_state;
1077
1078 i915->base.create_rasterizer_state = i915_create_rasterizer_state;
1079 i915->base.bind_rasterizer_state = i915_bind_rasterizer_state;
1080 i915->base.delete_rasterizer_state = i915_delete_rasterizer_state;
1081 i915->base.create_fs_state = i915_create_fs_state;
1082 i915->base.bind_fs_state = i915_bind_fs_state;
1083 i915->base.delete_fs_state = i915_delete_fs_state;
1084 i915->base.create_vs_state = i915_create_vs_state;
1085 i915->base.bind_vs_state = i915_bind_vs_state;
1086 i915->base.delete_vs_state = i915_delete_vs_state;
1087 i915->base.create_vertex_elements_state = i915_create_vertex_elements_state;
1088 i915->base.bind_vertex_elements_state = i915_bind_vertex_elements_state;
1089 i915->base.delete_vertex_elements_state = i915_delete_vertex_elements_state;
1090
1091 i915->base.set_blend_color = i915_set_blend_color;
1092 i915->base.set_stencil_ref = i915_set_stencil_ref;
1093 i915->base.set_clip_state = i915_set_clip_state;
1094 i915->base.set_sample_mask = i915_set_sample_mask;
1095 i915->base.set_constant_buffer = i915_set_constant_buffer;
1096 i915->base.set_framebuffer_state = i915_set_framebuffer_state;
1097
1098 i915->base.set_polygon_stipple = i915_set_polygon_stipple;
1099 i915->base.set_scissor_states = i915_set_scissor_states;
1100 i915->base.set_sampler_views = i915_set_sampler_views;
1101 i915->base.create_sampler_view = i915_create_sampler_view;
1102 i915->base.sampler_view_destroy = i915_sampler_view_destroy;
1103 i915->base.set_viewport_states = i915_set_viewport_states;
1104 i915->base.set_vertex_buffers = i915_set_vertex_buffers;
1105 }
1106