Merge commit 'origin/draw-vbuf-interface'
[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_pipe.h"
30 #include "util/u_math.h"
31 #include "util/u_memory.h"
32 #include "util/u_pack_color.h"
33
34 #include "i915_context.h"
35 #include "i915_winsys.h"
36 #include "i915_reg.h"
37 #include "i915_state.h"
38 #include "i915_batch.h"
39
40
41
42 /**
43 * Primitive emit to hardware. No support for vertex buffers or any
44 * nice fast paths.
45 */
46 struct setup_stage {
47 struct draw_stage stage; /**< This must be first (base class) */
48
49 struct i915_context *i915;
50 };
51
52
53
54 /**
55 * Basically a cast wrapper.
56 */
57 static INLINE struct setup_stage *setup_stage( struct draw_stage *stage )
58 {
59 return (struct setup_stage *)stage;
60 }
61
62
63 /**
64 * Extract the needed fields from vertex_header and emit i915 dwords.
65 * Recall that the vertices are constructed by the 'draw' module and
66 * have a couple of slots at the beginning (1-dword header, 4-dword
67 * clip pos) that we ignore here.
68 */
69 static INLINE void
70 emit_hw_vertex( struct i915_context *i915,
71 const struct vertex_header *vertex)
72 {
73 const struct vertex_info *vinfo = &i915->current.vertex_info;
74 uint i;
75 uint count = 0; /* for debug/sanity */
76
77 assert(!i915->dirty);
78
79 for (i = 0; i < vinfo->num_attribs; i++) {
80 const uint j = vinfo->attrib[i].src_index;
81 const float *attrib = vertex->data[j];
82 switch (vinfo->attrib[i].emit) {
83 case EMIT_1F:
84 OUT_BATCH( fui(attrib[0]) );
85 count++;
86 break;
87 case EMIT_2F:
88 OUT_BATCH( fui(attrib[0]) );
89 OUT_BATCH( fui(attrib[1]) );
90 count += 2;
91 break;
92 case EMIT_3F:
93 OUT_BATCH( fui(attrib[0]) );
94 OUT_BATCH( fui(attrib[1]) );
95 OUT_BATCH( fui(attrib[2]) );
96 count += 3;
97 break;
98 case EMIT_4F:
99 OUT_BATCH( fui(attrib[0]) );
100 OUT_BATCH( fui(attrib[1]) );
101 OUT_BATCH( fui(attrib[2]) );
102 OUT_BATCH( fui(attrib[3]) );
103 count += 4;
104 break;
105 case EMIT_4UB:
106 OUT_BATCH( pack_ub4(float_to_ubyte( attrib[2] ),
107 float_to_ubyte( attrib[1] ),
108 float_to_ubyte( attrib[0] ),
109 float_to_ubyte( attrib[3] )) );
110 count += 1;
111 break;
112 default:
113 assert(0);
114 }
115 }
116 assert(count == vinfo->size);
117 }
118
119
120
121 static INLINE void
122 emit_prim( struct draw_stage *stage,
123 struct prim_header *prim,
124 unsigned hwprim,
125 unsigned nr )
126 {
127 struct i915_context *i915 = setup_stage(stage)->i915;
128 unsigned vertex_size;
129 unsigned i;
130
131 if (i915->dirty)
132 i915_update_derived( i915 );
133
134 if (i915->hardware_dirty)
135 i915_emit_hardware_state( i915 );
136
137 /* need to do this after validation! */
138 vertex_size = i915->current.vertex_info.size * 4; /* in bytes */
139 assert(vertex_size >= 12); /* never smaller than 12 bytes */
140
141 if (!BEGIN_BATCH( 1 + nr * vertex_size / 4, 0 )) {
142 FLUSH_BATCH(NULL);
143
144 /* Make sure state is re-emitted after a flush:
145 */
146 i915_update_derived( i915 );
147 i915_emit_hardware_state( i915 );
148
149 if (!BEGIN_BATCH( 1 + nr * vertex_size / 4, 0 )) {
150 assert(0);
151 return;
152 }
153 }
154
155 /* Emit each triangle as a single primitive. I told you this was
156 * simple.
157 */
158 OUT_BATCH(_3DPRIMITIVE |
159 hwprim |
160 ((4 + vertex_size * nr)/4 - 2));
161
162 for (i = 0; i < nr; i++)
163 emit_hw_vertex(i915, prim->v[i]);
164 }
165
166
167 static void
168 setup_tri( struct draw_stage *stage, struct prim_header *prim )
169 {
170 emit_prim( stage, prim, PRIM3D_TRILIST, 3 );
171 }
172
173
174 static void
175 setup_line(struct draw_stage *stage, struct prim_header *prim)
176 {
177 emit_prim( stage, prim, PRIM3D_LINELIST, 2 );
178 }
179
180
181 static void
182 setup_point(struct draw_stage *stage, struct prim_header *prim)
183 {
184 emit_prim( stage, prim, PRIM3D_POINTLIST, 1 );
185 }
186
187
188 static void setup_flush( struct draw_stage *stage, unsigned flags )
189 {
190 }
191
192 static void reset_stipple_counter( struct draw_stage *stage )
193 {
194 }
195
196 static void render_destroy( struct draw_stage *stage )
197 {
198 FREE( stage );
199 }
200
201
202 /**
203 * Create a new primitive setup/render stage. This gets plugged into
204 * the 'draw' module's pipeline.
205 */
206 struct draw_stage *i915_draw_render_stage( struct i915_context *i915 )
207 {
208 struct setup_stage *setup = CALLOC_STRUCT(setup_stage);
209
210 setup->i915 = i915;
211 setup->stage.draw = i915->draw;
212 setup->stage.point = setup_point;
213 setup->stage.line = setup_line;
214 setup->stage.tri = setup_tri;
215 setup->stage.flush = setup_flush;
216 setup->stage.reset_stipple_counter = reset_stipple_counter;
217 setup->stage.destroy = render_destroy;
218
219 return &setup->stage;
220 }