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>
35 #include "main/macros.h"
37 #include "pipe/p_util.h"
39 #include "draw_context.h"
40 #include "draw_private.h"
42 struct draw_context
*draw_create( void )
44 struct draw_context
*draw
= CALLOC_STRUCT( draw_context
);
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
);
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 */
63 draw
->vf
= vf_create( GL_TRUE
);
66 /* Statically allocate maximum sized vertices for the cache - could be cleverer...
70 char *tmp
= malloc(Elements(draw
->vcache
.vertex
) * MAX_VERTEX_SIZE
);
72 for (i
= 0; i
< Elements(draw
->vcache
.vertex
); i
++)
73 draw
->vcache
.vertex
[i
] = (struct vertex_header
*)(tmp
+ i
* MAX_VERTEX_SIZE
);
80 void draw_destroy( struct draw_context
*draw
)
83 if (draw
->header
.storage
) {
84 ALIGN_FREE( draw
->header
.storage
);
87 vf_destroy( draw
->vf
);
90 free( draw
->vcache
.vertex
[0] ); /* Frees all the vertices. */
96 * Rebuild the rendering pipeline.
98 static void validate_pipeline( struct draw_context
*draw
)
100 struct draw_stage
*next
= draw
->pipeline
.setup
;
103 * NOTE: we build up the pipeline in end-to-start order.
105 * TODO: make the current primitive part of the state and build
106 * shorter pipelines for lines & points.
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
;
115 if (draw
->setup
.offset_cw
||
116 draw
->setup
.offset_ccw
) {
117 draw
->pipeline
.offset
->next
= next
;
118 next
= draw
->pipeline
.offset
;
121 if (draw
->setup
.light_twoside
) {
122 draw
->pipeline
.twoside
->next
= next
;
123 next
= draw
->pipeline
.twoside
;
126 /* Always run the cull stage as we calculate determinant there
130 draw
->pipeline
.cull
->next
= next
;
131 next
= draw
->pipeline
.cull
;
137 draw
->pipeline
.clip
->next
= next
;
138 next
= draw
->pipeline
.clip
;
141 /* Do software flatshading prior to clipping. XXX: should only do
142 * this for clipped primitives, ie it is a part of the clip
145 if (draw
->setup
.flatshade
) {
146 draw
->pipeline
.flatshade
->next
= next
;
147 next
= draw
->pipeline
.flatshade
;
150 draw
->pipeline
.first
= next
;
155 * Register new primitive setup/rendering state.
156 * This causes the drawing pipeline to be rebuilt.
158 void draw_set_setup_state( struct draw_context
*draw
,
159 const struct pipe_setup_state
*setup
)
161 draw
->setup
= *setup
; /* struct copy */
162 validate_pipeline( draw
);
167 * Plug in the primitive rendering/rasterization stage.
168 * This is provided by the device driver.
170 void draw_set_setup_stage( struct draw_context
*draw
,
171 struct draw_stage
*stage
)
173 draw
->pipeline
.setup
= stage
;
178 * Set the draw module's clipping state.
180 void draw_set_clip_state( struct draw_context
*draw
,
181 const struct pipe_clip_state
*clip
)
183 memcpy(&draw
->plane
[6], clip
->ucp
, clip
->nr
* sizeof(clip
->ucp
[0]));
184 draw
->nr_planes
= 6 + clip
->nr
;
189 * Set the draw module's viewport state.
191 void draw_set_viewport_state( struct draw_context
*draw
,
192 const struct pipe_viewport_state
*viewport
)
194 draw
->viewport
= *viewport
; /* struct copy */
197 vf_set_vp_scale_translate( draw
->vf
, viewport
->scale
, viewport
->translate
);
200 /* Using tnl/ and vf/ modules is temporary while getting started.
201 * Full pipe will have vertex shader, vertex fetch of its own.
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
)
210 draw
->vertex_buffer
= buffers
;
211 draw
->vertex_element
= elements
;