replace memcpy() with assignment, better type safety
[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 #ifdef MESA
35 #include "main/macros.h"
36 #else
37 #include "pipe/p_util.h"
38 #endif
39 #include "draw_context.h"
40 #include "draw_private.h"
41
42 struct draw_context *draw_create( void )
43 {
44 struct draw_context *draw = CALLOC_STRUCT( draw_context );
45
46 /* create pipeline stages */
47 draw->pipeline.unfilled = draw_unfilled_stage( draw );
48 draw->pipeline.twoside = draw_twoside_stage( draw );
49 draw->pipeline.offset = draw_offset_stage( draw );
50 draw->pipeline.clip = draw_clip_stage( draw );
51 draw->pipeline.flatshade = draw_flatshade_stage( draw );
52 draw->pipeline.cull = draw_cull_stage( draw );
53
54 ASSIGN_4V( draw->plane[0], -1, 0, 0, 1 );
55 ASSIGN_4V( draw->plane[1], 1, 0, 0, 1 );
56 ASSIGN_4V( draw->plane[2], 0, -1, 0, 1 );
57 ASSIGN_4V( draw->plane[3], 0, 1, 0, 1 );
58 ASSIGN_4V( draw->plane[4], 0, 0, 1, 1 ); /* yes these are correct */
59 ASSIGN_4V( draw->plane[5], 0, 0, -1, 1 ); /* mesa's a bit wonky */
60 draw->nr_planes = 6;
61
62 #ifdef MESA
63 draw->vf = vf_create( GL_TRUE );
64 #endif
65
66 /* Statically allocate maximum sized vertices for the cache - could be cleverer...
67 */
68 {
69 int i;
70 char *tmp = malloc(Elements(draw->vcache.vertex) * MAX_VERTEX_SIZE);
71
72 for (i = 0; i < Elements(draw->vcache.vertex); i++)
73 draw->vcache.vertex[i] = (struct vertex_header *)(tmp + i * MAX_VERTEX_SIZE);
74 }
75
76 return draw;
77 }
78
79
80 void draw_destroy( struct draw_context *draw )
81 {
82 #ifdef MESA
83 if (draw->header.storage) {
84 ALIGN_FREE( draw->header.storage );
85 }
86
87 vf_destroy( draw->vf );
88 #endif
89
90 free( draw->vcache.vertex[0] ); /* Frees all the vertices. */
91 free( draw );
92 }
93
94
95 /**
96 * Rebuild the rendering pipeline.
97 */
98 static void validate_pipeline( struct draw_context *draw )
99 {
100 struct draw_stage *next = draw->pipeline.setup;
101
102 /*
103 * NOTE: we build up the pipeline in end-to-start order.
104 *
105 * TODO: make the current primitive part of the state and build
106 * shorter pipelines for lines & points.
107 */
108
109 if (draw->setup.fill_cw != PIPE_POLYGON_MODE_FILL ||
110 draw->setup.fill_ccw != PIPE_POLYGON_MODE_FILL) {
111 draw->pipeline.unfilled->next = next;
112 next = draw->pipeline.unfilled;
113 }
114
115 if (draw->setup.offset_cw ||
116 draw->setup.offset_ccw) {
117 draw->pipeline.offset->next = next;
118 next = draw->pipeline.offset;
119 }
120
121 if (draw->setup.light_twoside) {
122 draw->pipeline.twoside->next = next;
123 next = draw->pipeline.twoside;
124 }
125
126 /* Always run the cull stage as we calculate determinant there
127 * also. Fix this..
128 */
129 {
130 draw->pipeline.cull->next = next;
131 next = draw->pipeline.cull;
132 }
133
134 /* Clip stage
135 */
136 {
137 draw->pipeline.clip->next = next;
138 next = draw->pipeline.clip;
139 }
140
141 /* Do software flatshading prior to clipping. XXX: should only do
142 * this for clipped primitives, ie it is a part of the clip
143 * routine.
144 */
145 if (draw->setup.flatshade) {
146 draw->pipeline.flatshade->next = next;
147 next = draw->pipeline.flatshade;
148 }
149
150 draw->pipeline.first = next;
151 }
152
153
154 /**
155 * Register new primitive setup/rendering state.
156 * This causes the drawing pipeline to be rebuilt.
157 */
158 void draw_set_setup_state( struct draw_context *draw,
159 const struct pipe_setup_state *setup )
160 {
161 draw->setup = *setup; /* struct copy */
162 validate_pipeline( draw );
163 }
164
165
166 /**
167 * Plug in the primitive rendering/rasterization stage.
168 * This is provided by the device driver.
169 */
170 void draw_set_setup_stage( struct draw_context *draw,
171 struct draw_stage *stage )
172 {
173 draw->pipeline.setup = stage;
174 }
175
176
177 /**
178 * Set the draw module's clipping state.
179 */
180 void draw_set_clip_state( struct draw_context *draw,
181 const struct pipe_clip_state *clip )
182 {
183 memcpy(&draw->plane[6], clip->ucp, clip->nr * sizeof(clip->ucp[0]));
184 draw->nr_planes = 6 + clip->nr;
185 }
186
187
188 /**
189 * Set the draw module's viewport state.
190 */
191 void draw_set_viewport_state( struct draw_context *draw,
192 const struct pipe_viewport_state *viewport )
193 {
194 draw->viewport = *viewport; /* struct copy */
195
196 #ifdef MESA
197 vf_set_vp_scale_translate( draw->vf, viewport->scale, viewport->translate );
198 #endif
199
200 /* Using tnl/ and vf/ modules is temporary while getting started.
201 * Full pipe will have vertex shader, vertex fetch of its own.
202 */
203 }
204
205
206 void draw_set_vertex_array_info(struct draw_context *draw,
207 const struct pipe_vertex_buffer *buffers,
208 const struct pipe_vertex_element *elements)
209 {
210 draw->vertex_buffer = buffers;
211 draw->vertex_element = elements;
212 }
213