mesa/src/gallium/drivers/r300/r300_state_derived.c

492 lines
16 KiB
C
Raw Normal View History

/*
* Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* on the rights to use, copy, modify, merge, publish, distribute, sub
* license, and/or sell copies of the Software, and to permit persons to whom
* the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
* USE OR OTHER DEALINGS IN THE SOFTWARE. */
2009-10-21 14:31:36 +01:00
#include "draw/draw_context.h"
#include "util/u_math.h"
#include "util/u_memory.h"
2009-10-21 14:31:36 +01:00
#include "r300_context.h"
#include "r300_fs.h"
2009-10-21 14:31:36 +01:00
#include "r300_screen.h"
#include "r300_state_derived.h"
#include "r300_state_inlines.h"
#include "r300_vs.h"
/* r300_state_derived: Various bits of state which are dependent upon
* currently bound CSO data. */
struct r300_shader_key {
struct r300_vertex_shader* vs;
struct r300_fragment_shader* fs;
};
struct r300_shader_derived_value {
struct r300_vertex_format* vformat;
struct r300_rs_block* rs_block;
};
unsigned r300_shader_key_hash(void* key) {
struct r300_shader_key* shader_key = (struct r300_shader_key*)key;
2009-11-19 21:40:11 +00:00
unsigned vs = (intptr_t)shader_key->vs;
unsigned fs = (intptr_t)shader_key->fs;
return (vs << 16) | (fs & 0xffff);
}
int r300_shader_key_compare(void* key1, void* key2) {
struct r300_shader_key* shader_key1 = (struct r300_shader_key*)key1;
struct r300_shader_key* shader_key2 = (struct r300_shader_key*)key2;
return (shader_key1->vs == shader_key2->vs) &&
(shader_key1->fs == shader_key2->fs);
}
/* Set up the vs_output_tab and routes. */
static void r300_vs_output_tab_routes(struct r300_context* r300,
int* vs_output_tab)
{
struct vertex_info* vinfo = &r300->vertex_info->vinfo;
boolean pos = FALSE, psize = FALSE, fog = FALSE;
int i, texs = 0, cols = 0;
struct tgsi_shader_info* info = &r300->fs->info;
/* XXX One day we should figure out how to handle a different number of
* VS outputs and FS inputs, as well as a different number of vertex streams
* and VS inputs. It's definitely one of the sources of hardlocks. */
for (i = 0; i < info->num_inputs; i++) {
switch (info->input_semantic_name[i]) {
case TGSI_SEMANTIC_POSITION:
pos = TRUE;
vs_output_tab[i] = 0;
break;
case TGSI_SEMANTIC_COLOR:
vs_output_tab[i] = 2 + cols;
cols++;
break;
case TGSI_SEMANTIC_PSIZE:
assert(psize == FALSE);
psize = TRUE;
vs_output_tab[i] = 15;
break;
case TGSI_SEMANTIC_FOG:
assert(fog == FALSE);
fog = TRUE;
/* Fall through */
case TGSI_SEMANTIC_GENERIC:
vs_output_tab[i] = 6 + texs;
texs++;
break;
default:
debug_printf("r300: Unknown vertex input %d\n",
info->input_semantic_name[i]);
break;
}
}
/* XXX magic */
assert(texs <= 8);
/* Do the actual vertex_info setup.
*
* vertex_info has four uints of hardware-specific data in it.
* vinfo.hwfmt[0] is R300_VAP_VTX_STATE_CNTL
* vinfo.hwfmt[1] is R300_VAP_VSM_VTX_ASSM
* vinfo.hwfmt[2] is R300_VAP_OUTPUT_VTX_FMT_0
* vinfo.hwfmt[3] is R300_VAP_OUTPUT_VTX_FMT_1 */
vinfo->hwfmt[0] = 0x5555; /* XXX this is classic Mesa bonghits */
/* We need to add vertex position attribute only for SW TCL case,
* for HW TCL case it could be generated by vertex shader */
if (!pos) {
2009-02-28 16:13:31 +00:00
/* Make room for the position attribute
* at the beginning of the vs_output_tab. */
for (i = 15; i > 0; i--) {
vs_output_tab[i] = vs_output_tab[i-1];
2009-02-28 16:13:31 +00:00
}
vs_output_tab[0] = 0;
}
/* Position. */
if (r300->draw) {
draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_PERSPECTIVE,
draw_find_vs_output(r300->draw, TGSI_SEMANTIC_POSITION, 0));
}
vinfo->hwfmt[1] |= R300_INPUT_CNTL_POS;
vinfo->hwfmt[2] |= R300_VAP_OUTPUT_VTX_FMT_0__POS_PRESENT;
/* Point size. */
if (psize) {
if (r300->draw) {
draw_emit_vertex_attr(vinfo, EMIT_1F_PSIZE, INTERP_POS,
draw_find_vs_output(r300->draw, TGSI_SEMANTIC_PSIZE, 0));
}
vinfo->hwfmt[2] |= R300_VAP_OUTPUT_VTX_FMT_0__PT_SIZE_PRESENT;
}
/* Colors. */
for (i = 0; i < cols; i++) {
if (r300->draw) {
draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_LINEAR,
draw_find_vs_output(r300->draw, TGSI_SEMANTIC_COLOR, i));
}
vinfo->hwfmt[1] |= R300_INPUT_CNTL_COLOR;
vinfo->hwfmt[2] |= (R300_VAP_OUTPUT_VTX_FMT_0__COLOR_0_PRESENT << i);
}
/* Init i right here, increment it if fog is enabled.
* This gets around a double-increment problem. */
i = 0;
/* Fog. This is a special-cased texcoord. */
if (fog) {
i++;
if (r300->draw) {
draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_PERSPECTIVE,
draw_find_vs_output(r300->draw, TGSI_SEMANTIC_FOG, 0));
}
vinfo->hwfmt[1] |= (R300_INPUT_CNTL_TC0 << i);
vinfo->hwfmt[3] |= (4 << (3 * i));
}
/* Texcoords. */
2009-09-27 11:31:55 +01:00
for (; i < texs; i++) {
if (r300->draw) {
draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_PERSPECTIVE,
draw_find_vs_output(r300->draw, TGSI_SEMANTIC_GENERIC, i));
}
vinfo->hwfmt[1] |= (R300_INPUT_CNTL_TC0 << i);
vinfo->hwfmt[3] |= (4 << (3 * i));
}
draw_compute_vertex_size(vinfo);
}
/* Update the PSC tables. */
static void r300_vertex_psc(struct r300_context* r300)
{
struct r300_vertex_info *vformat = r300->vertex_info;
uint16_t type, swizzle;
enum pipe_format format;
unsigned i;
/* Vertex shaders have no semantics on their inputs,
* so PSC should just route stuff based on the vertex elements,
* and not on attrib information. */
DBG(r300, DBG_DRAW, "r300: vs expects %d attribs, routing %d elements"
" in psc\n",
r300->vs->info.num_inputs,
r300->vertex_element_count);
for (i = 0; i < r300->vertex_element_count; i++) {
format = r300->vertex_element[i].src_format;
type = r300_translate_vertex_data_type(format) |
(i << R300_DST_VEC_LOC_SHIFT);
swizzle = r300_translate_vertex_data_swizzle(format);
if (i % 2) {
vformat->vap_prog_stream_cntl[i >> 1] |= type << 16;
vformat->vap_prog_stream_cntl_ext[i >> 1] |= swizzle << 16;
} else {
vformat->vap_prog_stream_cntl[i >> 1] |= type;
vformat->vap_prog_stream_cntl_ext[i >> 1] |= swizzle;
}
}
assert(i <= 15);
/* Set the last vector in the PSC. */
if (i) {
i -= 1;
}
vformat->vap_prog_stream_cntl[i >> 1] |=
(R300_LAST_VEC << (i & 1 ? 16 : 0));
}
/* Update the PSC tables for SW TCL, using Draw. */
static void r300_swtcl_vertex_psc(struct r300_context* r300,
int* vs_output_tab)
{
struct r300_vertex_info *vformat = r300->vertex_info;
struct vertex_info* vinfo = &vformat->vinfo;
uint16_t type, swizzle;
enum pipe_format format;
unsigned i, attrib_count;
/* For each Draw attribute, route it to the fragment shader according
* to the vs_output_tab. */
attrib_count = vinfo->num_attribs;
DBG(r300, DBG_DRAW, "r300: attrib count: %d\n", attrib_count);
for (i = 0; i < attrib_count; i++) {
DBG(r300, DBG_DRAW, "r300: attrib: offset %d, interp %d, size %d,"
" vs_output_tab %d\n", vinfo->attrib[i].src_index,
vinfo->attrib[i].interp_mode, vinfo->attrib[i].emit,
vs_output_tab[i]);
}
for (i = 0; i < attrib_count; i++) {
/* Make sure we have a proper destination for our attribute. */
assert(vs_output_tab[i] != -1);
format = draw_translate_vinfo_format(vinfo->attrib[i].emit);
/* Obtain the type of data in this attribute. */
type = r300_translate_vertex_data_type(format) |
vs_output_tab[i] << R300_DST_VEC_LOC_SHIFT;
/* Obtain the swizzle for this attribute. Note that the default
* swizzle in the hardware is not XYZW! */
swizzle = r300_translate_vertex_data_swizzle(format);
/* Add the attribute to the PSC table. */
if (i & 1) {
vformat->vap_prog_stream_cntl[i >> 1] |= type << 16;
vformat->vap_prog_stream_cntl_ext[i >> 1] |= swizzle << 16;
} else {
vformat->vap_prog_stream_cntl[i >> 1] |= type;
vformat->vap_prog_stream_cntl_ext[i >> 1] |= swizzle;
}
}
/* Set the last vector in the PSC. */
2009-10-18 23:54:39 +01:00
if (i) {
i -= 1;
}
vformat->vap_prog_stream_cntl[i >> 1] |=
(R300_LAST_VEC << (i & 1 ? 16 : 0));
}
/* Set up the RS block. This is the part of the chipset that actually does
* the rasterization of vertices into fragments. This is also the part of the
* chipset that locks up if any part of it is even slightly wrong. */
static void r300_update_rs_block(struct r300_context* r300)
{
struct r300_rs_block* rs = r300->rs_block;
struct tgsi_shader_info* info = &r300->fs->info;
int col_count = 0, fp_offset = 0, i, tex_count = 0;
int rs_tex_comp = 0;
if (r300_screen(r300->context.screen)->caps->is_r500) {
for (i = 0; i < info->num_inputs; i++) {
switch (info->input_semantic_name[i]) {
case TGSI_SEMANTIC_COLOR:
rs->ip[col_count] |=
R500_RS_COL_PTR(col_count) |
R500_RS_COL_FMT(R300_RS_COL_FMT_RGBA);
col_count++;
break;
case TGSI_SEMANTIC_GENERIC:
rs->ip[tex_count] |=
R500_RS_SEL_S(rs_tex_comp) |
R500_RS_SEL_T(rs_tex_comp + 1) |
R500_RS_SEL_R(rs_tex_comp + 2) |
R500_RS_SEL_Q(rs_tex_comp + 3);
tex_count++;
rs_tex_comp += 4;
break;
default:
break;
}
}
/* Rasterize at least one color, or bad things happen. */
if ((col_count == 0) && (tex_count == 0)) {
rs->ip[0] |= R500_RS_COL_FMT(R300_RS_COL_FMT_0001);
col_count++;
}
for (i = 0; i < col_count; i++) {
rs->inst[i] |= R500_RS_INST_COL_ID(i) |
R500_RS_INST_COL_CN_WRITE | R500_RS_INST_COL_ADDR(fp_offset);
fp_offset++;
}
for (i = 0; i < tex_count; i++) {
rs->inst[i] |= R500_RS_INST_TEX_ID(i) |
R500_RS_INST_TEX_CN_WRITE | R500_RS_INST_TEX_ADDR(fp_offset);
fp_offset++;
}
} else {
for (i = 0; i < info->num_inputs; i++) {
switch (info->input_semantic_name[i]) {
case TGSI_SEMANTIC_COLOR:
rs->ip[col_count] |=
R300_RS_COL_PTR(col_count) |
R300_RS_COL_FMT(R300_RS_COL_FMT_RGBA);
col_count++;
break;
case TGSI_SEMANTIC_GENERIC:
rs->ip[tex_count] |=
2009-09-27 21:42:25 +01:00
R300_RS_TEX_PTR(rs_tex_comp) |
R300_RS_SEL_S(R300_RS_SEL_C0) |
R300_RS_SEL_T(R300_RS_SEL_C1) |
R300_RS_SEL_R(R300_RS_SEL_C2) |
R300_RS_SEL_Q(R300_RS_SEL_C3);
tex_count++;
2009-09-27 21:42:25 +01:00
rs_tex_comp+=4;
break;
default:
break;
}
}
/* Rasterize at least one color, or bad things happen. */
if (col_count == 0) {
rs->ip[0] |= R300_RS_COL_FMT(R300_RS_COL_FMT_0001);
col_count++;
}
if (tex_count == 0) {
rs->ip[0] |=
R300_RS_SEL_S(R300_RS_SEL_K0) |
R300_RS_SEL_T(R300_RS_SEL_K0) |
R300_RS_SEL_R(R300_RS_SEL_K0) |
R300_RS_SEL_Q(R300_RS_SEL_K1);
}
for (i = 0; i < col_count; i++) {
rs->inst[i] |= R300_RS_INST_COL_ID(i) |
R300_RS_INST_COL_CN_WRITE | R300_RS_INST_COL_ADDR(fp_offset);
fp_offset++;
}
for (i = 0; i < tex_count; i++) {
rs->inst[i] |= R300_RS_INST_TEX_ID(i) |
R300_RS_INST_TEX_CN_WRITE | R300_RS_INST_TEX_ADDR(fp_offset);
fp_offset++;
}
}
rs->count = (rs_tex_comp) | (col_count << R300_IC_COUNT_SHIFT) |
R300_HIRES_EN;
2009-11-20 22:10:45 +00:00
rs->inst_count = MAX3(col_count - 1, tex_count - 1, 0);
}
/* Update the vertex format. */
static void r300_update_derived_shader_state(struct r300_context* r300)
{
struct r300_screen* r300screen = r300_screen(r300->context.screen);
int vs_output_tab[16];
int i;
/*
2009-10-25 12:53:25 +00:00
struct r300_shader_key* key;
struct r300_shader_derived_value* value;
key = CALLOC_STRUCT(r300_shader_key);
key->vs = r300->vs;
key->fs = r300->fs;
value = (struct r300_shader_derived_value*)
util_hash_table_get(r300->shader_hash_table, (void*)key);
if (value) {
//vformat = value->vformat;
rs_block = value->rs_block;
FREE(key);
} else {
rs_block = CALLOC_STRUCT(r300_rs_block);
value = CALLOC_STRUCT(r300_shader_derived_value);
2009-10-18 23:54:39 +01:00
r300_update_rs_block(r300, rs_block);
//value->vformat = vformat;
value->rs_block = rs_block;
util_hash_table_set(r300->shader_hash_table,
(void*)key, (void*)value);
} */
/* Reset structures */
memset(r300->rs_block, 0, sizeof(struct r300_rs_block));
memset(r300->vertex_info, 0, sizeof(struct r300_vertex_info));
for (i = 0; i < 16; i++) {
vs_output_tab[i] = -1;
}
/* Update states */
r300_vs_output_tab_routes(r300, vs_output_tab);
if (r300screen->caps->has_tcl) {
r300_vertex_psc(r300);
} else {
r300_swtcl_vertex_psc(r300, vs_output_tab);
}
r300_update_rs_block(r300);
r300->dirty_state |= R300_NEW_RS_BLOCK;
}
static void r300_update_ztop(struct r300_context* r300)
{
r300->ztop_state.z_buffer_top = R300_ZTOP_ENABLE;
/* This is important enough that I felt it warranted a comment.
*
* According to the docs, these are the conditions where ZTOP must be
* disabled:
* 1) Alpha testing enabled
* 2) Texture kill instructions in fragment shader
* 3) Chroma key culling enabled
* 4) W-buffering enabled
*
* The docs claim that for the first three cases, if no ZS writes happen,
* then ZTOP can be used.
*
* Additionally, the following conditions require disabled ZTOP:
* ~) Depth writes in fragment shader
* ~) Outstanding occlusion queries
*
* ~C.
*/
if (r300->dsa_state->alpha_function) {
r300->ztop_state.z_buffer_top = R300_ZTOP_DISABLE;
} else if (r300->fs->info.uses_kill) {
r300->ztop_state.z_buffer_top = R300_ZTOP_DISABLE;
} else if (r300_fragment_shader_writes_depth(r300->fs)) {
r300->ztop_state.z_buffer_top = R300_ZTOP_DISABLE;
} else if (r300->query_current) {
r300->ztop_state.z_buffer_top = R300_ZTOP_DISABLE;
}
}
void r300_update_derived_state(struct r300_context* r300)
{
if (r300->dirty_state &
(R300_NEW_FRAGMENT_SHADER | R300_NEW_VERTEX_SHADER |
R300_NEW_VERTEX_FORMAT)) {
r300_update_derived_shader_state(r300);
}
if (r300->dirty_state &
(R300_NEW_DSA | R300_NEW_FRAGMENT_SHADER | R300_NEW_QUERY)) {
r300_update_ztop(r300);
}
}