draw: rework handling of non-existing outputs in emit code
[mesa.git] / src / gallium / auxiliary / draw / draw_vertex.h
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 * Post-transform vertex format info. The vertex_info struct is used by
30 * the draw_vbuf code to emit hardware-specific vertex layouts into hw
31 * vertex buffers.
32 *
33 * Author:
34 * Brian Paul
35 */
36
37
38 #ifndef DRAW_VERTEX_H
39 #define DRAW_VERTEX_H
40
41
42 #include "pipe/p_compiler.h"
43 #include "pipe/p_state.h"
44 #include "util/u_debug.h"
45 #include "util/u_memory.h"
46
47 #define DRAW_ATTR_NONEXIST 255
48
49 /**
50 * Vertex attribute emit modes
51 */
52 enum attrib_emit {
53 EMIT_OMIT, /**< don't emit the attribute */
54 EMIT_1F,
55 EMIT_1F_PSIZE, /**< insert constant point size */
56 EMIT_2F,
57 EMIT_3F,
58 EMIT_4F,
59 EMIT_4UB, /**< is RGBA like the rest */
60 EMIT_4UB_BGRA
61 };
62
63
64 /**
65 * Attribute interpolation mode
66 */
67 enum interp_mode {
68 INTERP_NONE, /**< never interpolate vertex header info */
69 INTERP_POS, /**< special case for frag position */
70 INTERP_CONSTANT,
71 INTERP_LINEAR,
72 INTERP_PERSPECTIVE
73 };
74
75
76 /**
77 * Information about hardware/rasterization vertex layout.
78 */
79 struct vertex_info
80 {
81 uint num_attribs;
82 uint hwfmt[4]; /**< hardware format info for this format */
83 uint size; /**< total vertex size in dwords */
84
85 /* Keep this small and at the end of the struct to allow quick
86 * memcmp() comparisons.
87 */
88 struct {
89 unsigned interp_mode:4; /**< INTERP_x */
90 unsigned emit:4; /**< EMIT_x */
91 unsigned src_index:8; /**< map to post-xform attribs */
92 } attrib[PIPE_MAX_SHADER_OUTPUTS];
93 };
94
95 static inline size_t
96 draw_vinfo_size( const struct vertex_info *a )
97 {
98 return offsetof(const struct vertex_info, attrib[a->num_attribs]);
99 }
100
101 static inline int
102 draw_vinfo_compare( const struct vertex_info *a,
103 const struct vertex_info *b )
104 {
105 size_t sizea = draw_vinfo_size( a );
106 return memcmp( a, b, sizea );
107 }
108
109 static inline void
110 draw_vinfo_copy( struct vertex_info *dst,
111 const struct vertex_info *src )
112 {
113 size_t size = draw_vinfo_size( src );
114 memcpy( dst, src, size );
115 }
116
117
118
119 /**
120 * Add another attribute to the given vertex_info object.
121 * \param src_index indicates which post-transformed vertex attrib slot
122 * corresponds to this attribute.
123 * \return slot in which the attribute was added
124 */
125 static inline uint
126 draw_emit_vertex_attr(struct vertex_info *vinfo,
127 enum attrib_emit emit,
128 enum interp_mode interp, /* only used by softpipe??? */
129 int src_index)
130 {
131 const uint n = vinfo->num_attribs;
132
133 /* If the src_index is negative, meaning it hasn't been found
134 * we'll assign it all zeros later - set to DRAW_ATTR_NONEXIST */
135 if (src_index < 0) {
136 src_index = DRAW_ATTR_NONEXIST;
137 }
138
139 assert(n < Elements(vinfo->attrib));
140 vinfo->attrib[n].emit = emit;
141 vinfo->attrib[n].interp_mode = interp;
142 vinfo->attrib[n].src_index = src_index;
143 vinfo->num_attribs++;
144 return n;
145 }
146
147
148 extern void draw_compute_vertex_size(struct vertex_info *vinfo);
149
150 void draw_dump_emitted_vertex(const struct vertex_info *vinfo,
151 const uint8_t *data);
152
153
154 static inline enum pipe_format draw_translate_vinfo_format(enum attrib_emit emit)
155 {
156 switch (emit) {
157 case EMIT_OMIT:
158 return PIPE_FORMAT_NONE;
159 case EMIT_1F:
160 case EMIT_1F_PSIZE:
161 return PIPE_FORMAT_R32_FLOAT;
162 case EMIT_2F:
163 return PIPE_FORMAT_R32G32_FLOAT;
164 case EMIT_3F:
165 return PIPE_FORMAT_R32G32B32_FLOAT;
166 case EMIT_4F:
167 return PIPE_FORMAT_R32G32B32A32_FLOAT;
168 case EMIT_4UB:
169 return PIPE_FORMAT_R8G8B8A8_UNORM;
170 case EMIT_4UB_BGRA:
171 return PIPE_FORMAT_B8G8R8A8_UNORM;
172 default:
173 assert(!"unexpected format");
174 return PIPE_FORMAT_NONE;
175 }
176 }
177
178 static inline unsigned draw_translate_vinfo_size(enum attrib_emit emit)
179 {
180 switch (emit) {
181 case EMIT_OMIT:
182 return 0;
183 case EMIT_1F:
184 case EMIT_1F_PSIZE:
185 return 1 * sizeof(float);
186 case EMIT_2F:
187 return 2 * sizeof(float);
188 case EMIT_3F:
189 return 3 * sizeof(float);
190 case EMIT_4F:
191 return 4 * sizeof(float);
192 case EMIT_4UB:
193 return 4 * sizeof(unsigned char);
194 case EMIT_4UB_BGRA:
195 return 4 * sizeof(unsigned char);
196 default:
197 assert(!"unexpected format");
198 return 0;
199 }
200 }
201
202 #endif /* DRAW_VERTEX_H */