remove unneeded #includes, added assertion in draw_compute_vertex_size()
[mesa.git] / src / mesa / pipe / draw / draw_vertex.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 * Functions for specifying the post-transformation vertex layout.
30 *
31 * Author:
32 * Brian Paul
33 * Keith Whitwell
34 */
35
36
37 #include "pipe/draw/draw_private.h"
38 #include "pipe/draw/draw_vertex.h"
39
40
41 static INLINE void
42 emit_vertex_attr(struct vertex_info *vinfo,
43 enum attrib_format format, enum interp_mode interp)
44 {
45 const uint n = vinfo->num_attribs;
46 vinfo->interp_mode[n] = interp;
47 vinfo->format[n] = format;
48 vinfo->num_attribs++;
49 }
50
51
52 /**
53 * Compute the size of a vertex, in dwords/floats, to update the
54 * vinfo->size field.
55 */
56 void
57 draw_compute_vertex_size(struct vertex_info *vinfo)
58 {
59 uint i;
60
61 vinfo->size = 0;
62 for (i = 0; i < vinfo->num_attribs; i++) {
63 switch (vinfo->format[i]) {
64 case FORMAT_OMIT:
65 break;
66 case FORMAT_4UB:
67 /* fall-through */
68 case FORMAT_1F:
69 vinfo->size += 1;
70 break;
71 case FORMAT_2F:
72 vinfo->size += 2;
73 break;
74 case FORMAT_3F:
75 vinfo->size += 3;
76 break;
77 case FORMAT_4F:
78 case FORMAT_4F_VIEWPORT:
79 vinfo->size += 4;
80 break;
81 default:
82 assert(0);
83 }
84 }
85
86 assert(vinfo->size * 4 <= MAX_VERTEX_SIZE);
87 }
88
89
90 /**
91 * Tell the drawing module about the layout of post-transformation vertices
92 */
93 void
94 draw_set_vertex_attributes( struct draw_context *draw,
95 const uint *slot_to_vf_attr,
96 const enum interp_mode *interps,
97 unsigned nr_attrs )
98 {
99 struct vertex_info *vinfo = &draw->vertex_info;
100 unsigned i;
101
102 #if 0
103 assert(slot_to_vf_attr[0] == TGSI_ATTRIB_POS);
104 #endif
105
106 memset(vinfo, 0, sizeof(*vinfo));
107
108 /*
109 * First three attribs are always the same: header, clip pos, winpos
110 */
111 emit_vertex_attr(vinfo, FORMAT_1F, INTERP_NONE);
112 emit_vertex_attr(vinfo, FORMAT_4F, INTERP_LINEAR);
113 emit_vertex_attr(vinfo, FORMAT_4F_VIEWPORT, INTERP_LINEAR);
114
115 /*
116 * Remaining attribs (color, texcoords, etc)
117 */
118 for (i = 1; i < nr_attrs; i++) {
119 emit_vertex_attr(vinfo, FORMAT_4F, interps[i]);
120 }
121
122 draw_compute_vertex_size(vinfo);
123 }
124
125
126 /**
127 * This function is used to tell the draw module about attributes
128 * (like colors) that need to be selected based on front/back face
129 * orientation.
130 *
131 * The logic is:
132 * if (polygon is back-facing) {
133 * vertex->attrib[front0] = vertex->attrib[back0];
134 * vertex->attrib[front1] = vertex->attrib[back1];
135 * }
136 *
137 * \param front0 first attrib to replace if the polygon is back-facing
138 * \param back0 first attrib to copy if the polygon is back-facing
139 * \param front1 second attrib to replace if the polygon is back-facing
140 * \param back1 second attrib to copy if the polygon is back-facing
141 *
142 * Pass -1 to disable two-sided attributes.
143 */
144 void
145 draw_set_twoside_attributes(struct draw_context *draw,
146 uint front0, uint back0,
147 uint front1, uint back1)
148 {
149 /* XXX we could alternately pass an array of front/back attribs if there's
150 * ever need for more than two. One could imagine a shader extension
151 * that allows arbitrary attributes to be selected based on polygon
152 * orientation...
153 */
154 draw->attrib_front0 = front0;
155 draw->attrib_back0 = back0;
156 draw->attrib_front1 = front1;
157 draw->attrib_back1 = back1;
158 }