711bcd02f6f8c8befa426f71347fad74ca3ca834
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 #if defined(__i386__) || defined(__386__)
45 draw
->use_sse
= GETENV( "GALLIUM_NOSSE" ) == NULL
;
47 draw
->use_sse
= FALSE
;
50 /* create pipeline stages */
51 draw
->pipeline
.wide
= draw_wide_stage( draw
);
52 draw
->pipeline
.stipple
= draw_stipple_stage( draw
);
53 draw
->pipeline
.unfilled
= draw_unfilled_stage( draw
);
54 draw
->pipeline
.twoside
= draw_twoside_stage( draw
);
55 draw
->pipeline
.offset
= draw_offset_stage( draw
);
56 draw
->pipeline
.clip
= draw_clip_stage( draw
);
57 draw
->pipeline
.flatshade
= draw_flatshade_stage( draw
);
58 draw
->pipeline
.cull
= draw_cull_stage( draw
);
59 draw
->pipeline
.validate
= draw_validate_stage( draw
);
60 draw
->pipeline
.first
= draw
->pipeline
.validate
;
62 ASSIGN_4V( draw
->plane
[0], -1, 0, 0, 1 );
63 ASSIGN_4V( draw
->plane
[1], 1, 0, 0, 1 );
64 ASSIGN_4V( draw
->plane
[2], 0, -1, 0, 1 );
65 ASSIGN_4V( draw
->plane
[3], 0, 1, 0, 1 );
66 ASSIGN_4V( draw
->plane
[4], 0, 0, 1, 1 ); /* yes these are correct */
67 ASSIGN_4V( draw
->plane
[5], 0, 0, -1, 1 ); /* mesa's a bit wonky */
70 /* Statically allocate maximum sized vertices for the cache - could be cleverer...
74 const unsigned size
= (MAX_VERTEX_SIZE
+ 0x0f) & ~0x0f;
75 char *tmp
= align_malloc(Elements(draw
->vcache
.vertex
) * size
, 16);
77 for (i
= 0; i
< Elements(draw
->vcache
.vertex
); i
++)
78 draw
->vcache
.vertex
[i
] = (struct vertex_header
*)(tmp
+ i
* size
);
81 draw
->convert_wide_points
= TRUE
;
82 draw
->convert_wide_lines
= TRUE
;
84 draw
->reduced_prim
= ~0; /* != any of PIPE_PRIM_x */
86 draw_vertex_cache_invalidate( draw
);
87 draw_set_mapped_element_buffer( draw
, 0, NULL
);
93 void draw_destroy( struct draw_context
*draw
)
95 draw
->pipeline
.wide
->destroy( draw
->pipeline
.wide
);
96 draw
->pipeline
.stipple
->destroy( draw
->pipeline
.stipple
);
97 draw
->pipeline
.unfilled
->destroy( draw
->pipeline
.unfilled
);
98 draw
->pipeline
.twoside
->destroy( draw
->pipeline
.twoside
);
99 draw
->pipeline
.offset
->destroy( draw
->pipeline
.offset
);
100 draw
->pipeline
.clip
->destroy( draw
->pipeline
.clip
);
101 draw
->pipeline
.flatshade
->destroy( draw
->pipeline
.flatshade
);
102 draw
->pipeline
.cull
->destroy( draw
->pipeline
.cull
);
103 draw
->pipeline
.validate
->destroy( draw
->pipeline
.validate
);
104 if (draw
->pipeline
.rasterize
)
105 draw
->pipeline
.rasterize
->destroy( draw
->pipeline
.rasterize
);
106 tgsi_exec_machine_free_data(&draw
->machine
);
107 FREE( draw
->vcache
.vertex
[0] ); /* Frees all the vertices. */
113 void draw_flush( struct draw_context
*draw
)
115 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
121 * Register new primitive rasterization/rendering state.
122 * This causes the drawing pipeline to be rebuilt.
124 void draw_set_rasterizer_state( struct draw_context
*draw
,
125 const struct pipe_rasterizer_state
*raster
)
127 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
129 draw
->rasterizer
= raster
;
134 * Plug in the primitive rendering/rasterization stage (which is the last
135 * stage in the drawing pipeline).
136 * This is provided by the device driver.
138 void draw_set_rasterize_stage( struct draw_context
*draw
,
139 struct draw_stage
*stage
)
141 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
143 draw
->pipeline
.rasterize
= stage
;
148 * Set the draw module's clipping state.
150 void draw_set_clip_state( struct draw_context
*draw
,
151 const struct pipe_clip_state
*clip
)
153 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
155 assert(clip
->nr
<= PIPE_MAX_CLIP_PLANES
);
156 memcpy(&draw
->plane
[6], clip
->ucp
, clip
->nr
* sizeof(clip
->ucp
[0]));
157 draw
->nr_planes
= 6 + clip
->nr
;
162 * Set the draw module's viewport state.
164 void draw_set_viewport_state( struct draw_context
*draw
,
165 const struct pipe_viewport_state
*viewport
)
167 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
168 draw
->viewport
= *viewport
; /* struct copy */
174 draw_set_vertex_buffer(struct draw_context
*draw
,
176 const struct pipe_vertex_buffer
*buffer
)
178 draw_do_flush( draw
, DRAW_FLUSH_VERTEX_CACHE
/*STATE_CHANGE*/ );
179 assert(attr
< PIPE_ATTRIB_MAX
);
180 draw
->vertex_buffer
[attr
] = *buffer
;
185 draw_set_vertex_element(struct draw_context
*draw
,
187 const struct pipe_vertex_element
*element
)
189 draw_do_flush( draw
, DRAW_FLUSH_VERTEX_CACHE
/*STATE_CHANGE*/ );
190 assert(attr
< PIPE_ATTRIB_MAX
);
191 draw
->vertex_element
[attr
] = *element
;
196 * Tell drawing context where to find mapped vertex buffers.
199 draw_set_mapped_vertex_buffer(struct draw_context
*draw
,
200 unsigned attr
, const void *buffer
)
202 draw_do_flush( draw
, DRAW_FLUSH_VERTEX_CACHE
/*STATE_CHANGE*/ );
203 draw
->user
.vbuffer
[attr
] = buffer
;
208 draw_set_mapped_constant_buffer(struct draw_context
*draw
,
211 draw_do_flush( draw
, DRAW_FLUSH_VERTEX_CACHE
/*STATE_CHANGE*/ );
212 draw
->user
.constants
= buffer
;
217 * Tells the draw module whether to convert wide points (size != 1)
221 draw_convert_wide_points(struct draw_context
*draw
, boolean enable
)
223 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
224 draw
->convert_wide_points
= enable
;
229 * Tells the draw module whether to convert wide lines (width != 1)
233 draw_convert_wide_lines(struct draw_context
*draw
, boolean enable
)
235 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
236 draw
->convert_wide_lines
= enable
;
241 * Allocate space for temporary post-transform vertices, such as for clipping.
243 void draw_alloc_tmps( struct draw_stage
*stage
, unsigned nr
)
250 ubyte
*store
= (ubyte
*) MALLOC( MAX_VERTEX_SIZE
* nr
);
253 stage
->tmp
= (struct vertex_header
**) MALLOC( sizeof(struct vertex_header
*) * nr
);
255 for (i
= 0; i
< nr
; i
++)
256 stage
->tmp
[i
] = (struct vertex_header
*)(store
+ i
* MAX_VERTEX_SIZE
);
261 void draw_free_tmps( struct draw_stage
*stage
)
264 FREE( stage
->tmp
[0] );
271 boolean
draw_use_sse(struct draw_context
*draw
)
273 return (boolean
) draw
->use_sse
;
277 void draw_reset_vertex_ids(struct draw_context
*draw
)
279 struct draw_stage
*stage
= draw
->pipeline
.first
;
284 for (i
= 0; i
< stage
->nr_tmps
; i
++)
285 stage
->tmp
[i
]->vertex_id
= UNDEFINED_VERTEX_ID
;
290 draw_vertex_cache_reset_vertex_ids(draw
);