Merge remote branch 'nouveau/gallium-0.1' into nouveau-gallium-0.1
[mesa.git] / src / gallium / drivers / i915simple / i915_prim_emit.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 #include "draw/draw_private.h"
30 #include "pipe/p_util.h"
31
32 #include "i915_context.h"
33 #include "i915_winsys.h"
34 #include "i915_reg.h"
35 #include "i915_state.h"
36 #include "i915_batch.h"
37
38
39
40 /**
41 * Primitive emit to hardware. No support for vertex buffers or any
42 * nice fast paths.
43 */
44 struct setup_stage {
45 struct draw_stage stage; /**< This must be first (base class) */
46
47 struct i915_context *i915;
48 };
49
50
51
52 /**
53 * Basically a cast wrapper.
54 */
55 static INLINE struct setup_stage *setup_stage( struct draw_stage *stage )
56 {
57 return (struct setup_stage *)stage;
58 }
59
60
61 /**
62 * Extract the needed fields from vertex_header and emit i915 dwords.
63 * Recall that the vertices are constructed by the 'draw' module and
64 * have a couple of slots at the beginning (1-dword header, 4-dword
65 * clip pos) that we ignore here.
66 */
67 static INLINE void
68 emit_hw_vertex( struct i915_context *i915,
69 const struct vertex_header *vertex)
70 {
71 const struct vertex_info *vinfo = &i915->current.vertex_info;
72 uint i;
73 uint count = 0; /* for debug/sanity */
74
75 assert(!i915->dirty);
76
77 for (i = 0; i < vinfo->num_attribs; i++) {
78 const uint j = vinfo->src_index[i];
79 const float *attrib = vertex->data[j];
80 switch (vinfo->emit[i]) {
81 case EMIT_OMIT:
82 /* no-op */
83 break;
84 case EMIT_1F:
85 OUT_BATCH( fui(attrib[0]) );
86 count++;
87 break;
88 case EMIT_2F:
89 OUT_BATCH( fui(attrib[0]) );
90 OUT_BATCH( fui(attrib[1]) );
91 count += 2;
92 break;
93 case EMIT_3F:
94 OUT_BATCH( fui(attrib[0]) );
95 OUT_BATCH( fui(attrib[1]) );
96 OUT_BATCH( fui(attrib[2]) );
97 count += 3;
98 break;
99 case EMIT_4F:
100 OUT_BATCH( fui(attrib[0]) );
101 OUT_BATCH( fui(attrib[1]) );
102 OUT_BATCH( fui(attrib[2]) );
103 OUT_BATCH( fui(attrib[3]) );
104 count += 4;
105 break;
106 case EMIT_4UB:
107 OUT_BATCH( pack_ub4(float_to_ubyte( attrib[2] ),
108 float_to_ubyte( attrib[1] ),
109 float_to_ubyte( attrib[0] ),
110 float_to_ubyte( attrib[3] )) );
111 count += 1;
112 break;
113 default:
114 assert(0);
115 }
116 }
117 assert(count == vinfo->size);
118 }
119
120
121
122 static INLINE void
123 emit_prim( struct draw_stage *stage,
124 struct prim_header *prim,
125 unsigned hwprim,
126 unsigned nr )
127 {
128 struct i915_context *i915 = setup_stage(stage)->i915;
129 unsigned vertex_size;
130 unsigned i;
131
132 if (i915->dirty)
133 i915_update_derived( i915 );
134
135 if (i915->hardware_dirty)
136 i915_emit_hardware_state( i915 );
137
138 /* need to do this after validation! */
139 vertex_size = i915->current.vertex_info.size * 4; /* in bytes */
140 assert(vertex_size >= 12); /* never smaller than 12 bytes */
141
142 if (!BEGIN_BATCH( 1 + nr * vertex_size / 4, 0 )) {
143 FLUSH_BATCH();
144
145 /* Make sure state is re-emitted after a flush:
146 */
147 i915_update_derived( i915 );
148 i915_emit_hardware_state( i915 );
149
150 if (!BEGIN_BATCH( 1 + nr * vertex_size / 4, 0 )) {
151 assert(0);
152 return;
153 }
154 }
155
156 /* Emit each triangle as a single primitive. I told you this was
157 * simple.
158 */
159 OUT_BATCH(_3DPRIMITIVE |
160 hwprim |
161 ((4 + vertex_size * nr)/4 - 2));
162
163 for (i = 0; i < nr; i++)
164 emit_hw_vertex(i915, prim->v[i]);
165 }
166
167
168 static void
169 setup_tri( struct draw_stage *stage, struct prim_header *prim )
170 {
171 emit_prim( stage, prim, PRIM3D_TRILIST, 3 );
172 }
173
174
175 static void
176 setup_line(struct draw_stage *stage, struct prim_header *prim)
177 {
178 emit_prim( stage, prim, PRIM3D_LINELIST, 2 );
179 }
180
181
182 static void
183 setup_point(struct draw_stage *stage, struct prim_header *prim)
184 {
185 emit_prim( stage, prim, PRIM3D_POINTLIST, 1 );
186 }
187
188
189 static void setup_flush( struct draw_stage *stage, unsigned flags )
190 {
191 }
192
193 static void reset_stipple_counter( struct draw_stage *stage )
194 {
195 }
196
197 static void render_destroy( struct draw_stage *stage )
198 {
199 FREE( stage );
200 }
201
202
203 /**
204 * Create a new primitive setup/render stage. This gets plugged into
205 * the 'draw' module's pipeline.
206 */
207 struct draw_stage *i915_draw_render_stage( struct i915_context *i915 )
208 {
209 struct setup_stage *setup = CALLOC_STRUCT(setup_stage);
210
211 setup->i915 = i915;
212 setup->stage.draw = i915->draw;
213 setup->stage.point = setup_point;
214 setup->stage.line = setup_line;
215 setup->stage.tri = setup_tri;
216 setup->stage.flush = setup_flush;
217 setup->stage.reset_stipple_counter = reset_stipple_counter;
218 setup->stage.destroy = render_destroy;
219
220 return &setup->stage;
221 }