2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
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:
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
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. */
24 #include "draw/draw_context.h"
26 #include "util/u_math.h"
27 #include "util/u_memory.h"
29 #include "r300_context.h"
31 #include "r300_screen.h"
32 #include "r300_shader_semantics.h"
33 #include "r300_state_derived.h"
34 #include "r300_state_inlines.h"
37 /* r300_state_derived: Various bits of state which are dependent upon
38 * currently bound CSO data. */
40 struct r300_shader_key
{
41 struct r300_vertex_shader
* vs
;
42 struct r300_fragment_shader
* fs
;
45 struct r300_shader_derived_value
{
46 struct r300_vertex_format
* vformat
;
47 struct r300_rs_block
* rs_block
;
50 unsigned r300_shader_key_hash(void* key
) {
51 struct r300_shader_key
* shader_key
= (struct r300_shader_key
*)key
;
52 unsigned vs
= (intptr_t)shader_key
->vs
;
53 unsigned fs
= (intptr_t)shader_key
->fs
;
55 return (vs
<< 16) | (fs
& 0xffff);
58 int r300_shader_key_compare(void* key1
, void* key2
) {
59 struct r300_shader_key
* shader_key1
= (struct r300_shader_key
*)key1
;
60 struct r300_shader_key
* shader_key2
= (struct r300_shader_key
*)key2
;
62 return (shader_key1
->vs
== shader_key2
->vs
) &&
63 (shader_key1
->fs
== shader_key2
->fs
);
66 static void r300_draw_emit_attrib(struct r300_context
* r300
,
67 enum attrib_emit emit
,
68 enum interp_mode interp
,
71 struct tgsi_shader_info
* info
= &r300
->vs
->info
;
74 output
= draw_find_vs_output(r300
->draw
,
75 info
->output_semantic_name
[index
],
76 info
->output_semantic_index
[index
]);
77 draw_emit_vertex_attr(&r300
->vertex_info
->vinfo
, emit
, interp
, output
);
80 static void r300_draw_emit_all_attribs(struct r300_context
* r300
)
82 struct r300_shader_semantics
* vs_outputs
= &r300
->vs
->outputs
;
86 if (vs_outputs
->pos
!= ATTR_UNUSED
) {
87 r300_draw_emit_attrib(r300
, EMIT_4F
, INTERP_PERSPECTIVE
,
94 if (vs_outputs
->psize
!= ATTR_UNUSED
) {
95 r300_draw_emit_attrib(r300
, EMIT_1F_PSIZE
, INTERP_POS
,
100 for (i
= 0; i
< ATTR_COLOR_COUNT
; i
++) {
101 if (vs_outputs
->color
[i
] != ATTR_UNUSED
) {
102 r300_draw_emit_attrib(r300
, EMIT_4F
, INTERP_LINEAR
,
103 vs_outputs
->color
[i
]);
107 /* XXX Back-face colors. */
109 /* Texture coordinates. */
111 for (i
= 0; i
< ATTR_GENERIC_COUNT
; i
++) {
112 if (vs_outputs
->generic
[i
] != ATTR_UNUSED
) {
113 r300_draw_emit_attrib(r300
, EMIT_4F
, INTERP_PERSPECTIVE
,
114 vs_outputs
->generic
[i
]);
119 /* Fog coordinates. */
120 if (vs_outputs
->fog
!= ATTR_UNUSED
) {
121 r300_draw_emit_attrib(r300
, EMIT_4F
, INTERP_PERSPECTIVE
,
127 assert(gen_count
<= 8);
130 /* Update the PSC tables. */
131 static void r300_vertex_psc(struct r300_context
* r300
)
133 struct r300_vertex_info
*vformat
= r300
->vertex_info
;
134 uint16_t type
, swizzle
;
135 enum pipe_format format
;
138 /* Vertex shaders have no semantics on their inputs,
139 * so PSC should just route stuff based on the vertex elements,
140 * and not on attrib information. */
141 DBG(r300
, DBG_DRAW
, "r300: vs expects %d attribs, routing %d elements"
143 r300
->vs
->info
.num_inputs
,
144 r300
->vertex_element_count
);
146 for (i
= 0; i
< r300
->vertex_element_count
; i
++) {
147 format
= r300
->vertex_element
[i
].src_format
;
149 type
= r300_translate_vertex_data_type(format
) |
150 (i
<< R300_DST_VEC_LOC_SHIFT
);
151 swizzle
= r300_translate_vertex_data_swizzle(format
);
154 vformat
->vap_prog_stream_cntl
[i
>> 1] |= type
<< 16;
155 vformat
->vap_prog_stream_cntl_ext
[i
>> 1] |= swizzle
<< 16;
157 vformat
->vap_prog_stream_cntl
[i
>> 1] |= type
;
158 vformat
->vap_prog_stream_cntl_ext
[i
>> 1] |= swizzle
;
165 /* Set the last vector in the PSC. */
169 vformat
->vap_prog_stream_cntl
[i
>> 1] |=
170 (R300_LAST_VEC
<< (i
& 1 ? 16 : 0));
173 /* Update the PSC tables for SW TCL, using Draw. */
174 static void r300_swtcl_vertex_psc(struct r300_context
* r300
)
176 struct r300_vertex_info
*vformat
= r300
->vertex_info
;
177 struct vertex_info
* vinfo
= &vformat
->vinfo
;
178 uint16_t type
, swizzle
;
179 enum pipe_format format
;
180 unsigned i
, attrib_count
;
181 int* vs_output_tab
= r300
->vs
->output_stream_loc_swtcl
;
183 /* For each Draw attribute, route it to the fragment shader according
184 * to the vs_output_tab. */
185 attrib_count
= vinfo
->num_attribs
;
186 DBG(r300
, DBG_DRAW
, "r300: attrib count: %d\n", attrib_count
);
187 for (i
= 0; i
< attrib_count
; i
++) {
188 DBG(r300
, DBG_DRAW
, "r300: attrib: offset %d, interp %d, size %d,"
189 " vs_output_tab %d\n", vinfo
->attrib
[i
].src_index
,
190 vinfo
->attrib
[i
].interp_mode
, vinfo
->attrib
[i
].emit
,
194 for (i
= 0; i
< attrib_count
; i
++) {
195 /* Make sure we have a proper destination for our attribute. */
196 assert(vs_output_tab
[i
] != -1);
198 format
= draw_translate_vinfo_format(vinfo
->attrib
[i
].emit
);
200 /* Obtain the type of data in this attribute. */
201 type
= r300_translate_vertex_data_type(format
) |
202 vs_output_tab
[i
] << R300_DST_VEC_LOC_SHIFT
;
204 /* Obtain the swizzle for this attribute. Note that the default
205 * swizzle in the hardware is not XYZW! */
206 swizzle
= r300_translate_vertex_data_swizzle(format
);
208 /* Add the attribute to the PSC table. */
210 vformat
->vap_prog_stream_cntl
[i
>> 1] |= type
<< 16;
211 vformat
->vap_prog_stream_cntl_ext
[i
>> 1] |= swizzle
<< 16;
213 vformat
->vap_prog_stream_cntl
[i
>> 1] |= type
;
214 vformat
->vap_prog_stream_cntl_ext
[i
>> 1] |= swizzle
;
218 /* Set the last vector in the PSC. */
222 vformat
->vap_prog_stream_cntl
[i
>> 1] |=
223 (R300_LAST_VEC
<< (i
& 1 ? 16 : 0));
226 static void r300_rs_col(struct r300_rs_block
* rs
, int id
, int ptr
,
227 boolean swizzle_0001
)
229 rs
->ip
[id
] |= R300_RS_COL_PTR(ptr
);
231 rs
->ip
[id
] |= R300_RS_COL_FMT(R300_RS_COL_FMT_0001
);
233 rs
->ip
[id
] |= R300_RS_COL_FMT(R300_RS_COL_FMT_RGBA
);
235 rs
->inst
[id
] |= R300_RS_INST_COL_ID(id
);
238 static void r300_rs_col_write(struct r300_rs_block
* rs
, int id
, int fp_offset
)
240 rs
->inst
[id
] |= R300_RS_INST_COL_CN_WRITE
|
241 R300_RS_INST_COL_ADDR(fp_offset
);
244 static void r300_rs_tex(struct r300_rs_block
* rs
, int id
, int ptr
,
245 boolean swizzle_X001
)
248 rs
->ip
[id
] |= R300_RS_TEX_PTR(ptr
*4) |
249 R300_RS_SEL_S(R300_RS_SEL_C0
) |
250 R300_RS_SEL_T(R300_RS_SEL_K0
) |
251 R300_RS_SEL_R(R300_RS_SEL_K0
) |
252 R300_RS_SEL_Q(R300_RS_SEL_K1
);
254 rs
->ip
[id
] |= R300_RS_TEX_PTR(ptr
*4) |
255 R300_RS_SEL_S(R300_RS_SEL_C0
) |
256 R300_RS_SEL_T(R300_RS_SEL_C1
) |
257 R300_RS_SEL_R(R300_RS_SEL_C2
) |
258 R300_RS_SEL_Q(R300_RS_SEL_C3
);
260 rs
->inst
[id
] |= R300_RS_INST_TEX_ID(id
);
263 static void r300_rs_tex_write(struct r300_rs_block
* rs
, int id
, int fp_offset
)
265 rs
->inst
[id
] |= R300_RS_INST_TEX_CN_WRITE
|
266 R300_RS_INST_TEX_ADDR(fp_offset
);
269 static void r500_rs_col(struct r300_rs_block
* rs
, int id
, int ptr
,
270 boolean swizzle_0001
)
272 rs
->ip
[id
] |= R500_RS_COL_PTR(ptr
);
274 rs
->ip
[id
] |= R500_RS_COL_FMT(R300_RS_COL_FMT_0001
);
276 rs
->ip
[id
] |= R500_RS_COL_FMT(R300_RS_COL_FMT_RGBA
);
278 rs
->inst
[id
] |= R500_RS_INST_COL_ID(id
);
281 static void r500_rs_col_write(struct r300_rs_block
* rs
, int id
, int fp_offset
)
283 rs
->inst
[id
] |= R500_RS_INST_COL_CN_WRITE
|
284 R500_RS_INST_COL_ADDR(fp_offset
);
287 static void r500_rs_tex(struct r300_rs_block
* rs
, int id
, int ptr
,
288 boolean swizzle_X001
)
290 int rs_tex_comp
= ptr
*4;
293 rs
->ip
[id
] |= R500_RS_SEL_S(rs_tex_comp
) |
294 R500_RS_SEL_T(R500_RS_IP_PTR_K0
) |
295 R500_RS_SEL_R(R500_RS_IP_PTR_K0
) |
296 R500_RS_SEL_Q(R500_RS_IP_PTR_K1
);
298 rs
->ip
[id
] |= R500_RS_SEL_S(rs_tex_comp
) |
299 R500_RS_SEL_T(rs_tex_comp
+ 1) |
300 R500_RS_SEL_R(rs_tex_comp
+ 2) |
301 R500_RS_SEL_Q(rs_tex_comp
+ 3);
303 rs
->inst
[id
] |= R500_RS_INST_TEX_ID(id
);
306 static void r500_rs_tex_write(struct r300_rs_block
* rs
, int id
, int fp_offset
)
308 rs
->inst
[id
] |= R500_RS_INST_TEX_CN_WRITE
|
309 R500_RS_INST_TEX_ADDR(fp_offset
);
312 /* Set up the RS block.
314 * This is the part of the chipset that actually does the rasterization
315 * of vertices into fragments. This is also the part of the chipset that
316 * locks up if any part of it is even slightly wrong. */
317 static void r300_update_rs_block(struct r300_context
* r300
,
318 struct r300_shader_semantics
* vs_outputs
,
319 struct r300_shader_semantics
* fs_inputs
)
321 struct r300_rs_block
* rs
= r300
->rs_block
;
322 int i
, col_count
= 0, tex_count
= 0, fp_offset
= 0;
323 void (*rX00_rs_col
)(struct r300_rs_block
*, int, int, boolean
);
324 void (*rX00_rs_col_write
)(struct r300_rs_block
*, int, int);
325 void (*rX00_rs_tex
)(struct r300_rs_block
*, int, int, boolean
);
326 void (*rX00_rs_tex_write
)(struct r300_rs_block
*, int, int);
328 if (r300_screen(r300
->context
.screen
)->caps
->is_r500
) {
329 rX00_rs_col
= r500_rs_col
;
330 rX00_rs_col_write
= r500_rs_col_write
;
331 rX00_rs_tex
= r500_rs_tex
;
332 rX00_rs_tex_write
= r500_rs_tex_write
;
334 rX00_rs_col
= r300_rs_col
;
335 rX00_rs_col_write
= r300_rs_col_write
;
336 rX00_rs_tex
= r300_rs_tex
;
337 rX00_rs_tex_write
= r300_rs_tex_write
;
340 /* Rasterize colors. */
341 for (i
= 0; i
< ATTR_COLOR_COUNT
; i
++) {
342 if (vs_outputs
->color
[i
] != ATTR_UNUSED
) {
343 /* Always rasterize if it's written by the VS,
344 * otherwise it locks up. */
345 rX00_rs_col(rs
, col_count
, i
, FALSE
);
347 /* Write it to the FS input register if it's used by the FS. */
348 if (fs_inputs
->color
[i
] != ATTR_UNUSED
) {
349 rX00_rs_col_write(rs
, col_count
, fp_offset
);
354 /* Skip the FS input register, leave it uninitialized. */
355 /* If we try to set it to (0,0,0,1), it will lock up. */
356 if (fs_inputs
->color
[i
] != ATTR_UNUSED
) {
362 /* Rasterize texture coordinates. */
363 for (i
= 0; i
< ATTR_GENERIC_COUNT
; i
++) {
364 if (vs_outputs
->generic
[i
] != ATTR_UNUSED
) {
365 /* Always rasterize if it's written by the VS,
366 * otherwise it locks up. */
367 rX00_rs_tex(rs
, tex_count
, tex_count
, FALSE
);
369 /* Write it to the FS input register if it's used by the FS. */
370 if (fs_inputs
->generic
[i
] != ATTR_UNUSED
) {
371 rX00_rs_tex_write(rs
, tex_count
, fp_offset
);
376 /* Skip the FS input register, leave it uninitialized. */
377 /* If we try to set it to (0,0,0,1), it will lock up. */
378 if (fs_inputs
->generic
[i
] != ATTR_UNUSED
) {
384 /* Rasterize fog coordinates. */
385 if (vs_outputs
->fog
!= ATTR_UNUSED
) {
386 /* Always rasterize if it's written by the VS,
387 * otherwise it locks up. */
388 rX00_rs_tex(rs
, tex_count
, tex_count
, TRUE
);
390 /* Write it to the FS input register if it's used by the FS. */
391 if (fs_inputs
->fog
!= ATTR_UNUSED
) {
392 rX00_rs_tex_write(rs
, tex_count
, fp_offset
);
397 /* Skip the FS input register, leave it uninitialized. */
398 /* If we try to set it to (0,0,0,1), it will lock up. */
399 if (fs_inputs
->fog
!= ATTR_UNUSED
) {
404 /* Rasterize at least one color, or bad things happen. */
405 if (col_count
== 0 && tex_count
== 0) {
406 rX00_rs_col(rs
, 0, 0, TRUE
);
410 rs
->count
= (tex_count
*4) | (col_count
<< R300_IC_COUNT_SHIFT
) |
413 rs
->inst_count
= MAX3(col_count
- 1, tex_count
- 1, 0);
416 /* Update the vertex format. */
417 static void r300_update_derived_shader_state(struct r300_context
* r300
)
419 struct r300_screen
* r300screen
= r300_screen(r300
->context
.screen
);
422 struct r300_shader_key* key;
423 struct r300_shader_derived_value* value;
424 key = CALLOC_STRUCT(r300_shader_key);
428 value = (struct r300_shader_derived_value*)
429 util_hash_table_get(r300->shader_hash_table, (void*)key);
431 //vformat = value->vformat;
432 rs_block = value->rs_block;
436 rs_block = CALLOC_STRUCT(r300_rs_block);
437 value = CALLOC_STRUCT(r300_shader_derived_value);
439 r300_update_rs_block(r300, rs_block);
441 //value->vformat = vformat;
442 value->rs_block = rs_block;
443 util_hash_table_set(r300->shader_hash_table,
444 (void*)key, (void*)value);
447 /* Reset structures */
448 memset(r300
->rs_block
, 0, sizeof(struct r300_rs_block
));
449 memset(r300
->vertex_info
, 0, sizeof(struct r300_vertex_info
));
450 memcpy(r300
->vertex_info
->vinfo
.hwfmt
, r300
->vs
->hwfmt
, sizeof(uint
)*4);
452 r300_update_rs_block(r300
, &r300
->vs
->outputs
, &r300
->fs
->inputs
);
454 if (r300screen
->caps
->has_tcl
) {
455 r300_vertex_psc(r300
);
457 r300_draw_emit_all_attribs(r300
);
458 draw_compute_vertex_size(&r300
->vertex_info
->vinfo
);
459 r300_swtcl_vertex_psc(r300
);
462 r300
->dirty_state
|= R300_NEW_RS_BLOCK
;
465 static void r300_update_ztop(struct r300_context
* r300
)
467 r300
->ztop_state
.z_buffer_top
= R300_ZTOP_ENABLE
;
469 /* This is important enough that I felt it warranted a comment.
471 * According to the docs, these are the conditions where ZTOP must be
473 * 1) Alpha testing enabled
474 * 2) Texture kill instructions in fragment shader
475 * 3) Chroma key culling enabled
476 * 4) W-buffering enabled
478 * The docs claim that for the first three cases, if no ZS writes happen,
479 * then ZTOP can be used.
481 * Additionally, the following conditions require disabled ZTOP:
482 * ~) Depth writes in fragment shader
483 * ~) Outstanding occlusion queries
487 if (r300
->dsa_state
->alpha_function
) {
488 r300
->ztop_state
.z_buffer_top
= R300_ZTOP_DISABLE
;
489 } else if (r300
->fs
->info
.uses_kill
) {
490 r300
->ztop_state
.z_buffer_top
= R300_ZTOP_DISABLE
;
491 } else if (r300_fragment_shader_writes_depth(r300
->fs
)) {
492 r300
->ztop_state
.z_buffer_top
= R300_ZTOP_DISABLE
;
493 } else if (r300
->query_current
) {
494 r300
->ztop_state
.z_buffer_top
= R300_ZTOP_DISABLE
;
498 void r300_update_derived_state(struct r300_context
* r300
)
500 if (r300
->dirty_state
&
501 (R300_NEW_FRAGMENT_SHADER
| R300_NEW_VERTEX_SHADER
|
502 R300_NEW_VERTEX_FORMAT
)) {
503 r300_update_derived_shader_state(r300
);
506 if (r300
->dirty_state
&
507 (R300_NEW_DSA
| R300_NEW_FRAGMENT_SHADER
| R300_NEW_QUERY
)) {
508 r300_update_ztop(r300
);