r300: further cleanup
[mesa.git] / src / mesa / drivers / dri / r300 / r300_context.h
index bc43953ff38099800251cdd173b39b4749767933..ad8b5a2ae4550213712c65b42bb474c05a129610 100644 (file)
@@ -27,54 +27,33 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
 **************************************************************************/
 
-/*
- * Authors:
- *   Keith Whitwell <keith@tungstengraphics.com>
- *   Nicolai Haehnle <prefect_@gmx.net>
+/**
+ * \file
+ *
+ * \author Keith Whitwell <keith@tungstengraphics.com>
+ * \author Nicolai Haehnle <prefect_@gmx.net>
  */
 
 #ifndef __R300_CONTEXT_H__
 #define __R300_CONTEXT_H__
 
-#include "tnl/t_vertex.h"
 #include "drm.h"
 #include "radeon_drm.h"
 #include "dri_util.h"
-#include "texmem.h"
-
-#include "macros.h"
-#include "mtypes.h"
-#include "colormac.h"
-#include "radeon_context.h"
-
-#define USER_BUFFERS
-/* KW: Disable this code.  Driver should hook into vbo module
- * directly, see i965 driver for example.
- */
-/* #define RADEON_VTXFMT_A */
-#define HW_VBOS
-
-/* We don't handle 16 bits elts swapping yet */
-#ifdef MESA_BIG_ENDIAN
-#define FORCE_32BITS_ELTS
-#endif
+#include "radeon_common.h"
 
-//#define OPTIMIZE_ELTS
+#include "main/mtypes.h"
+#include "shader/prog_instruction.h"
 
 struct r300_context;
 typedef struct r300_context r300ContextRec;
 typedef struct r300_context *r300ContextPtr;
 
-#include "radeon_lock.h"
-#include "mm.h"
 
-/* Checkpoint.. for convenience */
-#define CPT    { fprintf(stderr, "%s:%s line %d\n", __FILE__, __FUNCTION__, __LINE__); }
-/* From http://gcc.gnu.org/onlinedocs/gcc-3.2.3/gcc/Variadic-Macros.html .
-   I suppose we could inline this and use macro to fetch out __LINE__ and stuff in case we run into trouble 
+/* From http://gcc. gnu.org/onlinedocs/gcc-3.2.3/gcc/Variadic-Macros.html .
+   I suppose we could inline this and use macro to fetch out __LINE__ and stuff in case we run into trouble
    with other compilers ... GLUE!
 */
-#if 1
 #define WARN_ONCE(a, ...)      { \
        static int warn##__LINE__=1; \
        if(warn##__LINE__){ \
@@ -86,129 +65,8 @@ typedef struct r300_context *r300ContextPtr;
                warn##__LINE__=0;\
                } \
        }
-#else
-#define WARN_ONCE(a, ...) {}
-#endif
-
-  /* We should probably change types within vertex_shader
-      and pixel_shader structure later on */
-#define CARD32 GLuint
-#include "vertex_shader.h"
-#include "r300_fragprog.h"
-#undef CARD32
-
-static __inline__ uint32_t r300PackFloat32(float fl)
-{
-       union { float fl; uint32_t u; } u;
-
-       u.fl = fl;
-       return u.u;
-}
-
-
-/************ DMA BUFFERS **************/
-
-/* Need refcounting on dma buffers:
- */
-struct r300_dma_buffer {
-       int refcount;           /* the number of retained regions in buf */
-       drmBufPtr buf;
-       int id;
-};
-#undef GET_START
-#ifdef USER_BUFFERS
-#define GET_START(rvb) (r300GartOffsetFromVirtual(rmesa, (rvb)->address+(rvb)->start))
-#else
-#define GET_START(rvb) (rmesa->radeon.radeonScreen->gart_buffer_offset +               \
-                       (rvb)->address - rmesa->dma.buf0_address +      \
-                       (rvb)->start)
-#endif
-/* A retained region, eg vertices for indexed vertices.
- */
-struct r300_dma_region {
-       struct r300_dma_buffer *buf;
-       char *address;          /* == buf->address */
-       int start, end, ptr;    /* offsets from start of buf */
-
-    int aos_offset;     /* address in GART memory */
-    int aos_stride;     /* distance between elements, in dwords */
-    int aos_size;       /* number of components (1-4) */
-    int aos_reg;        /* VAP register assignment */
-};
-
-struct r300_dma {
-       /* Active dma region.  Allocations for vertices and retained
-        * regions come from here.  Also used for emitting random vertices,
-        * these may be flushed by calling flush_current();
-        */
-       struct r300_dma_region current;
-
-       void (*flush) (r300ContextPtr);
-
-       char *buf0_address;     /* start of buf[0], for index calcs */
-
-       /* Number of "in-flight" DMA buffers, i.e. the number of buffers
-        * for which a DISCARD command is currently queued in the command buffer.
-        */
-       GLuint nr_released_bufs;
-};
-
-       /* Texture related */
-
-typedef struct r300_tex_obj r300TexObj, *r300TexObjPtr;
-
-/* Texture object in locally shared texture space.
- */
-struct r300_tex_obj {
-       driTextureObject base;
-
-       GLuint bufAddr;         /* Offset to start of locally
-                                  shared texture block */
-
-       GLuint dirty_state;     /* Flags (1 per texunit) for
-                                  whether or not this texobj
-                                  has dirty hardware state
-                                  (pp_*) that needs to be
-                                  brought into the
-                                  texunit. */
-
-       drm_radeon_tex_image_t image[6][RADEON_MAX_TEXTURE_LEVELS];
-       /* Six, for the cube faces */
-
-
-       GLuint pitch; /* this isn't sent to hardware just used in calculations */
-       /* hardware register values */
-       /* Note that R200 has 8 registers per texture and R300 only 7 */
-       GLuint filter;
-       GLuint filter_1;
-       GLuint pitch_reg;
-       GLuint size;    /* npot only */
-       GLuint format;
-       GLuint offset;  /* Image location in the card's address space.
-                                  All cube faces follow. */
-       GLuint unknown4;
-       GLuint unknown5;
-       /* end hardware registers */
-
-       /* registers computed by r200 code - keep them here to
-          compare against what is actually written.
-
-          to be removed later.. */
-       GLuint pp_border_color;
-       GLuint pp_cubic_faces;  /* cube face 1,2,3,4 log2 sizes */
-       GLuint format_x;
-
-
-       GLboolean border_fallback;
-
-       GLuint tile_bits; /* hw texture tile bits used on this texture */
-};
 
