xref: /aosp_15_r20/external/mesa3d/src/gallium/auxiliary/draw/draw_vertex.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 "util/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  * Information about hardware/rasterization vertex layout.
66  */
67 struct vertex_info
68 {
69    unsigned num_attribs;
70    unsigned size;      /**< total vertex size in dwords */
71 
72    /* Keep this small and at the end of the struct to allow quick
73     * memcmp() comparisons.
74     */
75    struct {
76       unsigned emit:8;             /**< EMIT_x */
77       unsigned src_index:8;          /**< map to post-xform attribs */
78    } attrib[PIPE_MAX_SHADER_OUTPUTS];
79 };
80 
81 
82 static inline size_t
draw_vinfo_size(const struct vertex_info * a)83 draw_vinfo_size(const struct vertex_info *a)
84 {
85    return offsetof(const struct vertex_info, attrib[a->num_attribs]);
86 }
87 
88 
89 static inline int
draw_vinfo_compare(const struct vertex_info * a,const struct vertex_info * b)90 draw_vinfo_compare(const struct vertex_info *a,
91                    const struct vertex_info *b)
92 {
93    size_t sizea = draw_vinfo_size(a);
94    return memcmp(a, b, sizea);
95 }
96 
97 
98 static inline void
draw_vinfo_copy(struct vertex_info * dst,const struct vertex_info * src)99 draw_vinfo_copy(struct vertex_info *dst,
100                 const struct vertex_info *src)
101 {
102    size_t size = draw_vinfo_size(src);
103    memcpy(dst, src, size);
104 }
105 
106 
107 
108 /**
109  * Add another attribute to the given vertex_info object.
110  * \param src_index  indicates which post-transformed vertex attrib slot
111  *                   corresponds to this attribute.
112  * \return slot in which the attribute was added
113  */
114 static inline uint
draw_emit_vertex_attr(struct vertex_info * vinfo,enum attrib_emit emit,int src_index)115 draw_emit_vertex_attr(struct vertex_info *vinfo,
116                       enum attrib_emit emit,
117                       int src_index)
118 {
119    const unsigned n = vinfo->num_attribs;
120 
121    /* If the src_index is negative, meaning it hasn't been found
122     * we'll assign it all zeros later - set to DRAW_ATTR_NONEXIST */
123    if (src_index < 0) {
124       src_index = DRAW_ATTR_NONEXIST;
125    }
126 
127    assert(n < ARRAY_SIZE(vinfo->attrib));
128    vinfo->attrib[n].emit = emit;
129    vinfo->attrib[n].src_index = src_index;
130    vinfo->num_attribs++;
131    return n;
132 }
133 
134 
135 void
136 draw_compute_vertex_size(struct vertex_info *vinfo);
137 
138 
139 void
140 draw_dump_emitted_vertex(const struct vertex_info *vinfo,
141                          const uint8_t *data);
142 
143 
144 static inline enum pipe_format
draw_translate_vinfo_format(enum attrib_emit emit)145 draw_translate_vinfo_format(enum attrib_emit emit)
146 {
147    switch (emit) {
148    case EMIT_OMIT:
149       return PIPE_FORMAT_NONE;
150    case EMIT_1F:
151    case EMIT_1F_PSIZE:
152       return PIPE_FORMAT_R32_FLOAT;
153    case EMIT_2F:
154       return PIPE_FORMAT_R32G32_FLOAT;
155    case EMIT_3F:
156       return PIPE_FORMAT_R32G32B32_FLOAT;
157    case EMIT_4F:
158       return PIPE_FORMAT_R32G32B32A32_FLOAT;
159    case EMIT_4UB:
160       return PIPE_FORMAT_R8G8B8A8_UNORM;
161    case EMIT_4UB_BGRA:
162       return PIPE_FORMAT_B8G8R8A8_UNORM;
163    default:
164       assert(!"unexpected format");
165       return PIPE_FORMAT_NONE;
166    }
167 }
168 
169 
170 static inline unsigned
draw_translate_vinfo_size(enum attrib_emit emit)171 draw_translate_vinfo_size(enum attrib_emit emit)
172 {
173    switch (emit) {
174    case EMIT_OMIT:
175       return 0;
176    case EMIT_1F:
177    case EMIT_1F_PSIZE:
178       return 1 * sizeof(float);
179    case EMIT_2F:
180       return 2 * sizeof(float);
181    case EMIT_3F:
182       return 3 * sizeof(float);
183    case EMIT_4F:
184       return 4 * sizeof(float);
185    case EMIT_4UB:
186       return 4 * sizeof(unsigned char);
187    case EMIT_4UB_BGRA:
188       return 4 * sizeof(unsigned char);
189    default:
190       assert(!"unexpected format");
191       return 0;
192    }
193 }
194 
195 #endif /* DRAW_VERTEX_H */
196