9acbc53742b357d1e3e70afcbb6c55651324855c
[mesa.git] / src / mesa / pipe / draw / draw_context.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 * Authors:
30 * Keith Whitwell <keith@tungstengraphics.com>
31 */
32
33
34 #include "pipe/p_util.h"
35 #include "draw_context.h"
36 #include "draw_private.h"
37
38
39
40 struct draw_context *draw_create( void )
41 {
42 struct draw_context *draw = CALLOC_STRUCT( draw_context );
43
44 /* create pipeline stages */
45 draw->pipeline.unfilled = draw_unfilled_stage( draw );
46 draw->pipeline.twoside = draw_twoside_stage( draw );
47 draw->pipeline.offset = draw_offset_stage( draw );
48 draw->pipeline.clip = draw_clip_stage( draw );
49 draw->pipeline.flatshade = draw_flatshade_stage( draw );
50 draw->pipeline.cull = draw_cull_stage( draw );
51 draw->pipeline.feedback = draw_feedback_stage( draw );
52
53 ASSIGN_4V( draw->plane[0], -1, 0, 0, 1 );
54 ASSIGN_4V( draw->plane[1], 1, 0, 0, 1 );
55 ASSIGN_4V( draw->plane[2], 0, -1, 0, 1 );
56 ASSIGN_4V( draw->plane[3], 0, 1, 0, 1 );
57 ASSIGN_4V( draw->plane[4], 0, 0, 1, 1 ); /* yes these are correct */
58 ASSIGN_4V( draw->plane[5], 0, 0, -1, 1 ); /* mesa's a bit wonky */
59 draw->nr_planes = 6;
60
61 /* Statically allocate maximum sized vertices for the cache - could be cleverer...
62 */
63 {
64 int i;
65 char *tmp = malloc(Elements(draw->vcache.vertex) * MAX_VERTEX_SIZE);
66
67 for (i = 0; i < Elements(draw->vcache.vertex); i++)
68 draw->vcache.vertex[i] = (struct vertex_header *)(tmp + i * MAX_VERTEX_SIZE);
69 }
70
71 draw->attrib_front0 = -1;
72 draw->attrib_back0 = -1;
73 draw->attrib_front1 = -1;
74 draw->attrib_back1 = -1;
75
76 return draw;
77 }
78
79
80 void draw_destroy( struct draw_context *draw )
81 {
82 free( draw->vcache.vertex[0] ); /* Frees all the vertices. */
83 free( draw );
84 }
85
86
87 /**
88 * Rebuild the rendering pipeline.
89 */
90 static void validate_pipeline( struct draw_context *draw )
91 {
92 struct draw_stage *next = draw->pipeline.rasterize;
93
94 /*
95 * NOTE: we build up the pipeline in end-to-start order.
96 *
97 * TODO: make the current primitive part of the state and build
98 * shorter pipelines for lines & points.
99 */
100
101 if (draw->rasterizer->fill_cw != PIPE_POLYGON_MODE_FILL ||
102 draw->rasterizer->fill_ccw != PIPE_POLYGON_MODE_FILL) {
103 draw->pipeline.unfilled->next = next;
104 next = draw->pipeline.unfilled;
105 }
106
107 if (draw->rasterizer->offset_cw ||
108 draw->rasterizer->offset_ccw) {
109 draw->pipeline.offset->next = next;
110 next = draw->pipeline.offset;
111 }
112
113 if (draw->rasterizer->light_twoside) {
114 draw->pipeline.twoside->next = next;
115 next = draw->pipeline.twoside;
116 }
117
118 /* Always run the cull stage as we calculate determinant there
119 * also. Fix this..
120 */
121 {
122 draw->pipeline.cull->next = next;
123 next = draw->pipeline.cull;
124 }
125
126 /* Clip stage
127 */
128 {
129 draw->pipeline.clip->next = next;
130 next = draw->pipeline.clip;
131 }
132
133 /* Do software flatshading prior to clipping. XXX: should only do
134 * this for clipped primitives, ie it is a part of the clip
135 * routine.
136 */
137 if (draw->rasterizer->flatshade) {
138 draw->pipeline.flatshade->next = next;
139 next = draw->pipeline.flatshade;
140 }
141
142 if (draw->feedback.enabled || draw->feedback.discard) {
143 draw->pipeline.feedback->next = next;
144 next = draw->pipeline.feedback;
145 }
146
147 draw->pipeline.first = next;
148 }
149
150
151 void draw_set_feedback_state( struct draw_context *draw,
152 const struct pipe_feedback_state *feedback )
153 {
154 draw->feedback = *feedback;
155 validate_pipeline( draw );
156 }
157
158
159 /**
160 * Register new primitive rasterization/rendering state.
161 * This causes the drawing pipeline to be rebuilt.
162 */
163 void draw_set_rasterizer_state( struct draw_context *draw,
164 const struct pipe_rasterizer_state *raster )
165 {
166 draw->rasterizer = raster;
167 validate_pipeline( draw );
168 }
169
170
171 /**
172 * Plug in the primitive rendering/rasterization stage.
173 * This is provided by the device driver.
174 */
175 void draw_set_rasterize_stage( struct draw_context *draw,
176 struct draw_stage *stage )
177 {
178 draw->pipeline.rasterize = stage;
179 }
180
181
182 /**
183 * Set the draw module's clipping state.
184 */
185 void draw_set_clip_state( struct draw_context *draw,
186 const struct pipe_clip_state *clip )
187 {
188 assert(clip->nr <= PIPE_MAX_CLIP_PLANES);
189 memcpy(&draw->plane[6], clip->ucp, clip->nr * sizeof(clip->ucp[0]));
190 draw->nr_planes = 6 + clip->nr;
191 /* bitmask of the enabled user-defined clip planes */
192 draw->user_clipmask = ((1 << clip->nr) - 1) << 6;
193 }
194
195
196 /**
197 * Set the draw module's viewport state.
198 */
199 void draw_set_viewport_state( struct draw_context *draw,
200 const struct pipe_viewport_state *viewport )
201 {
202 draw->viewport = *viewport; /* struct copy */
203 }
204
205
206 void
207 draw_set_vertex_shader(struct draw_context *draw,
208 const struct pipe_shader_state *shader)
209 {
210 draw->vertex_shader = *shader;
211 }
212
213
214 void
215 draw_set_vertex_buffer(struct draw_context *draw,
216 unsigned attr,
217 const struct pipe_vertex_buffer *buffer)
218 {
219 assert(attr < PIPE_ATTRIB_MAX);
220 draw->vertex_buffer[attr] = *buffer;
221 }
222
223
224 void
225 draw_set_vertex_element(struct draw_context *draw,
226 unsigned attr,
227 const struct pipe_vertex_element *element)
228 {
229 assert(attr < PIPE_ATTRIB_MAX);
230 draw->vertex_element[attr] = *element;
231 }
232
233
234 /**
235 * Tell drawing context where to find mapped vertex buffers.
236 */
237 void
238 draw_set_mapped_vertex_buffer(struct draw_context *draw,
239 unsigned attr, const void *buffer)
240 {
241 draw->mapped_vbuffer[attr] = buffer;
242 }
243
244
245 void
246 draw_set_mapped_constant_buffer(struct draw_context *draw,
247 const void *buffer)
248 {
249 draw->mapped_constants = buffer;
250 }
251
252
253 void
254 draw_set_mapped_feedback_buffer(struct draw_context *draw, uint index,
255 void *buffer, uint size)
256 {
257 assert(index < PIPE_MAX_FEEDBACK_ATTRIBS);
258 draw->mapped_feedback_buffer[index] = buffer;
259 draw->mapped_feedback_buffer_size[index] = size; /* in bytes */
260 }
261