-struct r300_texture_env_state {
-       r300TexObjPtr texobj;
-       GLenum format;
-       GLenum envMode;
-};
+#include "r300_vertprog.h"
 
 
 /* The blit width for texture uploads
@@ -216,28 +74,6 @@ struct r300_texture_env_state {
 #define R300_BLIT_WIDTH_BYTES 1024
 #define R300_MAX_TEXTURE_UNITS 8
 
-struct r300_texture_state {
-       struct r300_texture_env_state unit[R300_MAX_TEXTURE_UNITS];
-       int tc_count; /* number of incoming texture coordinates from VAP */
-};
-
-/**
- * A block of hardware state.
- *
- * When check returns non-zero, the returned number of dwords must be
- * copied verbatim into the command buffer in order to update a state atom
- * when it is dirty.
- */
-struct r300_state_atom {
-       struct r300_state_atom *next, *prev;
-       const char* name;       /* for debug */
-       int cmd_size;           /* maximum size in dwords */
-       GLuint idx;             /* index in an array (e.g. textures) */
-       uint32_t* cmd;
-       GLboolean dirty;
-
-       int (*check)(r300ContextPtr, struct r300_state_atom* atom);
-};
 
 
 #define R300_VPT_CMD_0         0
@@ -249,7 +85,7 @@ struct r300_state_atom {
 #define R300_VPT_ZOFFSET       6
 #define R300_VPT_CMDSIZE       7
 
-#define R300_VIR_CMD_0         0 /* vir is variable size (at least 1) */
+#define R300_VIR_CMD_0         0       /* vir is variable size (at least 1) */
 #define R300_VIR_CNTL_0                1
 #define R300_VIR_CNTL_1                2
 #define R300_VIR_CNTL_2                3
@@ -270,7 +106,6 @@ struct r300_state_atom {
 #define R300_VOF_CNTL_1                2
 #define R300_VOF_CMDSIZE       3
 
-
 #define R300_PVS_CMD_0         0
 #define R300_PVS_CNTL_1                1
 #define R300_PVS_CNTL_2                2
@@ -281,9 +116,11 @@ struct r300_state_atom {
 #define R300_GB_MISC_MSPOS_0           1
 #define R300_GB_MISC_MSPOS_1           2
 #define R300_GB_MISC_TILE_CONFIG       3
-#define R300_GB_MISC_SELECT            4
-#define R300_GB_MISC_AA_CONFIG         5
-#define R300_GB_MISC_CMDSIZE           6
+#define R300_GB_MISC_CMDSIZE           4
+#define R300_GB_MISC2_CMD_0                0
+#define R300_GB_MISC2_SELECT           1
+#define R300_GB_MISC2_AA_CONFIG                2
+#define R300_GB_MISC2_CMDSIZE          3
 
 #define R300_TXE_CMD_0         0
 #define R300_TXE_ENABLE                1
@@ -320,15 +157,17 @@ struct r300_state_atom {
 #define R300_RI_INTERP_7       8
 #define R300_RI_CMDSIZE                9
 
-#define R300_RR_CMD_0          0 /* rr is variable size (at least 1) */
-#define R300_RR_ROUTE_0                1
-#define R300_RR_ROUTE_1                2
-#define R300_RR_ROUTE_2                3
-#define R300_RR_ROUTE_3                4
-#define R300_RR_ROUTE_4                5
-#define R300_RR_ROUTE_5                6
-#define R300_RR_ROUTE_6                7
-#define R300_RR_ROUTE_7                8
+#define R500_RI_CMDSIZE               17
+
+#define R300_RR_CMD_0          0       /* rr is variable size (at least 1) */
+#define R300_RR_INST_0         1
+#define R300_RR_INST_1         2
+#define R300_RR_INST_2         3
+#define R300_RR_INST_3         4
+#define R300_RR_INST_4         5
+#define R300_RR_INST_5         6
+#define R300_RR_INST_6         7
+#define R300_RR_INST_7         8
 #define R300_RR_CMDSIZE                9
 
 #define R300_FP_CMD_0          0
@@ -342,6 +181,17 @@ struct r300_state_atom {
 #define R300_FP_NODE3          8
 #define R300_FP_CMDSIZE                9
 
+#define R500_FP_CMD_0           0
+#define R500_FP_CNTL            1
+#define R500_FP_PIXSIZE         2
+#define R500_FP_CMD_1           3
+#define R500_FP_CODE_ADDR       4
+#define R500_FP_CODE_RANGE      5
+#define R500_FP_CODE_OFFSET     6
+#define R500_FP_CMD_2           7
+#define R500_FP_FC_CNTL         8
+#define R500_FP_CMDSIZE         9
+
 #define R300_FPT_CMD_0         0
 #define R300_FPT_INSTR_0       1
 #define R300_FPT_CMDSIZE       65
@@ -349,10 +199,14 @@ struct r300_state_atom {
 #define R300_FPI_CMD_0         0
 #define R300_FPI_INSTR_0       1
 #define R300_FPI_CMDSIZE       65
+/* R500 has space for 512 instructions - 6 dwords per instruction */
+#define R500_FPI_CMDSIZE       (512*6+1)
 
 #define R300_FPP_CMD_0         0
 #define R300_FPP_PARAM_0       1
 #define R300_FPP_CMDSIZE       (32*4+1)
+/* R500 has spcae for 256 constants - 4 dwords per constant */
+#define R500_FPP_CMDSIZE       (256*4+1)
 
 #define R300_FOGS_CMD_0                0
 #define R300_FOGS_STATE                1
@@ -400,13 +254,26 @@ struct r300_state_atom {
 #define R300_ZB_PITCH          2
 #define R300_ZB_CMDSIZE                3
 
+#define R300_VAP_CNTL_FLUSH     0
+#define R300_VAP_CNTL_FLUSH_1   1
+#define R300_VAP_CNTL_CMD       2
+#define R300_VAP_CNTL_INSTR     3
+#define R300_VAP_CNTL_SIZE      4
+
 #define R300_VPI_CMD_0         0
 #define R300_VPI_INSTR_0       1
-#define R300_VPI_CMDSIZE       1025 /* 256 16 byte instructions */
+#define R300_VPI_CMDSIZE       1025    /* 256 16 byte instructions */
 
 #define R300_VPP_CMD_0         0
 #define R300_VPP_PARAM_0       1
-#define R300_VPP_CMDSIZE       1025 /* 256 4-component parameters */
+#define R300_VPP_CMDSIZE       1025    /* 256 4-component parameters */
+
+#define R300_VPUCP_CMD_0               0
+#define R300_VPUCP_X            1
+#define R300_VPUCP_Y            2
+#define R300_VPUCP_Z            3
+#define R300_VPUCP_W            4
+#define R300_VPUCP_CMDSIZE     5       /* 256 4-component parameters */
 
 #define R300_VPS_CMD_0         0
 #define R300_VPS_ZERO_0                1
@@ -426,207 +293,151 @@ struct r300_state_atom {
  * Cache for hardware register state.
  */
 struct r300_hw_state {
-       struct r300_state_atom atomlist;
-
-       GLboolean       is_dirty;
-       GLboolean       all_dirty;
-       int             max_state_size; /* in dwords */
-
-       struct r300_state_atom vpt;     /* viewport (1D98) */
-       struct r300_state_atom vap_cntl;
-       struct r300_state_atom vof;     /* VAP output format register 0x2090 */
-       struct r300_state_atom vte;     /* (20B0) */
-       struct r300_state_atom unk2134; /* (2134) */
-       struct r300_state_atom vap_cntl_status;
-       struct r300_state_atom vir[2];  /* vap input route (2150/21E0) */
-       struct r300_state_atom vic;     /* vap input control (2180) */
-       struct r300_state_atom unk21DC; /* (21DC) */
-       struct r300_state_atom unk221C; /* (221C) */
-       struct r300_state_atom unk2220; /* (2220) */
-       struct r300_state_atom unk2288; /* (2288) */
-       struct r300_state_atom pvs;     /* pvs_cntl (22D0) */
-       struct r300_state_atom gb_enable; /* (4008) */
-       struct r300_state_atom gb_misc; /* Multisampling position shifts ? (4010) */
-       struct r300_state_atom unk4200; /* (4200) */
-       struct r300_state_atom unk4214; /* (4214) */
-       struct r300_state_atom ps;      /* pointsize (421C) */
-       struct r300_state_atom unk4230; /* (4230) */
-       struct r300_state_atom lcntl;   /* line control */
-       struct r300_state_atom unk4260; /* (4260) */
-       struct r300_state_atom shade;
-       struct r300_state_atom polygon_mode;
-       struct r300_state_atom fogp;    /* fog parameters (4294) */
-       struct r300_state_atom unk429C; /* (429C) */
-       struct r300_state_atom zbias_cntl;
-       struct r300_state_atom zbs;     /* zbias (42A4) */
-       struct r300_state_atom occlusion_cntl;
-       struct r300_state_atom cul;     /* cull cntl (42B8) */
-       struct r300_state_atom unk42C0; /* (42C0) */
-       struct r300_state_atom rc;      /* rs control (4300) */
-       struct r300_state_atom ri;      /* rs interpolators (4310) */
-       struct r300_state_atom rr;      /* rs route (4330) */
-       struct r300_state_atom unk43A4; /* (43A4) */
-       struct r300_state_atom unk43E8; /* (43E8) */
-       struct r300_state_atom fp;      /* fragment program cntl + nodes (4600) */
-       struct r300_state_atom fpt;     /* texi - (4620) */
-       struct r300_state_atom unk46A4; /* (46A4) */
-       struct r300_state_atom fpi[4];  /* fp instructions (46C0/47C0/48C0/49C0) */
-       struct r300_state_atom fogs;    /* fog state (4BC0) */
-       struct r300_state_atom fogc;    /* fog color (4BC8) */
-       struct r300_state_atom at;      /* alpha test (4BD4) */
-       struct r300_state_atom unk4BD8; /* (4BD8) */
-       struct r300_state_atom fpp;     /* 0x4C00 and following */
-       struct r300_state_atom unk4E00; /* (4E00) */
-       struct r300_state_atom bld;     /* blending (4E04) */
-       struct r300_state_atom cmk;     /* colormask (4E0C) */
-       struct r300_state_atom blend_color;     /* constant blend color */
-       struct r300_state_atom cb;      /* colorbuffer (4E28) */
-       struct r300_state_atom unk4E50; /* (4E50) */
-       struct r300_state_atom unk4E88; /* (4E88) */
-       struct r300_state_atom unk4EA0; /* (4E88) I saw it only written on RV350 hardware..  */
-       struct r300_state_atom zs;      /* zstencil control (4F00) */
-       struct r300_state_atom zstencil_format;
-       struct r300_state_atom zb;      /* z buffer (4F20) */
-       struct r300_state_atom unk4F28; /* (4F28) */
-       struct r300_state_atom unk4F30; /* (4F30) */
-       struct r300_state_atom unk4F44; /* (4F44) */
-       struct r300_state_atom unk4F54; /* (4F54) */
-
-       struct r300_state_atom vpi;     /* vp instructions */
-       struct r300_state_atom vpp;     /* vp parameters */
-       struct r300_state_atom vps;     /* vertex point size (?) */
-               /* 8 texture units */
-               /* the state is grouped by function and not by
-                  texture unit. This makes single unit updates
-                  really awkward - we are much better off
-                  updating the whole thing at once */
+       struct radeon_state_atom vpt;   /* viewport (1D98) */
+       struct radeon_state_atom vap_cntl;
+       struct radeon_state_atom vap_index_offset; /* 0x208c r5xx only */
+       struct radeon_state_atom vof;   /* VAP output format register 0x2090 */
+       struct radeon_state_atom vte;   /* (20B0) */
+       struct radeon_state_atom vap_vf_max_vtx_indx;   /* Maximum Vertex Indx Clamp (2134) */
+       struct radeon_state_atom vap_cntl_status;
+       struct radeon_state_atom vir[2];        /* vap input route (2150/21E0) */
+       struct radeon_state_atom vic;   /* vap input control (2180) */
+       struct radeon_state_atom vap_psc_sgn_norm_cntl; /* Programmable Stream Control Signed Normalize Control (21DC) */
+       struct radeon_state_atom vap_clip_cntl;
+       struct radeon_state_atom vap_clip;
+       struct radeon_state_atom vap_pvs_vtx_timeout_reg;       /* Vertex timeout register (2288) */
+       struct radeon_state_atom pvs;   /* pvs_cntl (22D0) */
+       struct radeon_state_atom gb_enable;     /* (4008) */
+       struct radeon_state_atom gb_misc;       /* Multisampling position shifts ? (4010) */
+       struct radeon_state_atom gb_misc2;      /* Multisampling position shifts ? (4010) */
+       struct radeon_state_atom ga_point_s0;   /* S Texture Coordinate of Vertex 0 for Point texture stuffing (LLC) (4200) */
+       struct radeon_state_atom ga_triangle_stipple;   /* (4214) */
+       struct radeon_state_atom ps;    /* pointsize (421C) */
+       struct radeon_state_atom ga_point_minmax;       /* (4230) */
+       struct radeon_state_atom lcntl; /* line control */
+       struct radeon_state_atom ga_line_stipple;       /* (4260) */
+       struct radeon_state_atom shade;
+       struct radeon_state_atom shade2;
+       struct radeon_state_atom polygon_mode;
+       struct radeon_state_atom fogp;  /* fog parameters (4294) */
+       struct radeon_state_atom ga_soft_reset; /* (429C) */
+       struct radeon_state_atom zbias_cntl;
+       struct radeon_state_atom zbs;   /* zbias (42A4) */
+       struct radeon_state_atom occlusion_cntl;
+       struct radeon_state_atom cul;   /* cull cntl (42B8) */
+       struct radeon_state_atom su_depth_scale;        /* (42C0) */
+       struct radeon_state_atom rc;    /* rs control (4300) */
+       struct radeon_state_atom ri;    /* rs interpolators (4310) */
+       struct radeon_state_atom rr;    /* rs route (4330) */
+       struct radeon_state_atom sc_hyperz;     /* (43A4) */
+       struct radeon_state_atom sc_screendoor; /* (43E8) */
+       struct radeon_state_atom fp;    /* fragment program cntl + nodes (4600) */
+       struct radeon_state_atom fpt;   /* texi - (4620) */
+       struct radeon_state_atom us_out_fmt;    /* (46A4) */
+       struct radeon_state_atom r500fp;        /* r500 fp instructions */
+       struct radeon_state_atom r500fp_const;  /* r500 fp constants */
+       struct radeon_state_atom fpi[4];        /* fp instructions (46C0/47C0/48C0/49C0) */
+       struct radeon_state_atom fogs;  /* fog state (4BC0) */
+       struct radeon_state_atom fogc;  /* fog color (4BC8) */
+       struct radeon_state_atom at;    /* alpha test (4BD4) */
+       struct radeon_state_atom fg_depth_src;  /* (4BD8) */
+       struct radeon_state_atom fpp;   /* 0x4C00 and following */
+       struct radeon_state_atom rb3d_cctl;     /* (4E00) */
+       struct radeon_state_atom bld;   /* blending (4E04) */
+       struct radeon_state_atom cmk;   /* colormask (4E0C) */
+       struct radeon_state_atom blend_color;   /* constant blend color */
+       struct radeon_state_atom rop;   /* ropcntl */
+       struct radeon_state_atom cb;    /* colorbuffer (4E28) */
+       struct radeon_state_atom rb3d_dither_ctl;       /* (4E50) */
+       struct radeon_state_atom rb3d_aaresolve_ctl;    /* (4E88) */
+       struct radeon_state_atom rb3d_discard_src_pixel_lte_threshold;  /* (4E88) I saw it only written on RV350 hardware..  */
+       struct radeon_state_atom zs;    /* zstencil control (4F00) */
+       struct radeon_state_atom zstencil_format;
+       struct radeon_state_atom zb;    /* z buffer (4F20) */
+       struct radeon_state_atom zb_depthclearvalue;    /* (4F28) */
+       struct radeon_state_atom zb_zmask;      /* (4F30) */
+       struct radeon_state_atom zb_hiz_offset; /* (4F44) */
+       struct radeon_state_atom zb_hiz_pitch;  /* (4F54) */
+
+       struct radeon_state_atom vpi;   /* vp instructions */
+       struct radeon_state_atom vpp;   /* vp parameters */
+       struct radeon_state_atom vps;   /* vertex point size (?) */
+       struct radeon_state_atom vpucp[6];      /* vp user clip plane - 6 */
+       /* 8 texture units */
+       /* the state is grouped by function and not by
+          texture unit. This makes single unit updates
+          really awkward - we are much better off
+          updating the whole thing at once */
        struct {
-               struct r300_state_atom filter;
-               struct r300_state_atom filter_1;
-               struct r300_state_atom size;
-               struct r300_state_atom format;
-               struct r300_state_atom pitch;
-               struct r300_state_atom offset;
-               struct r300_state_atom chroma_key;
-               struct r300_state_atom border_color;
-               } tex;
-       struct r300_state_atom txe;     /* tex enable (4104) */
-};
-
+               struct radeon_state_atom filter;
+               struct radeon_state_atom filter_1;
+               struct radeon_state_atom size;
+               struct radeon_state_atom format;
+               struct radeon_state_atom pitch;
+               struct radeon_state_atom offset;
+               struct radeon_state_atom chroma_key;
+               struct radeon_state_atom border_color;
+       } tex;
+       struct radeon_state_atom txe;   /* tex enable (4104) */
 
-/**
- * This structure holds the command buffer while it is being constructed.
- *
- * The first batch of commands in the buffer is always the state that needs
- * to be re-emitted when the context is lost. This batch can be skipped
- * otherwise.
- */
-struct r300_cmdbuf {
-       int             size;           /* DWORDs allocated for buffer */
-       uint32_t*       cmd_buf;
-       int             count_used;     /* DWORDs filled so far */
-       int             count_reemit;   /* size of re-emission batch */
+       radeonTexObj *textures[R300_MAX_TEXTURE_UNITS];
 };
 
-
 /**
  * State cache
  */
 
-struct r300_depthbuffer_state {
-       GLfloat scale;
-};
-
-struct r300_stencilbuffer_state {
-       GLuint clear;
-       GLboolean hw_stencil;
-
-};
-
 /* Vertex shader state */
 
 /* Perhaps more if we store programs in vmem? */
 /* drm_r300_cmd_header_t->vpu->count is unsigned char */
 #define VSF_MAX_FRAGMENT_LENGTH (255*4)
-       
+
 /* Can be tested with colormat currently. */
 #define VSF_MAX_FRAGMENT_TEMPS (14)
 
 #define STATE_R300_WINDOW_DIMENSION (STATE_INTERNAL_DRIVER+0)
+#define STATE_R300_TEXRECT_FACTOR (STATE_INTERNAL_DRIVER+1)
 
 struct r300_vertex_shader_fragment {
        int length;
        union {
                GLuint d[VSF_MAX_FRAGMENT_LENGTH];
                float f[VSF_MAX_FRAGMENT_LENGTH];
-               VERTEX_SHADER_INSTRUCTION i[VSF_MAX_FRAGMENT_LENGTH/4];
-               } body;
-       };
-
-#define VSF_DEST_PROGRAM       0x0
-#define VSF_DEST_MATRIX0       0x200
-#define VSF_DEST_MATRIX1       0x204
-#define VSF_DEST_MATRIX2       0x208
-#define VSF_DEST_VECTOR0       0x20c
-#define VSF_DEST_VECTOR1       0x20d
-#define VSF_DEST_UNKNOWN1      0x400
-#define VSF_DEST_UNKNOWN2      0x406
+               GLuint i[VSF_MAX_FRAGMENT_LENGTH];
+       } body;
+};
 
 struct r300_vertex_shader_state {
        struct r300_vertex_shader_fragment program;
+};
 
-       /* a bit of a waste - each uses only a subset of allocated space..
-           but easier to program */
-       struct r300_vertex_shader_fragment matrix[3];
-       struct r300_vertex_shader_fragment vector[2];
-
-       struct r300_vertex_shader_fragment unknown1;
-       struct r300_vertex_shader_fragment unknown2;
-
-       int program_start;
-       int unknown_ptr1;  /* pointer within program space */
-       int program_end;
-
-       int param_offset;
-       int param_count;
+#define COLOR_IS_RGBA
+#define TAG(x) r300##x
+#include "tnl_dd/t_dd_vertex.h"
+#undef TAG
 
-       int unknown_ptr2;  /* pointer within program space */
-       int unknown_ptr3;  /* pointer within program space */
-       };
-       
-extern int hw_tcl_on;
-
-//#define CURRENT_VERTEX_SHADER(ctx) (ctx->VertexProgram._Current)
 #define CURRENT_VERTEX_SHADER(ctx) (R300_CONTEXT(ctx)->selected_vp)
 
-/* Should but doesnt work */
-//#define CURRENT_VERTEX_SHADER(ctx) (R300_CONTEXT(ctx)->curr_vp)
-
-//#define TMU_ENABLED(ctx, unit) (hw_tcl_on ? ctx->Texture.Unit[unit]._ReallyEnabled && (OutputsWritten & (1<<(VERT_RESULT_TEX0+(unit)))) : 
-//     (r300->state.render_inputs & (_TNL_BIT_TEX0<<(unit))))
-//#define TMU_ENABLED(ctx, unit) (hw_tcl_on ? ctx->Texture.Unit[unit]._ReallyEnabled && OutputsWritten & (1<<(VERT_RESULT_TEX0+(unit))) : 
-//     ctx->Texture.Unit[unit]._ReallyEnabled && r300->state.render_inputs & (_TNL_BIT_TEX0<<(unit)))
-
-#define TMU_ENABLED(ctx, unit) (ctx->Texture.Unit[unit]._ReallyEnabled)
-
 /* r300_vertex_shader_state and r300_vertex_program should probably be merged together someday.
  * Keeping them them seperate for now should ensure fixed pipeline keeps functioning properly.
- */    
+ */
 
 struct r300_vertex_program_key {
        GLuint InputsRead;
        GLuint OutputsWritten;
+       GLuint OutputsAdded;
 };
 
 struct r300_vertex_program {
        struct r300_vertex_program *next;
        struct r300_vertex_program_key key;
        int translated;
-       
+
        struct r300_vertex_shader_fragment program;
-       
+
        int pos_end;
-       int num_temporaries; /* Number of temp vars used by program */
+       int num_temporaries;    /* Number of temp vars used by program */
        int wpos_idx;
        int inputs[VERT_ATTRIB_MAX];
        int outputs[VERT_RESULT_MAX];
@@ -636,314 +447,235 @@ struct r300_vertex_program {
 };
 
 struct r300_vertex_program_cont {
-       struct gl_vertex_program mesa_program; /* Must be first */
+       struct gl_vertex_program mesa_program;  /* Must be first */
        struct r300_vertex_shader_fragment params;
        struct r300_vertex_program *progs;
 };
 
-#define PFS_MAX_ALU_INST       64
-#define PFS_MAX_TEX_INST       64
-#define PFS_MAX_TEX_INDIRECT 4
-#define PFS_NUM_TEMP_REGS      32
-#define PFS_NUM_CONST_REGS     16
-
-/* Mapping Mesa registers to R300 temporaries */
-struct reg_acc {
-       int reg;        /* Assigned hw temp */
-       unsigned int refcount; /* Number of uses by mesa program */
-};
+#define R300_PFS_MAX_ALU_INST  64
+#define R300_PFS_MAX_TEX_INST  32
+#define R300_PFS_MAX_TEX_INDIRECT 4
+#define R300_PFS_NUM_TEMP_REGS 32
+#define R300_PFS_NUM_CONST_REGS        32
 
-/**
- * Describe the current lifetime information for an R300 temporary
- */
-struct reg_lifetime {
-       /* Index of the first slot where this register is free in the sense
-          that it can be used as a new destination register.
-          This is -1 if the register has been assigned to a Mesa register
-          and the last access to the register has not yet been emitted */
-       int free;
-       
-       /* Index of the first slot where this register is currently reserved.
-          This is used to stop e.g. a scalar operation from being moved
-          before the allocation time of a register that was first allocated
-          for a vector operation. */
-       int reserved;
-       
-       /* Index of the first slot in which the register can be used as a
-          source without losing the value that is written by the last
-          emitted instruction that writes to the register */
-       int vector_valid;
-       int scalar_valid;
-};
+#define R500_PFS_MAX_INST 512
+#define R500_PFS_NUM_TEMP_REGS 128
+#define R500_PFS_NUM_CONST_REGS 256
 
+struct r300_pfs_compile_state;
+struct r500_pfs_compile_state;
 
 /**
- * Store usage information about an ALU instruction slot during the
- * compilation of a fragment program.
+ * Stores state that influences the compilation of a fragment program.
  */
-#define SLOT_SRC_VECTOR  (1<<0)
-#define SLOT_SRC_SCALAR  (1<<3)
-#define SLOT_SRC_BOTH    (SLOT_SRC_VECTOR | SLOT_SRC_SCALAR)
-#define SLOT_OP_VECTOR   (1<<16)
-#define SLOT_OP_SCALAR   (1<<17)
-#define SLOT_OP_BOTH     (SLOT_OP_VECTOR | SLOT_OP_SCALAR)
-
-struct r300_pfs_compile_slot {
-       /* Bitmask indicating which parts of the slot are used, using SLOT_ constants 
-          defined above */
-       unsigned int used;
-
-       /* Selected sources */
-       int vsrc[3];
-       int ssrc[3];
+struct r300_fragment_program_external_state {
+       struct {
+               /**
+                * If the sampler is used as a shadow sampler,
+                * this field is:
+                *  0 - GL_LUMINANCE
+                *  1 - GL_INTENSITY
+                *  2 - GL_ALPHA
+                * depending on the depth texture mode.
+                */
+               GLuint depth_texture_mode : 2;
+
+               /**
+                * If the sampler is used as a shadow sampler,
+                * this field is (texture_compare_func - GL_NEVER).
+                * [e.g. if compare function is GL_LEQUAL, this field is 3]
+                *
+                * Otherwise, this field is 0.
+                */
+               GLuint texture_compare_func : 3;
+       } unit[16];
 };
 
-/**
- * Store information during compilation of fragment programs.
- */
-struct r300_pfs_compile_state {
-       int nrslots;       /* number of ALU slots used so far */
-       
-       /* Track which (parts of) slots are already filled with instructions */
-       struct r300_pfs_compile_slot slot[PFS_MAX_ALU_INST];
-       
-       /* Track the validity of R300 temporaries */
-       struct reg_lifetime hwtemps[PFS_NUM_TEMP_REGS];
-       
-       /* Used to map Mesa's inputs/temps onto hardware temps */
-       int temp_in_use;
-       struct reg_acc temps[PFS_NUM_TEMP_REGS];
-       struct reg_acc inputs[32]; /* don't actually need 32... */
-       
-       /* Track usage of hardware temps, for register allocation,
-        * indirection detection, etc. */
-       GLuint used_in_node;
-       GLuint dest_in_node;
+
+struct r300_fragment_program_node {
+       int tex_offset; /**< first tex instruction */
+       int tex_end; /**< last tex instruction, relative to tex_offset */
+       int alu_offset; /**< first ALU instruction */
+       int alu_end; /**< last ALU instruction, relative to alu_offset */
+       int flags;
 };
 
 /**
- * Store everything about a fragment program that is needed
- * to render with that program.
+ * Stores an R300 fragment program in its compiled-to-hardware form.
  */
-struct r300_fragment_program {
-       struct gl_fragment_program mesa_program;
-
-       GLcontext *ctx;
-       GLboolean translated;
-       GLboolean error;
-       struct r300_pfs_compile_state *cs;
-
+struct r300_fragment_program_code {
        struct {
-               int length;
-               GLuint inst[PFS_MAX_TEX_INST];
+               int length; /**< total # of texture instructions used */
+               GLuint inst[R300_PFS_MAX_TEX_INST];
        } tex;
 
        struct {
+               int length; /**< total # of ALU instructions used */
                struct {
                        GLuint inst0;
                        GLuint inst1;
                        GLuint inst2;
                        GLuint inst3;
-               } inst[PFS_MAX_ALU_INST];
+               } inst[R300_PFS_MAX_ALU_INST];
        } alu;
 
-       struct {
-               int tex_offset;
-               int tex_end;
-               int alu_offset;
-               int alu_end;
-               int flags;
-       } node[4];
+       struct r300_fragment_program_node node[4];
        int cur_node;
        int first_node_has_tex;
 
-       int alu_offset;
-       int alu_end;
-       int tex_offset;
-       int tex_end;
-
-       /* Hardware constants */
-       GLfloat constant[PFS_NUM_CONST_REGS][4];
+       /**
+        * Remember which program register a given hardware constant
+        * belongs to.
+        */
+       struct prog_src_register constant[R300_PFS_NUM_CONST_REGS];
        int const_nr;
 
-       /* Tracked parameters */
+       int max_temp_idx;
+};
+
+
+struct r500_fragment_program_code {
        struct {
-               int idx;                        /* hardware index */
-               GLfloat *values;        /* pointer to values */
-       } param[PFS_NUM_CONST_REGS];
-       int param_nr;
-       GLboolean params_uptodate;
+               GLuint inst0;
+               GLuint inst1;
+               GLuint inst2;
+               GLuint inst3;
+               GLuint inst4;
+               GLuint inst5;
+       } inst[R500_PFS_MAX_INST];
+
+       int inst_offset;
+       int inst_end;
+
+       /**
+        * Remember which program register a given hardware constant
+        * belongs to.
+        */
+       struct prog_src_register constant[R500_PFS_NUM_CONST_REGS];
+       int const_nr;
 
        int max_temp_idx;
-
-       /* the index of the sin constant is stored here */
-       GLint const_sin[2];
-       
-       GLuint optimization;
 };
 
-#define R300_MAX_AOS_ARRAYS            16
+/**
+* Store everything about a fragment program that is needed
+* to render with that program.
+*/
+struct r300_fragment_program {
+       struct gl_fragment_program Base;
 
-#define AOS_FORMAT_USHORT      0
-#define AOS_FORMAT_FLOAT       1
-#define AOS_FORMAT_UBYTE       2
-#define AOS_FORMAT_FLOAT_COLOR 3
+       GLboolean translated;
+       GLboolean error;
 
-#define REG_COORDS     0
-#define REG_COLOR0     1
-#define REG_TEX0       2
+       struct r300_fragment_program_external_state state;
+       union rX00_fragment_program_code {
+               struct r300_fragment_program_code r300;
+               struct r500_fragment_program_code r500;
+       } code;
 
-struct dt {
-       GLint size;
-       GLenum type;
-       GLsizei stride;
-       void *data;
+       GLboolean writes_depth;
+       GLuint optimization;
 };
 
-struct radeon_vertex_buffer {
-       int Count;
-       void *Elts;
-       int elt_size;
-       int elt_min, elt_max; /* debug */
-       
-       struct dt AttribPtr[VERT_ATTRIB_MAX];
-       
-       const struct _mesa_prim  *Primitive;                  
-       GLuint      PrimitiveCount;
-       GLint LockFirst;
-       GLsizei LockCount;
-       int lock_uptodate;
+struct r300_fragment_program_compiler {
+       r300ContextPtr r300;
+       struct r300_fragment_program *fp;
+       union rX00_fragment_program_code *code;
+       struct gl_program *program;
 };
 
-struct r300_aos_rec {
-       GLuint offset;
-       int element_size; /* in dwords */
-       int stride;       /* distance between elements, in dwords */
-
-       int format;
-
-       int ncomponents; /* number of components - between 1 and 4, inclusive */
+#define R300_MAX_AOS_ARRAYS            16
 
-       int reg; /* which register they are assigned to. */
 
-       };
+#define R300_FALLBACK_NONE 0
+#define R300_FALLBACK_TCL 1
+#define R300_FALLBACK_RAST 2
 
-struct r300_state {
-       struct r300_depthbuffer_state depth;
-       struct r300_texture_state texture;
-       int sw_tcl_inputs[VERT_ATTRIB_MAX];
-       struct r300_vertex_shader_state vertex_shader;
-       struct r300_pfs_compile_state pfs_compile;
-       struct r300_dma_region aos[R300_MAX_AOS_ARRAYS];
-       int aos_count;
-       struct radeon_vertex_buffer VB;
+/* r300_swtcl.c
+ */
+struct r300_swtcl_info {
+  /*
+    * Offset of the 4UB color data within a hardware (swtcl) vertex.
+    */
+   GLuint coloroffset;
+
+   /**
+    * Offset of the 3UB specular color data within a hardware (swtcl) vertex.
+    */
+   GLuint specoffset;
+
+   struct vertex_attribute{
+       GLuint attr;
+       GLubyte format;
+       GLubyte dst_loc;
+       GLuint swizzle;
+       GLubyte write_mask;
+   } vert_attrs[VERT_ATTRIB_MAX];
+
+   GLubyte vertex_attr_count;
+
+   int sw_tcl_inputs[VERT_ATTRIB_MAX];
+};
 
-       GLuint *Elts;
-       struct r300_dma_region elt_dma;
-       
-       DECLARE_RENDERINPUTS(render_inputs_bitset); /* actual render inputs that R300 was configured for. 
-                                They are the same as tnl->render_inputs for fixed pipeline */  
-       
-       struct {
-               int transform_offset;  /* Transform matrix offset, -1 if none */
-               } vap_param;  /* vertex processor parameter allocation - tells where to write parameters */
-       
-       struct r300_stencilbuffer_state stencil;
-       
+struct r300_vtable {
+       void (* SetupRSUnit)(GLcontext *ctx);
+       void (* SetupFragmentShaderTextures)(GLcontext *ctx, int *tmu_mappings);
+       GLboolean (* FragmentProgramEmit)(struct r300_fragment_program_compiler *compiler);
+       void (* FragmentProgramDump)(union rX00_fragment_program_code *code);
+       GLboolean (* SetupPixelShader)(GLcontext *ctx);
 };
 
-#define R300_FALLBACK_NONE 0
-#define R300_FALLBACK_TCL 1
-#define R300_FALLBACK_RAST 2
 
 /**
- * R300 context structure.
+ * \brief R300 context structure.
  */
 struct r300_context {
-       struct radeon_context radeon; /* parent class, must be first */
+       struct radeon_context radeon;   /* parent class, must be first */
+
+       struct r300_vtable vtbl;
 
        struct r300_hw_state hw;
-       struct r300_cmdbuf cmdbuf;
-       struct r300_state state;
-       struct gl_vertex_program *curr_vp;
+
+       struct r300_vertex_shader_state vertex_shader;
        struct r300_vertex_program *selected_vp;
 
        /* Vertex buffers
         */
-       struct r300_dma dma;
-       GLboolean save_on_next_unlock;
-       GLuint NewGLState;
-
-       /* Texture object bookkeeping
-        */
-       unsigned nr_heaps;
-       driTexHeap *texture_heaps[RADEON_NR_TEX_HEAPS];
-       driTextureObject swapped;
-       int texture_depth;
-       float initialMaxAnisotropy;
-
-       /* Clientdata textures;
-        */
-       GLuint prefer_gart_client_texturing;
-
-#ifdef USER_BUFFERS
-       struct radeon_memory_manager *rmm;
        GLvector4f dummy_attrib[_TNL_ATTRIB_MAX];
        GLvector4f *temp_attrib[_TNL_ATTRIB_MAX];
-#endif
 
-       GLboolean texmicrotile;
-       GLboolean span_dlocking;
        GLboolean disable_lowimpact_fallback;
-};
 
-struct r300_buffer_object {
-       struct gl_buffer_object mesa_obj;
-       int id;
+       struct r300_options {
+               uint32_t conformance_mode:1;
+               uint32_t hw_tcl_enabled:1;
+               uint32_t s3tc_force_enabled:1;
+               uint32_t s3tc_force_disabled:1;
+               uint32_t stencil_two_side_disabled:1;
+       } options;
+       
+       struct r300_swtcl_info swtcl;
+       GLboolean vap_flush_needed;
+
+       DECLARE_RENDERINPUTS(render_inputs_bitset);
 };
-               
+
 #define R300_CONTEXT(ctx)              ((r300ContextPtr)(ctx->DriverCtx))
 
-static __inline GLuint r300PackColor( GLuint cpp,
-                                       GLubyte r, GLubyte g,
-                                       GLubyte b, GLubyte a )
-{
-   switch ( cpp ) {
-   case 2:
-      return PACK_COLOR_565( r, g, b );
-   case 4:
-      return PACK_COLOR_8888( r, g, b, a );
-   default:
-      return 0;
-   }
-}
 extern void r300DestroyContext(__DRIcontextPrivate * driContextPriv);
 extern GLboolean r300CreateContext(const __GLcontextModes * glVisual,
                                   __DRIcontextPrivate * driContextPriv,
                                   void *sharedContextPrivate);
 
-extern int r300_get_num_verts(r300ContextPtr rmesa, int num_verts, int prim);
-
-extern void r300_select_vertex_shader(r300ContextPtr r300);
+extern void r300SelectVertexShader(r300ContextPtr r300);
 extern void r300InitShaderFuncs(struct dd_function_table *functions);
-extern int r300VertexProgUpdateParams(GLcontext *ctx, struct r300_vertex_program_cont *vp, float *dst);
-extern int r300Fallback(GLcontext *ctx);
-
-extern void radeon_vb_to_rvb(r300ContextPtr rmesa, struct radeon_vertex_buffer *rvb, struct vertex_buffer *vb);
-extern GLboolean r300_run_vb_render(GLcontext *ctx, struct tnl_pipeline_stage *stage);
-
-#ifdef RADEON_VTXFMT_A
-extern void radeon_init_vtxfmt_a(r300ContextPtr rmesa);
-#endif
+extern int r300VertexProgUpdateParams(GLcontext * ctx,
+                                     struct r300_vertex_program_cont *vp,
+                                     float *dst);
 
-#ifdef HW_VBOS
-extern void r300_init_vbo_funcs(struct dd_function_table *functions);
-extern void r300_evict_vbos(GLcontext *ctx, int amount);
-#endif
+extern void r300InitShaderFunctions(r300ContextPtr r300);
 
-#define RADEON_D_CAPTURE 0
-#define RADEON_D_PLAYBACK 1
-#define RADEON_D_PLAYBACK_RAW 2
-#define RADEON_D_T 3
+#define r300PackFloat32 radeonPackFloat32
+#define r300PackFloat24 radeonPackFloat24
 
 #endif                         /* __R300_CONTEXT_H__ */