blob: dd75f9aefc3211d3ca876471dd92325752a88064 [file] [log] [blame]
Keith Whitwell8e4a95a2007-05-24 10:41:34 +01001/**************************************************************************
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
Brian279ffe32007-07-09 16:14:26 -060028/**
29 * Private data structures, etc for the draw module.
Keith Whitwell8e4a95a2007-05-24 10:41:34 +010030 */
31
Keith Whitwell8e4a95a2007-05-24 10:41:34 +010032
Brian279ffe32007-07-09 16:14:26 -060033/**
34 * Authors:
35 * Keith Whitwell <keith@tungstengraphics.com>
36 * Brian Paul
Keith Whitwell8e4a95a2007-05-24 10:41:34 +010037 */
Keith Whitwell8e4a95a2007-05-24 10:41:34 +010038
Brian279ffe32007-07-09 16:14:26 -060039
40#ifndef DRAW_PRIVATE_H
41#define DRAW_PRIVATE_H
42
43
Brian279ffe32007-07-09 16:14:26 -060044#include "pipe/p_state.h"
45#include "pipe/p_defines.h"
Brian766fa512007-08-20 17:02:07 -060046
Zack Rusina1a989f2007-09-28 04:33:55 -040047#include "x86/rtasm/x86sse.h"
José Fonseca6acd63a2008-02-15 17:50:12 +090048#include "tgsi/exec/tgsi_exec.h"
Brian0d13ade2007-10-02 11:46:11 -060049
Brian279ffe32007-07-09 16:14:26 -060050
Brianaceeb802008-02-18 16:19:05 -070051struct pipe_context;
Zack Rusin25b17b22007-10-29 08:27:32 -040052struct gallivm_prog;
53struct gallivm_cpu_engine;
José Fonsecac28fdf32007-11-07 12:08:19 +000054
Brian279ffe32007-07-09 16:14:26 -060055/**
56 * Basic vertex info.
57 * Carry some useful information around with the vertices in the prim pipe.
58 */
59struct vertex_header {
Brianfd0a6d62007-08-16 12:52:20 -060060 unsigned clipmask:12;
61 unsigned edgeflag:1;
Keith Whitwella37e0da2007-09-25 13:20:53 +010062 unsigned pad:3;
63 unsigned vertex_id:16;
Brian279ffe32007-07-09 16:14:26 -060064
Brianfd0a6d62007-08-16 12:52:20 -060065 float clip[4];
Brian279ffe32007-07-09 16:14:26 -060066
Brianfd0a6d62007-08-16 12:52:20 -060067 float data[][4]; /* Note variable size */
Brian279ffe32007-07-09 16:14:26 -060068};
69
José Fonsecac28fdf32007-11-07 12:08:19 +000070/* NOTE: It should match vertex_id size above */
71#define UNDEFINED_VERTEX_ID 0xffff
72
Briand8b16d42007-08-23 17:00:47 -060073/* XXX This is too large */
Brian08673452007-09-20 13:42:37 -060074#define MAX_VERTEX_SIZE ((2 + PIPE_MAX_SHADER_OUTPUTS) * 4 * sizeof(float))
Keith Whitwell4bb21342007-08-14 15:44:41 +010075
76
Brian279ffe32007-07-09 16:14:26 -060077
78/**
79 * Basic info for a point/line/triangle primitive.
80 */
81struct prim_header {
Brianfd0a6d62007-08-16 12:52:20 -060082 float det; /**< front/back face determinant */
83 unsigned reset_line_stipple:1;
84 unsigned edgeflags:3;
85 unsigned pad:28;
Brian279ffe32007-07-09 16:14:26 -060086 struct vertex_header *v[3]; /**< 1 to 3 vertex pointers */
87};
88
89
90
91struct draw_context;
92
93/**
94 * Base class for all primitive drawing stages.
95 */
Brianea470ee2007-07-12 13:32:31 -060096struct draw_stage
Brian279ffe32007-07-09 16:14:26 -060097{
98 struct draw_context *draw; /**< parent context */
99
Brianea470ee2007-07-12 13:32:31 -0600100 struct draw_stage *next; /**< next stage in pipeline */
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100101
Brian193c85e2007-11-09 07:54:46 -0700102 struct vertex_header **tmp; /**< temp vert storage, such as for clipping */
Brianfd0a6d62007-08-16 12:52:20 -0600103 unsigned nr_tmps;
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100104
Brianea470ee2007-07-12 13:32:31 -0600105 void (*point)( struct draw_stage *,
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100106 struct prim_header * );
107
Brianea470ee2007-07-12 13:32:31 -0600108 void (*line)( struct draw_stage *,
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100109 struct prim_header * );
110
Brianea470ee2007-07-12 13:32:31 -0600111 void (*tri)( struct draw_stage *,
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100112 struct prim_header * );
Zack Rusina1a989f2007-09-28 04:33:55 -0400113
Brian0bfd0852008-01-25 15:59:27 -0700114 void (*flush)( struct draw_stage *,
Brian1603a332008-01-25 17:21:05 -0700115 unsigned flags );
Brian0360b492007-07-25 15:48:09 -0600116
117 void (*reset_stipple_counter)( struct draw_stage * );
Michald7545482007-11-23 11:30:51 +0000118
119 void (*destroy)( struct draw_stage * );
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100120};
121
122
Keith Whitwell4bb21342007-08-14 15:44:41 +0100123#define PRIM_QUEUE_LENGTH 16
124#define VCACHE_SIZE 32
125#define VCACHE_OVERFLOW 4
126#define VS_QUEUE_LENGTH (VCACHE_SIZE + VCACHE_OVERFLOW + 1) /* can never fill up */
127
Zack Rusina1a989f2007-09-28 04:33:55 -0400128/**
129 * Private version of the compiled vertex_shader
130 */
131struct draw_vertex_shader {
Keith Whitwellb29d8d22008-02-15 13:37:01 +0000132
133 /* This member will disappear shortly:
134 */
michalbc99ea92007-10-26 17:15:30 +0100135 const struct pipe_shader_state *state;
Keith Whitwellb29d8d22008-02-15 13:37:01 +0000136
137 void (*prepare)( struct draw_vertex_shader *shader,
138 struct draw_context *draw );
139
140 /* Run the shader - this interface will get cleaned up in the
141 * future:
142 */
143 void (*run)( struct draw_vertex_shader *shader,
144 struct draw_context *draw,
145 const unsigned *elts,
146 unsigned count,
147 struct vertex_header *vOut[] );
148
149
150 void (*delete)( struct draw_vertex_shader * );
Zack Rusina1a989f2007-09-28 04:33:55 -0400151};
Keith Whitwell4bb21342007-08-14 15:44:41 +0100152
Keith Whitwell027983f2008-01-24 11:19:06 +0000153
154/* Internal function for vertex fetch.
155 */
156typedef void (*fetch_func)(const void *ptr, float *attrib);
Keith Whitwell88858e02008-01-28 12:40:29 +0000157typedef void (*full_fetch_func)( struct draw_context *draw,
158 struct tgsi_exec_machine *machine,
159 const unsigned *elts,
160 unsigned count );
Keith Whitwell027983f2008-01-24 11:19:06 +0000161
162
163
Brian279ffe32007-07-09 16:14:26 -0600164/**
165 * Private context for the drawing module.
166 */
167struct draw_context
168{
Brianfd0a6d62007-08-16 12:52:20 -0600169 /** Drawing/primitive pipeline stages */
Brian279ffe32007-07-09 16:14:26 -0600170 struct {
Brianea470ee2007-07-12 13:32:31 -0600171 struct draw_stage *first; /**< one of the following */
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100172
Keith Whitwell08589f72007-09-26 11:56:17 +0100173 struct draw_stage *validate;
174
Brian279ffe32007-07-09 16:14:26 -0600175 /* stages (in logical order) */
Brianea470ee2007-07-12 13:32:31 -0600176 struct draw_stage *flatshade;
177 struct draw_stage *clip;
178 struct draw_stage *cull;
179 struct draw_stage *twoside;
180 struct draw_stage *offset;
181 struct draw_stage *unfilled;
Brian329a8472008-01-21 14:08:20 -0700182 struct draw_stage *stipple;
Brianaceeb802008-02-18 16:19:05 -0700183 struct draw_stage *aaline;
Briane3444de2007-10-22 11:01:34 -0600184 struct draw_stage *wide;
Zack Rusinde69fc12007-09-18 10:02:16 -0400185 struct draw_stage *rasterize;
Brian279ffe32007-07-09 16:14:26 -0600186 } pipeline;
187
188 /* pipe state that we need: */
Zack Rusin29440182007-09-17 12:59:50 -0400189 const struct pipe_rasterizer_state *rasterizer;
Brian279ffe32007-07-09 16:14:26 -0600190 struct pipe_viewport_state viewport;
Brian0a262992007-08-20 15:11:11 -0600191 struct pipe_vertex_buffer vertex_buffer[PIPE_ATTRIB_MAX];
192 struct pipe_vertex_element vertex_element[PIPE_ATTRIB_MAX];
Keith Whitwellb29d8d22008-02-15 13:37:01 +0000193 struct draw_vertex_shader *vertex_shader;
Brian3e2be1c2007-08-15 18:54:05 -0600194
Briancd364362008-01-23 12:48:41 -0700195 uint num_vs_outputs; /**< convenience, from vertex_shader */
196
Brian64469862007-11-08 17:07:12 -0700197 /* user-space vertex data, buffers */
198 struct {
199 /** vertex element/index buffer (ex: glDrawElements) */
200 const void *elts;
201 /** bytes per index (0, 1, 2 or 4) */
202 unsigned eltSize;
Brian3e2be1c2007-08-15 18:54:05 -0600203
Brian64469862007-11-08 17:07:12 -0700204 /** vertex arrays */
205 const void *vbuffer[PIPE_ATTRIB_MAX];
206
207 /** constant buffer (for vertex shader) */
208 const void *constants;
Brian64469862007-11-08 17:07:12 -0700209 } user;
Brian63a51ae2007-09-06 17:07:09 -0600210
Brian279ffe32007-07-09 16:14:26 -0600211 /* Clip derived state:
212 */
Brianfd0a6d62007-08-16 12:52:20 -0600213 float plane[12][4];
214 unsigned nr_planes;
Brian279ffe32007-07-09 16:14:26 -0600215
Brianf1fb69a2008-01-22 10:16:30 -0700216 boolean convert_wide_points; /**< convert wide points to tris? */
Brianaceeb802008-02-18 16:19:05 -0700217 boolean convert_wide_lines; /**< convert wide lines to tris? */
Keith Whitwellb29d8d22008-02-15 13:37:01 +0000218 boolean use_sse;
Brianf1fb69a2008-01-22 10:16:30 -0700219
Brianaceeb802008-02-18 16:19:05 -0700220 /* If a prim stage introduces new vertex attributes, they'll be stored here
221 */
222 struct {
223 uint semantic_name;
224 uint semantic_index;
225 int slot;
226 } extra_vp_outputs;
227
Brianfd0a6d62007-08-16 12:52:20 -0600228 unsigned reduced_prim;
229
Brian0d13ade2007-10-02 11:46:11 -0600230 /** TGSI program interpreter runtime state */
231 struct tgsi_exec_machine machine;
Keith Whitwell4bb21342007-08-14 15:44:41 +0100232
Keith Whitwell027983f2008-01-24 11:19:06 +0000233 /* Vertex fetch internal state
234 */
235 struct {
236 const ubyte *src_ptr[PIPE_ATTRIB_MAX];
237 unsigned pitch[PIPE_ATTRIB_MAX];
238 fetch_func fetch[PIPE_ATTRIB_MAX];
239 unsigned nr_attrs;
Keith Whitwell88858e02008-01-28 12:40:29 +0000240 full_fetch_func fetch_func;
Keith Whitwell027983f2008-01-24 11:19:06 +0000241 } vertex_fetch;
242
Keith Whitwell4bb21342007-08-14 15:44:41 +0100243 /* Post-tnl vertex cache:
244 */
245 struct {
Brian3df65af2007-10-23 15:02:02 -0600246 unsigned referenced; /**< bitfield */
Brianfd0a6d62007-08-16 12:52:20 -0600247 unsigned idx[VCACHE_SIZE + VCACHE_OVERFLOW];
Keith Whitwell4bb21342007-08-14 15:44:41 +0100248 struct vertex_header *vertex[VCACHE_SIZE + VCACHE_OVERFLOW];
Brianfd0a6d62007-08-16 12:52:20 -0600249 unsigned overflow;
Keith Whitwell08589f72007-09-26 11:56:17 +0100250
Brian64469862007-11-08 17:07:12 -0700251 /** To find space in the vertex cache: */
Keith Whitwell08589f72007-09-26 11:56:17 +0100252 struct vertex_header *(*get_vertex)( struct draw_context *draw,
253 unsigned i );
Keith Whitwell4bb21342007-08-14 15:44:41 +0100254 } vcache;
255
256 /* Vertex shader queue:
257 */
258 struct {
259 struct {
Brian48863cd2007-11-08 16:32:24 -0700260 unsigned elt; /**< index into the user's vertex arrays */
261 struct vertex_header *dest; /**< points into vcache.vertex[] array */
Keith Whitwell4bb21342007-08-14 15:44:41 +0100262 } queue[VS_QUEUE_LENGTH];
Brianfd0a6d62007-08-16 12:52:20 -0600263 unsigned queue_nr;
Keith Whitwell4bb21342007-08-14 15:44:41 +0100264 } vs;
265
Ian Romanick3d136052008-01-29 11:43:04 -0800266 /**
267 * Run the vertex shader on all vertices in the vertex queue.
268 */
269 void (*shader_queue_flush)(struct draw_context *draw);
270
Keith Whitwell4bb21342007-08-14 15:44:41 +0100271 /* Prim pipeline queue:
272 */
273 struct {
Keith Whitwell4bb21342007-08-14 15:44:41 +0100274 /* Need to queue up primitives until their vertices have been
275 * transformed by a vs queue flush.
276 */
277 struct prim_header queue[PRIM_QUEUE_LENGTH];
Brianfd0a6d62007-08-16 12:52:20 -0600278 unsigned queue_nr;
Keith Whitwell4bb21342007-08-14 15:44:41 +0100279 } pq;
Zack Rusin8731e392007-09-28 07:33:54 -0400280
Keith Whitwellb29d8d22008-02-15 13:37:01 +0000281
282 /* This (and the tgsi_exec_machine struct) probably need to be moved somewhere private.
283 */
284 struct gallivm_cpu_engine *engine;
Ian Romanicka89ee8a2008-01-30 20:10:45 -0800285 void *driver_private;
Brian279ffe32007-07-09 16:14:26 -0600286};
287
288
289
Brianea470ee2007-07-12 13:32:31 -0600290extern struct draw_stage *draw_unfilled_stage( struct draw_context *context );
291extern struct draw_stage *draw_twoside_stage( struct draw_context *context );
292extern struct draw_stage *draw_offset_stage( struct draw_context *context );
293extern struct draw_stage *draw_clip_stage( struct draw_context *context );
294extern struct draw_stage *draw_flatshade_stage( struct draw_context *context );
295extern struct draw_stage *draw_cull_stage( struct draw_context *context );
Brian329a8472008-01-21 14:08:20 -0700296extern struct draw_stage *draw_stipple_stage( struct draw_context *context );
Briane3444de2007-10-22 11:01:34 -0600297extern struct draw_stage *draw_wide_stage( struct draw_context *context );
Keith Whitwell08589f72007-09-26 11:56:17 +0100298extern struct draw_stage *draw_validate_stage( struct draw_context *context );
Brian279ffe32007-07-09 16:14:26 -0600299
300
Brianb08102a2008-02-13 10:25:38 -0700301extern void draw_free_temp_verts( struct draw_stage *stage );
302
303extern void draw_alloc_temp_verts( struct draw_stage *stage, unsigned nr );
Brian279ffe32007-07-09 16:14:26 -0600304
José Fonseca3e221802007-11-07 13:07:20 +0000305extern void draw_reset_vertex_ids( struct draw_context *draw );
306
Brian279ffe32007-07-09 16:14:26 -0600307
Brian9a11a4a2007-08-31 11:28:31 -0600308extern int draw_vertex_cache_check_space( struct draw_context *draw,
309 unsigned nr_verts );
310
Brian9a11a4a2007-08-31 11:28:31 -0600311extern void draw_vertex_cache_invalidate( struct draw_context *draw );
312extern void draw_vertex_cache_unreference( struct draw_context *draw );
Keith Whitwella37e0da2007-09-25 13:20:53 +0100313extern void draw_vertex_cache_reset_vertex_ids( struct draw_context *draw );
314
Brian9a11a4a2007-08-31 11:28:31 -0600315extern void draw_vertex_shader_queue_flush( struct draw_context *draw );
316
317struct tgsi_exec_machine;
318
Keith Whitwell027983f2008-01-24 11:19:06 +0000319extern void draw_update_vertex_fetch( struct draw_context *draw );
Brian9a11a4a2007-08-31 11:28:31 -0600320
Brian279ffe32007-07-09 16:14:26 -0600321
Brian1603a332008-01-25 17:21:05 -0700322#define DRAW_FLUSH_SHADER_QUEUE 0x1 /* sized not to overflow, never raised */
323#define DRAW_FLUSH_PRIM_QUEUE 0x2
324#define DRAW_FLUSH_VERTEX_CACHE 0x4
325#define DRAW_FLUSH_STATE_CHANGE 0x8
326#define DRAW_FLUSH_BACKEND 0x10
Keith Whitwell08589f72007-09-26 11:56:17 +0100327
328
Brian1603a332008-01-25 17:21:05 -0700329void draw_do_flush( struct draw_context *draw, unsigned flags );
Keith Whitwell08589f72007-09-26 11:56:17 +0100330
331
332
Brian279ffe32007-07-09 16:14:26 -0600333/**
334 * Get a writeable copy of a vertex.
335 * \param stage drawing stage info
336 * \param vert the vertex to copy (source)
337 * \param idx index into stage's tmp[] array to put the copy (dest)
338 * \return pointer to the copied vertex
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100339 */
340static INLINE struct vertex_header *
Brianea470ee2007-07-12 13:32:31 -0600341dup_vert( struct draw_stage *stage,
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100342 const struct vertex_header *vert,
Brianfd0a6d62007-08-16 12:52:20 -0600343 unsigned idx )
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100344{
345 struct vertex_header *tmp = stage->tmp[idx];
Briancd364362008-01-23 12:48:41 -0700346 const uint vsize = sizeof(struct vertex_header)
347 + stage->draw->num_vs_outputs * 4 * sizeof(float);
348 memcpy(tmp, vert, vsize);
José Fonsecac28fdf32007-11-07 12:08:19 +0000349 tmp->vertex_id = UNDEFINED_VERTEX_ID;
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100350 return tmp;
351}
352
Zack Rusinb46926c2007-10-03 10:31:42 -0400353static INLINE float
354dot4(const float *a, const float *b)
355{
356 float result = (a[0]*b[0] +
357 a[1]*b[1] +
358 a[2]*b[2] +
359 a[3]*b[3]);
360
361 return result;
362}
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100363
Brian279ffe32007-07-09 16:14:26 -0600364#endif /* DRAW_PRIVATE_H */