r600g: make framebuffer atom rely on dual src blend state.
[mesa.git] / src / gallium / drivers / r600 / r600_state_common.c
1 /*
2 * Copyright 2010 Red Hat Inc.
3 * 2010 Jerome Glisse
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 *
24 * Authors: Dave Airlie <airlied@redhat.com>
25 * Jerome Glisse <jglisse@redhat.com>
26 */
27 #include "r600_formats.h"
28 #include "r600_shader.h"
29 #include "r600d.h"
30
31 #include "util/u_format_s3tc.h"
32 #include "util/u_index_modify.h"
33 #include "util/u_memory.h"
34 #include "util/u_upload_mgr.h"
35 #include "util/u_math.h"
36 #include "tgsi/tgsi_parse.h"
37 #include "tgsi/tgsi_scan.h"
38 #include "tgsi/tgsi_ureg.h"
39
40 void r600_init_command_buffer(struct r600_command_buffer *cb, unsigned num_dw)
41 {
42 assert(!cb->buf);
43 cb->buf = CALLOC(1, 4 * num_dw);
44 cb->max_num_dw = num_dw;
45 }
46
47 void r600_release_command_buffer(struct r600_command_buffer *cb)
48 {
49 FREE(cb->buf);
50 }
51
52 void r600_add_atom(struct r600_context *rctx,
53 struct r600_atom *atom,
54 unsigned id)
55 {
56 assert(id < R600_NUM_ATOMS);
57 assert(rctx->atoms[id] == NULL);
58 rctx->atoms[id] = atom;
59 atom->id = id;
60 }
61
62 void r600_init_atom(struct r600_context *rctx,
63 struct r600_atom *atom,
64 unsigned id,
65 void (*emit)(struct r600_context *ctx, struct r600_atom *state),
66 unsigned num_dw)
67 {
68 atom->emit = (void*)emit;
69 atom->num_dw = num_dw;
70 r600_add_atom(rctx, atom, id);
71 }
72
73 void r600_emit_cso_state(struct r600_context *rctx, struct r600_atom *atom)
74 {
75 r600_emit_command_buffer(rctx->b.gfx.cs, ((struct r600_cso_state*)atom)->cb);
76 }
77
78 void r600_emit_alphatest_state(struct r600_context *rctx, struct r600_atom *atom)
79 {
80 struct radeon_winsys_cs *cs = rctx->b.gfx.cs;
81 struct r600_alphatest_state *a = (struct r600_alphatest_state*)atom;
82 unsigned alpha_ref = a->sx_alpha_ref;
83
84 if (rctx->b.chip_class >= EVERGREEN && a->cb0_export_16bpc) {
85 alpha_ref &= ~0x1FFF;
86 }
87
88 radeon_set_context_reg(cs, R_028410_SX_ALPHA_TEST_CONTROL,
89 a->sx_alpha_test_control |
90 S_028410_ALPHA_TEST_BYPASS(a->bypass));
91 radeon_set_context_reg(cs, R_028438_SX_ALPHA_REF, alpha_ref);
92 }
93
94 static void r600_texture_barrier(struct pipe_context *ctx, unsigned flags)
95 {
96 struct r600_context *rctx = (struct r600_context *)ctx;
97
98 rctx->b.flags |= R600_CONTEXT_INV_TEX_CACHE |
99 R600_CONTEXT_FLUSH_AND_INV_CB |
100 R600_CONTEXT_FLUSH_AND_INV |
101 R600_CONTEXT_WAIT_3D_IDLE;
102 }
103
104 static unsigned r600_conv_pipe_prim(unsigned prim)
105 {
106 static const unsigned prim_conv[] = {
107 [PIPE_PRIM_POINTS] = V_008958_DI_PT_POINTLIST,
108 [PIPE_PRIM_LINES] = V_008958_DI_PT_LINELIST,
109 [PIPE_PRIM_LINE_LOOP] = V_008958_DI_PT_LINELOOP,
110 [PIPE_PRIM_LINE_STRIP] = V_008958_DI_PT_LINESTRIP,
111 [PIPE_PRIM_TRIANGLES] = V_008958_DI_PT_TRILIST,
112 [PIPE_PRIM_TRIANGLE_STRIP] = V_008958_DI_PT_TRISTRIP,
113 [PIPE_PRIM_TRIANGLE_FAN] = V_008958_DI_PT_TRIFAN,
114 [PIPE_PRIM_QUADS] = V_008958_DI_PT_QUADLIST,
115 [PIPE_PRIM_QUAD_STRIP] = V_008958_DI_PT_QUADSTRIP,
116 [PIPE_PRIM_POLYGON] = V_008958_DI_PT_POLYGON,
117 [PIPE_PRIM_LINES_ADJACENCY] = V_008958_DI_PT_LINELIST_ADJ,
118 [PIPE_PRIM_LINE_STRIP_ADJACENCY] = V_008958_DI_PT_LINESTRIP_ADJ,
119 [PIPE_PRIM_TRIANGLES_ADJACENCY] = V_008958_DI_PT_TRILIST_ADJ,
120 [PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY] = V_008958_DI_PT_TRISTRIP_ADJ,
121 [PIPE_PRIM_PATCHES] = V_008958_DI_PT_PATCH,
122 [R600_PRIM_RECTANGLE_LIST] = V_008958_DI_PT_RECTLIST
123 };
124 assert(prim < ARRAY_SIZE(prim_conv));
125 return prim_conv[prim];
126 }
127
128 unsigned r600_conv_prim_to_gs_out(unsigned mode)
129 {
130 static const int prim_conv[] = {
131 [PIPE_PRIM_POINTS] = V_028A6C_OUTPRIM_TYPE_POINTLIST,
132 [PIPE_PRIM_LINES] = V_028A6C_OUTPRIM_TYPE_LINESTRIP,
133 [PIPE_PRIM_LINE_LOOP] = V_028A6C_OUTPRIM_TYPE_LINESTRIP,
134 [PIPE_PRIM_LINE_STRIP] = V_028A6C_OUTPRIM_TYPE_LINESTRIP,
135 [PIPE_PRIM_TRIANGLES] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
136 [PIPE_PRIM_TRIANGLE_STRIP] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
137 [PIPE_PRIM_TRIANGLE_FAN] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
138 [PIPE_PRIM_QUADS] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
139 [PIPE_PRIM_QUAD_STRIP] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
140 [PIPE_PRIM_POLYGON] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
141 [PIPE_PRIM_LINES_ADJACENCY] = V_028A6C_OUTPRIM_TYPE_LINESTRIP,
142 [PIPE_PRIM_LINE_STRIP_ADJACENCY] = V_028A6C_OUTPRIM_TYPE_LINESTRIP,
143 [PIPE_PRIM_TRIANGLES_ADJACENCY] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
144 [PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY] = V_028A6C_OUTPRIM_TYPE_TRISTRIP,
145 [PIPE_PRIM_PATCHES] = V_028A6C_OUTPRIM_TYPE_POINTLIST,
146 [R600_PRIM_RECTANGLE_LIST] = V_028A6C_OUTPRIM_TYPE_TRISTRIP
147 };
148 assert(mode < ARRAY_SIZE(prim_conv));
149
150 return prim_conv[mode];
151 }
152
153 /* common state between evergreen and r600 */
154
155 static void r600_bind_blend_state_internal(struct r600_context *rctx,
156 struct r600_blend_state *blend, bool blend_disable)
157 {
158 unsigned color_control;
159 bool update_cb = false;
160
161 rctx->alpha_to_one = blend->alpha_to_one;
162 rctx->dual_src_blend = blend->dual_src_blend;
163
164 if (!blend_disable) {
165 r600_set_cso_state_with_cb(rctx, &rctx->blend_state, blend, &blend->buffer);
166 color_control = blend->cb_color_control;
167 } else {
168 /* Blending is disabled. */
169 r600_set_cso_state_with_cb(rctx, &rctx->blend_state, blend, &blend->buffer_no_blend);
170 color_control = blend->cb_color_control_no_blend;
171 }
172
173 /* Update derived states. */
174 if (rctx->cb_misc_state.blend_colormask != blend->cb_target_mask) {
175 rctx->cb_misc_state.blend_colormask = blend->cb_target_mask;
176 update_cb = true;
177 }
178 if (rctx->b.chip_class <= R700 &&
179 rctx->cb_misc_state.cb_color_control != color_control) {
180 rctx->cb_misc_state.cb_color_control = color_control;
181 update_cb = true;
182 }
183 if (rctx->cb_misc_state.dual_src_blend != blend->dual_src_blend) {
184 rctx->cb_misc_state.dual_src_blend = blend->dual_src_blend;
185 update_cb = true;
186 }
187 if (update_cb) {
188 r600_mark_atom_dirty(rctx, &rctx->cb_misc_state.atom);
189 }
190 if (rctx->framebuffer.dual_src_blend != blend->dual_src_blend) {
191 rctx->framebuffer.dual_src_blend = blend->dual_src_blend;
192 r600_mark_atom_dirty(rctx, &rctx->framebuffer.atom);
193 }
194 }
195
196 static void r600_bind_blend_state(struct pipe_context *ctx, void *state)
197 {
198 struct r600_context *rctx = (struct r600_context *)ctx;
199 struct r600_blend_state *blend = (struct r600_blend_state *)state;
200
201 if (!blend) {
202 r600_set_cso_state_with_cb(rctx, &rctx->blend_state, NULL, NULL);
203 return;
204 }
205
206 r600_bind_blend_state_internal(rctx, blend, rctx->force_blend_disable);
207 }
208
209 static void r600_set_blend_color(struct pipe_context *ctx,
210 const struct pipe_blend_color *state)
211 {
212 struct r600_context *rctx = (struct r600_context *)ctx;
213
214 rctx->blend_color.state = *state;
215 r600_mark_atom_dirty(rctx, &rctx->blend_color.atom);
216 }
217
218 void r600_emit_blend_color(struct r600_context *rctx, struct r600_atom *atom)
219 {
220 struct radeon_winsys_cs *cs = rctx->b.gfx.cs;
221 struct pipe_blend_color *state = &rctx->blend_color.state;
222
223 radeon_set_context_reg_seq(cs, R_028414_CB_BLEND_RED, 4);
224 radeon_emit(cs, fui(state->color[0])); /* R_028414_CB_BLEND_RED */
225 radeon_emit(cs, fui(state->color[1])); /* R_028418_CB_BLEND_GREEN */
226 radeon_emit(cs, fui(state->color[2])); /* R_02841C_CB_BLEND_BLUE */
227 radeon_emit(cs, fui(state->color[3])); /* R_028420_CB_BLEND_ALPHA */
228 }
229
230 void r600_emit_vgt_state(struct r600_context *rctx, struct r600_atom *atom)
231 {
232 struct radeon_winsys_cs *cs = rctx->b.gfx.cs;
233 struct r600_vgt_state *a = (struct r600_vgt_state *)atom;
234
235 radeon_set_context_reg(cs, R_028A94_VGT_MULTI_PRIM_IB_RESET_EN, a->vgt_multi_prim_ib_reset_en);
236 radeon_set_context_reg_seq(cs, R_028408_VGT_INDX_OFFSET, 2);
237 radeon_emit(cs, a->vgt_indx_offset); /* R_028408_VGT_INDX_OFFSET */
238 radeon_emit(cs, a->vgt_multi_prim_ib_reset_indx); /* R_02840C_VGT_MULTI_PRIM_IB_RESET_INDX */
239 if (a->last_draw_was_indirect) {
240 a->last_draw_was_indirect = false;
241 radeon_set_ctl_const(cs, R_03CFF0_SQ_VTX_BASE_VTX_LOC, 0);
242 }
243 }
244
245 static void r600_set_clip_state(struct pipe_context *ctx,
246 const struct pipe_clip_state *state)
247 {
248 struct r600_context *rctx = (struct r600_context *)ctx;
249
250 rctx->clip_state.state = *state;
251 r600_mark_atom_dirty(rctx, &rctx->clip_state.atom);
252 rctx->driver_consts[PIPE_SHADER_VERTEX].vs_ucp_dirty = true;
253 }
254
255 static void r600_set_stencil_ref(struct pipe_context *ctx,
256 const struct r600_stencil_ref *state)
257 {
258 struct r600_context *rctx = (struct r600_context *)ctx;
259
260 rctx->stencil_ref.state = *state;
261 r600_mark_atom_dirty(rctx, &rctx->stencil_ref.atom);
262 }
263
264 void r600_emit_stencil_ref(struct r600_context *rctx, struct r600_atom *atom)
265 {
266 struct radeon_winsys_cs *cs = rctx->b.gfx.cs;
267 struct r600_stencil_ref_state *a = (struct r600_stencil_ref_state*)atom;
268
269 radeon_set_context_reg_seq(cs, R_028430_DB_STENCILREFMASK, 2);
270 radeon_emit(cs, /* R_028430_DB_STENCILREFMASK */
271 S_028430_STENCILREF(a->state.ref_value[0]) |
272 S_028430_STENCILMASK(a->state.valuemask[0]) |
273 S_028430_STENCILWRITEMASK(a->state.writemask[0]));
274 radeon_emit(cs, /* R_028434_DB_STENCILREFMASK_BF */
275 S_028434_STENCILREF_BF(a->state.ref_value[1]) |
276 S_028434_STENCILMASK_BF(a->state.valuemask[1]) |
277 S_028434_STENCILWRITEMASK_BF(a->state.writemask[1]));
278 }
279
280 static void r600_set_pipe_stencil_ref(struct pipe_context *ctx,
281 const struct pipe_stencil_ref *state)
282 {
283 struct r600_context *rctx = (struct r600_context *)ctx;
284 struct r600_dsa_state *dsa = (struct r600_dsa_state*)rctx->dsa_state.cso;
285 struct r600_stencil_ref ref;
286
287 rctx->stencil_ref.pipe_state = *state;
288
289 if (!dsa)
290 return;
291
292 ref.ref_value[0] = state->ref_value[0];
293 ref.ref_value[1] = state->ref_value[1];
294 ref.valuemask[0] = dsa->valuemask[0];
295 ref.valuemask[1] = dsa->valuemask[1];
296 ref.writemask[0] = dsa->writemask[0];
297 ref.writemask[1] = dsa->writemask[1];
298
299 r600_set_stencil_ref(ctx, &ref);
300 }
301
302 static void r600_bind_dsa_state(struct pipe_context *ctx, void *state)
303 {
304 struct r600_context *rctx = (struct r600_context *)ctx;
305 struct r600_dsa_state *dsa = state;
306 struct r600_stencil_ref ref;
307
308 if (!state) {
309 r600_set_cso_state_with_cb(rctx, &rctx->dsa_state, NULL, NULL);
310 return;
311 }
312
313 r600_set_cso_state_with_cb(rctx, &rctx->dsa_state, dsa, &dsa->buffer);
314
315 ref.ref_value[0] = rctx->stencil_ref.pipe_state.ref_value[0];
316 ref.ref_value[1] = rctx->stencil_ref.pipe_state.ref_value[1];
317 ref.valuemask[0] = dsa->valuemask[0];
318 ref.valuemask[1] = dsa->valuemask[1];
319 ref.writemask[0] = dsa->writemask[0];
320 ref.writemask[1] = dsa->writemask[1];
321 if (rctx->zwritemask != dsa->zwritemask) {
322 rctx->zwritemask = dsa->zwritemask;
323 if (rctx->b.chip_class >= EVERGREEN) {
324 /* work around some issue when not writing to zbuffer
325 * we are having lockup on evergreen so do not enable
326 * hyperz when not writing zbuffer
327 */
328 r600_mark_atom_dirty(rctx, &rctx->db_misc_state.atom);
329 }
330 }
331
332 r600_set_stencil_ref(ctx, &ref);
333
334 /* Update alphatest state. */
335 if (rctx->alphatest_state.sx_alpha_test_control != dsa->sx_alpha_test_control ||
336 rctx->alphatest_state.sx_alpha_ref != dsa->alpha_ref) {
337 rctx->alphatest_state.sx_alpha_test_control = dsa->sx_alpha_test_control;
338 rctx->alphatest_state.sx_alpha_ref = dsa->alpha_ref;
339 r600_mark_atom_dirty(rctx, &rctx->alphatest_state.atom);
340 }
341 }
342
343 static void r600_bind_rs_state(struct pipe_context *ctx, void *state)
344 {
345 struct r600_rasterizer_state *rs = (struct r600_rasterizer_state *)state;
346 struct r600_context *rctx = (struct r600_context *)ctx;
347
348 if (!state)
349 return;
350
351 rctx->rasterizer = rs;
352
353 r600_set_cso_state_with_cb(rctx, &rctx->rasterizer_state, rs, &rs->buffer);
354
355 if (rs->offset_enable &&
356 (rs->offset_units != rctx->poly_offset_state.offset_units ||
357 rs->offset_scale != rctx->poly_offset_state.offset_scale ||
358 rs->offset_units_unscaled != rctx->poly_offset_state.offset_units_unscaled)) {
359 rctx->poly_offset_state.offset_units = rs->offset_units;
360 rctx->poly_offset_state.offset_scale = rs->offset_scale;
361 rctx->poly_offset_state.offset_units_unscaled = rs->offset_units_unscaled;
362 r600_mark_atom_dirty(rctx, &rctx->poly_offset_state.atom);
363 }
364
365 /* Update clip_misc_state. */
366 if (rctx->clip_misc_state.pa_cl_clip_cntl != rs->pa_cl_clip_cntl ||
367 rctx->clip_misc_state.clip_plane_enable != rs->clip_plane_enable) {
368 rctx->clip_misc_state.pa_cl_clip_cntl = rs->pa_cl_clip_cntl;
369 rctx->clip_misc_state.clip_plane_enable = rs->clip_plane_enable;
370 r600_mark_atom_dirty(rctx, &rctx->clip_misc_state.atom);
371 }
372
373 r600_viewport_set_rast_deps(&rctx->b, rs->scissor_enable, rs->clip_halfz);
374
375 /* Re-emit PA_SC_LINE_STIPPLE. */
376 rctx->last_primitive_type = -1;
377 }
378
379 static void r600_delete_rs_state(struct pipe_context *ctx, void *state)
380 {
381 struct r600_rasterizer_state *rs = (struct r600_rasterizer_state *)state;
382
383 r600_release_command_buffer(&rs->buffer);
384 FREE(rs);
385 }
386
387 static void r600_sampler_view_destroy(struct pipe_context *ctx,
388 struct pipe_sampler_view *state)
389 {
390 struct r600_pipe_sampler_view *view = (struct r600_pipe_sampler_view *)state;
391
392 if (view->tex_resource->gpu_address &&
393 view->tex_resource->b.b.target == PIPE_BUFFER)
394 LIST_DELINIT(&view->list);
395
396 pipe_resource_reference(&state->texture, NULL);
397 FREE(view);
398 }
399
400 void r600_sampler_states_dirty(struct r600_context *rctx,
401 struct r600_sampler_states *state)
402 {
403 if (state->dirty_mask) {
404 if (state->dirty_mask & state->has_bordercolor_mask) {
405 rctx->b.flags |= R600_CONTEXT_WAIT_3D_IDLE;
406 }
407 state->atom.num_dw =
408 util_bitcount(state->dirty_mask & state->has_bordercolor_mask) * 11 +
409 util_bitcount(state->dirty_mask & ~state->has_bordercolor_mask) * 5;
410 r600_mark_atom_dirty(rctx, &state->atom);
411 }
412 }
413
414 static void r600_bind_sampler_states(struct pipe_context *pipe,
415 enum pipe_shader_type shader,
416 unsigned start,
417 unsigned count, void **states)
418 {
419 struct r600_context *rctx = (struct r600_context *)pipe;
420 struct r600_textures_info *dst = &rctx->samplers[shader];
421 struct r600_pipe_sampler_state **rstates = (struct r600_pipe_sampler_state**)states;
422 int seamless_cube_map = -1;
423 unsigned i;
424 /* This sets 1-bit for states with index >= count. */
425 uint32_t disable_mask = ~((1ull << count) - 1);
426 /* These are the new states set by this function. */
427 uint32_t new_mask = 0;
428
429 assert(start == 0); /* XXX fix below */
430
431 if (!states) {
432 disable_mask = ~0u;
433 count = 0;
434 }
435
436 for (i = 0; i < count; i++) {
437 struct r600_pipe_sampler_state *rstate = rstates[i];
438
439 if (rstate == dst->states.states[i]) {
440 continue;
441 }
442
443 if (rstate) {
444 if (rstate->border_color_use) {
445 dst->states.has_bordercolor_mask |= 1 << i;
446 } else {
447 dst->states.has_bordercolor_mask &= ~(1 << i);
448 }
449 seamless_cube_map = rstate->seamless_cube_map;
450
451 new_mask |= 1 << i;
452 } else {
453 disable_mask |= 1 << i;
454 }
455 }
456
457 memcpy(dst->states.states, rstates, sizeof(void*) * count);
458 memset(dst->states.states + count, 0, sizeof(void*) * (NUM_TEX_UNITS - count));
459
460 dst->states.enabled_mask &= ~disable_mask;
461 dst->states.dirty_mask &= dst->states.enabled_mask;
462 dst->states.enabled_mask |= new_mask;
463 dst->states.dirty_mask |= new_mask;
464 dst->states.has_bordercolor_mask &= dst->states.enabled_mask;
465
466 r600_sampler_states_dirty(rctx, &dst->states);
467
468 /* Seamless cubemap state. */
469 if (rctx->b.chip_class <= R700 &&
470 seamless_cube_map != -1 &&
471 seamless_cube_map != rctx->seamless_cube_map.enabled) {
472 /* change in TA_CNTL_AUX need a pipeline flush */
473 rctx->b.flags |= R600_CONTEXT_WAIT_3D_IDLE;
474 rctx->seamless_cube_map.enabled = seamless_cube_map;
475 r600_mark_atom_dirty(rctx, &rctx->seamless_cube_map.atom);
476 }
477 }
478
479 static void r600_delete_sampler_state(struct pipe_context *ctx, void *state)
480 {
481 free(state);
482 }
483
484 static void r600_delete_blend_state(struct pipe_context *ctx, void *state)
485 {
486 struct r600_context *rctx = (struct r600_context *)ctx;
487 struct r600_blend_state *blend = (struct r600_blend_state*)state;
488
489 if (rctx->blend_state.cso == state) {
490 ctx->bind_blend_state(ctx, NULL);
491 }
492
493 r600_release_command_buffer(&blend->buffer);
494 r600_release_command_buffer(&blend->buffer_no_blend);
495 FREE(blend);
496 }
497
498 static void r600_delete_dsa_state(struct pipe_context *ctx, void *state)
499 {
500 struct r600_context *rctx = (struct r600_context *)ctx;
501 struct r600_dsa_state *dsa = (struct r600_dsa_state *)state;
502
503 if (rctx->dsa_state.cso == state) {
504 ctx->bind_depth_stencil_alpha_state(ctx, NULL);
505 }
506
507 r600_release_command_buffer(&dsa->buffer);
508 free(dsa);
509 }
510
511 static void r600_bind_vertex_elements(struct pipe_context *ctx, void *state)
512 {
513 struct r600_context *rctx = (struct r600_context *)ctx;
514
515 r600_set_cso_state(rctx, &rctx->vertex_fetch_shader, state);
516 }
517
518 static void r600_delete_vertex_elements(struct pipe_context *ctx, void *state)
519 {
520 struct r600_fetch_shader *shader = (struct r600_fetch_shader*)state;
521 r600_resource_reference(&shader->buffer, NULL);
522 FREE(shader);
523 }
524
525 static void r600_set_index_buffer(struct pipe_context *ctx,
526 const struct pipe_index_buffer *ib)
527 {
528 struct r600_context *rctx = (struct r600_context *)ctx;
529
530 if (ib) {
531 pipe_resource_reference(&rctx->index_buffer.buffer, ib->buffer);
532 memcpy(&rctx->index_buffer, ib, sizeof(*ib));
533 r600_context_add_resource_size(ctx, ib->buffer);
534 } else {
535 pipe_resource_reference(&rctx->index_buffer.buffer, NULL);
536 }
537 }
538
539 void r600_vertex_buffers_dirty(struct r600_context *rctx)
540 {
541 if (rctx->vertex_buffer_state.dirty_mask) {
542 rctx->vertex_buffer_state.atom.num_dw = (rctx->b.chip_class >= EVERGREEN ? 12 : 11) *
543 util_bitcount(rctx->vertex_buffer_state.dirty_mask);
544 r600_mark_atom_dirty(rctx, &rctx->vertex_buffer_state.atom);
545 }
546 }
547
548 static void r600_set_vertex_buffers(struct pipe_context *ctx,
549 unsigned start_slot, unsigned count,
550 const struct pipe_vertex_buffer *input)
551 {
552 struct r600_context *rctx = (struct r600_context *)ctx;
553 struct r600_vertexbuf_state *state = &rctx->vertex_buffer_state;
554 struct pipe_vertex_buffer *vb = state->vb + start_slot;
555 unsigned i;
556 uint32_t disable_mask = 0;
557 /* These are the new buffers set by this function. */
558 uint32_t new_buffer_mask = 0;
559
560 /* Set vertex buffers. */
561 if (input) {
562 for (i = 0; i < count; i++) {
563 if (memcmp(&input[i], &vb[i], sizeof(struct pipe_vertex_buffer))) {
564 if (input[i].buffer) {
565 vb[i].stride = input[i].stride;
566 vb[i].buffer_offset = input[i].buffer_offset;
567 pipe_resource_reference(&vb[i].buffer, input[i].buffer);
568 new_buffer_mask |= 1 << i;
569 r600_context_add_resource_size(ctx, input[i].buffer);
570 } else {
571 pipe_resource_reference(&vb[i].buffer, NULL);
572 disable_mask |= 1 << i;
573 }
574 }
575 }
576 } else {
577 for (i = 0; i < count; i++) {
578 pipe_resource_reference(&vb[i].buffer, NULL);
579 }
580 disable_mask = ((1ull << count) - 1);
581 }
582
583 disable_mask <<= start_slot;
584 new_buffer_mask <<= start_slot;
585
586 rctx->vertex_buffer_state.enabled_mask &= ~disable_mask;
587 rctx->vertex_buffer_state.dirty_mask &= rctx->vertex_buffer_state.enabled_mask;
588 rctx->vertex_buffer_state.enabled_mask |= new_buffer_mask;
589 rctx->vertex_buffer_state.dirty_mask |= new_buffer_mask;
590
591 r600_vertex_buffers_dirty(rctx);
592 }
593
594 void r600_sampler_views_dirty(struct r600_context *rctx,
595 struct r600_samplerview_state *state)
596 {
597 if (state->dirty_mask) {
598 state->atom.num_dw = (rctx->b.chip_class >= EVERGREEN ? 14 : 13) *
599 util_bitcount(state->dirty_mask);
600 r600_mark_atom_dirty(rctx, &state->atom);
601 }
602 }
603
604 static void r600_set_sampler_views(struct pipe_context *pipe,
605 enum pipe_shader_type shader,
606 unsigned start, unsigned count,
607 struct pipe_sampler_view **views)
608 {
609 struct r600_context *rctx = (struct r600_context *) pipe;
610 struct r600_textures_info *dst = &rctx->samplers[shader];
611 struct r600_pipe_sampler_view **rviews = (struct r600_pipe_sampler_view **)views;
612 uint32_t dirty_sampler_states_mask = 0;
613 unsigned i;
614 /* This sets 1-bit for textures with index >= count. */
615 uint32_t disable_mask = ~((1ull << count) - 1);
616 /* These are the new textures set by this function. */
617 uint32_t new_mask = 0;
618
619 /* Set textures with index >= count to NULL. */
620 uint32_t remaining_mask;
621
622 assert(start == 0); /* XXX fix below */
623
624 if (!views) {
625 disable_mask = ~0u;
626 count = 0;
627 }
628
629 remaining_mask = dst->views.enabled_mask & disable_mask;
630
631 while (remaining_mask) {
632 i = u_bit_scan(&remaining_mask);
633 assert(dst->views.views[i]);
634
635 pipe_sampler_view_reference((struct pipe_sampler_view **)&dst->views.views[i], NULL);
636 }
637
638 for (i = 0; i < count; i++) {
639 if (rviews[i] == dst->views.views[i]) {
640 continue;
641 }
642
643 if (rviews[i]) {
644 struct r600_texture *rtex =
645 (struct r600_texture*)rviews[i]->base.texture;
646 bool is_buffer = rviews[i]->base.texture->target == PIPE_BUFFER;
647
648 if (!is_buffer && rtex->db_compatible) {
649 dst->views.compressed_depthtex_mask |= 1 << i;
650 } else {
651 dst->views.compressed_depthtex_mask &= ~(1 << i);
652 }
653
654 /* Track compressed colorbuffers. */
655 if (!is_buffer && rtex->cmask.size) {
656 dst->views.compressed_colortex_mask |= 1 << i;
657 } else {
658 dst->views.compressed_colortex_mask &= ~(1 << i);
659 }
660
661 /* Changing from array to non-arrays textures and vice versa requires
662 * updating TEX_ARRAY_OVERRIDE in sampler states on R6xx-R7xx. */
663 if (rctx->b.chip_class <= R700 &&
664 (dst->states.enabled_mask & (1 << i)) &&
665 (rviews[i]->base.texture->target == PIPE_TEXTURE_1D_ARRAY ||
666 rviews[i]->base.texture->target == PIPE_TEXTURE_2D_ARRAY) != dst->is_array_sampler[i]) {
667 dirty_sampler_states_mask |= 1 << i;
668 }
669
670 pipe_sampler_view_reference((struct pipe_sampler_view **)&dst->views.views[i], views[i]);
671 new_mask |= 1 << i;
672 r600_context_add_resource_size(pipe, views[i]->texture);
673 } else {
674 pipe_sampler_view_reference((struct pipe_sampler_view **)&dst->views.views[i], NULL);
675 disable_mask |= 1 << i;
676 }
677 }
678
679 dst->views.enabled_mask &= ~disable_mask;
680 dst->views.dirty_mask &= dst->views.enabled_mask;
681 dst->views.enabled_mask |= new_mask;
682 dst->views.dirty_mask |= new_mask;
683 dst->views.compressed_depthtex_mask &= dst->views.enabled_mask;
684 dst->views.compressed_colortex_mask &= dst->views.enabled_mask;
685 dst->views.dirty_buffer_constants = TRUE;
686 r600_sampler_views_dirty(rctx, &dst->views);
687
688 if (dirty_sampler_states_mask) {
689 dst->states.dirty_mask |= dirty_sampler_states_mask;
690 r600_sampler_states_dirty(rctx, &dst->states);
691 }
692 }
693
694 static void r600_update_compressed_colortex_mask(struct r600_samplerview_state *views)
695 {
696 uint32_t mask = views->enabled_mask;
697
698 while (mask) {
699 unsigned i = u_bit_scan(&mask);
700 struct pipe_resource *res = views->views[i]->base.texture;
701
702 if (res && res->target != PIPE_BUFFER) {
703 struct r600_texture *rtex = (struct r600_texture *)res;
704
705 if (rtex->cmask.size) {
706 views->compressed_colortex_mask |= 1 << i;
707 } else {
708 views->compressed_colortex_mask &= ~(1 << i);
709 }
710 }
711 }
712 }
713
714 /* Compute the key for the hw shader variant */
715 static inline union r600_shader_key r600_shader_selector_key(struct pipe_context * ctx,
716 struct r600_pipe_shader_selector * sel)
717 {
718 struct r600_context *rctx = (struct r600_context *)ctx;
719 union r600_shader_key key;
720 memset(&key, 0, sizeof(key));
721
722 switch (sel->type) {
723 case PIPE_SHADER_VERTEX: {
724 key.vs.as_ls = (rctx->tes_shader != NULL);
725 if (!key.vs.as_ls)
726 key.vs.as_es = (rctx->gs_shader != NULL);
727
728 if (rctx->ps_shader->current->shader.gs_prim_id_input && !rctx->gs_shader) {
729 key.vs.as_gs_a = true;
730 key.vs.prim_id_out = rctx->ps_shader->current->shader.input[rctx->ps_shader->current->shader.ps_prim_id_input].spi_sid;
731 }
732 break;
733 }
734 case PIPE_SHADER_GEOMETRY:
735 break;
736 case PIPE_SHADER_FRAGMENT: {
737 key.ps.color_two_side = rctx->rasterizer && rctx->rasterizer->two_side;
738 key.ps.alpha_to_one = rctx->alpha_to_one &&
739 rctx->rasterizer && rctx->rasterizer->multisample_enable &&
740 !rctx->framebuffer.cb0_is_integer;
741 key.ps.nr_cbufs = rctx->framebuffer.state.nr_cbufs;
742 /* Dual-source blending only makes sense with nr_cbufs == 1. */
743 if (key.ps.nr_cbufs == 1 && rctx->dual_src_blend)
744 key.ps.nr_cbufs = 2;
745 break;
746 }
747 case PIPE_SHADER_TESS_EVAL:
748 key.tes.as_es = (rctx->gs_shader != NULL);
749 break;
750 case PIPE_SHADER_TESS_CTRL:
751 key.tcs.prim_mode = rctx->tes_shader->info.properties[TGSI_PROPERTY_TES_PRIM_MODE];
752 break;
753 default:
754 assert(0);
755 }
756
757 return key;
758 }
759
760 /* Select the hw shader variant depending on the current state.
761 * (*dirty) is set to 1 if current variant was changed */
762 static int r600_shader_select(struct pipe_context *ctx,
763 struct r600_pipe_shader_selector* sel,
764 bool *dirty)
765 {
766 union r600_shader_key key;
767 struct r600_pipe_shader * shader = NULL;
768 int r;
769
770 memset(&key, 0, sizeof(key));
771 key = r600_shader_selector_key(ctx, sel);
772
773 /* Check if we don't need to change anything.
774 * This path is also used for most shaders that don't need multiple
775 * variants, it will cost just a computation of the key and this
776 * test. */
777 if (likely(sel->current && memcmp(&sel->current->key, &key, sizeof(key)) == 0)) {
778 return 0;
779 }
780
781 /* lookup if we have other variants in the list */
782 if (sel->num_shaders > 1) {
783 struct r600_pipe_shader *p = sel->current, *c = p->next_variant;
784
785 while (c && memcmp(&c->key, &key, sizeof(key)) != 0) {
786 p = c;
787 c = c->next_variant;
788 }
789
790 if (c) {
791 p->next_variant = c->next_variant;
792 shader = c;
793 }
794 }
795
796 if (unlikely(!shader)) {
797 shader = CALLOC(1, sizeof(struct r600_pipe_shader));
798 shader->selector = sel;
799
800 r = r600_pipe_shader_create(ctx, shader, key);
801 if (unlikely(r)) {
802 R600_ERR("Failed to build shader variant (type=%u) %d\n",
803 sel->type, r);
804 sel->current = NULL;
805 FREE(shader);
806 return r;
807 }
808
809 /* We don't know the value of nr_ps_max_color_exports until we built
810 * at least one variant, so we may need to recompute the key after
811 * building first variant. */
812 if (sel->type == PIPE_SHADER_FRAGMENT &&
813 sel->num_shaders == 0) {
814 sel->nr_ps_max_color_exports = shader->shader.nr_ps_max_color_exports;
815 key = r600_shader_selector_key(ctx, sel);
816 }
817
818 memcpy(&shader->key, &key, sizeof(key));
819 sel->num_shaders++;
820 }
821
822 if (dirty)
823 *dirty = true;
824
825 shader->next_variant = sel->current;
826 sel->current = shader;
827
828 return 0;
829 }
830
831 static void *r600_create_shader_state(struct pipe_context *ctx,
832 const struct pipe_shader_state *state,
833 unsigned pipe_shader_type)
834 {
835 struct r600_pipe_shader_selector *sel = CALLOC_STRUCT(r600_pipe_shader_selector);
836 int i;
837
838 sel->type = pipe_shader_type;
839 sel->tokens = tgsi_dup_tokens(state->tokens);
840 sel->so = state->stream_output;
841 tgsi_scan_shader(state->tokens, &sel->info);
842
843 switch (pipe_shader_type) {
844 case PIPE_SHADER_GEOMETRY:
845 sel->gs_output_prim =
846 sel->info.properties[TGSI_PROPERTY_GS_OUTPUT_PRIM];
847 sel->gs_max_out_vertices =
848 sel->info.properties[TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES];
849 sel->gs_num_invocations =
850 sel->info.properties[TGSI_PROPERTY_GS_INVOCATIONS];
851 break;
852 case PIPE_SHADER_VERTEX:
853 case PIPE_SHADER_TESS_CTRL:
854 sel->lds_patch_outputs_written_mask = 0;
855 sel->lds_outputs_written_mask = 0;
856
857 for (i = 0; i < sel->info.num_outputs; i++) {
858 unsigned name = sel->info.output_semantic_name[i];
859 unsigned index = sel->info.output_semantic_index[i];
860
861 switch (name) {
862 case TGSI_SEMANTIC_TESSINNER:
863 case TGSI_SEMANTIC_TESSOUTER:
864 case TGSI_SEMANTIC_PATCH:
865 sel->lds_patch_outputs_written_mask |=
866 1llu << r600_get_lds_unique_index(name, index);
867 break;
868 default:
869 sel->lds_outputs_written_mask |=
870 1llu << r600_get_lds_unique_index(name, index);
871 }
872 }
873 break;
874 default:
875 break;
876 }
877
878 return sel;
879 }
880
881 static void *r600_create_ps_state(struct pipe_context *ctx,
882 const struct pipe_shader_state *state)
883 {
884 return r600_create_shader_state(ctx, state, PIPE_SHADER_FRAGMENT);
885 }
886
887 static void *r600_create_vs_state(struct pipe_context *ctx,
888 const struct pipe_shader_state *state)
889 {
890 return r600_create_shader_state(ctx, state, PIPE_SHADER_VERTEX);
891 }
892
893 static void *r600_create_gs_state(struct pipe_context *ctx,
894 const struct pipe_shader_state *state)
895 {
896 return r600_create_shader_state(ctx, state, PIPE_SHADER_GEOMETRY);
897 }
898
899 static void *r600_create_tcs_state(struct pipe_context *ctx,
900 const struct pipe_shader_state *state)
901 {
902 return r600_create_shader_state(ctx, state, PIPE_SHADER_TESS_CTRL);
903 }
904
905 static void *r600_create_tes_state(struct pipe_context *ctx,
906 const struct pipe_shader_state *state)
907 {
908 return r600_create_shader_state(ctx, state, PIPE_SHADER_TESS_EVAL);
909 }
910
911 static void r600_bind_ps_state(struct pipe_context *ctx, void *state)
912 {
913 struct r600_context *rctx = (struct r600_context *)ctx;
914
915 if (!state)
916 state = rctx->dummy_pixel_shader;
917
918 rctx->ps_shader = (struct r600_pipe_shader_selector *)state;
919 }
920
921 static struct tgsi_shader_info *r600_get_vs_info(struct r600_context *rctx)
922 {
923 if (rctx->gs_shader)
924 return &rctx->gs_shader->info;
925 else if (rctx->tes_shader)
926 return &rctx->tes_shader->info;
927 else if (rctx->vs_shader)
928 return &rctx->vs_shader->info;
929 else
930 return NULL;
931 }
932
933 static void r600_bind_vs_state(struct pipe_context *ctx, void *state)
934 {
935 struct r600_context *rctx = (struct r600_context *)ctx;
936
937 if (!state)
938 return;
939
940 rctx->vs_shader = (struct r600_pipe_shader_selector *)state;
941 r600_update_vs_writes_viewport_index(&rctx->b, r600_get_vs_info(rctx));
942 rctx->b.streamout.stride_in_dw = rctx->vs_shader->so.stride;
943 }
944
945 static void r600_bind_gs_state(struct pipe_context *ctx, void *state)
946 {
947 struct r600_context *rctx = (struct r600_context *)ctx;
948
949 rctx->gs_shader = (struct r600_pipe_shader_selector *)state;
950 r600_update_vs_writes_viewport_index(&rctx->b, r600_get_vs_info(rctx));
951
952 if (!state)
953 return;
954 rctx->b.streamout.stride_in_dw = rctx->gs_shader->so.stride;
955 }
956
957 static void r600_bind_tcs_state(struct pipe_context *ctx, void *state)
958 {
959 struct r600_context *rctx = (struct r600_context *)ctx;
960
961 rctx->tcs_shader = (struct r600_pipe_shader_selector *)state;
962 }
963
964 static void r600_bind_tes_state(struct pipe_context *ctx, void *state)
965 {
966 struct r600_context *rctx = (struct r600_context *)ctx;
967
968 rctx->tes_shader = (struct r600_pipe_shader_selector *)state;
969 r600_update_vs_writes_viewport_index(&rctx->b, r600_get_vs_info(rctx));
970
971 if (!state)
972 return;
973 rctx->b.streamout.stride_in_dw = rctx->tes_shader->so.stride;
974 }
975
976 static void r600_delete_shader_selector(struct pipe_context *ctx,
977 struct r600_pipe_shader_selector *sel)
978 {
979 struct r600_pipe_shader *p = sel->current, *c;
980 while (p) {
981 c = p->next_variant;
982 r600_pipe_shader_destroy(ctx, p);
983 free(p);
984 p = c;
985 }
986
987 free(sel->tokens);
988 free(sel);
989 }
990
991
992 static void r600_delete_ps_state(struct pipe_context *ctx, void *state)
993 {
994 struct r600_context *rctx = (struct r600_context *)ctx;
995 struct r600_pipe_shader_selector *sel = (struct r600_pipe_shader_selector *)state;
996
997 if (rctx->ps_shader == sel) {
998 rctx->ps_shader = NULL;
999 }
1000
1001 r600_delete_shader_selector(ctx, sel);
1002 }
1003
1004 static void r600_delete_vs_state(struct pipe_context *ctx, void *state)
1005 {
1006 struct r600_context *rctx = (struct r600_context *)ctx;
1007 struct r600_pipe_shader_selector *sel = (struct r600_pipe_shader_selector *)state;
1008
1009 if (rctx->vs_shader == sel) {
1010 rctx->vs_shader = NULL;
1011 }
1012
1013 r600_delete_shader_selector(ctx, sel);
1014 }
1015
1016
1017 static void r600_delete_gs_state(struct pipe_context *ctx, void *state)
1018 {
1019 struct r600_context *rctx = (struct r600_context *)ctx;
1020 struct r600_pipe_shader_selector *sel = (struct r600_pipe_shader_selector *)state;
1021
1022 if (rctx->gs_shader == sel) {
1023 rctx->gs_shader = NULL;
1024 }
1025
1026 r600_delete_shader_selector(ctx, sel);
1027 }
1028
1029 static void r600_delete_tcs_state(struct pipe_context *ctx, void *state)
1030 {
1031 struct r600_context *rctx = (struct r600_context *)ctx;
1032 struct r600_pipe_shader_selector *sel = (struct r600_pipe_shader_selector *)state;
1033
1034 if (rctx->tcs_shader == sel) {
1035 rctx->tcs_shader = NULL;
1036 }
1037
1038 r600_delete_shader_selector(ctx, sel);
1039 }
1040
1041 static void r600_delete_tes_state(struct pipe_context *ctx, void *state)
1042 {
1043 struct r600_context *rctx = (struct r600_context *)ctx;
1044 struct r600_pipe_shader_selector *sel = (struct r600_pipe_shader_selector *)state;
1045
1046 if (rctx->tes_shader == sel) {
1047 rctx->tes_shader = NULL;
1048 }
1049
1050 r600_delete_shader_selector(ctx, sel);
1051 }
1052
1053 void r600_constant_buffers_dirty(struct r600_context *rctx, struct r600_constbuf_state *state)
1054 {
1055 if (state->dirty_mask) {
1056 state->atom.num_dw = rctx->b.chip_class >= EVERGREEN ? util_bitcount(state->dirty_mask)*20
1057 : util_bitcount(state->dirty_mask)*19;
1058 r600_mark_atom_dirty(rctx, &state->atom);
1059 }
1060 }
1061
1062 static void r600_set_constant_buffer(struct pipe_context *ctx,
1063 enum pipe_shader_type shader, uint index,
1064 const struct pipe_constant_buffer *input)
1065 {
1066 struct r600_context *rctx = (struct r600_context *)ctx;
1067 struct r600_constbuf_state *state = &rctx->constbuf_state[shader];
1068 struct pipe_constant_buffer *cb;
1069 const uint8_t *ptr;
1070
1071 /* Note that the state tracker can unbind constant buffers by
1072 * passing NULL here.
1073 */
1074 if (unlikely(!input || (!input->buffer && !input->user_buffer))) {
1075 state->enabled_mask &= ~(1 << index);
1076 state->dirty_mask &= ~(1 << index);
1077 pipe_resource_reference(&state->cb[index].buffer, NULL);
1078 return;
1079 }
1080
1081 cb = &state->cb[index];
1082 cb->buffer_size = input->buffer_size;
1083
1084 ptr = input->user_buffer;
1085
1086 if (ptr) {
1087 /* Upload the user buffer. */
1088 if (R600_BIG_ENDIAN) {
1089 uint32_t *tmpPtr;
1090 unsigned i, size = input->buffer_size;
1091
1092 if (!(tmpPtr = malloc(size))) {
1093 R600_ERR("Failed to allocate BE swap buffer.\n");
1094 return;
1095 }
1096
1097 for (i = 0; i < size / 4; ++i) {
1098 tmpPtr[i] = util_cpu_to_le32(((uint32_t *)ptr)[i]);
1099 }
1100
1101 u_upload_data(ctx->stream_uploader, 0, size, 256,
1102 tmpPtr, &cb->buffer_offset, &cb->buffer);
1103 free(tmpPtr);
1104 } else {
1105 u_upload_data(ctx->stream_uploader, 0,
1106 input->buffer_size, 256, ptr,
1107 &cb->buffer_offset, &cb->buffer);
1108 }
1109 /* account it in gtt */
1110 rctx->b.gtt += input->buffer_size;
1111 } else {
1112 /* Setup the hw buffer. */
1113 cb->buffer_offset = input->buffer_offset;
1114 pipe_resource_reference(&cb->buffer, input->buffer);
1115 r600_context_add_resource_size(ctx, input->buffer);
1116 }
1117
1118 state->enabled_mask |= 1 << index;
1119 state->dirty_mask |= 1 << index;
1120 r600_constant_buffers_dirty(rctx, state);
1121 }
1122
1123 static void r600_set_sample_mask(struct pipe_context *pipe, unsigned sample_mask)
1124 {
1125 struct r600_context *rctx = (struct r600_context*)pipe;
1126
1127 if (rctx->sample_mask.sample_mask == (uint16_t)sample_mask)
1128 return;
1129
1130 rctx->sample_mask.sample_mask = sample_mask;
1131 r600_mark_atom_dirty(rctx, &rctx->sample_mask.atom);
1132 }
1133
1134 static void r600_update_driver_const_buffers(struct r600_context *rctx)
1135 {
1136 int sh, size;
1137 void *ptr;
1138 struct pipe_constant_buffer cb;
1139 for (sh = 0; sh < PIPE_SHADER_TYPES; sh++) {
1140 struct r600_shader_driver_constants_info *info = &rctx->driver_consts[sh];
1141 if (!info->vs_ucp_dirty &&
1142 !info->texture_const_dirty &&
1143 !info->ps_sample_pos_dirty)
1144 continue;
1145
1146 ptr = info->constants;
1147 size = info->alloc_size;
1148 if (info->vs_ucp_dirty) {
1149 assert(sh == PIPE_SHADER_VERTEX);
1150 if (!size) {
1151 ptr = rctx->clip_state.state.ucp;
1152 size = R600_UCP_SIZE;
1153 } else {
1154 memcpy(ptr, rctx->clip_state.state.ucp, R600_UCP_SIZE);
1155 }
1156 info->vs_ucp_dirty = false;
1157 }
1158
1159 if (info->ps_sample_pos_dirty) {
1160 assert(sh == PIPE_SHADER_FRAGMENT);
1161 if (!size) {
1162 ptr = rctx->sample_positions;
1163 size = R600_UCP_SIZE;
1164 } else {
1165 memcpy(ptr, rctx->sample_positions, R600_UCP_SIZE);
1166 }
1167 info->ps_sample_pos_dirty = false;
1168 }
1169
1170 if (info->texture_const_dirty) {
1171 assert (ptr);
1172 assert (size);
1173 if (sh == PIPE_SHADER_VERTEX)
1174 memcpy(ptr, rctx->clip_state.state.ucp, R600_UCP_SIZE);
1175 if (sh == PIPE_SHADER_FRAGMENT)
1176 memcpy(ptr, rctx->sample_positions, R600_UCP_SIZE);
1177 }
1178 info->texture_const_dirty = false;
1179
1180 cb.buffer = NULL;
1181 cb.user_buffer = ptr;
1182 cb.buffer_offset = 0;
1183 cb.buffer_size = size;
1184 rctx->b.b.set_constant_buffer(&rctx->b.b, sh, R600_BUFFER_INFO_CONST_BUFFER, &cb);
1185 pipe_resource_reference(&cb.buffer, NULL);
1186 }
1187 }
1188
1189 static void *r600_alloc_buf_consts(struct r600_context *rctx, int shader_type,
1190 int array_size, uint32_t *base_offset)
1191 {
1192 struct r600_shader_driver_constants_info *info = &rctx->driver_consts[shader_type];
1193 if (array_size + R600_UCP_SIZE > info->alloc_size) {
1194 info->constants = realloc(info->constants, array_size + R600_UCP_SIZE);
1195 info->alloc_size = array_size + R600_UCP_SIZE;
1196 }
1197 memset(info->constants + (R600_UCP_SIZE / 4), 0, array_size);
1198 info->texture_const_dirty = true;
1199 *base_offset = R600_UCP_SIZE;
1200 return info->constants;
1201 }
1202 /*
1203 * On r600/700 hw we don't have vertex fetch swizzle, though TBO
1204 * doesn't require full swizzles it does need masking and setting alpha
1205 * to one, so we setup a set of 5 constants with the masks + alpha value
1206 * then in the shader, we AND the 4 components with 0xffffffff or 0,
1207 * then OR the alpha with the value given here.
1208 * We use a 6th constant to store the txq buffer size in
1209 * we use 7th slot for number of cube layers in a cube map array.
1210 */
1211 static void r600_setup_buffer_constants(struct r600_context *rctx, int shader_type)
1212 {
1213 struct r600_textures_info *samplers = &rctx->samplers[shader_type];
1214 int bits;
1215 uint32_t array_size;
1216 int i, j;
1217 uint32_t *constants;
1218 uint32_t base_offset;
1219 if (!samplers->views.dirty_buffer_constants)
1220 return;
1221
1222 samplers->views.dirty_buffer_constants = FALSE;
1223
1224 bits = util_last_bit(samplers->views.enabled_mask);
1225 array_size = bits * 8 * sizeof(uint32_t) * 4;
1226
1227 constants = r600_alloc_buf_consts(rctx, shader_type, array_size, &base_offset);
1228
1229 for (i = 0; i < bits; i++) {
1230 if (samplers->views.enabled_mask & (1 << i)) {
1231 int offset = (base_offset / 4) + i * 8;
1232 const struct util_format_description *desc;
1233 desc = util_format_description(samplers->views.views[i]->base.format);
1234
1235 for (j = 0; j < 4; j++)
1236 if (j < desc->nr_channels)
1237 constants[offset+j] = 0xffffffff;
1238 else
1239 constants[offset+j] = 0x0;
1240 if (desc->nr_channels < 4) {
1241 if (desc->channel[0].pure_integer)
1242 constants[offset+4] = 1;
1243 else
1244 constants[offset+4] = fui(1.0);
1245 } else
1246 constants[offset + 4] = 0;
1247
1248 constants[offset + 5] = samplers->views.views[i]->base.texture->width0 / util_format_get_blocksize(samplers->views.views[i]->base.format);
1249 constants[offset + 6] = samplers->views.views[i]->base.texture->array_size / 6;
1250 }
1251 }
1252
1253 }
1254
1255 /* On evergreen we store two values
1256 * 1. buffer size for TXQ
1257 * 2. number of cube layers in a cube map array.
1258 */
1259 static void eg_setup_buffer_constants(struct r600_context *rctx, int shader_type)
1260 {
1261 struct r600_textures_info *samplers = &rctx->samplers[shader_type];
1262 int bits;
1263 uint32_t array_size;
1264 int i;
1265 uint32_t *constants;
1266 uint32_t base_offset;
1267 if (!samplers->views.dirty_buffer_constants)
1268 return;
1269
1270 samplers->views.dirty_buffer_constants = FALSE;
1271
1272 bits = util_last_bit(samplers->views.enabled_mask);
1273 array_size = bits * 2 * sizeof(uint32_t) * 4;
1274
1275 constants = r600_alloc_buf_consts(rctx, shader_type, array_size,
1276 &base_offset);
1277
1278 for (i = 0; i < bits; i++) {
1279 if (samplers->views.enabled_mask & (1 << i)) {
1280 uint32_t offset = (base_offset / 4) + i * 2;
1281 constants[offset] = samplers->views.views[i]->base.texture->width0 / util_format_get_blocksize(samplers->views.views[i]->base.format);
1282 constants[offset + 1] = samplers->views.views[i]->base.texture->array_size / 6;
1283 }
1284 }
1285 }
1286
1287 /* set sample xy locations as array of fragment shader constants */
1288 void r600_set_sample_locations_constant_buffer(struct r600_context *rctx)
1289 {
1290 int i;
1291 struct pipe_context *ctx = &rctx->b.b;
1292
1293 assert(rctx->framebuffer.nr_samples < R600_UCP_SIZE);
1294 assert(rctx->framebuffer.nr_samples <= ARRAY_SIZE(rctx->sample_positions)/4);
1295
1296 memset(rctx->sample_positions, 0, 4 * 4 * 16);
1297 for (i = 0; i < rctx->framebuffer.nr_samples; i++) {
1298 ctx->get_sample_position(ctx, rctx->framebuffer.nr_samples, i, &rctx->sample_positions[4*i]);
1299 /* Also fill in center-zeroed positions used for interpolateAtSample */
1300 rctx->sample_positions[4*i + 2] = rctx->sample_positions[4*i + 0] - 0.5f;
1301 rctx->sample_positions[4*i + 3] = rctx->sample_positions[4*i + 1] - 0.5f;
1302 }
1303
1304 rctx->driver_consts[PIPE_SHADER_FRAGMENT].ps_sample_pos_dirty = true;
1305 }
1306
1307 static void update_shader_atom(struct pipe_context *ctx,
1308 struct r600_shader_state *state,
1309 struct r600_pipe_shader *shader)
1310 {
1311 struct r600_context *rctx = (struct r600_context *)ctx;
1312
1313 state->shader = shader;
1314 if (shader) {
1315 state->atom.num_dw = shader->command_buffer.num_dw;
1316 r600_context_add_resource_size(ctx, (struct pipe_resource *)shader->bo);
1317 } else {
1318 state->atom.num_dw = 0;
1319 }
1320 r600_mark_atom_dirty(rctx, &state->atom);
1321 }
1322
1323 static void update_gs_block_state(struct r600_context *rctx, unsigned enable)
1324 {
1325 if (rctx->shader_stages.geom_enable != enable) {
1326 rctx->shader_stages.geom_enable = enable;
1327 r600_mark_atom_dirty(rctx, &rctx->shader_stages.atom);
1328 }
1329
1330 if (rctx->gs_rings.enable != enable) {
1331 rctx->gs_rings.enable = enable;
1332 r600_mark_atom_dirty(rctx, &rctx->gs_rings.atom);
1333
1334 if (enable && !rctx->gs_rings.esgs_ring.buffer) {
1335 unsigned size = 0x1C000;
1336 rctx->gs_rings.esgs_ring.buffer =
1337 pipe_buffer_create(rctx->b.b.screen, 0,
1338 PIPE_USAGE_DEFAULT, size);
1339 rctx->gs_rings.esgs_ring.buffer_size = size;
1340
1341 size = 0x4000000;
1342
1343 rctx->gs_rings.gsvs_ring.buffer =
1344 pipe_buffer_create(rctx->b.b.screen, 0,
1345 PIPE_USAGE_DEFAULT, size);
1346 rctx->gs_rings.gsvs_ring.buffer_size = size;
1347 }
1348
1349 if (enable) {
1350 r600_set_constant_buffer(&rctx->b.b, PIPE_SHADER_GEOMETRY,
1351 R600_GS_RING_CONST_BUFFER, &rctx->gs_rings.esgs_ring);
1352 if (rctx->tes_shader) {
1353 r600_set_constant_buffer(&rctx->b.b, PIPE_SHADER_TESS_EVAL,
1354 R600_GS_RING_CONST_BUFFER, &rctx->gs_rings.gsvs_ring);
1355 } else {
1356 r600_set_constant_buffer(&rctx->b.b, PIPE_SHADER_VERTEX,
1357 R600_GS_RING_CONST_BUFFER, &rctx->gs_rings.gsvs_ring);
1358 }
1359 } else {
1360 r600_set_constant_buffer(&rctx->b.b, PIPE_SHADER_GEOMETRY,
1361 R600_GS_RING_CONST_BUFFER, NULL);
1362 r600_set_constant_buffer(&rctx->b.b, PIPE_SHADER_VERTEX,
1363 R600_GS_RING_CONST_BUFFER, NULL);
1364 r600_set_constant_buffer(&rctx->b.b, PIPE_SHADER_TESS_EVAL,
1365 R600_GS_RING_CONST_BUFFER, NULL);
1366 }
1367 }
1368 }
1369
1370 static void r600_update_clip_state(struct r600_context *rctx,
1371 struct r600_pipe_shader *current)
1372 {
1373 if (current->pa_cl_vs_out_cntl != rctx->clip_misc_state.pa_cl_vs_out_cntl ||
1374 current->shader.clip_dist_write != rctx->clip_misc_state.clip_dist_write ||
1375 current->shader.vs_position_window_space != rctx->clip_misc_state.clip_disable ||
1376 current->shader.vs_out_viewport != rctx->clip_misc_state.vs_out_viewport) {
1377 rctx->clip_misc_state.pa_cl_vs_out_cntl = current->pa_cl_vs_out_cntl;
1378 rctx->clip_misc_state.clip_dist_write = current->shader.clip_dist_write;
1379 rctx->clip_misc_state.clip_disable = current->shader.vs_position_window_space;
1380 rctx->clip_misc_state.vs_out_viewport = current->shader.vs_out_viewport;
1381 r600_mark_atom_dirty(rctx, &rctx->clip_misc_state.atom);
1382 }
1383 }
1384
1385 static void r600_generate_fixed_func_tcs(struct r600_context *rctx)
1386 {
1387 struct ureg_src const0, const1;
1388 struct ureg_dst tessouter, tessinner;
1389 struct ureg_program *ureg = ureg_create(PIPE_SHADER_TESS_CTRL);
1390
1391 if (!ureg)
1392 return; /* if we get here, we're screwed */
1393
1394 assert(!rctx->fixed_func_tcs_shader);
1395
1396 ureg_DECL_constant2D(ureg, 0, 3, R600_LDS_INFO_CONST_BUFFER);
1397 const0 = ureg_src_dimension(ureg_src_register(TGSI_FILE_CONSTANT, 2),
1398 R600_LDS_INFO_CONST_BUFFER);
1399 const1 = ureg_src_dimension(ureg_src_register(TGSI_FILE_CONSTANT, 3),
1400 R600_LDS_INFO_CONST_BUFFER);
1401
1402 tessouter = ureg_DECL_output(ureg, TGSI_SEMANTIC_TESSOUTER, 0);
1403 tessinner = ureg_DECL_output(ureg, TGSI_SEMANTIC_TESSINNER, 0);
1404
1405 ureg_MOV(ureg, tessouter, const0);
1406 ureg_MOV(ureg, tessinner, const1);
1407 ureg_END(ureg);
1408
1409 rctx->fixed_func_tcs_shader =
1410 ureg_create_shader_and_destroy(ureg, &rctx->b.b);
1411 }
1412
1413 #define SELECT_SHADER_OR_FAIL(x) do { \
1414 r600_shader_select(ctx, rctx->x##_shader, &x##_dirty); \
1415 if (unlikely(!rctx->x##_shader->current)) \
1416 return false; \
1417 } while(0)
1418
1419 #define UPDATE_SHADER(hw, sw) do { \
1420 if (sw##_dirty || (rctx->hw_shader_stages[(hw)].shader != rctx->sw##_shader->current)) \
1421 update_shader_atom(ctx, &rctx->hw_shader_stages[(hw)], rctx->sw##_shader->current); \
1422 } while(0)
1423
1424 #define UPDATE_SHADER_CLIP(hw, sw) do { \
1425 if (sw##_dirty || (rctx->hw_shader_stages[(hw)].shader != rctx->sw##_shader->current)) { \
1426 update_shader_atom(ctx, &rctx->hw_shader_stages[(hw)], rctx->sw##_shader->current); \
1427 clip_so_current = rctx->sw##_shader->current; \
1428 } \
1429 } while(0)
1430
1431 #define UPDATE_SHADER_GS(hw, hw2, sw) do { \
1432 if (sw##_dirty || (rctx->hw_shader_stages[(hw)].shader != rctx->sw##_shader->current)) { \
1433 update_shader_atom(ctx, &rctx->hw_shader_stages[(hw)], rctx->sw##_shader->current); \
1434 update_shader_atom(ctx, &rctx->hw_shader_stages[(hw2)], rctx->sw##_shader->current->gs_copy_shader); \
1435 clip_so_current = rctx->sw##_shader->current->gs_copy_shader; \
1436 } \
1437 } while(0)
1438
1439 #define SET_NULL_SHADER(hw) do { \
1440 if (rctx->hw_shader_stages[(hw)].shader) \
1441 update_shader_atom(ctx, &rctx->hw_shader_stages[(hw)], NULL); \
1442 } while (0)
1443
1444 static bool r600_update_derived_state(struct r600_context *rctx)
1445 {
1446 struct pipe_context * ctx = (struct pipe_context*)rctx;
1447 bool ps_dirty = false, vs_dirty = false, gs_dirty = false;
1448 bool tcs_dirty = false, tes_dirty = false, fixed_func_tcs_dirty = false;
1449 bool blend_disable;
1450 bool need_buf_const;
1451 struct r600_pipe_shader *clip_so_current = NULL;
1452
1453 if (!rctx->blitter->running) {
1454 unsigned i;
1455 unsigned counter;
1456
1457 counter = p_atomic_read(&rctx->screen->b.compressed_colortex_counter);
1458 if (counter != rctx->b.last_compressed_colortex_counter) {
1459 rctx->b.last_compressed_colortex_counter = counter;
1460
1461 for (i = 0; i < PIPE_SHADER_TYPES; ++i) {
1462 r600_update_compressed_colortex_mask(&rctx->samplers[i].views);
1463 }
1464 }
1465
1466 /* Decompress textures if needed. */
1467 for (i = 0; i < PIPE_SHADER_TYPES; i++) {
1468 struct r600_samplerview_state *views = &rctx->samplers[i].views;
1469 if (views->compressed_depthtex_mask) {
1470 r600_decompress_depth_textures(rctx, views);
1471 }
1472 if (views->compressed_colortex_mask) {
1473 r600_decompress_color_textures(rctx, views);
1474 }
1475 }
1476 }
1477
1478 SELECT_SHADER_OR_FAIL(ps);
1479
1480 r600_mark_atom_dirty(rctx, &rctx->shader_stages.atom);
1481
1482 update_gs_block_state(rctx, rctx->gs_shader != NULL);
1483
1484 if (rctx->gs_shader)
1485 SELECT_SHADER_OR_FAIL(gs);
1486
1487 /* Hull Shader */
1488 if (rctx->tcs_shader) {
1489 SELECT_SHADER_OR_FAIL(tcs);
1490
1491 UPDATE_SHADER(EG_HW_STAGE_HS, tcs);
1492 } else if (rctx->tes_shader) {
1493 if (!rctx->fixed_func_tcs_shader) {
1494 r600_generate_fixed_func_tcs(rctx);
1495 if (!rctx->fixed_func_tcs_shader)
1496 return false;
1497
1498 }
1499 SELECT_SHADER_OR_FAIL(fixed_func_tcs);
1500
1501 UPDATE_SHADER(EG_HW_STAGE_HS, fixed_func_tcs);
1502 } else
1503 SET_NULL_SHADER(EG_HW_STAGE_HS);
1504
1505 if (rctx->tes_shader) {
1506 SELECT_SHADER_OR_FAIL(tes);
1507 }
1508
1509 SELECT_SHADER_OR_FAIL(vs);
1510
1511 if (rctx->gs_shader) {
1512 if (!rctx->shader_stages.geom_enable) {
1513 rctx->shader_stages.geom_enable = true;
1514 r600_mark_atom_dirty(rctx, &rctx->shader_stages.atom);
1515 }
1516
1517 /* gs_shader provides GS and VS (copy shader) */
1518 UPDATE_SHADER_GS(R600_HW_STAGE_GS, R600_HW_STAGE_VS, gs);
1519
1520 /* vs_shader is used as ES */
1521
1522 if (rctx->tes_shader) {
1523 /* VS goes to LS, TES goes to ES */
1524 UPDATE_SHADER(R600_HW_STAGE_ES, tes);
1525 UPDATE_SHADER(EG_HW_STAGE_LS, vs);
1526 } else {
1527 /* vs_shader is used as ES */
1528 UPDATE_SHADER(R600_HW_STAGE_ES, vs);
1529 SET_NULL_SHADER(EG_HW_STAGE_LS);
1530 }
1531 } else {
1532 if (unlikely(rctx->hw_shader_stages[R600_HW_STAGE_GS].shader)) {
1533 SET_NULL_SHADER(R600_HW_STAGE_GS);
1534 SET_NULL_SHADER(R600_HW_STAGE_ES);
1535 rctx->shader_stages.geom_enable = false;
1536 r600_mark_atom_dirty(rctx, &rctx->shader_stages.atom);
1537 }
1538
1539 if (rctx->tes_shader) {
1540 /* if TES is loaded and no geometry, TES runs on hw VS, VS runs on hw LS */
1541 UPDATE_SHADER_CLIP(R600_HW_STAGE_VS, tes);
1542 UPDATE_SHADER(EG_HW_STAGE_LS, vs);
1543 } else {
1544 SET_NULL_SHADER(EG_HW_STAGE_LS);
1545 UPDATE_SHADER_CLIP(R600_HW_STAGE_VS, vs);
1546 }
1547 }
1548
1549 /* Update clip misc state. */
1550 if (clip_so_current) {
1551 r600_update_clip_state(rctx, clip_so_current);
1552 rctx->b.streamout.enabled_stream_buffers_mask = clip_so_current->enabled_stream_buffers_mask;
1553 }
1554
1555 if (unlikely(ps_dirty || rctx->hw_shader_stages[R600_HW_STAGE_PS].shader != rctx->ps_shader->current ||
1556 rctx->rasterizer->sprite_coord_enable != rctx->ps_shader->current->sprite_coord_enable ||
1557 rctx->rasterizer->flatshade != rctx->ps_shader->current->flatshade)) {
1558
1559 if (rctx->cb_misc_state.nr_ps_color_outputs != rctx->ps_shader->current->nr_ps_color_outputs) {
1560 rctx->cb_misc_state.nr_ps_color_outputs = rctx->ps_shader->current->nr_ps_color_outputs;
1561 r600_mark_atom_dirty(rctx, &rctx->cb_misc_state.atom);
1562 }
1563
1564 if (rctx->b.chip_class <= R700) {
1565 bool multiwrite = rctx->ps_shader->current->shader.fs_write_all;
1566
1567 if (rctx->cb_misc_state.multiwrite != multiwrite) {
1568 rctx->cb_misc_state.multiwrite = multiwrite;
1569 r600_mark_atom_dirty(rctx, &rctx->cb_misc_state.atom);
1570 }
1571 }
1572
1573 if (unlikely(!ps_dirty && rctx->ps_shader && rctx->rasterizer &&
1574 ((rctx->rasterizer->sprite_coord_enable != rctx->ps_shader->current->sprite_coord_enable) ||
1575 (rctx->rasterizer->flatshade != rctx->ps_shader->current->flatshade)))) {
1576
1577 if (rctx->b.chip_class >= EVERGREEN)
1578 evergreen_update_ps_state(ctx, rctx->ps_shader->current);
1579 else
1580 r600_update_ps_state(ctx, rctx->ps_shader->current);
1581 }
1582
1583 r600_mark_atom_dirty(rctx, &rctx->shader_stages.atom);
1584 }
1585 UPDATE_SHADER(R600_HW_STAGE_PS, ps);
1586
1587 if (rctx->b.chip_class >= EVERGREEN) {
1588 evergreen_update_db_shader_control(rctx);
1589 } else {
1590 r600_update_db_shader_control(rctx);
1591 }
1592
1593 /* on R600 we stuff masks + txq info into one constant buffer */
1594 /* on evergreen we only need a txq info one */
1595 if (rctx->ps_shader) {
1596 need_buf_const = rctx->ps_shader->current->shader.uses_tex_buffers || rctx->ps_shader->current->shader.has_txq_cube_array_z_comp;
1597 if (need_buf_const) {
1598 if (rctx->b.chip_class < EVERGREEN)
1599 r600_setup_buffer_constants(rctx, PIPE_SHADER_FRAGMENT);
1600 else
1601 eg_setup_buffer_constants(rctx, PIPE_SHADER_FRAGMENT);
1602 }
1603 }
1604
1605 if (rctx->vs_shader) {
1606 need_buf_const = rctx->vs_shader->current->shader.uses_tex_buffers || rctx->vs_shader->current->shader.has_txq_cube_array_z_comp;
1607 if (need_buf_const) {
1608 if (rctx->b.chip_class < EVERGREEN)
1609 r600_setup_buffer_constants(rctx, PIPE_SHADER_VERTEX);
1610 else
1611 eg_setup_buffer_constants(rctx, PIPE_SHADER_VERTEX);
1612 }
1613 }
1614
1615 if (rctx->gs_shader) {
1616 need_buf_const = rctx->gs_shader->current->shader.uses_tex_buffers || rctx->gs_shader->current->shader.has_txq_cube_array_z_comp;
1617 if (need_buf_const) {
1618 if (rctx->b.chip_class < EVERGREEN)
1619 r600_setup_buffer_constants(rctx, PIPE_SHADER_GEOMETRY);
1620 else
1621 eg_setup_buffer_constants(rctx, PIPE_SHADER_GEOMETRY);
1622 }
1623 }
1624
1625 r600_update_driver_const_buffers(rctx);
1626
1627 if (rctx->b.chip_class < EVERGREEN && rctx->ps_shader && rctx->vs_shader) {
1628 if (!r600_adjust_gprs(rctx)) {
1629 /* discard rendering */
1630 return false;
1631 }
1632 }
1633
1634 if (rctx->b.chip_class == EVERGREEN) {
1635 if (!evergreen_adjust_gprs(rctx)) {
1636 /* discard rendering */
1637 return false;
1638 }
1639 }
1640
1641 blend_disable = (rctx->dual_src_blend &&
1642 rctx->ps_shader->current->nr_ps_color_outputs < 2);
1643
1644 if (blend_disable != rctx->force_blend_disable) {
1645 rctx->force_blend_disable = blend_disable;
1646 r600_bind_blend_state_internal(rctx,
1647 rctx->blend_state.cso,
1648 blend_disable);
1649 }
1650
1651 return true;
1652 }
1653
1654 void r600_emit_clip_misc_state(struct r600_context *rctx, struct r600_atom *atom)
1655 {
1656 struct radeon_winsys_cs *cs = rctx->b.gfx.cs;
1657 struct r600_clip_misc_state *state = &rctx->clip_misc_state;
1658
1659 radeon_set_context_reg(cs, R_028810_PA_CL_CLIP_CNTL,
1660 state->pa_cl_clip_cntl |
1661 (state->clip_dist_write ? 0 : state->clip_plane_enable & 0x3F) |
1662 S_028810_CLIP_DISABLE(state->clip_disable));
1663 radeon_set_context_reg(cs, R_02881C_PA_CL_VS_OUT_CNTL,
1664 state->pa_cl_vs_out_cntl |
1665 (state->clip_plane_enable & state->clip_dist_write));
1666 /* reuse needs to be set off if we write oViewport */
1667 if (rctx->b.chip_class >= EVERGREEN)
1668 radeon_set_context_reg(cs, R_028AB4_VGT_REUSE_OFF,
1669 S_028AB4_REUSE_OFF(state->vs_out_viewport));
1670 }
1671
1672 static void r600_draw_vbo(struct pipe_context *ctx, const struct pipe_draw_info *dinfo)
1673 {
1674 struct r600_context *rctx = (struct r600_context *)ctx;
1675 struct pipe_draw_info info = *dinfo;
1676 struct pipe_index_buffer ib = {};
1677 struct radeon_winsys_cs *cs = rctx->b.gfx.cs;
1678 bool render_cond_bit = rctx->b.render_cond && !rctx->b.render_cond_force_off;
1679 uint64_t mask;
1680 unsigned num_patches, dirty_tex_counter;
1681
1682 if (!info.indirect && !info.count && (info.indexed || !info.count_from_stream_output)) {
1683 return;
1684 }
1685
1686 if (!rctx->vs_shader || !rctx->ps_shader) {
1687 assert(0);
1688 return;
1689 }
1690
1691 /* make sure that the gfx ring is only one active */
1692 if (radeon_emitted(rctx->b.dma.cs, 0)) {
1693 rctx->b.dma.flush(rctx, RADEON_FLUSH_ASYNC, NULL);
1694 }
1695
1696 /* Re-emit the framebuffer state if needed. */
1697 dirty_tex_counter = p_atomic_read(&rctx->b.screen->dirty_tex_counter);
1698 if (dirty_tex_counter != rctx->b.last_dirty_tex_counter) {
1699 rctx->b.last_dirty_tex_counter = dirty_tex_counter;
1700 r600_mark_atom_dirty(rctx, &rctx->framebuffer.atom);
1701 }
1702
1703 if (!r600_update_derived_state(rctx)) {
1704 /* useless to render because current rendering command
1705 * can't be achieved
1706 */
1707 return;
1708 }
1709
1710 if (info.indexed) {
1711 /* Initialize the index buffer struct. */
1712 pipe_resource_reference(&ib.buffer, rctx->index_buffer.buffer);
1713 ib.user_buffer = rctx->index_buffer.user_buffer;
1714 ib.index_size = rctx->index_buffer.index_size;
1715 ib.offset = rctx->index_buffer.offset;
1716 if (!info.indirect) {
1717 ib.offset += info.start * ib.index_size;
1718 }
1719
1720 /* Translate 8-bit indices to 16-bit. */
1721 if (unlikely(ib.index_size == 1)) {
1722 struct pipe_resource *out_buffer = NULL;
1723 unsigned out_offset;
1724 void *ptr;
1725 unsigned start, count;
1726
1727 if (likely(!info.indirect)) {
1728 start = 0;
1729 count = info.count;
1730 }
1731 else {
1732 /* Have to get start/count from indirect buffer, slow path ahead... */
1733 struct r600_resource *indirect_resource = (struct r600_resource *)info.indirect;
1734 unsigned *data = r600_buffer_map_sync_with_rings(&rctx->b, indirect_resource,
1735 PIPE_TRANSFER_READ);
1736 if (data) {
1737 data += info.indirect_offset / sizeof(unsigned);
1738 start = data[2] * ib.index_size;
1739 count = data[0];
1740 }
1741 else {
1742 start = 0;
1743 count = 0;
1744 }
1745 }
1746
1747 u_upload_alloc(ctx->stream_uploader, start, count * 2,
1748 256, &out_offset, &out_buffer, &ptr);
1749
1750 util_shorten_ubyte_elts_to_userptr(
1751 &rctx->b.b, &ib, 0, 0, ib.offset + start, count, ptr);
1752
1753 pipe_resource_reference(&ib.buffer, NULL);
1754 ib.user_buffer = NULL;
1755 ib.buffer = out_buffer;
1756 ib.offset = out_offset;
1757 ib.index_size = 2;
1758 }
1759
1760 /* Upload the index buffer.
1761 * The upload is skipped for small index counts on little-endian machines
1762 * and the indices are emitted via PKT3_DRAW_INDEX_IMMD.
1763 * Indirect draws never use immediate indices.
1764 * Note: Instanced rendering in combination with immediate indices hangs. */
1765 if (ib.user_buffer && (R600_BIG_ENDIAN || info.indirect ||
1766 info.instance_count > 1 ||
1767 info.count*ib.index_size > 20)) {
1768 u_upload_data(ctx->stream_uploader, 0,
1769 info.count * ib.index_size, 256,
1770 ib.user_buffer, &ib.offset, &ib.buffer);
1771 ib.user_buffer = NULL;
1772 }
1773 } else {
1774 info.index_bias = info.start;
1775 }
1776
1777 /* Set the index offset and primitive restart. */
1778 if (rctx->vgt_state.vgt_multi_prim_ib_reset_en != info.primitive_restart ||
1779 rctx->vgt_state.vgt_multi_prim_ib_reset_indx != info.restart_index ||
1780 rctx->vgt_state.vgt_indx_offset != info.index_bias ||
1781 (rctx->vgt_state.last_draw_was_indirect && !info.indirect)) {
1782 rctx->vgt_state.vgt_multi_prim_ib_reset_en = info.primitive_restart;
1783 rctx->vgt_state.vgt_multi_prim_ib_reset_indx = info.restart_index;
1784 rctx->vgt_state.vgt_indx_offset = info.index_bias;
1785 r600_mark_atom_dirty(rctx, &rctx->vgt_state.atom);
1786 }
1787
1788 /* Workaround for hardware deadlock on certain R600 ASICs: write into a CB register. */
1789 if (rctx->b.chip_class == R600) {
1790 rctx->b.flags |= R600_CONTEXT_PS_PARTIAL_FLUSH;
1791 r600_mark_atom_dirty(rctx, &rctx->cb_misc_state.atom);
1792 }
1793
1794 if (rctx->b.chip_class >= EVERGREEN)
1795 evergreen_setup_tess_constants(rctx, &info, &num_patches);
1796
1797 /* Emit states. */
1798 r600_need_cs_space(rctx, ib.user_buffer ? 5 : 0, TRUE);
1799 r600_flush_emit(rctx);
1800
1801 mask = rctx->dirty_atoms;
1802 while (mask != 0) {
1803 r600_emit_atom(rctx, rctx->atoms[u_bit_scan64(&mask)]);
1804 }
1805
1806 if (rctx->b.chip_class == CAYMAN) {
1807 /* Copied from radeonsi. */
1808 unsigned primgroup_size = 128; /* recommended without a GS */
1809 bool ia_switch_on_eop = false;
1810 bool partial_vs_wave = false;
1811
1812 if (rctx->gs_shader)
1813 primgroup_size = 64; /* recommended with a GS */
1814
1815 if ((rctx->rasterizer && rctx->rasterizer->pa_sc_line_stipple) ||
1816 (rctx->b.screen->debug_flags & DBG_SWITCH_ON_EOP)) {
1817 ia_switch_on_eop = true;
1818 }
1819
1820 if (r600_get_strmout_en(&rctx->b))
1821 partial_vs_wave = true;
1822
1823 radeon_set_context_reg(cs, CM_R_028AA8_IA_MULTI_VGT_PARAM,
1824 S_028AA8_SWITCH_ON_EOP(ia_switch_on_eop) |
1825 S_028AA8_PARTIAL_VS_WAVE_ON(partial_vs_wave) |
1826 S_028AA8_PRIMGROUP_SIZE(primgroup_size - 1));
1827 }
1828
1829 if (rctx->b.chip_class >= EVERGREEN) {
1830 uint32_t ls_hs_config = evergreen_get_ls_hs_config(rctx, &info,
1831 num_patches);
1832
1833 evergreen_set_ls_hs_config(rctx, cs, ls_hs_config);
1834 evergreen_set_lds_alloc(rctx, cs, rctx->lds_alloc);
1835 }
1836
1837 /* On R6xx, CULL_FRONT=1 culls all points, lines, and rectangles,
1838 * even though it should have no effect on those. */
1839 if (rctx->b.chip_class == R600 && rctx->rasterizer) {
1840 unsigned su_sc_mode_cntl = rctx->rasterizer->pa_su_sc_mode_cntl;
1841 unsigned prim = info.mode;
1842
1843 if (rctx->gs_shader) {
1844 prim = rctx->gs_shader->gs_output_prim;
1845 }
1846 prim = r600_conv_prim_to_gs_out(prim); /* decrease the number of types to 3 */
1847
1848 if (prim == V_028A6C_OUTPRIM_TYPE_POINTLIST ||
1849 prim == V_028A6C_OUTPRIM_TYPE_LINESTRIP ||
1850 info.mode == R600_PRIM_RECTANGLE_LIST) {
1851 su_sc_mode_cntl &= C_028814_CULL_FRONT;
1852 }
1853 radeon_set_context_reg(cs, R_028814_PA_SU_SC_MODE_CNTL, su_sc_mode_cntl);
1854 }
1855
1856 /* Update start instance. */
1857 if (!info.indirect && rctx->last_start_instance != info.start_instance) {
1858 radeon_set_ctl_const(cs, R_03CFF4_SQ_VTX_START_INST_LOC, info.start_instance);
1859 rctx->last_start_instance = info.start_instance;
1860 }
1861
1862 /* Update the primitive type. */
1863 if (rctx->last_primitive_type != info.mode) {
1864 unsigned ls_mask = 0;
1865
1866 if (info.mode == PIPE_PRIM_LINES)
1867 ls_mask = 1;
1868 else if (info.mode == PIPE_PRIM_LINE_STRIP ||
1869 info.mode == PIPE_PRIM_LINE_LOOP)
1870 ls_mask = 2;
1871
1872 radeon_set_context_reg(cs, R_028A0C_PA_SC_LINE_STIPPLE,
1873 S_028A0C_AUTO_RESET_CNTL(ls_mask) |
1874 (rctx->rasterizer ? rctx->rasterizer->pa_sc_line_stipple : 0));
1875 radeon_set_config_reg(cs, R_008958_VGT_PRIMITIVE_TYPE,
1876 r600_conv_pipe_prim(info.mode));
1877
1878 rctx->last_primitive_type = info.mode;
1879 }
1880
1881 /* Draw packets. */
1882 if (!info.indirect) {
1883 radeon_emit(cs, PKT3(PKT3_NUM_INSTANCES, 0, 0));
1884 radeon_emit(cs, info.instance_count);
1885 }
1886
1887 if (unlikely(info.indirect)) {
1888 uint64_t va = r600_resource(info.indirect)->gpu_address;
1889 assert(rctx->b.chip_class >= EVERGREEN);
1890
1891 // Invalidate so non-indirect draw calls reset this state
1892 rctx->vgt_state.last_draw_was_indirect = true;
1893 rctx->last_start_instance = -1;
1894
1895 radeon_emit(cs, PKT3(EG_PKT3_SET_BASE, 2, 0));
1896 radeon_emit(cs, EG_DRAW_INDEX_INDIRECT_PATCH_TABLE_BASE);
1897 radeon_emit(cs, va);
1898 radeon_emit(cs, (va >> 32UL) & 0xFF);
1899
1900 radeon_emit(cs, PKT3(PKT3_NOP, 0, 0));
1901 radeon_emit(cs, radeon_add_to_buffer_list(&rctx->b, &rctx->b.gfx,
1902 (struct r600_resource*)info.indirect,
1903 RADEON_USAGE_READ,
1904 RADEON_PRIO_DRAW_INDIRECT));
1905 }
1906
1907 if (info.indexed) {
1908 radeon_emit(cs, PKT3(PKT3_INDEX_TYPE, 0, 0));
1909 radeon_emit(cs, ib.index_size == 4 ?
1910 (VGT_INDEX_32 | (R600_BIG_ENDIAN ? VGT_DMA_SWAP_32_BIT : 0)) :
1911 (VGT_INDEX_16 | (R600_BIG_ENDIAN ? VGT_DMA_SWAP_16_BIT : 0)));
1912
1913 if (ib.user_buffer) {
1914 unsigned size_bytes = info.count*ib.index_size;
1915 unsigned size_dw = align(size_bytes, 4) / 4;
1916 radeon_emit(cs, PKT3(PKT3_DRAW_INDEX_IMMD, 1 + size_dw, render_cond_bit));
1917 radeon_emit(cs, info.count);
1918 radeon_emit(cs, V_0287F0_DI_SRC_SEL_IMMEDIATE);
1919 radeon_emit_array(cs, ib.user_buffer, size_dw);
1920 } else {
1921 uint64_t va = r600_resource(ib.buffer)->gpu_address + ib.offset;
1922
1923 if (likely(!info.indirect)) {
1924 radeon_emit(cs, PKT3(PKT3_DRAW_INDEX, 3, render_cond_bit));
1925 radeon_emit(cs, va);
1926 radeon_emit(cs, (va >> 32UL) & 0xFF);
1927 radeon_emit(cs, info.count);
1928 radeon_emit(cs, V_0287F0_DI_SRC_SEL_DMA);
1929 radeon_emit(cs, PKT3(PKT3_NOP, 0, 0));
1930 radeon_emit(cs, radeon_add_to_buffer_list(&rctx->b, &rctx->b.gfx,
1931 (struct r600_resource*)ib.buffer,
1932 RADEON_USAGE_READ,
1933 RADEON_PRIO_INDEX_BUFFER));
1934 }
1935 else {
1936 uint32_t max_size = (ib.buffer->width0 - ib.offset) / ib.index_size;
1937
1938 radeon_emit(cs, PKT3(EG_PKT3_INDEX_BASE, 1, 0));
1939 radeon_emit(cs, va);
1940 radeon_emit(cs, (va >> 32UL) & 0xFF);
1941
1942 radeon_emit(cs, PKT3(PKT3_NOP, 0, 0));
1943 radeon_emit(cs, radeon_add_to_buffer_list(&rctx->b, &rctx->b.gfx,
1944 (struct r600_resource*)ib.buffer,
1945 RADEON_USAGE_READ,
1946 RADEON_PRIO_INDEX_BUFFER));
1947
1948 radeon_emit(cs, PKT3(EG_PKT3_INDEX_BUFFER_SIZE, 0, 0));
1949 radeon_emit(cs, max_size);
1950
1951 radeon_emit(cs, PKT3(EG_PKT3_DRAW_INDEX_INDIRECT, 1, render_cond_bit));
1952 radeon_emit(cs, info.indirect_offset);
1953 radeon_emit(cs, V_0287F0_DI_SRC_SEL_DMA);
1954 }
1955 }
1956 } else {
1957 if (unlikely(info.count_from_stream_output)) {
1958 struct r600_so_target *t = (struct r600_so_target*)info.count_from_stream_output;
1959 uint64_t va = t->buf_filled_size->gpu_address + t->buf_filled_size_offset;
1960
1961 radeon_set_context_reg(cs, R_028B30_VGT_STRMOUT_DRAW_OPAQUE_VERTEX_STRIDE, t->stride_in_dw);
1962
1963 radeon_emit(cs, PKT3(PKT3_COPY_DW, 4, 0));
1964 radeon_emit(cs, COPY_DW_SRC_IS_MEM | COPY_DW_DST_IS_REG);
1965 radeon_emit(cs, va & 0xFFFFFFFFUL); /* src address lo */
1966 radeon_emit(cs, (va >> 32UL) & 0xFFUL); /* src address hi */
1967 radeon_emit(cs, R_028B2C_VGT_STRMOUT_DRAW_OPAQUE_BUFFER_FILLED_SIZE >> 2); /* dst register */
1968 radeon_emit(cs, 0); /* unused */
1969
1970 radeon_emit(cs, PKT3(PKT3_NOP, 0, 0));
1971 radeon_emit(cs, radeon_add_to_buffer_list(&rctx->b, &rctx->b.gfx,
1972 t->buf_filled_size, RADEON_USAGE_READ,
1973 RADEON_PRIO_SO_FILLED_SIZE));
1974 }
1975
1976 if (likely(!info.indirect)) {
1977 radeon_emit(cs, PKT3(PKT3_DRAW_INDEX_AUTO, 1, render_cond_bit));
1978 radeon_emit(cs, info.count);
1979 }
1980 else {
1981 radeon_emit(cs, PKT3(EG_PKT3_DRAW_INDIRECT, 1, render_cond_bit));
1982 radeon_emit(cs, info.indirect_offset);
1983 }
1984 radeon_emit(cs, V_0287F0_DI_SRC_SEL_AUTO_INDEX |
1985 (info.count_from_stream_output ? S_0287F0_USE_OPAQUE(1) : 0));
1986 }
1987
1988 /* SMX returns CONTEXT_DONE too early workaround */
1989 if (rctx->b.family == CHIP_R600 ||
1990 rctx->b.family == CHIP_RV610 ||
1991 rctx->b.family == CHIP_RV630 ||
1992 rctx->b.family == CHIP_RV635) {
1993 /* if we have gs shader or streamout
1994 we need to do a wait idle after every draw */
1995 if (rctx->gs_shader || r600_get_strmout_en(&rctx->b)) {
1996 radeon_set_config_reg(cs, R_008040_WAIT_UNTIL, S_008040_WAIT_3D_IDLE(1));
1997 }
1998 }
1999
2000 /* ES ring rolling over at EOP - workaround */
2001 if (rctx->b.chip_class == R600) {
2002 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
2003 radeon_emit(cs, EVENT_TYPE(EVENT_TYPE_SQ_NON_EVENT));
2004 }
2005
2006 /* Set the depth buffer as dirty. */
2007 if (rctx->framebuffer.state.zsbuf) {
2008 struct pipe_surface *surf = rctx->framebuffer.state.zsbuf;
2009 struct r600_texture *rtex = (struct r600_texture *)surf->texture;
2010
2011 rtex->dirty_level_mask |= 1 << surf->u.tex.level;
2012
2013 if (rtex->surface.flags & RADEON_SURF_SBUFFER)
2014 rtex->stencil_dirty_level_mask |= 1 << surf->u.tex.level;
2015 }
2016 if (rctx->framebuffer.compressed_cb_mask) {
2017 struct pipe_surface *surf;
2018 struct r600_texture *rtex;
2019 unsigned mask = rctx->framebuffer.compressed_cb_mask;
2020
2021 do {
2022 unsigned i = u_bit_scan(&mask);
2023 surf = rctx->framebuffer.state.cbufs[i];
2024 rtex = (struct r600_texture*)surf->texture;
2025
2026 rtex->dirty_level_mask |= 1 << surf->u.tex.level;
2027
2028 } while (mask);
2029 }
2030
2031 pipe_resource_reference(&ib.buffer, NULL);
2032 rctx->b.num_draw_calls++;
2033 }
2034
2035 uint32_t r600_translate_stencil_op(int s_op)
2036 {
2037 switch (s_op) {
2038 case PIPE_STENCIL_OP_KEEP:
2039 return V_028800_STENCIL_KEEP;
2040 case PIPE_STENCIL_OP_ZERO:
2041 return V_028800_STENCIL_ZERO;
2042 case PIPE_STENCIL_OP_REPLACE:
2043 return V_028800_STENCIL_REPLACE;
2044 case PIPE_STENCIL_OP_INCR:
2045 return V_028800_STENCIL_INCR;
2046 case PIPE_STENCIL_OP_DECR:
2047 return V_028800_STENCIL_DECR;
2048 case PIPE_STENCIL_OP_INCR_WRAP:
2049 return V_028800_STENCIL_INCR_WRAP;
2050 case PIPE_STENCIL_OP_DECR_WRAP:
2051 return V_028800_STENCIL_DECR_WRAP;
2052 case PIPE_STENCIL_OP_INVERT:
2053 return V_028800_STENCIL_INVERT;
2054 default:
2055 R600_ERR("Unknown stencil op %d", s_op);
2056 assert(0);
2057 break;
2058 }
2059 return 0;
2060 }
2061
2062 uint32_t r600_translate_fill(uint32_t func)
2063 {
2064 switch(func) {
2065 case PIPE_POLYGON_MODE_FILL:
2066 return 2;
2067 case PIPE_POLYGON_MODE_LINE:
2068 return 1;
2069 case PIPE_POLYGON_MODE_POINT:
2070 return 0;
2071 default:
2072 assert(0);
2073 return 0;
2074 }
2075 }
2076
2077 unsigned r600_tex_wrap(unsigned wrap)
2078 {
2079 switch (wrap) {
2080 default:
2081 case PIPE_TEX_WRAP_REPEAT:
2082 return V_03C000_SQ_TEX_WRAP;
2083 case PIPE_TEX_WRAP_CLAMP:
2084 return V_03C000_SQ_TEX_CLAMP_HALF_BORDER;
2085 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
2086 return V_03C000_SQ_TEX_CLAMP_LAST_TEXEL;
2087 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
2088 return V_03C000_SQ_TEX_CLAMP_BORDER;
2089 case PIPE_TEX_WRAP_MIRROR_REPEAT:
2090 return V_03C000_SQ_TEX_MIRROR;
2091 case PIPE_TEX_WRAP_MIRROR_CLAMP:
2092 return V_03C000_SQ_TEX_MIRROR_ONCE_HALF_BORDER;
2093 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
2094 return V_03C000_SQ_TEX_MIRROR_ONCE_LAST_TEXEL;
2095 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
2096 return V_03C000_SQ_TEX_MIRROR_ONCE_BORDER;
2097 }
2098 }
2099
2100 unsigned r600_tex_mipfilter(unsigned filter)
2101 {
2102 switch (filter) {
2103 case PIPE_TEX_MIPFILTER_NEAREST:
2104 return V_03C000_SQ_TEX_Z_FILTER_POINT;
2105 case PIPE_TEX_MIPFILTER_LINEAR:
2106 return V_03C000_SQ_TEX_Z_FILTER_LINEAR;
2107 default:
2108 case PIPE_TEX_MIPFILTER_NONE:
2109 return V_03C000_SQ_TEX_Z_FILTER_NONE;
2110 }
2111 }
2112
2113 unsigned r600_tex_compare(unsigned compare)
2114 {
2115 switch (compare) {
2116 default:
2117 case PIPE_FUNC_NEVER:
2118 return V_03C000_SQ_TEX_DEPTH_COMPARE_NEVER;
2119 case PIPE_FUNC_LESS:
2120 return V_03C000_SQ_TEX_DEPTH_COMPARE_LESS;
2121 case PIPE_FUNC_EQUAL:
2122 return V_03C000_SQ_TEX_DEPTH_COMPARE_EQUAL;
2123 case PIPE_FUNC_LEQUAL:
2124 return V_03C000_SQ_TEX_DEPTH_COMPARE_LESSEQUAL;
2125 case PIPE_FUNC_GREATER:
2126 return V_03C000_SQ_TEX_DEPTH_COMPARE_GREATER;
2127 case PIPE_FUNC_NOTEQUAL:
2128 return V_03C000_SQ_TEX_DEPTH_COMPARE_NOTEQUAL;
2129 case PIPE_FUNC_GEQUAL:
2130 return V_03C000_SQ_TEX_DEPTH_COMPARE_GREATEREQUAL;
2131 case PIPE_FUNC_ALWAYS:
2132 return V_03C000_SQ_TEX_DEPTH_COMPARE_ALWAYS;
2133 }
2134 }
2135
2136 static bool wrap_mode_uses_border_color(unsigned wrap, bool linear_filter)
2137 {
2138 return wrap == PIPE_TEX_WRAP_CLAMP_TO_BORDER ||
2139 wrap == PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER ||
2140 (linear_filter &&
2141 (wrap == PIPE_TEX_WRAP_CLAMP ||
2142 wrap == PIPE_TEX_WRAP_MIRROR_CLAMP));
2143 }
2144
2145 bool sampler_state_needs_border_color(const struct pipe_sampler_state *state)
2146 {
2147 bool linear_filter = state->min_img_filter != PIPE_TEX_FILTER_NEAREST ||
2148 state->mag_img_filter != PIPE_TEX_FILTER_NEAREST;
2149
2150 return (state->border_color.ui[0] || state->border_color.ui[1] ||
2151 state->border_color.ui[2] || state->border_color.ui[3]) &&
2152 (wrap_mode_uses_border_color(state->wrap_s, linear_filter) ||
2153 wrap_mode_uses_border_color(state->wrap_t, linear_filter) ||
2154 wrap_mode_uses_border_color(state->wrap_r, linear_filter));
2155 }
2156
2157 void r600_emit_shader(struct r600_context *rctx, struct r600_atom *a)
2158 {
2159
2160 struct radeon_winsys_cs *cs = rctx->b.gfx.cs;
2161 struct r600_pipe_shader *shader = ((struct r600_shader_state*)a)->shader;
2162
2163 if (!shader)
2164 return;
2165
2166 r600_emit_command_buffer(cs, &shader->command_buffer);
2167 radeon_emit(cs, PKT3(PKT3_NOP, 0, 0));
2168 radeon_emit(cs, radeon_add_to_buffer_list(&rctx->b, &rctx->b.gfx, shader->bo,
2169 RADEON_USAGE_READ, RADEON_PRIO_SHADER_BINARY));
2170 }
2171
2172 unsigned r600_get_swizzle_combined(const unsigned char *swizzle_format,
2173 const unsigned char *swizzle_view,
2174 boolean vtx)
2175 {
2176 unsigned i;
2177 unsigned char swizzle[4];
2178 unsigned result = 0;
2179 const uint32_t tex_swizzle_shift[4] = {
2180 16, 19, 22, 25,
2181 };
2182 const uint32_t vtx_swizzle_shift[4] = {
2183 3, 6, 9, 12,
2184 };
2185 const uint32_t swizzle_bit[4] = {
2186 0, 1, 2, 3,
2187 };
2188 const uint32_t *swizzle_shift = tex_swizzle_shift;
2189
2190 if (vtx)
2191 swizzle_shift = vtx_swizzle_shift;
2192
2193 if (swizzle_view) {
2194 util_format_compose_swizzles(swizzle_format, swizzle_view, swizzle);
2195 } else {
2196 memcpy(swizzle, swizzle_format, 4);
2197 }
2198
2199 /* Get swizzle. */
2200 for (i = 0; i < 4; i++) {
2201 switch (swizzle[i]) {
2202 case PIPE_SWIZZLE_Y:
2203 result |= swizzle_bit[1] << swizzle_shift[i];
2204 break;
2205 case PIPE_SWIZZLE_Z:
2206 result |= swizzle_bit[2] << swizzle_shift[i];
2207 break;
2208 case PIPE_SWIZZLE_W:
2209 result |= swizzle_bit[3] << swizzle_shift[i];
2210 break;
2211 case PIPE_SWIZZLE_0:
2212 result |= V_038010_SQ_SEL_0 << swizzle_shift[i];
2213 break;
2214 case PIPE_SWIZZLE_1:
2215 result |= V_038010_SQ_SEL_1 << swizzle_shift[i];
2216 break;
2217 default: /* PIPE_SWIZZLE_X */
2218 result |= swizzle_bit[0] << swizzle_shift[i];
2219 }
2220 }
2221 return result;
2222 }
2223
2224 /* texture format translate */
2225 uint32_t r600_translate_texformat(struct pipe_screen *screen,
2226 enum pipe_format format,
2227 const unsigned char *swizzle_view,
2228 uint32_t *word4_p, uint32_t *yuv_format_p,
2229 bool do_endian_swap)
2230 {
2231 struct r600_screen *rscreen = (struct r600_screen *)screen;
2232 uint32_t result = 0, word4 = 0, yuv_format = 0;
2233 const struct util_format_description *desc;
2234 boolean uniform = TRUE;
2235 bool is_srgb_valid = FALSE;
2236 const unsigned char swizzle_xxxx[4] = {0, 0, 0, 0};
2237 const unsigned char swizzle_yyyy[4] = {1, 1, 1, 1};
2238 const unsigned char swizzle_xxxy[4] = {0, 0, 0, 1};
2239 const unsigned char swizzle_zyx1[4] = {2, 1, 0, 5};
2240 const unsigned char swizzle_zyxw[4] = {2, 1, 0, 3};
2241
2242 int i;
2243 const uint32_t sign_bit[4] = {
2244 S_038010_FORMAT_COMP_X(V_038010_SQ_FORMAT_COMP_SIGNED),
2245 S_038010_FORMAT_COMP_Y(V_038010_SQ_FORMAT_COMP_SIGNED),
2246 S_038010_FORMAT_COMP_Z(V_038010_SQ_FORMAT_COMP_SIGNED),
2247 S_038010_FORMAT_COMP_W(V_038010_SQ_FORMAT_COMP_SIGNED)
2248 };
2249
2250 /* Need to replace the specified texture formats in case of big-endian.
2251 * These formats are formats that have channels with number of bits
2252 * not divisible by 8.
2253 * Mesa conversion functions don't swap bits for those formats, and because
2254 * we transmit this over a serial bus to the GPU (PCIe), the
2255 * bit-endianess is important!!!
2256 * In case we have an "opposite" format, just use that for the swizzling
2257 * information. If we don't have such an "opposite" format, we need
2258 * to use a fixed swizzle info instead (see below)
2259 */
2260 if (format == PIPE_FORMAT_R4A4_UNORM && do_endian_swap)
2261 format = PIPE_FORMAT_A4R4_UNORM;
2262
2263 desc = util_format_description(format);
2264
2265 /* Depth and stencil swizzling is handled separately. */
2266 if (desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS) {
2267 /* Need to check for specific texture formats that don't have
2268 * an "opposite" format we can use. For those formats, we directly
2269 * specify the swizzling, which is the LE swizzling as defined in
2270 * u_format.csv
2271 */
2272 if (do_endian_swap) {
2273 if (format == PIPE_FORMAT_L4A4_UNORM)
2274 word4 |= r600_get_swizzle_combined(swizzle_xxxy, swizzle_view, FALSE);
2275 else if (format == PIPE_FORMAT_B4G4R4A4_UNORM)
2276 word4 |= r600_get_swizzle_combined(swizzle_zyxw, swizzle_view, FALSE);
2277 else if (format == PIPE_FORMAT_B4G4R4X4_UNORM || format == PIPE_FORMAT_B5G6R5_UNORM)
2278 word4 |= r600_get_swizzle_combined(swizzle_zyx1, swizzle_view, FALSE);
2279 else
2280 word4 |= r600_get_swizzle_combined(desc->swizzle, swizzle_view, FALSE);
2281 } else {
2282 word4 |= r600_get_swizzle_combined(desc->swizzle, swizzle_view, FALSE);
2283 }
2284 }
2285
2286 /* Colorspace (return non-RGB formats directly). */
2287 switch (desc->colorspace) {
2288 /* Depth stencil formats */
2289 case UTIL_FORMAT_COLORSPACE_ZS:
2290 switch (format) {
2291 /* Depth sampler formats. */
2292 case PIPE_FORMAT_Z16_UNORM:
2293 word4 |= r600_get_swizzle_combined(swizzle_xxxx, swizzle_view, FALSE);
2294 result = FMT_16;
2295 goto out_word4;
2296 case PIPE_FORMAT_Z24X8_UNORM:
2297 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
2298 word4 |= r600_get_swizzle_combined(swizzle_xxxx, swizzle_view, FALSE);
2299 result = FMT_8_24;
2300 goto out_word4;
2301 case PIPE_FORMAT_X8Z24_UNORM:
2302 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
2303 if (rscreen->b.chip_class < EVERGREEN)
2304 goto out_unknown;
2305 word4 |= r600_get_swizzle_combined(swizzle_yyyy, swizzle_view, FALSE);
2306 result = FMT_24_8;
2307 goto out_word4;
2308 case PIPE_FORMAT_Z32_FLOAT:
2309 word4 |= r600_get_swizzle_combined(swizzle_xxxx, swizzle_view, FALSE);
2310 result = FMT_32_FLOAT;
2311 goto out_word4;
2312 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
2313 word4 |= r600_get_swizzle_combined(swizzle_xxxx, swizzle_view, FALSE);
2314 result = FMT_X24_8_32_FLOAT;
2315 goto out_word4;
2316 /* Stencil sampler formats. */
2317 case PIPE_FORMAT_S8_UINT:
2318 word4 |= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT);
2319 word4 |= r600_get_swizzle_combined(swizzle_xxxx, swizzle_view, FALSE);
2320 result = FMT_8;
2321 goto out_word4;
2322 case PIPE_FORMAT_X24S8_UINT:
2323 word4 |= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT);
2324 word4 |= r600_get_swizzle_combined(swizzle_yyyy, swizzle_view, FALSE);
2325 result = FMT_8_24;
2326 goto out_word4;
2327 case PIPE_FORMAT_S8X24_UINT:
2328 if (rscreen->b.chip_class < EVERGREEN)
2329 goto out_unknown;
2330 word4 |= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT);
2331 word4 |= r600_get_swizzle_combined(swizzle_xxxx, swizzle_view, FALSE);
2332 result = FMT_24_8;
2333 goto out_word4;
2334 case PIPE_FORMAT_X32_S8X24_UINT:
2335 word4 |= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT);
2336 word4 |= r600_get_swizzle_combined(swizzle_yyyy, swizzle_view, FALSE);
2337 result = FMT_X24_8_32_FLOAT;
2338 goto out_word4;
2339 default:
2340 goto out_unknown;
2341 }
2342
2343 case UTIL_FORMAT_COLORSPACE_YUV:
2344 yuv_format |= (1 << 30);
2345 switch (format) {
2346 case PIPE_FORMAT_UYVY:
2347 case PIPE_FORMAT_YUYV:
2348 default:
2349 break;
2350 }
2351 goto out_unknown; /* XXX */
2352
2353 case UTIL_FORMAT_COLORSPACE_SRGB:
2354 word4 |= S_038010_FORCE_DEGAMMA(1);
2355 break;
2356
2357 default:
2358 break;
2359 }
2360
2361 if (desc->layout == UTIL_FORMAT_LAYOUT_RGTC) {
2362 switch (format) {
2363 case PIPE_FORMAT_RGTC1_SNORM:
2364 case PIPE_FORMAT_LATC1_SNORM:
2365 word4 |= sign_bit[0];
2366 case PIPE_FORMAT_RGTC1_UNORM:
2367 case PIPE_FORMAT_LATC1_UNORM:
2368 result = FMT_BC4;
2369 goto out_word4;
2370 case PIPE_FORMAT_RGTC2_SNORM:
2371 case PIPE_FORMAT_LATC2_SNORM:
2372 word4 |= sign_bit[0] | sign_bit[1];
2373 case PIPE_FORMAT_RGTC2_UNORM:
2374 case PIPE_FORMAT_LATC2_UNORM:
2375 result = FMT_BC5;
2376 goto out_word4;
2377 default:
2378 goto out_unknown;
2379 }
2380 }
2381
2382 if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
2383 if (!util_format_s3tc_enabled) {
2384 goto out_unknown;
2385 }
2386
2387 switch (format) {
2388 case PIPE_FORMAT_DXT1_RGB:
2389 case PIPE_FORMAT_DXT1_RGBA:
2390 case PIPE_FORMAT_DXT1_SRGB:
2391 case PIPE_FORMAT_DXT1_SRGBA:
2392 result = FMT_BC1;
2393 is_srgb_valid = TRUE;
2394 goto out_word4;
2395 case PIPE_FORMAT_DXT3_RGBA:
2396 case PIPE_FORMAT_DXT3_SRGBA:
2397 result = FMT_BC2;
2398 is_srgb_valid = TRUE;
2399 goto out_word4;
2400 case PIPE_FORMAT_DXT5_RGBA:
2401 case PIPE_FORMAT_DXT5_SRGBA:
2402 result = FMT_BC3;
2403 is_srgb_valid = TRUE;
2404 goto out_word4;
2405 default:
2406 goto out_unknown;
2407 }
2408 }
2409
2410 if (desc->layout == UTIL_FORMAT_LAYOUT_BPTC) {
2411 if (rscreen->b.chip_class < EVERGREEN)
2412 goto out_unknown;
2413
2414 switch (format) {
2415 case PIPE_FORMAT_BPTC_RGBA_UNORM:
2416 case PIPE_FORMAT_BPTC_SRGBA:
2417 result = FMT_BC7;
2418 is_srgb_valid = TRUE;
2419 goto out_word4;
2420 case PIPE_FORMAT_BPTC_RGB_FLOAT:
2421 word4 |= sign_bit[0] | sign_bit[1] | sign_bit[2];
2422 /* fall through */
2423 case PIPE_FORMAT_BPTC_RGB_UFLOAT:
2424 result = FMT_BC6;
2425 goto out_word4;
2426 default:
2427 goto out_unknown;
2428 }
2429 }
2430
2431 if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED) {
2432 switch (format) {
2433 case PIPE_FORMAT_R8G8_B8G8_UNORM:
2434 case PIPE_FORMAT_G8R8_B8R8_UNORM:
2435 result = FMT_GB_GR;
2436 goto out_word4;
2437 case PIPE_FORMAT_G8R8_G8B8_UNORM:
2438 case PIPE_FORMAT_R8G8_R8B8_UNORM:
2439 result = FMT_BG_RG;
2440 goto out_word4;
2441 default:
2442 goto out_unknown;
2443 }
2444 }
2445
2446 if (format == PIPE_FORMAT_R9G9B9E5_FLOAT) {
2447 result = FMT_5_9_9_9_SHAREDEXP;
2448 goto out_word4;
2449 } else if (format == PIPE_FORMAT_R11G11B10_FLOAT) {
2450 result = FMT_10_11_11_FLOAT;
2451 goto out_word4;
2452 }
2453
2454
2455 for (i = 0; i < desc->nr_channels; i++) {
2456 if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
2457 word4 |= sign_bit[i];
2458 }
2459 }
2460
2461 /* R8G8Bx_SNORM - XXX CxV8U8 */
2462
2463 /* See whether the components are of the same size. */
2464 for (i = 1; i < desc->nr_channels; i++) {
2465 uniform = uniform && desc->channel[0].size == desc->channel[i].size;
2466 }
2467
2468 /* Non-uniform formats. */
2469 if (!uniform) {
2470 if (desc->colorspace != UTIL_FORMAT_COLORSPACE_SRGB &&
2471 desc->channel[0].pure_integer)
2472 word4 |= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT);
2473 switch(desc->nr_channels) {
2474 case 3:
2475 if (desc->channel[0].size == 5 &&
2476 desc->channel[1].size == 6 &&
2477 desc->channel[2].size == 5) {
2478 result = FMT_5_6_5;
2479 goto out_word4;
2480 }
2481 goto out_unknown;
2482 case 4:
2483 if (desc->channel[0].size == 5 &&
2484 desc->channel[1].size == 5 &&
2485 desc->channel[2].size == 5 &&
2486 desc->channel[3].size == 1) {
2487 result = FMT_1_5_5_5;
2488 goto out_word4;
2489 }
2490 if (desc->channel[0].size == 10 &&
2491 desc->channel[1].size == 10 &&
2492 desc->channel[2].size == 10 &&
2493 desc->channel[3].size == 2) {
2494 result = FMT_2_10_10_10;
2495 goto out_word4;
2496 }
2497 goto out_unknown;
2498 }
2499 goto out_unknown;
2500 }
2501
2502 /* Find the first non-VOID channel. */
2503 for (i = 0; i < 4; i++) {
2504 if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID) {
2505 break;
2506 }
2507 }
2508
2509 if (i == 4)
2510 goto out_unknown;
2511
2512 /* uniform formats */
2513 switch (desc->channel[i].type) {
2514 case UTIL_FORMAT_TYPE_UNSIGNED:
2515 case UTIL_FORMAT_TYPE_SIGNED:
2516 #if 0
2517 if (!desc->channel[i].normalized &&
2518 desc->colorspace != UTIL_FORMAT_COLORSPACE_SRGB) {
2519 goto out_unknown;
2520 }
2521 #endif
2522 if (desc->colorspace != UTIL_FORMAT_COLORSPACE_SRGB &&
2523 desc->channel[i].pure_integer)
2524 word4 |= S_038010_NUM_FORMAT_ALL(V_038010_SQ_NUM_FORMAT_INT);
2525
2526 switch (desc->channel[i].size) {
2527 case 4:
2528 switch (desc->nr_channels) {
2529 case 2:
2530 result = FMT_4_4;
2531 goto out_word4;
2532 case 4:
2533 result = FMT_4_4_4_4;
2534 goto out_word4;
2535 }
2536 goto out_unknown;
2537 case 8:
2538 switch (desc->nr_channels) {
2539 case 1:
2540 result = FMT_8;
2541 goto out_word4;
2542 case 2:
2543 result = FMT_8_8;
2544 goto out_word4;
2545 case 4:
2546 result = FMT_8_8_8_8;
2547 is_srgb_valid = TRUE;
2548 goto out_word4;
2549 }
2550 goto out_unknown;
2551 case 16:
2552 switch (desc->nr_channels) {
2553 case 1:
2554 result = FMT_16;
2555 goto out_word4;
2556 case 2:
2557 result = FMT_16_16;
2558 goto out_word4;
2559 case 4:
2560 result = FMT_16_16_16_16;
2561 goto out_word4;
2562 }
2563 goto out_unknown;
2564 case 32:
2565 switch (desc->nr_channels) {
2566 case 1:
2567 result = FMT_32;
2568 goto out_word4;
2569 case 2:
2570 result = FMT_32_32;
2571 goto out_word4;
2572 case 4:
2573 result = FMT_32_32_32_32;
2574 goto out_word4;
2575 }
2576 }
2577 goto out_unknown;
2578
2579 case UTIL_FORMAT_TYPE_FLOAT:
2580 switch (desc->channel[i].size) {
2581 case 16:
2582 switch (desc->nr_channels) {
2583 case 1:
2584 result = FMT_16_FLOAT;
2585 goto out_word4;
2586 case 2:
2587 result = FMT_16_16_FLOAT;
2588 goto out_word4;
2589 case 4:
2590 result = FMT_16_16_16_16_FLOAT;
2591 goto out_word4;
2592 }
2593 goto out_unknown;
2594 case 32:
2595 switch (desc->nr_channels) {
2596 case 1:
2597 result = FMT_32_FLOAT;
2598 goto out_word4;
2599 case 2:
2600 result = FMT_32_32_FLOAT;
2601 goto out_word4;
2602 case 4:
2603 result = FMT_32_32_32_32_FLOAT;
2604 goto out_word4;
2605 }
2606 }
2607 goto out_unknown;
2608 }
2609
2610 out_word4:
2611
2612 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB && !is_srgb_valid)
2613 return ~0;
2614 if (word4_p)
2615 *word4_p = word4;
2616 if (yuv_format_p)
2617 *yuv_format_p = yuv_format;
2618 return result;
2619 out_unknown:
2620 /* R600_ERR("Unable to handle texformat %d %s\n", format, util_format_name(format)); */
2621 return ~0;
2622 }
2623
2624 uint32_t r600_translate_colorformat(enum chip_class chip, enum pipe_format format,
2625 bool do_endian_swap)
2626 {
2627 const struct util_format_description *desc = util_format_description(format);
2628 int channel = util_format_get_first_non_void_channel(format);
2629 bool is_float;
2630
2631 #define HAS_SIZE(x,y,z,w) \
2632 (desc->channel[0].size == (x) && desc->channel[1].size == (y) && \
2633 desc->channel[2].size == (z) && desc->channel[3].size == (w))
2634
2635 if (format == PIPE_FORMAT_R11G11B10_FLOAT) /* isn't plain */
2636 return V_0280A0_COLOR_10_11_11_FLOAT;
2637
2638 if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN ||
2639 channel == -1)
2640 return ~0U;
2641
2642 is_float = desc->channel[channel].type == UTIL_FORMAT_TYPE_FLOAT;
2643
2644 switch (desc->nr_channels) {
2645 case 1:
2646 switch (desc->channel[0].size) {
2647 case 8:
2648 return V_0280A0_COLOR_8;
2649 case 16:
2650 if (is_float)
2651 return V_0280A0_COLOR_16_FLOAT;
2652 else
2653 return V_0280A0_COLOR_16;
2654 case 32:
2655 if (is_float)
2656 return V_0280A0_COLOR_32_FLOAT;
2657 else
2658 return V_0280A0_COLOR_32;
2659 }
2660 break;
2661 case 2:
2662 if (desc->channel[0].size == desc->channel[1].size) {
2663 switch (desc->channel[0].size) {
2664 case 4:
2665 if (chip <= R700)
2666 return V_0280A0_COLOR_4_4;
2667 else
2668 return ~0U; /* removed on Evergreen */
2669 case 8:
2670 return V_0280A0_COLOR_8_8;
2671 case 16:
2672 if (is_float)
2673 return V_0280A0_COLOR_16_16_FLOAT;
2674 else
2675 return V_0280A0_COLOR_16_16;
2676 case 32:
2677 if (is_float)
2678 return V_0280A0_COLOR_32_32_FLOAT;
2679 else
2680 return V_0280A0_COLOR_32_32;
2681 }
2682 } else if (HAS_SIZE(8,24,0,0)) {
2683 return (do_endian_swap ? V_0280A0_COLOR_8_24 : V_0280A0_COLOR_24_8);
2684 } else if (HAS_SIZE(24,8,0,0)) {
2685 return V_0280A0_COLOR_8_24;
2686 }
2687 break;
2688 case 3:
2689 if (HAS_SIZE(5,6,5,0)) {
2690 return V_0280A0_COLOR_5_6_5;
2691 } else if (HAS_SIZE(32,8,24,0)) {
2692 return V_0280A0_COLOR_X24_8_32_FLOAT;
2693 }
2694 break;
2695 case 4:
2696 if (desc->channel[0].size == desc->channel[1].size &&
2697 desc->channel[0].size == desc->channel[2].size &&
2698 desc->channel[0].size == desc->channel[3].size) {
2699 switch (desc->channel[0].size) {
2700 case 4:
2701 return V_0280A0_COLOR_4_4_4_4;
2702 case 8:
2703 return V_0280A0_COLOR_8_8_8_8;
2704 case 16:
2705 if (is_float)
2706 return V_0280A0_COLOR_16_16_16_16_FLOAT;
2707 else
2708 return V_0280A0_COLOR_16_16_16_16;
2709 case 32:
2710 if (is_float)
2711 return V_0280A0_COLOR_32_32_32_32_FLOAT;
2712 else
2713 return V_0280A0_COLOR_32_32_32_32;
2714 }
2715 } else if (HAS_SIZE(5,5,5,1)) {
2716 return V_0280A0_COLOR_1_5_5_5;
2717 } else if (HAS_SIZE(10,10,10,2)) {
2718 return V_0280A0_COLOR_2_10_10_10;
2719 }
2720 break;
2721 }
2722 return ~0U;
2723 }
2724
2725 uint32_t r600_colorformat_endian_swap(uint32_t colorformat, bool do_endian_swap)
2726 {
2727 if (R600_BIG_ENDIAN) {
2728 switch(colorformat) {
2729 /* 8-bit buffers. */
2730 case V_0280A0_COLOR_4_4:
2731 case V_0280A0_COLOR_8:
2732 return ENDIAN_NONE;
2733
2734 /* 16-bit buffers. */
2735 case V_0280A0_COLOR_8_8:
2736 /*
2737 * No need to do endian swaps on array formats,
2738 * as mesa<-->pipe formats conversion take into account
2739 * the endianess
2740 */
2741 return ENDIAN_NONE;
2742
2743 case V_0280A0_COLOR_5_6_5:
2744 case V_0280A0_COLOR_1_5_5_5:
2745 case V_0280A0_COLOR_4_4_4_4:
2746 case V_0280A0_COLOR_16:
2747 return (do_endian_swap ? ENDIAN_8IN16 : ENDIAN_NONE);
2748
2749 /* 32-bit buffers. */
2750 case V_0280A0_COLOR_8_8_8_8:
2751 /*
2752 * No need to do endian swaps on array formats,
2753 * as mesa<-->pipe formats conversion take into account
2754 * the endianess
2755 */
2756 return ENDIAN_NONE;
2757
2758 case V_0280A0_COLOR_2_10_10_10:
2759 case V_0280A0_COLOR_8_24:
2760 case V_0280A0_COLOR_24_8:
2761 case V_0280A0_COLOR_32_FLOAT:
2762 return (do_endian_swap ? ENDIAN_8IN32 : ENDIAN_NONE);
2763
2764 case V_0280A0_COLOR_16_16_FLOAT:
2765 case V_0280A0_COLOR_16_16:
2766 return ENDIAN_8IN16;
2767
2768 /* 64-bit buffers. */
2769 case V_0280A0_COLOR_16_16_16_16:
2770 case V_0280A0_COLOR_16_16_16_16_FLOAT:
2771 return ENDIAN_8IN16;
2772
2773 case V_0280A0_COLOR_32_32_FLOAT:
2774 case V_0280A0_COLOR_32_32:
2775 case V_0280A0_COLOR_X24_8_32_FLOAT:
2776 return ENDIAN_8IN32;
2777
2778 /* 128-bit buffers. */
2779 case V_0280A0_COLOR_32_32_32_32_FLOAT:
2780 case V_0280A0_COLOR_32_32_32_32:
2781 return ENDIAN_8IN32;
2782 default:
2783 return ENDIAN_NONE; /* Unsupported. */
2784 }
2785 } else {
2786 return ENDIAN_NONE;
2787 }
2788 }
2789
2790 static void r600_invalidate_buffer(struct pipe_context *ctx, struct pipe_resource *buf)
2791 {
2792 struct r600_context *rctx = (struct r600_context*)ctx;
2793 struct r600_resource *rbuffer = r600_resource(buf);
2794 unsigned i, shader, mask;
2795 struct r600_pipe_sampler_view *view;
2796
2797 /* Reallocate the buffer in the same pipe_resource. */
2798 r600_alloc_resource(&rctx->screen->b, rbuffer);
2799
2800 /* We changed the buffer, now we need to bind it where the old one was bound. */
2801 /* Vertex buffers. */
2802 mask = rctx->vertex_buffer_state.enabled_mask;
2803 while (mask) {
2804 i = u_bit_scan(&mask);
2805 if (rctx->vertex_buffer_state.vb[i].buffer == &rbuffer->b.b) {
2806 rctx->vertex_buffer_state.dirty_mask |= 1 << i;
2807 r600_vertex_buffers_dirty(rctx);
2808 }
2809 }
2810 /* Streamout buffers. */
2811 for (i = 0; i < rctx->b.streamout.num_targets; i++) {
2812 if (rctx->b.streamout.targets[i] &&
2813 rctx->b.streamout.targets[i]->b.buffer == &rbuffer->b.b) {
2814 if (rctx->b.streamout.begin_emitted) {
2815 r600_emit_streamout_end(&rctx->b);
2816 }
2817 rctx->b.streamout.append_bitmask = rctx->b.streamout.enabled_mask;
2818 r600_streamout_buffers_dirty(&rctx->b);
2819 }
2820 }
2821
2822 /* Constant buffers. */
2823 for (shader = 0; shader < PIPE_SHADER_TYPES; shader++) {
2824 struct r600_constbuf_state *state = &rctx->constbuf_state[shader];
2825 bool found = false;
2826 uint32_t mask = state->enabled_mask;
2827
2828 while (mask) {
2829 unsigned i = u_bit_scan(&mask);
2830 if (state->cb[i].buffer == &rbuffer->b.b) {
2831 found = true;
2832 state->dirty_mask |= 1 << i;
2833 }
2834 }
2835 if (found) {
2836 r600_constant_buffers_dirty(rctx, state);
2837 }
2838 }
2839
2840 /* Texture buffer objects - update the virtual addresses in descriptors. */
2841 LIST_FOR_EACH_ENTRY(view, &rctx->texture_buffers, list) {
2842 if (view->base.texture == &rbuffer->b.b) {
2843 uint64_t offset = view->base.u.buf.offset;
2844 uint64_t va = rbuffer->gpu_address + offset;
2845
2846 view->tex_resource_words[0] = va;
2847 view->tex_resource_words[2] &= C_038008_BASE_ADDRESS_HI;
2848 view->tex_resource_words[2] |= S_038008_BASE_ADDRESS_HI(va >> 32);
2849 }
2850 }
2851 /* Texture buffer objects - make bindings dirty if needed. */
2852 for (shader = 0; shader < PIPE_SHADER_TYPES; shader++) {
2853 struct r600_samplerview_state *state = &rctx->samplers[shader].views;
2854 bool found = false;
2855 uint32_t mask = state->enabled_mask;
2856
2857 while (mask) {
2858 unsigned i = u_bit_scan(&mask);
2859 if (state->views[i]->base.texture == &rbuffer->b.b) {
2860 found = true;
2861 state->dirty_mask |= 1 << i;
2862 }
2863 }
2864 if (found) {
2865 r600_sampler_views_dirty(rctx, state);
2866 }
2867 }
2868 }
2869
2870 static void r600_set_active_query_state(struct pipe_context *ctx, boolean enable)
2871 {
2872 struct r600_context *rctx = (struct r600_context*)ctx;
2873
2874 /* Pipeline stat & streamout queries. */
2875 if (enable) {
2876 rctx->b.flags &= ~R600_CONTEXT_STOP_PIPELINE_STATS;
2877 rctx->b.flags |= R600_CONTEXT_START_PIPELINE_STATS;
2878 } else {
2879 rctx->b.flags &= ~R600_CONTEXT_START_PIPELINE_STATS;
2880 rctx->b.flags |= R600_CONTEXT_STOP_PIPELINE_STATS;
2881 }
2882
2883 /* Occlusion queries. */
2884 if (rctx->db_misc_state.occlusion_queries_disabled != !enable) {
2885 rctx->db_misc_state.occlusion_queries_disabled = !enable;
2886 r600_mark_atom_dirty(rctx, &rctx->db_misc_state.atom);
2887 }
2888 }
2889
2890 static void r600_set_occlusion_query_state(struct pipe_context *ctx, bool enable)
2891 {
2892 struct r600_context *rctx = (struct r600_context*)ctx;
2893
2894 r600_mark_atom_dirty(rctx, &rctx->db_misc_state.atom);
2895 }
2896
2897 static void r600_need_gfx_cs_space(struct pipe_context *ctx, unsigned num_dw,
2898 bool include_draw_vbo)
2899 {
2900 r600_need_cs_space((struct r600_context*)ctx, num_dw, include_draw_vbo);
2901 }
2902
2903 /* keep this at the end of this file, please */
2904 void r600_init_common_state_functions(struct r600_context *rctx)
2905 {
2906 rctx->b.b.create_fs_state = r600_create_ps_state;
2907 rctx->b.b.create_vs_state = r600_create_vs_state;
2908 rctx->b.b.create_gs_state = r600_create_gs_state;
2909 rctx->b.b.create_tcs_state = r600_create_tcs_state;
2910 rctx->b.b.create_tes_state = r600_create_tes_state;
2911 rctx->b.b.create_vertex_elements_state = r600_create_vertex_fetch_shader;
2912 rctx->b.b.bind_blend_state = r600_bind_blend_state;
2913 rctx->b.b.bind_depth_stencil_alpha_state = r600_bind_dsa_state;
2914 rctx->b.b.bind_sampler_states = r600_bind_sampler_states;
2915 rctx->b.b.bind_fs_state = r600_bind_ps_state;
2916 rctx->b.b.bind_rasterizer_state = r600_bind_rs_state;
2917 rctx->b.b.bind_vertex_elements_state = r600_bind_vertex_elements;
2918 rctx->b.b.bind_vs_state = r600_bind_vs_state;
2919 rctx->b.b.bind_gs_state = r600_bind_gs_state;
2920 rctx->b.b.bind_tcs_state = r600_bind_tcs_state;
2921 rctx->b.b.bind_tes_state = r600_bind_tes_state;
2922 rctx->b.b.delete_blend_state = r600_delete_blend_state;
2923 rctx->b.b.delete_depth_stencil_alpha_state = r600_delete_dsa_state;
2924 rctx->b.b.delete_fs_state = r600_delete_ps_state;
2925 rctx->b.b.delete_rasterizer_state = r600_delete_rs_state;
2926 rctx->b.b.delete_sampler_state = r600_delete_sampler_state;
2927 rctx->b.b.delete_vertex_elements_state = r600_delete_vertex_elements;
2928 rctx->b.b.delete_vs_state = r600_delete_vs_state;
2929 rctx->b.b.delete_gs_state = r600_delete_gs_state;
2930 rctx->b.b.delete_tcs_state = r600_delete_tcs_state;
2931 rctx->b.b.delete_tes_state = r600_delete_tes_state;
2932 rctx->b.b.set_blend_color = r600_set_blend_color;
2933 rctx->b.b.set_clip_state = r600_set_clip_state;
2934 rctx->b.b.set_constant_buffer = r600_set_constant_buffer;
2935 rctx->b.b.set_sample_mask = r600_set_sample_mask;
2936 rctx->b.b.set_stencil_ref = r600_set_pipe_stencil_ref;
2937 rctx->b.b.set_vertex_buffers = r600_set_vertex_buffers;
2938 rctx->b.b.set_index_buffer = r600_set_index_buffer;
2939 rctx->b.b.set_sampler_views = r600_set_sampler_views;
2940 rctx->b.b.sampler_view_destroy = r600_sampler_view_destroy;
2941 rctx->b.b.texture_barrier = r600_texture_barrier;
2942 rctx->b.b.set_stream_output_targets = r600_set_streamout_targets;
2943 rctx->b.b.set_active_query_state = r600_set_active_query_state;
2944 rctx->b.b.draw_vbo = r600_draw_vbo;
2945 rctx->b.invalidate_buffer = r600_invalidate_buffer;
2946 rctx->b.set_occlusion_query_state = r600_set_occlusion_query_state;
2947 rctx->b.need_gfx_cs_space = r600_need_gfx_cs_space;
2948 }