1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Keith Whitwell <keith@tungstengraphics.com>
34 #include "pipe/p_util.h"
35 #include "draw_context.h"
36 #include "draw_private.h"
40 struct draw_context
*draw_create( void )
42 struct draw_context
*draw
= CALLOC_STRUCT( draw_context
);
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
);
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 */
61 /* Statically allocate maximum sized vertices for the cache - could be cleverer...
65 char *tmp
= malloc(Elements(draw
->vcache
.vertex
) * MAX_VERTEX_SIZE
);
67 for (i
= 0; i
< Elements(draw
->vcache
.vertex
); i
++)
68 draw
->vcache
.vertex
[i
] = (struct vertex_header
*)(tmp
+ i
* MAX_VERTEX_SIZE
);
71 draw
->attrib_front0
= -1;
72 draw
->attrib_back0
= -1;
73 draw
->attrib_front1
= -1;
74 draw
->attrib_back1
= -1;
80 void draw_destroy( struct draw_context
*draw
)
82 free( draw
->vcache
.vertex
[0] ); /* Frees all the vertices. */
88 * Rebuild the rendering pipeline.
90 static void validate_pipeline( struct draw_context
*draw
)
92 struct draw_stage
*next
= draw
->pipeline
.rasterize
;
95 * NOTE: we build up the pipeline in end-to-start order.
97 * TODO: make the current primitive part of the state and build
98 * shorter pipelines for lines & points.
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
;
107 if (draw
->rasterizer
->offset_cw
||
108 draw
->rasterizer
->offset_ccw
) {
109 draw
->pipeline
.offset
->next
= next
;
110 next
= draw
->pipeline
.offset
;
113 if (draw
->rasterizer
->light_twoside
) {
114 draw
->pipeline
.twoside
->next
= next
;
115 next
= draw
->pipeline
.twoside
;
118 /* Always run the cull stage as we calculate determinant there
122 draw
->pipeline
.cull
->next
= next
;
123 next
= draw
->pipeline
.cull
;
129 draw
->pipeline
.clip
->next
= next
;
130 next
= draw
->pipeline
.clip
;
133 /* Do software flatshading prior to clipping. XXX: should only do
134 * this for clipped primitives, ie it is a part of the clip
137 if (draw
->rasterizer
->flatshade
) {
138 draw
->pipeline
.flatshade
->next
= next
;
139 next
= draw
->pipeline
.flatshade
;
142 if (draw
->feedback
.enabled
|| draw
->feedback
.discard
) {
143 draw
->pipeline
.feedback
->next
= next
;
144 next
= draw
->pipeline
.feedback
;
147 draw
->pipeline
.first
= next
;
151 void draw_set_feedback_state( struct draw_context
*draw
,
152 const struct pipe_feedback_state
*feedback
)
154 draw
->feedback
= *feedback
;
155 validate_pipeline( draw
);
160 * Register new primitive rasterization/rendering state.
161 * This causes the drawing pipeline to be rebuilt.
163 void draw_set_rasterizer_state( struct draw_context
*draw
,
164 const struct pipe_rasterizer_state
*raster
)
166 draw
->rasterizer
= raster
;
167 validate_pipeline( draw
);
172 * Plug in the primitive rendering/rasterization stage.
173 * This is provided by the device driver.
175 void draw_set_rasterize_stage( struct draw_context
*draw
,
176 struct draw_stage
*stage
)
178 draw
->pipeline
.rasterize
= stage
;
183 * Set the draw module's clipping state.
185 void draw_set_clip_state( struct draw_context
*draw
,
186 const struct pipe_clip_state
*clip
)
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;
197 * Set the draw module's viewport state.
199 void draw_set_viewport_state( struct draw_context
*draw
,
200 const struct pipe_viewport_state
*viewport
)
202 draw
->viewport
= *viewport
; /* struct copy */
207 draw_set_vertex_shader(struct draw_context
*draw
,
208 const struct pipe_shader_state
*shader
)
210 draw
->vertex_shader
= *shader
;
215 draw_set_vertex_buffer(struct draw_context
*draw
,
217 const struct pipe_vertex_buffer
*buffer
)
219 assert(attr
< PIPE_ATTRIB_MAX
);
220 draw
->vertex_buffer
[attr
] = *buffer
;
225 draw_set_vertex_element(struct draw_context
*draw
,
227 const struct pipe_vertex_element
*element
)
229 assert(attr
< PIPE_ATTRIB_MAX
);
230 draw
->vertex_element
[attr
] = *element
;
235 * Tell drawing context where to find mapped vertex buffers.
238 draw_set_mapped_vertex_buffer(struct draw_context
*draw
,
239 unsigned attr
, const void *buffer
)
241 draw
->mapped_vbuffer
[attr
] = buffer
;
246 draw_set_mapped_constant_buffer(struct draw_context
*draw
,
249 draw
->mapped_constants
= buffer
;
254 draw_set_mapped_feedback_buffer(struct draw_context
*draw
, uint index
,
255 void *buffer
, uint size
)
257 assert(index
< PIPE_MAX_FEEDBACK_ATTRIBS
);
258 draw
->mapped_feedback_buffer
[index
] = buffer
;
259 draw
->mapped_feedback_buffer_size
[index
] = size
; /* in bytes */