Merge branch 'mesa_7_5_branch'
[mesa.git] / src / mesa / drivers / dri / r300 / r300_emit.c
1 /*
2 Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
3
4 The Weather Channel (TM) funded Tungsten Graphics to develop the
5 initial release of the Radeon 8500 driver under the XFree86 license.
6 This notice must be preserved.
7
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the
10 "Software"), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
15
16 The above copyright notice and this permission notice (including the
17 next paragraph) shall be included in all copies or substantial
18 portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
24 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
28 **************************************************************************/
29
30 /**
31 * \file
32 *
33 * \author Keith Whitwell <keith@tungstengraphics.com>
34 * \author Maciej Cencora <m.cencora@gmail.com>
35 */
36
37 #include "main/glheader.h"
38 #include "main/mtypes.h"
39 #include "main/colormac.h"
40 #include "main/imports.h"
41 #include "main/macros.h"
42 #include "main/image.h"
43
44 #include "swrast_setup/swrast_setup.h"
45 #include "math/m_translate.h"
46 #include "tnl/tnl.h"
47 #include "tnl/t_context.h"
48
49 #include "r300_context.h"
50 #include "r300_state.h"
51 #include "r300_emit.h"
52 #include "r300_ioctl.h"
53 #include "r300_render.h"
54 #include "r300_swtcl.h"
55
56 GLuint r300VAPInputCntl0(GLcontext * ctx, GLuint InputsRead)
57 {
58 /* No idea what this value means. I have seen other values written to
59 * this register... */
60 return 0x5555;
61 }
62
63 GLuint r300VAPInputCntl1(GLcontext * ctx, GLuint InputsRead)
64 {
65 GLuint i, vic_1 = 0;
66
67 if (InputsRead & (1 << VERT_ATTRIB_POS))
68 vic_1 |= R300_INPUT_CNTL_POS;
69
70 if (InputsRead & (1 << VERT_ATTRIB_NORMAL))
71 vic_1 |= R300_INPUT_CNTL_NORMAL;
72
73 if (InputsRead & (1 << VERT_ATTRIB_COLOR0))
74 vic_1 |= R300_INPUT_CNTL_COLOR;
75
76 for (i = 0; i < ctx->Const.MaxTextureUnits; i++)
77 if (InputsRead & (1 << (VERT_ATTRIB_TEX0 + i))) {
78 vic_1 |= R300_INPUT_CNTL_TC0 << i;
79 }
80
81 return vic_1;
82 }
83
84 GLuint r300VAPOutputCntl0(GLcontext * ctx, GLuint vp_writes, GLuint fp_reads)
85 {
86 GLuint ret = 0;
87
88 if (vp_writes & (1 << VERT_RESULT_HPOS))
89 ret |= R300_VAP_OUTPUT_VTX_FMT_0__POS_PRESENT;
90
91 if (vp_writes & (1 << VERT_RESULT_COL0) && fp_reads & FRAG_BIT_COL0)
92 ret |= R300_VAP_OUTPUT_VTX_FMT_0__COLOR_0_PRESENT;
93
94 if (vp_writes & (1 << VERT_RESULT_COL1) && fp_reads & FRAG_BIT_COL1)
95 ret |= R300_VAP_OUTPUT_VTX_FMT_0__COLOR_1_PRESENT;
96
97 /* Two sided lighting works only if all 4 colors are written */
98 if (vp_writes & (1 << VERT_RESULT_BFC0) || vp_writes & (1 << VERT_RESULT_BFC1))
99 ret |= R300_VAP_OUTPUT_VTX_FMT_0__COLOR_0_PRESENT | R300_VAP_OUTPUT_VTX_FMT_0__COLOR_1_PRESENT |
100 R300_VAP_OUTPUT_VTX_FMT_0__COLOR_2_PRESENT | R300_VAP_OUTPUT_VTX_FMT_0__COLOR_3_PRESENT;
101
102 if (vp_writes & (1 << VERT_RESULT_PSIZ))
103 ret |= R300_VAP_OUTPUT_VTX_FMT_0__PT_SIZE_PRESENT;
104
105 return ret;
106 }
107
108 GLuint r300VAPOutputCntl1(GLcontext * ctx, GLuint vp_writes, GLuint fp_reads)
109 {
110 GLuint i, ret = 0, first_free_texcoord = 0;
111
112 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
113 if (vp_writes & (1 << (VERT_RESULT_TEX0 + i)) && fp_reads & FRAG_BIT_TEX(i)) {
114 ret |= (4 << (3 * first_free_texcoord));
115 ++first_free_texcoord;
116 }
117 }
118
119 if (fp_reads & FRAG_BIT_WPOS) {
120 ret |= (4 << (3 * first_free_texcoord));
121 ++first_free_texcoord;
122 }
123
124 if (vp_writes & (1 << VERT_RESULT_FOGC) && fp_reads & FRAG_BIT_FOGC) {
125 ret |= 4 << (3 * first_free_texcoord);
126 }
127
128 if (first_free_texcoord > 8) {
129 fprintf(stderr, "\tout of free texcoords\n");
130 _mesa_exit(-1);
131 }
132
133 return ret;
134 }
135
136 GLboolean r300EmitArrays(GLcontext * ctx)
137 {
138 r300ContextPtr r300 = R300_CONTEXT(ctx);
139 struct r300_vertex_buffer *vbuf = &r300->vbuf;
140 GLuint InputsRead, OutputsWritten;
141
142 r300ChooseSwtclVertexFormat(ctx, &InputsRead, &OutputsWritten);
143
144 r300SwitchFallback(ctx, R300_FALLBACK_AOS_LIMIT, vbuf->num_attribs > R300_MAX_AOS_ARRAYS);
145 if (r300->fallback & R300_RASTER_FALLBACK_MASK)
146 return GL_FALSE;
147
148 {
149 struct vertex_buffer *mesa_vb = &TNL_CONTEXT(ctx)->vb;
150 GLuint attr, i;
151
152 for (i = 0; i < vbuf->num_attribs; i++) {
153 attr = vbuf->attribs[i].element;
154 rcommon_emit_vector(ctx, &r300->radeon.tcl.aos[i], mesa_vb->AttribPtr[attr]->data,
155 mesa_vb->AttribPtr[attr]->size, mesa_vb->AttribPtr[attr]->stride, mesa_vb->Count);
156 }
157
158 r300->radeon.tcl.aos_count = vbuf->num_attribs;
159
160 /* Fill index buffer info */
161 r300->ind_buf.ptr = mesa_vb->Elts;
162 r300->ind_buf.is_32bit = GL_TRUE;
163 r300->ind_buf.free_needed = GL_FALSE;
164 }
165
166 r300SetupVAP(ctx, InputsRead, OutputsWritten);
167
168 return GL_TRUE;
169 }
170
171 void r300EmitCacheFlush(r300ContextPtr rmesa)
172 {
173 BATCH_LOCALS(&rmesa->radeon);
174
175 BEGIN_BATCH_NO_AUTOSTATE(4);
176 OUT_BATCH_REGVAL(R300_RB3D_DSTCACHE_CTLSTAT,
177 R300_RB3D_DSTCACHE_CTLSTAT_DC_FREE_FREE_3D_TAGS |
178 R300_RB3D_DSTCACHE_CTLSTAT_DC_FLUSH_FLUSH_DIRTY_3D);
179 OUT_BATCH_REGVAL(R300_ZB_ZCACHE_CTLSTAT,
180 R300_ZB_ZCACHE_CTLSTAT_ZC_FLUSH_FLUSH_AND_FREE |
181 R300_ZB_ZCACHE_CTLSTAT_ZC_FREE_FREE);
182 END_BATCH();
183 COMMIT_BATCH();
184 }