Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
  3.  * Copyright 2009 Marek Olšák <maraeo@gmail.com>
  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. #include "draw/draw_context.h"
  25.  
  26. #include "util/u_math.h"
  27. #include "util/u_memory.h"
  28. #include "util/u_pack_color.h"
  29.  
  30. #include "r300_context.h"
  31. #include "r300_fs.h"
  32. #include "r300_screen.h"
  33. #include "r300_shader_semantics.h"
  34. #include "r300_state_inlines.h"
  35. #include "r300_texture.h"
  36. #include "r300_vs.h"
  37.  
  38. /* r300_state_derived: Various bits of state which are dependent upon
  39.  * currently bound CSO data. */
  40.  
  41. enum r300_rs_swizzle {
  42.     SWIZ_XYZW = 0,
  43.     SWIZ_X001,
  44.     SWIZ_XY01,
  45.     SWIZ_0001,
  46. };
  47.  
  48. enum r300_rs_col_write_type {
  49.     WRITE_COLOR = 0,
  50.     WRITE_FACE
  51. };
  52.  
  53. static void r300_draw_emit_attrib(struct r300_context* r300,
  54.                                   enum attrib_emit emit,
  55.                                   enum interp_mode interp,
  56.                                   int index)
  57. {
  58.     struct r300_vertex_shader* vs = r300->vs_state.state;
  59.     struct tgsi_shader_info* info = &vs->info;
  60.     int output;
  61.  
  62.     output = draw_find_shader_output(r300->draw,
  63.                                      info->output_semantic_name[index],
  64.                                      info->output_semantic_index[index]);
  65.     draw_emit_vertex_attr(&r300->vertex_info, emit, interp, output);
  66. }
  67.  
  68. static void r300_draw_emit_all_attribs(struct r300_context* r300)
  69. {
  70.     struct r300_vertex_shader* vs = r300->vs_state.state;
  71.     struct r300_shader_semantics* vs_outputs = &vs->outputs;
  72.     int i, gen_count;
  73.  
  74.     /* Position. */
  75.     if (vs_outputs->pos != ATTR_UNUSED) {
  76.         r300_draw_emit_attrib(r300, EMIT_4F, INTERP_PERSPECTIVE,
  77.                               vs_outputs->pos);
  78.     } else {
  79.         assert(0);
  80.     }
  81.  
  82.     /* Point size. */
  83.     if (vs_outputs->psize != ATTR_UNUSED) {
  84.         r300_draw_emit_attrib(r300, EMIT_1F_PSIZE, INTERP_POS,
  85.                               vs_outputs->psize);
  86.     }
  87.  
  88.     /* Colors. */
  89.     for (i = 0; i < ATTR_COLOR_COUNT; i++) {
  90.         if (vs_outputs->color[i] != ATTR_UNUSED) {
  91.             r300_draw_emit_attrib(r300, EMIT_4F, INTERP_LINEAR,
  92.                                   vs_outputs->color[i]);
  93.         }
  94.     }
  95.  
  96.     /* Back-face colors. */
  97.     for (i = 0; i < ATTR_COLOR_COUNT; i++) {
  98.         if (vs_outputs->bcolor[i] != ATTR_UNUSED) {
  99.             r300_draw_emit_attrib(r300, EMIT_4F, INTERP_LINEAR,
  100.                                   vs_outputs->bcolor[i]);
  101.         }
  102.     }
  103.  
  104.     /* Texture coordinates. */
  105.     /* Only 8 generic vertex attributes can be used. If there are more,
  106.      * they won't be rasterized. */
  107.     gen_count = 0;
  108.     for (i = 0; i < ATTR_GENERIC_COUNT && gen_count < 8; i++) {
  109.         if (vs_outputs->generic[i] != ATTR_UNUSED &&
  110.             !(r300->sprite_coord_enable & (1 << i))) {
  111.             r300_draw_emit_attrib(r300, EMIT_4F, INTERP_PERSPECTIVE,
  112.                                   vs_outputs->generic[i]);
  113.             gen_count++;
  114.         }
  115.     }
  116.  
  117.     /* Fog coordinates. */
  118.     if (gen_count < 8 && vs_outputs->fog != ATTR_UNUSED) {
  119.         r300_draw_emit_attrib(r300, EMIT_4F, INTERP_PERSPECTIVE,
  120.                               vs_outputs->fog);
  121.         gen_count++;
  122.     }
  123.  
  124.     /* WPOS. */
  125.     if (r300_fs(r300)->shader->inputs.wpos != ATTR_UNUSED && gen_count < 8) {
  126.         DBG(r300, DBG_SWTCL, "draw_emit_attrib: WPOS, index: %i\n",
  127.             vs_outputs->wpos);
  128.         r300_draw_emit_attrib(r300, EMIT_4F, INTERP_PERSPECTIVE,
  129.                               vs_outputs->wpos);
  130.     }
  131. }
  132.  
  133. /* Update the PSC tables for SW TCL, using Draw. */
  134. static void r300_swtcl_vertex_psc(struct r300_context *r300)
  135. {
  136.     struct r300_vertex_stream_state *vstream = r300->vertex_stream_state.state;
  137.     struct vertex_info *vinfo = &r300->vertex_info;
  138.     uint16_t type, swizzle;
  139.     enum pipe_format format;
  140.     unsigned i, attrib_count;
  141.     int* vs_output_tab = r300->stream_loc_notcl;
  142.  
  143.     memset(vstream, 0, sizeof(struct r300_vertex_stream_state));
  144.  
  145.     /* For each Draw attribute, route it to the fragment shader according
  146.      * to the vs_output_tab. */
  147.     attrib_count = vinfo->num_attribs;
  148.     DBG(r300, DBG_SWTCL, "r300: attrib count: %d\n", attrib_count);
  149.     for (i = 0; i < attrib_count; i++) {
  150.         if (vs_output_tab[i] == -1) {
  151.             assert(0);
  152.             abort();
  153.         }
  154.  
  155.         format = draw_translate_vinfo_format(vinfo->attrib[i].emit);
  156.  
  157.         DBG(r300, DBG_SWTCL,
  158.             "r300: swtcl_vertex_psc [%i] <- %s\n",
  159.             vs_output_tab[i], util_format_short_name(format));
  160.  
  161.         /* Obtain the type of data in this attribute. */
  162.         type = r300_translate_vertex_data_type(format);
  163.         if (type == R300_INVALID_FORMAT) {
  164.             fprintf(stderr, "r300: Bad vertex format %s.\n",
  165.                     util_format_short_name(format));
  166.             assert(0);
  167.             abort();
  168.         }
  169.  
  170.         type |= vs_output_tab[i] << R300_DST_VEC_LOC_SHIFT;
  171.  
  172.         /* Obtain the swizzle for this attribute. Note that the default
  173.          * swizzle in the hardware is not XYZW! */
  174.         swizzle = r300_translate_vertex_data_swizzle(format);
  175.  
  176.         /* Add the attribute to the PSC table. */
  177.         if (i & 1) {
  178.             vstream->vap_prog_stream_cntl[i >> 1] |= type << 16;
  179.             vstream->vap_prog_stream_cntl_ext[i >> 1] |= swizzle << 16;
  180.         } else {
  181.             vstream->vap_prog_stream_cntl[i >> 1] |= type;
  182.             vstream->vap_prog_stream_cntl_ext[i >> 1] |= swizzle;
  183.         }
  184.     }
  185.  
  186.     /* Set the last vector in the PSC. */
  187.     if (i) {
  188.         i -= 1;
  189.     }
  190.     vstream->vap_prog_stream_cntl[i >> 1] |=
  191.         (R300_LAST_VEC << (i & 1 ? 16 : 0));
  192.  
  193.     vstream->count = (i >> 1) + 1;
  194.     r300_mark_atom_dirty(r300, &r300->vertex_stream_state);
  195.     r300->vertex_stream_state.size = (1 + vstream->count) * 2;
  196. }
  197.  
  198. static void r300_rs_col(struct r300_rs_block* rs, int id, int ptr,
  199.                         enum r300_rs_swizzle swiz)
  200. {
  201.     rs->ip[id] |= R300_RS_COL_PTR(ptr);
  202.     if (swiz == SWIZ_0001) {
  203.         rs->ip[id] |= R300_RS_COL_FMT(R300_RS_COL_FMT_0001);
  204.     } else {
  205.         rs->ip[id] |= R300_RS_COL_FMT(R300_RS_COL_FMT_RGBA);
  206.     }
  207.     rs->inst[id] |= R300_RS_INST_COL_ID(id);
  208. }
  209.  
  210. static void r300_rs_col_write(struct r300_rs_block* rs, int id, int fp_offset,
  211.                               enum r300_rs_col_write_type type)
  212. {
  213.     assert(type == WRITE_COLOR);
  214.     rs->inst[id] |= R300_RS_INST_COL_CN_WRITE |
  215.                     R300_RS_INST_COL_ADDR(fp_offset);
  216. }
  217.  
  218. static void r300_rs_tex(struct r300_rs_block* rs, int id, int ptr,
  219.                         enum r300_rs_swizzle swiz)
  220. {
  221.     if (swiz == SWIZ_X001) {
  222.         rs->ip[id] |= R300_RS_TEX_PTR(ptr) |
  223.                       R300_RS_SEL_S(R300_RS_SEL_C0) |
  224.                       R300_RS_SEL_T(R300_RS_SEL_K0) |
  225.                       R300_RS_SEL_R(R300_RS_SEL_K0) |
  226.                       R300_RS_SEL_Q(R300_RS_SEL_K1);
  227.     } else if (swiz == SWIZ_XY01) {
  228.         rs->ip[id] |= R300_RS_TEX_PTR(ptr) |
  229.                       R300_RS_SEL_S(R300_RS_SEL_C0) |
  230.                       R300_RS_SEL_T(R300_RS_SEL_C1) |
  231.                       R300_RS_SEL_R(R300_RS_SEL_K0) |
  232.                       R300_RS_SEL_Q(R300_RS_SEL_K1);
  233.     } else {
  234.         rs->ip[id] |= R300_RS_TEX_PTR(ptr) |
  235.                       R300_RS_SEL_S(R300_RS_SEL_C0) |
  236.                       R300_RS_SEL_T(R300_RS_SEL_C1) |
  237.                       R300_RS_SEL_R(R300_RS_SEL_C2) |
  238.                       R300_RS_SEL_Q(R300_RS_SEL_C3);
  239.     }
  240.     rs->inst[id] |= R300_RS_INST_TEX_ID(id);
  241. }
  242.  
  243. static void r300_rs_tex_write(struct r300_rs_block* rs, int id, int fp_offset)
  244. {
  245.     rs->inst[id] |= R300_RS_INST_TEX_CN_WRITE |
  246.                     R300_RS_INST_TEX_ADDR(fp_offset);
  247. }
  248.  
  249. static void r500_rs_col(struct r300_rs_block* rs, int id, int ptr,
  250.                         enum r300_rs_swizzle swiz)
  251. {
  252.     rs->ip[id] |= R500_RS_COL_PTR(ptr);
  253.     if (swiz == SWIZ_0001) {
  254.         rs->ip[id] |= R500_RS_COL_FMT(R300_RS_COL_FMT_0001);
  255.     } else {
  256.         rs->ip[id] |= R500_RS_COL_FMT(R300_RS_COL_FMT_RGBA);
  257.     }
  258.     rs->inst[id] |= R500_RS_INST_COL_ID(id);
  259. }
  260.  
  261. static void r500_rs_col_write(struct r300_rs_block* rs, int id, int fp_offset,
  262.                               enum r300_rs_col_write_type type)
  263. {
  264.     if (type == WRITE_FACE)
  265.         rs->inst[id] |= R500_RS_INST_COL_CN_WRITE_BACKFACE |
  266.                         R500_RS_INST_COL_ADDR(fp_offset);
  267.     else
  268.         rs->inst[id] |= R500_RS_INST_COL_CN_WRITE |
  269.                         R500_RS_INST_COL_ADDR(fp_offset);
  270.  
  271. }
  272.  
  273. static void r500_rs_tex(struct r300_rs_block* rs, int id, int ptr,
  274.                         enum r300_rs_swizzle swiz)
  275. {
  276.     if (swiz == SWIZ_X001) {
  277.         rs->ip[id] |= R500_RS_SEL_S(ptr) |
  278.                       R500_RS_SEL_T(R500_RS_IP_PTR_K0) |
  279.                       R500_RS_SEL_R(R500_RS_IP_PTR_K0) |
  280.                       R500_RS_SEL_Q(R500_RS_IP_PTR_K1);
  281.     } else if (swiz == SWIZ_XY01) {
  282.         rs->ip[id] |= R500_RS_SEL_S(ptr) |
  283.                       R500_RS_SEL_T(ptr + 1) |
  284.                       R500_RS_SEL_R(R500_RS_IP_PTR_K0) |
  285.                       R500_RS_SEL_Q(R500_RS_IP_PTR_K1);
  286.     } else {
  287.         rs->ip[id] |= R500_RS_SEL_S(ptr) |
  288.                       R500_RS_SEL_T(ptr + 1) |
  289.                       R500_RS_SEL_R(ptr + 2) |
  290.                       R500_RS_SEL_Q(ptr + 3);
  291.     }
  292.     rs->inst[id] |= R500_RS_INST_TEX_ID(id);
  293. }
  294.  
  295. static void r500_rs_tex_write(struct r300_rs_block* rs, int id, int fp_offset)
  296. {
  297.     rs->inst[id] |= R500_RS_INST_TEX_CN_WRITE |
  298.                     R500_RS_INST_TEX_ADDR(fp_offset);
  299. }
  300.  
  301. /* Set up the RS block.
  302.  *
  303.  * This is the part of the chipset that is responsible for linking vertex
  304.  * and fragment shaders and stuffed texture coordinates.
  305.  *
  306.  * The rasterizer reads data from VAP, which produces vertex shader outputs,
  307.  * and GA, which produces stuffed texture coordinates. VAP outputs have
  308.  * precedence over GA. All outputs must be rasterized otherwise it locks up.
  309.  * If there are more outputs rasterized than is set in VAP/GA, it locks up
  310.  * too. The funky part is that this info has been pretty much obtained by trial
  311.  * and error. */
  312. static void r300_update_rs_block(struct r300_context *r300)
  313. {
  314.     struct r300_vertex_shader *vs = r300->vs_state.state;
  315.     struct r300_shader_semantics *vs_outputs = &vs->outputs;
  316.     struct r300_shader_semantics *fs_inputs = &r300_fs(r300)->shader->inputs;
  317.     struct r300_rs_block rs = {0};
  318.     int i, col_count = 0, tex_count = 0, fp_offset = 0, count, loc = 0, tex_ptr = 0;
  319.     int gen_offset = 0;
  320.     void (*rX00_rs_col)(struct r300_rs_block*, int, int, enum r300_rs_swizzle);
  321.     void (*rX00_rs_col_write)(struct r300_rs_block*, int, int, enum r300_rs_col_write_type);
  322.     void (*rX00_rs_tex)(struct r300_rs_block*, int, int, enum r300_rs_swizzle);
  323.     void (*rX00_rs_tex_write)(struct r300_rs_block*, int, int);
  324.     boolean any_bcolor_used = vs_outputs->bcolor[0] != ATTR_UNUSED ||
  325.                               vs_outputs->bcolor[1] != ATTR_UNUSED;
  326.     int *stream_loc_notcl = r300->stream_loc_notcl;
  327.     uint32_t stuffing_enable = 0;
  328.  
  329.     if (r300->screen->caps.is_r500) {
  330.         rX00_rs_col       = r500_rs_col;
  331.         rX00_rs_col_write = r500_rs_col_write;
  332.         rX00_rs_tex       = r500_rs_tex;
  333.         rX00_rs_tex_write = r500_rs_tex_write;
  334.     } else {
  335.         rX00_rs_col       = r300_rs_col;
  336.         rX00_rs_col_write = r300_rs_col_write;
  337.         rX00_rs_tex       = r300_rs_tex;
  338.         rX00_rs_tex_write = r300_rs_tex_write;
  339.     }
  340.  
  341.     /* 0x5555 copied from classic, which means:
  342.      * Select user color 0 for COLOR0 up to COLOR7.
  343.      * What the hell does that mean? */
  344.     rs.vap_vtx_state_cntl = 0x5555;
  345.  
  346.     /* The position is always present in VAP. */
  347.     rs.vap_vsm_vtx_assm |= R300_INPUT_CNTL_POS;
  348.     rs.vap_out_vtx_fmt[0] |= R300_VAP_OUTPUT_VTX_FMT_0__POS_PRESENT;
  349.     stream_loc_notcl[loc++] = 0;
  350.  
  351.     /* Set up the point size in VAP. */
  352.     if (vs_outputs->psize != ATTR_UNUSED) {
  353.         rs.vap_out_vtx_fmt[0] |= R300_VAP_OUTPUT_VTX_FMT_0__PT_SIZE_PRESENT;
  354.         stream_loc_notcl[loc++] = 1;
  355.     }
  356.  
  357.     /* Set up and rasterize colors. */
  358.     for (i = 0; i < ATTR_COLOR_COUNT; i++) {
  359.         if (vs_outputs->color[i] != ATTR_UNUSED || any_bcolor_used ||
  360.             vs_outputs->color[1] != ATTR_UNUSED) {
  361.             /* Set up the color in VAP. */
  362.             rs.vap_vsm_vtx_assm |= R300_INPUT_CNTL_COLOR;
  363.             rs.vap_out_vtx_fmt[0] |=
  364.                     R300_VAP_OUTPUT_VTX_FMT_0__COLOR_0_PRESENT << i;
  365.             stream_loc_notcl[loc++] = 2 + i;
  366.  
  367.             /* Rasterize it. */
  368.             rX00_rs_col(&rs, col_count, col_count, SWIZ_XYZW);
  369.  
  370.             /* Write it to the FS input register if it's needed by the FS. */
  371.             if (fs_inputs->color[i] != ATTR_UNUSED) {
  372.                 rX00_rs_col_write(&rs, col_count, fp_offset, WRITE_COLOR);
  373.                 fp_offset++;
  374.  
  375.                 DBG(r300, DBG_RS,
  376.                     "r300: Rasterized color %i written to FS.\n", i);
  377.             } else {
  378.                 DBG(r300, DBG_RS, "r300: Rasterized color %i unused.\n", i);
  379.             }
  380.             col_count++;
  381.         } else {
  382.             /* Skip the FS input register, leave it uninitialized. */
  383.             /* If we try to set it to (0,0,0,1), it will lock up. */
  384.             if (fs_inputs->color[i] != ATTR_UNUSED) {
  385.                 fp_offset++;
  386.  
  387.                 DBG(r300, DBG_RS, "r300: FS input color %i unassigned%s.\n",
  388.                     i);
  389.             }
  390.         }
  391.     }
  392.  
  393.     /* Set up back-face colors. The rasterizer will do the color selection
  394.      * automatically. */
  395.     if (any_bcolor_used) {
  396.         if (r300->two_sided_color) {
  397.             /* Rasterize as back-face colors. */
  398.             for (i = 0; i < ATTR_COLOR_COUNT; i++) {
  399.                 rs.vap_vsm_vtx_assm |= R300_INPUT_CNTL_COLOR;
  400.                 rs.vap_out_vtx_fmt[0] |= R300_VAP_OUTPUT_VTX_FMT_0__COLOR_0_PRESENT << (2+i);
  401.                 stream_loc_notcl[loc++] = 4 + i;
  402.             }
  403.         } else {
  404.             /* Rasterize two fake texcoords to prevent from the two-sided color
  405.              * selection. */
  406.             /* XXX Consider recompiling the vertex shader to save 2 RS units. */
  407.             for (i = 0; i < 2; i++) {
  408.                 rs.vap_vsm_vtx_assm |= (R300_INPUT_CNTL_TC0 << tex_count);
  409.                 rs.vap_out_vtx_fmt[1] |= (4 << (3 * tex_count));
  410.                 stream_loc_notcl[loc++] = 6 + tex_count;
  411.  
  412.                 /* Rasterize it. */
  413.                 rX00_rs_tex(&rs, tex_count, tex_ptr, SWIZ_XYZW);
  414.                 tex_count++;
  415.                 tex_ptr += 4;
  416.             }
  417.         }
  418.     }
  419.  
  420.     /* gl_FrontFacing.
  421.      * Note that we can use either the two-sided color selection based on
  422.      * the front and back vertex shader colors, or gl_FrontFacing,
  423.      * but not both! It locks up otherwise.
  424.      *
  425.      * In Direct3D 9, the two-sided color selection can be used
  426.      * with shaders 2.0 only, while gl_FrontFacing can be used
  427.      * with shaders 3.0 only. The hardware apparently hasn't been designed
  428.      * to support both at the same time. */
  429.     if (r300->screen->caps.is_r500 && fs_inputs->face != ATTR_UNUSED &&
  430.         !(any_bcolor_used && r300->two_sided_color)) {
  431.         rX00_rs_col(&rs, col_count, col_count, SWIZ_XYZW);
  432.         rX00_rs_col_write(&rs, col_count, fp_offset, WRITE_FACE);
  433.         fp_offset++;
  434.         col_count++;
  435.         DBG(r300, DBG_RS, "r300: Rasterized FACE written to FS.\n");
  436.     } else if (fs_inputs->face != ATTR_UNUSED) {
  437.         fprintf(stderr, "r300: ERROR: FS input FACE unassigned.\n");
  438.     }
  439.  
  440.     /* Re-use color varyings for texcoords if possible.
  441.      *
  442.      * The colors are interpolated as 20-bit floats (reduced precision),
  443.      * Use this hack only if there are too many generic varyings.
  444.      * (number of generic varyings + fog + wpos > 8) */
  445.     if (r300->screen->caps.is_r500 && !any_bcolor_used && !r300->flatshade &&
  446.         fs_inputs->face == ATTR_UNUSED &&
  447.         vs_outputs->num_generic + (vs_outputs->fog != ATTR_UNUSED) +
  448.         (fs_inputs->wpos != ATTR_UNUSED) > 8) {
  449.         for (i = 0; i < ATTR_GENERIC_COUNT && col_count < 2; i++) {
  450.             /* Cannot use color varyings for sprite coords. */
  451.             if (fs_inputs->generic[i] != ATTR_UNUSED &&
  452.                 (r300->sprite_coord_enable & (1 << i))) {
  453.                 break;
  454.             }
  455.  
  456.             if (vs_outputs->generic[i] != ATTR_UNUSED) {
  457.                 /* Set up the color in VAP. */
  458.                 rs.vap_vsm_vtx_assm |= R300_INPUT_CNTL_COLOR;
  459.                 rs.vap_out_vtx_fmt[0] |=
  460.                         R300_VAP_OUTPUT_VTX_FMT_0__COLOR_0_PRESENT << col_count;
  461.                 stream_loc_notcl[loc++] = 2 + col_count;
  462.  
  463.                 /* Rasterize it. */
  464.                 rX00_rs_col(&rs, col_count, col_count, SWIZ_XYZW);
  465.  
  466.                 /* Write it to the FS input register if it's needed by the FS. */
  467.                 if (fs_inputs->generic[i] != ATTR_UNUSED) {
  468.                     rX00_rs_col_write(&rs, col_count, fp_offset, WRITE_COLOR);
  469.                     fp_offset++;
  470.  
  471.                     DBG(r300, DBG_RS,
  472.                         "r300: Rasterized generic %i redirected to color %i and written to FS.\n",
  473.                         i, col_count);
  474.                 } else {
  475.                     DBG(r300, DBG_RS, "r300: Rasterized generic %i redirected to color %i unused.\n",
  476.                         i, col_count);
  477.                 }
  478.                 col_count++;
  479.             } else {
  480.                 /* Skip the FS input register, leave it uninitialized. */
  481.                 /* If we try to set it to (0,0,0,1), it will lock up. */
  482.                 if (fs_inputs->generic[i] != ATTR_UNUSED) {
  483.                     fp_offset++;
  484.  
  485.                     DBG(r300, DBG_RS, "r300: FS input generic %i unassigned%s.\n", i);
  486.                 }
  487.             }
  488.         }
  489.         gen_offset = i;
  490.     }
  491.  
  492.     /* Rasterize texture coordinates. */
  493.     for (i = gen_offset; i < ATTR_GENERIC_COUNT && tex_count < 8; i++) {
  494.         boolean sprite_coord = false;
  495.  
  496.         if (fs_inputs->generic[i] != ATTR_UNUSED) {
  497.             sprite_coord = !!(r300->sprite_coord_enable & (1 << i));
  498.         }
  499.  
  500.         if (vs_outputs->generic[i] != ATTR_UNUSED || sprite_coord) {
  501.             if (!sprite_coord) {
  502.                 /* Set up the texture coordinates in VAP. */
  503.                 rs.vap_vsm_vtx_assm |= (R300_INPUT_CNTL_TC0 << tex_count);
  504.                 rs.vap_out_vtx_fmt[1] |= (4 << (3 * tex_count));
  505.                 stream_loc_notcl[loc++] = 6 + tex_count;
  506.             } else
  507.                 stuffing_enable |=
  508.                     R300_GB_TEX_ST << (R300_GB_TEX0_SOURCE_SHIFT + (tex_count*2));
  509.  
  510.             /* Rasterize it. */
  511.             rX00_rs_tex(&rs, tex_count, tex_ptr,
  512.                         sprite_coord ? SWIZ_XY01 : SWIZ_XYZW);
  513.  
  514.             /* Write it to the FS input register if it's needed by the FS. */
  515.             if (fs_inputs->generic[i] != ATTR_UNUSED) {
  516.                 rX00_rs_tex_write(&rs, tex_count, fp_offset);
  517.                 fp_offset++;
  518.  
  519.                 DBG(r300, DBG_RS,
  520.                     "r300: Rasterized generic %i written to FS%s in texcoord %d.\n",
  521.                     i, sprite_coord ? " (sprite coord)" : "", tex_count);
  522.             } else {
  523.                 DBG(r300, DBG_RS,
  524.                     "r300: Rasterized generic %i unused%s.\n",
  525.                     i, sprite_coord ? " (sprite coord)" : "");
  526.             }
  527.             tex_count++;
  528.             tex_ptr += sprite_coord ? 2 : 4;
  529.         } else {
  530.             /* Skip the FS input register, leave it uninitialized. */
  531.             /* If we try to set it to (0,0,0,1), it will lock up. */
  532.             if (fs_inputs->generic[i] != ATTR_UNUSED) {
  533.                 fp_offset++;
  534.  
  535.                 DBG(r300, DBG_RS, "r300: FS input generic %i unassigned%s.\n",
  536.                     i, sprite_coord ? " (sprite coord)" : "");
  537.             }
  538.         }
  539.     }
  540.  
  541.     for (; i < ATTR_GENERIC_COUNT; i++) {
  542.         if (fs_inputs->generic[i] != ATTR_UNUSED) {
  543.             fprintf(stderr, "r300: ERROR: FS input generic %i unassigned, "
  544.                     "not enough hardware slots (it's not a bug, do not "
  545.                     "report it).\n", i);
  546.         }
  547.     }
  548.  
  549.     /* Rasterize fog coordinates. */
  550.     if (vs_outputs->fog != ATTR_UNUSED && tex_count < 8) {
  551.         /* Set up the fog coordinates in VAP. */
  552.         rs.vap_vsm_vtx_assm |= (R300_INPUT_CNTL_TC0 << tex_count);
  553.         rs.vap_out_vtx_fmt[1] |= (4 << (3 * tex_count));
  554.         stream_loc_notcl[loc++] = 6 + tex_count;
  555.  
  556.         /* Rasterize it. */
  557.         rX00_rs_tex(&rs, tex_count, tex_ptr, SWIZ_X001);
  558.  
  559.         /* Write it to the FS input register if it's needed by the FS. */
  560.         if (fs_inputs->fog != ATTR_UNUSED) {
  561.             rX00_rs_tex_write(&rs, tex_count, fp_offset);
  562.             fp_offset++;
  563.  
  564.             DBG(r300, DBG_RS, "r300: Rasterized fog written to FS.\n");
  565.         } else {
  566.             DBG(r300, DBG_RS, "r300: Rasterized fog unused.\n");
  567.         }
  568.         tex_count++;
  569.         tex_ptr += 4;
  570.     } else {
  571.         /* Skip the FS input register, leave it uninitialized. */
  572.         /* If we try to set it to (0,0,0,1), it will lock up. */
  573.         if (fs_inputs->fog != ATTR_UNUSED) {
  574.             fp_offset++;
  575.  
  576.             if (tex_count < 8) {
  577.                 DBG(r300, DBG_RS, "r300: FS input fog unassigned.\n");
  578.             } else {
  579.                 fprintf(stderr, "r300: ERROR: FS input fog unassigned, "
  580.                         "not enough hardware slots. (it's not a bug, "
  581.                         "do not report it)\n");
  582.             }
  583.         }
  584.     }
  585.  
  586.     /* Rasterize WPOS. */
  587.     /* Don't set it in VAP if the FS doesn't need it. */
  588.     if (fs_inputs->wpos != ATTR_UNUSED && tex_count < 8) {
  589.         /* Set up the WPOS coordinates in VAP. */
  590.         rs.vap_vsm_vtx_assm |= (R300_INPUT_CNTL_TC0 << tex_count);
  591.         rs.vap_out_vtx_fmt[1] |= (4 << (3 * tex_count));
  592.         stream_loc_notcl[loc++] = 6 + tex_count;
  593.  
  594.         /* Rasterize it. */
  595.         rX00_rs_tex(&rs, tex_count, tex_ptr, SWIZ_XYZW);
  596.  
  597.         /* Write it to the FS input register. */
  598.         rX00_rs_tex_write(&rs, tex_count, fp_offset);
  599.  
  600.         DBG(r300, DBG_RS, "r300: Rasterized WPOS written to FS.\n");
  601.  
  602.         fp_offset++;
  603.         tex_count++;
  604.         tex_ptr += 4;
  605.     } else {
  606.         if (fs_inputs->wpos != ATTR_UNUSED && tex_count >= 8) {
  607.             fprintf(stderr, "r300: ERROR: FS input WPOS unassigned, "
  608.                     "not enough hardware slots. (it's not a bug, do not "
  609.                     "report it)\n");
  610.         }
  611.     }
  612.  
  613.     /* Invalidate the rest of the no-TCL (GA) stream locations. */
  614.     for (; loc < 16;) {
  615.         stream_loc_notcl[loc++] = -1;
  616.     }
  617.  
  618.     /* Rasterize at least one color, or bad things happen. */
  619.     if (col_count == 0 && tex_count == 0) {
  620.         rX00_rs_col(&rs, 0, 0, SWIZ_0001);
  621.         col_count++;
  622.  
  623.         DBG(r300, DBG_RS, "r300: Rasterized color 0 to prevent lockups.\n");
  624.     }
  625.  
  626.     DBG(r300, DBG_RS, "r300: --- Rasterizer status ---: colors: %i, "
  627.         "generics: %i.\n", col_count, tex_count);
  628.  
  629.     rs.count = MIN2(tex_ptr, 32) | (col_count << R300_IC_COUNT_SHIFT) |
  630.         R300_HIRES_EN;
  631.  
  632.     count = MAX3(col_count, tex_count, 1);
  633.     rs.inst_count = count - 1;
  634.  
  635.     /* set the GB enable flags */
  636.     if (r300->sprite_coord_enable)
  637.         stuffing_enable |= R300_GB_POINT_STUFF_ENABLE;
  638.  
  639.     rs.gb_enable = stuffing_enable;
  640.  
  641.     /* Now, after all that, see if we actually need to update the state. */
  642.     if (memcmp(r300->rs_block_state.state, &rs, sizeof(struct r300_rs_block))) {
  643.         memcpy(r300->rs_block_state.state, &rs, sizeof(struct r300_rs_block));
  644.         r300->rs_block_state.size = 13 + count*2;
  645.     }
  646. }
  647.  
  648. static void rgba_to_bgra(float color[4])
  649. {
  650.     float x = color[0];
  651.     color[0] = color[2];
  652.     color[2] = x;
  653. }
  654.  
  655. static uint32_t r300_get_border_color(enum pipe_format format,
  656.                                       const float border[4],
  657.                                       boolean is_r500)
  658. {
  659.     const struct util_format_description *desc;
  660.     float border_swizzled[4] = {0};
  661.     union util_color uc = {0};
  662.  
  663.     desc = util_format_description(format);
  664.  
  665.     /* Do depth formats first. */
  666.     if (util_format_is_depth_or_stencil(format)) {
  667.         switch (format) {
  668.         case PIPE_FORMAT_Z16_UNORM:
  669.             return util_pack_z(PIPE_FORMAT_Z16_UNORM, border[0]);
  670.         case PIPE_FORMAT_X8Z24_UNORM:
  671.         case PIPE_FORMAT_S8_UINT_Z24_UNORM:
  672.             if (is_r500) {
  673.                 return util_pack_z(PIPE_FORMAT_X8Z24_UNORM, border[0]);
  674.             } else {
  675.                 return util_pack_z(PIPE_FORMAT_Z16_UNORM, border[0]) << 16;
  676.             }
  677.         default:
  678.             assert(0);
  679.             return 0;
  680.         }
  681.     }
  682.  
  683.     /* Apply inverse swizzle of the format. */
  684.     util_format_unswizzle_4f(border_swizzled, border, desc->swizzle);
  685.  
  686.     /* Compressed formats. */
  687.     if (util_format_is_compressed(format)) {
  688.         switch (format) {
  689.         case PIPE_FORMAT_RGTC1_SNORM:
  690.         case PIPE_FORMAT_LATC1_SNORM:
  691.             border_swizzled[0] = border_swizzled[0] < 0 ?
  692.                                  border_swizzled[0]*0.5+1 :
  693.                                  border_swizzled[0]*0.5;
  694.             /* Pass through. */
  695.  
  696.         case PIPE_FORMAT_RGTC1_UNORM:
  697.         case PIPE_FORMAT_LATC1_UNORM:
  698.             /* Add 1/32 to round the border color instead of truncating. */
  699.             /* The Y component is used for the border color. */
  700.             border_swizzled[1] = border_swizzled[0] + 1.0f/32;
  701.             util_pack_color(border_swizzled, PIPE_FORMAT_B4G4R4A4_UNORM, &uc);
  702.             return uc.ui;
  703.         case PIPE_FORMAT_RGTC2_SNORM:
  704.         case PIPE_FORMAT_LATC2_SNORM:
  705.             util_pack_color(border_swizzled, PIPE_FORMAT_R8G8B8A8_SNORM, &uc);
  706.             return uc.ui;
  707.         case PIPE_FORMAT_RGTC2_UNORM:
  708.         case PIPE_FORMAT_LATC2_UNORM:
  709.             util_pack_color(border_swizzled, PIPE_FORMAT_R8G8B8A8_UNORM, &uc);
  710.             return uc.ui;
  711.         case PIPE_FORMAT_DXT1_SRGB:
  712.         case PIPE_FORMAT_DXT1_SRGBA:
  713.         case PIPE_FORMAT_DXT3_SRGBA:
  714.         case PIPE_FORMAT_DXT5_SRGBA:
  715.             util_pack_color(border_swizzled, PIPE_FORMAT_B8G8R8A8_SRGB, &uc);
  716.             return uc.ui;
  717.         default:
  718.             util_pack_color(border_swizzled, PIPE_FORMAT_B8G8R8A8_UNORM, &uc);
  719.             return uc.ui;
  720.         }
  721.     }
  722.  
  723.     switch (desc->channel[0].size) {
  724.         case 2:
  725.             rgba_to_bgra(border_swizzled);
  726.             util_pack_color(border_swizzled, PIPE_FORMAT_B2G3R3_UNORM, &uc);
  727.             break;
  728.  
  729.         case 4:
  730.             rgba_to_bgra(border_swizzled);
  731.             util_pack_color(border_swizzled, PIPE_FORMAT_B4G4R4A4_UNORM, &uc);
  732.             break;
  733.  
  734.         case 5:
  735.             rgba_to_bgra(border_swizzled);
  736.             if (desc->channel[1].size == 5) {
  737.                 util_pack_color(border_swizzled, PIPE_FORMAT_B5G5R5A1_UNORM, &uc);
  738.             } else if (desc->channel[1].size == 6) {
  739.                 util_pack_color(border_swizzled, PIPE_FORMAT_B5G6R5_UNORM, &uc);
  740.             } else {
  741.                 assert(0);
  742.             }
  743.             break;
  744.  
  745.         default:
  746.         case 8:
  747.             if (desc->channel[0].type == UTIL_FORMAT_TYPE_SIGNED) {
  748.                 util_pack_color(border_swizzled, PIPE_FORMAT_R8G8B8A8_SNORM, &uc);
  749.             } else if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
  750.                 if (desc->nr_channels == 2) {
  751.                     border_swizzled[3] = border_swizzled[1];
  752.                     util_pack_color(border_swizzled, PIPE_FORMAT_L8A8_SRGB, &uc);
  753.                 } else {
  754.                     util_pack_color(border_swizzled, PIPE_FORMAT_R8G8B8A8_SRGB, &uc);
  755.                 }
  756.             } else {
  757.                 util_pack_color(border_swizzled, PIPE_FORMAT_R8G8B8A8_UNORM, &uc);
  758.             }
  759.             break;
  760.  
  761.         case 10:
  762.             util_pack_color(border_swizzled, PIPE_FORMAT_R10G10B10A2_UNORM, &uc);
  763.             break;
  764.  
  765.         case 16:
  766.             if (desc->nr_channels <= 2) {
  767.                 if (desc->channel[0].type == UTIL_FORMAT_TYPE_FLOAT) {
  768.                     util_pack_color(border_swizzled, PIPE_FORMAT_R16G16_FLOAT, &uc);
  769.                 } else if (desc->channel[0].type == UTIL_FORMAT_TYPE_SIGNED) {
  770.                     util_pack_color(border_swizzled, PIPE_FORMAT_R16G16_SNORM, &uc);
  771.                 } else {
  772.                     util_pack_color(border_swizzled, PIPE_FORMAT_R16G16_UNORM, &uc);
  773.                 }
  774.             } else {
  775.                 if (desc->channel[0].type == UTIL_FORMAT_TYPE_SIGNED) {
  776.                     util_pack_color(border_swizzled, PIPE_FORMAT_R8G8B8A8_SNORM, &uc);
  777.                 } else {
  778.                     util_pack_color(border_swizzled, PIPE_FORMAT_R8G8B8A8_UNORM, &uc);
  779.                 }
  780.             }
  781.             break;
  782.  
  783.         case 32:
  784.             if (desc->nr_channels == 1) {
  785.                 util_pack_color(border_swizzled, PIPE_FORMAT_R32_FLOAT, &uc);
  786.             } else {
  787.                 util_pack_color(border_swizzled, PIPE_FORMAT_R8G8B8A8_UNORM, &uc);
  788.             }
  789.             break;
  790.     }
  791.  
  792.     return uc.ui;
  793. }
  794.  
  795. static void r300_merge_textures_and_samplers(struct r300_context* r300)
  796. {
  797.     struct r300_textures_state *state =
  798.         (struct r300_textures_state*)r300->textures_state.state;
  799.     struct r300_texture_sampler_state *texstate;
  800.     struct r300_sampler_state *sampler;
  801.     struct r300_sampler_view *view;
  802.     struct r300_resource *tex;
  803.     unsigned base_level, min_level, level_count, i, j, size;
  804.     unsigned count = MIN2(state->sampler_view_count,
  805.                           state->sampler_state_count);
  806.     boolean has_us_format = r300->screen->caps.has_us_format;
  807.  
  808.     /* The KIL opcode fix, see below. */
  809.     if (!count && !r300->screen->caps.is_r500)
  810.         count = 1;
  811.  
  812.     state->tx_enable = 0;
  813.     state->count = 0;
  814.     size = 2;
  815.  
  816.     for (i = 0; i < count; i++) {
  817.         if (state->sampler_views[i] && state->sampler_states[i]) {
  818.             state->tx_enable |= 1 << i;
  819.  
  820.             view = state->sampler_views[i];
  821.             tex = r300_resource(view->base.texture);
  822.             sampler = state->sampler_states[i];
  823.  
  824.             texstate = &state->regs[i];
  825.             texstate->format = view->format;
  826.             texstate->filter0 = sampler->filter0;
  827.             texstate->filter1 = sampler->filter1;
  828.  
  829.             /* Set the border color. */
  830.             texstate->border_color =
  831.                 r300_get_border_color(view->base.format,
  832.                                       sampler->state.border_color.f,
  833.                                       r300->screen->caps.is_r500);
  834.  
  835.             /* determine min/max levels */
  836.             base_level = view->base.u.tex.first_level;
  837.             min_level = sampler->min_lod;
  838.             level_count = MIN3(sampler->max_lod,
  839.                                tex->b.b.last_level - base_level,
  840.                                view->base.u.tex.last_level - base_level);
  841.  
  842.             if (base_level + min_level) {
  843.                 unsigned offset;
  844.  
  845.                 if (tex->tex.is_npot) {
  846.                     /* Even though we do not implement mipmapping for NPOT
  847.                      * textures, we should at least honor the minimum level
  848.                      * which is allowed to be displayed. We do this by setting up
  849.                      * an i-th mipmap level as the zero level. */
  850.                     base_level += min_level;
  851.                 }
  852.                 offset = tex->tex.offset_in_bytes[base_level];
  853.  
  854.                 r300_texture_setup_format_state(r300->screen, tex,
  855.                                                 view->base.format,
  856.                                                 base_level,
  857.                                                 view->width0_override,
  858.                                                 view->height0_override,
  859.                                                 &texstate->format);
  860.                 texstate->format.tile_config |= offset & 0xffffffe0;
  861.                 assert((offset & 0x1f) == 0);
  862.             }
  863.  
  864.             /* Assign a texture cache region. */
  865.             texstate->format.format1 |= view->texcache_region;
  866.  
  867.             /* Depth textures are kinda special. */
  868.             if (util_format_is_depth_or_stencil(view->base.format)) {
  869.                 unsigned char depth_swizzle[4];
  870.  
  871.                 if (!r300->screen->caps.is_r500 &&
  872.                     util_format_get_blocksizebits(view->base.format) == 32) {
  873.                     /* X24x8 is sampled as Y16X16 on r3xx-r4xx.
  874.                      * The depth here is at the Y component. */
  875.                     for (j = 0; j < 4; j++)
  876.                         depth_swizzle[j] = UTIL_FORMAT_SWIZZLE_Y;
  877.                 } else {
  878.                     for (j = 0; j < 4; j++)
  879.                         depth_swizzle[j] = UTIL_FORMAT_SWIZZLE_X;
  880.                 }
  881.  
  882.                 /* If compare mode is disabled, sampler view swizzles
  883.                  * are stored in the format.
  884.                  * Otherwise, the swizzles must be applied after the compare
  885.                  * mode in the fragment shader. */
  886.                 if (sampler->state.compare_mode == PIPE_TEX_COMPARE_NONE) {
  887.                     texstate->format.format1 |=
  888.                         r300_get_swizzle_combined(depth_swizzle,
  889.                                                   view->swizzle, FALSE);
  890.                 } else {
  891.                     texstate->format.format1 |=
  892.                         r300_get_swizzle_combined(depth_swizzle, 0, FALSE);
  893.                 }
  894.             }
  895.  
  896.             if (r300->screen->caps.dxtc_swizzle &&
  897.                 util_format_is_compressed(view->base.format)) {
  898.                 texstate->filter1 |= R400_DXTC_SWIZZLE_ENABLE;
  899.             }
  900.  
  901.             /* to emulate 1D textures through 2D ones correctly */
  902.             if (tex->b.b.target == PIPE_TEXTURE_1D) {
  903.                 texstate->filter0 &= ~R300_TX_WRAP_T_MASK;
  904.                 texstate->filter0 |= R300_TX_WRAP_T(R300_TX_CLAMP_TO_EDGE);
  905.             }
  906.  
  907.             /* The hardware doesn't like CLAMP and CLAMP_TO_BORDER
  908.              * for the 3rd coordinate if the texture isn't 3D. */
  909.             if (tex->b.b.target != PIPE_TEXTURE_3D) {
  910.                 texstate->filter0 &= ~R300_TX_WRAP_R_MASK;
  911.             }
  912.  
  913.             if (tex->tex.is_npot) {
  914.                 /* NPOT textures don't support mip filter, unfortunately.
  915.                  * This prevents incorrect rendering. */
  916.                 texstate->filter0 &= ~R300_TX_MIN_FILTER_MIP_MASK;
  917.  
  918.                 /* Mask out the mirrored flag. */
  919.                 if (texstate->filter0 & R300_TX_WRAP_S(R300_TX_MIRRORED)) {
  920.                     texstate->filter0 &= ~R300_TX_WRAP_S(R300_TX_MIRRORED);
  921.                 }
  922.                 if (texstate->filter0 & R300_TX_WRAP_T(R300_TX_MIRRORED)) {
  923.                     texstate->filter0 &= ~R300_TX_WRAP_T(R300_TX_MIRRORED);
  924.                 }
  925.  
  926.                 /* Change repeat to clamp-to-edge.
  927.                  * (the repeat bit has a value of 0, no masking needed). */
  928.                 if ((texstate->filter0 & R300_TX_WRAP_S_MASK) ==
  929.                     R300_TX_WRAP_S(R300_TX_REPEAT)) {
  930.                     texstate->filter0 |= R300_TX_WRAP_S(R300_TX_CLAMP_TO_EDGE);
  931.                 }
  932.                 if ((texstate->filter0 & R300_TX_WRAP_T_MASK) ==
  933.                     R300_TX_WRAP_T(R300_TX_REPEAT)) {
  934.                     texstate->filter0 |= R300_TX_WRAP_T(R300_TX_CLAMP_TO_EDGE);
  935.                 }
  936.             } else {
  937.                 /* the MAX_MIP level is the largest (finest) one */
  938.                 texstate->format.format0 |= R300_TX_NUM_LEVELS(level_count);
  939.                 texstate->filter0 |= R300_TX_MAX_MIP_LEVEL(min_level);
  940.             }
  941.  
  942.             /* Float textures only support nearest and mip-nearest filtering. */
  943.             if (util_format_is_float(view->base.format)) {
  944.                 /* No MAG linear filtering. */
  945.                 if ((texstate->filter0 & R300_TX_MAG_FILTER_MASK) ==
  946.                     R300_TX_MAG_FILTER_LINEAR) {
  947.                     texstate->filter0 &= ~R300_TX_MAG_FILTER_MASK;
  948.                     texstate->filter0 |= R300_TX_MAG_FILTER_NEAREST;
  949.                 }
  950.                 /* No MIN linear filtering. */
  951.                 if ((texstate->filter0 & R300_TX_MIN_FILTER_MASK) ==
  952.                     R300_TX_MIN_FILTER_LINEAR) {
  953.                     texstate->filter0 &= ~R300_TX_MIN_FILTER_MASK;
  954.                     texstate->filter0 |= R300_TX_MIN_FILTER_NEAREST;
  955.                 }
  956.                 /* No mipmap linear filtering. */
  957.                 if ((texstate->filter0 & R300_TX_MIN_FILTER_MIP_MASK) ==
  958.                     R300_TX_MIN_FILTER_MIP_LINEAR) {
  959.                     texstate->filter0 &= ~R300_TX_MIN_FILTER_MIP_MASK;
  960.                     texstate->filter0 |= R300_TX_MIN_FILTER_MIP_NEAREST;
  961.                 }
  962.                 /* No anisotropic filtering. */
  963.                 texstate->filter0 &= ~R300_TX_MAX_ANISO_MASK;
  964.                 texstate->filter1 &= ~R500_TX_MAX_ANISO_MASK;
  965.                 texstate->filter1 &= ~R500_TX_ANISO_HIGH_QUALITY;
  966.             }
  967.  
  968.             texstate->filter0 |= i << 28;
  969.  
  970.             size += 16 + (has_us_format ? 2 : 0);
  971.             state->count = i+1;
  972.         } else {
  973.             /* For the KIL opcode to work on r3xx-r4xx, the texture unit
  974.              * assigned to this opcode (it's always the first one) must be
  975.              * enabled. Otherwise the opcode doesn't work.
  976.              *
  977.              * In order to not depend on the fragment shader, we just make
  978.              * the first unit enabled all the time. */
  979.             if (i == 0 && !r300->screen->caps.is_r500) {
  980.                 pipe_sampler_view_reference(
  981.                         (struct pipe_sampler_view**)&state->sampler_views[i],
  982.                         &r300->texkill_sampler->base);
  983.  
  984.                 state->tx_enable |= 1 << i;
  985.  
  986.                 texstate = &state->regs[i];
  987.  
  988.                 /* Just set some valid state. */
  989.                 texstate->format = r300->texkill_sampler->format;
  990.                 texstate->filter0 =
  991.                         r300_translate_tex_filters(PIPE_TEX_FILTER_NEAREST,
  992.                                                    PIPE_TEX_FILTER_NEAREST,
  993.                                                    PIPE_TEX_FILTER_NEAREST,
  994.                                                    FALSE);
  995.                 texstate->filter1 = 0;
  996.                 texstate->border_color = 0;
  997.  
  998.                 texstate->filter0 |= i << 28;
  999.                 size += 16 + (has_us_format ? 2 : 0);
  1000.                 state->count = i+1;
  1001.             }
  1002.         }
  1003.     }
  1004.  
  1005.     r300->textures_state.size = size;
  1006.  
  1007.     /* Pick a fragment shader based on either the texture compare state
  1008.      * or the uses_pitch flag or some other external state. */
  1009.     if (count &&
  1010.         r300->fs_status == FRAGMENT_SHADER_VALID) {
  1011.         r300->fs_status = FRAGMENT_SHADER_MAYBE_DIRTY;
  1012.     }
  1013. }
  1014.  
  1015. static void r300_decompress_depth_textures(struct r300_context *r300)
  1016. {
  1017.     struct r300_textures_state *state =
  1018.         (struct r300_textures_state*)r300->textures_state.state;
  1019.     struct pipe_resource *tex;
  1020.     unsigned count = MIN2(state->sampler_view_count,
  1021.                           state->sampler_state_count);
  1022.     unsigned i;
  1023.  
  1024.     if (!r300->locked_zbuffer) {
  1025.         return;
  1026.     }
  1027.  
  1028.     for (i = 0; i < count; i++) {
  1029.         if (state->sampler_views[i] && state->sampler_states[i]) {
  1030.             tex = state->sampler_views[i]->base.texture;
  1031.  
  1032.             if (tex == r300->locked_zbuffer->texture) {
  1033.                 r300_decompress_zmask_locked(r300);
  1034.                 return;
  1035.             }
  1036.         }
  1037.     }
  1038. }
  1039.  
  1040. static void r300_validate_fragment_shader(struct r300_context *r300)
  1041. {
  1042.     struct pipe_framebuffer_state *fb = r300->fb_state.state;
  1043.  
  1044.     if (r300->fs.state && r300->fs_status != FRAGMENT_SHADER_VALID) {
  1045.         /* Pick the fragment shader based on external states.
  1046.          * Then mark the state dirty if the fragment shader is either dirty
  1047.          * or the function r300_pick_fragment_shader changed the shader. */
  1048.         if (r300_pick_fragment_shader(r300) ||
  1049.             r300->fs_status == FRAGMENT_SHADER_DIRTY) {
  1050.             /* Mark the state atom as dirty. */
  1051.             r300_mark_fs_code_dirty(r300);
  1052.  
  1053.             /* Does Multiwrite need to be changed? */
  1054.             if (fb->nr_cbufs > 1) {
  1055.                 boolean new_multiwrite =
  1056.                     r300_fragment_shader_writes_all(r300_fs(r300));
  1057.  
  1058.                 if (r300->fb_multiwrite != new_multiwrite) {
  1059.                     r300->fb_multiwrite = new_multiwrite;
  1060.                     r300_mark_fb_state_dirty(r300, R300_CHANGED_MULTIWRITE);
  1061.                 }
  1062.             }
  1063.         }
  1064.         r300->fs_status = FRAGMENT_SHADER_VALID;
  1065.     }
  1066. }
  1067.  
  1068. void r300_update_derived_state(struct r300_context* r300)
  1069. {
  1070.     if (r300->textures_state.dirty) {
  1071.         r300_decompress_depth_textures(r300);
  1072.         r300_merge_textures_and_samplers(r300);
  1073.     }
  1074.  
  1075.     r300_validate_fragment_shader(r300);
  1076.  
  1077.     if (r300->rs_block_state.dirty) {
  1078.         r300_update_rs_block(r300);
  1079.  
  1080.         if (r300->draw) {
  1081.             memset(&r300->vertex_info, 0, sizeof(struct vertex_info));
  1082.             r300_draw_emit_all_attribs(r300);
  1083.             draw_compute_vertex_size(&r300->vertex_info);
  1084.             r300_swtcl_vertex_psc(r300);
  1085.         }
  1086.     }
  1087.  
  1088.     r300_update_hyperz_state(r300);
  1089. }
  1090.