• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   * Authors:
30   *   Keith Whitwell <keithw@vmware.com>
31   */
32 
33 #include "util/u_memory.h"
34 #include "util/u_format.h"
35 #include "util/u_half.h"
36 #include "util/u_math.h"
37 #include "pipe/p_state.h"
38 #include "translate.h"
39 
40 
41 #define DRAW_DBG 0
42 
43 typedef void (*fetch_func)(void *dst,
44                            const uint8_t *src,
45                            unsigned i, unsigned j);
46 typedef void (*emit_func)(const void *attrib, void *ptr);
47 
48 
49 
50 struct translate_generic {
51    struct translate translate;
52 
53    struct {
54       enum translate_element_type type;
55 
56       fetch_func fetch;
57       unsigned buffer;
58       unsigned input_offset;
59       unsigned instance_divisor;
60 
61       emit_func emit;
62       unsigned output_offset;
63 
64       const uint8_t *input_ptr;
65       unsigned input_stride;
66       unsigned max_index;
67 
68       /* this value is set to -1 if this is a normal element with output_format != input_format:
69        * in this case, u_format is used to do a full conversion
70        *
71        * this value is set to the format size in bytes if output_format == input_format or for 32-bit instance ids:
72        * in this case, memcpy is used to copy this amount of bytes
73        */
74       int copy_size;
75 
76    } attrib[TRANSLATE_MAX_ATTRIBS];
77 
78    unsigned nr_attrib;
79 };
80 
81 
translate_generic(struct translate * translate)82 static struct translate_generic *translate_generic( struct translate *translate )
83 {
84    return (struct translate_generic *)translate;
85 }
86 
87 /**
88  * Fetch a dword[4] vertex attribute from memory, doing format/type
89  * conversion as needed.
90  *
91  * This is probably needed/dupliocated elsewhere, eg format
92  * conversion, texture sampling etc.
93  */
94 #define ATTRIB( NAME, SZ, SRCTYPE, DSTTYPE, TO )        \
95 static void						\
96 emit_##NAME(const void *attrib, void *ptr)		\
97 {  \
98    unsigned i;						\
99    SRCTYPE *in = (SRCTYPE *)attrib;                     \
100    DSTTYPE *out = (DSTTYPE *)ptr;			\
101 							\
102    for (i = 0; i < SZ; i++) {				\
103       out[i] = TO(in[i]);				\
104    }							\
105 }
106 
107 
108 #define TO_64_FLOAT(x)   ((double) x)
109 #define TO_32_FLOAT(x)   (x)
110 #define TO_16_FLOAT(x)   util_float_to_half(x)
111 
112 #define TO_8_USCALED(x)  ((unsigned char) x)
113 #define TO_16_USCALED(x) ((unsigned short) x)
114 #define TO_32_USCALED(x) ((unsigned int) x)
115 
116 #define TO_8_SSCALED(x)  ((char) x)
117 #define TO_16_SSCALED(x) ((short) x)
118 #define TO_32_SSCALED(x) ((int) x)
119 
120 #define TO_8_UNORM(x)    ((unsigned char) (x * 255.0f))
121 #define TO_16_UNORM(x)   ((unsigned short) (x * 65535.0f))
122 #define TO_32_UNORM(x)   ((unsigned int) (x * 4294967295.0f))
123 
124 #define TO_8_SNORM(x)    ((char) (x * 127.0f))
125 #define TO_16_SNORM(x)   ((short) (x * 32767.0f))
126 #define TO_32_SNORM(x)   ((int) (x * 2147483647.0f))
127 
128 #define TO_32_FIXED(x)   ((int) (x * 65536.0f))
129 
130 #define TO_INT(x)        (x)
131 
132 
133 ATTRIB( R64G64B64A64_FLOAT,   4, float, double, TO_64_FLOAT )
134 ATTRIB( R64G64B64_FLOAT,      3, float, double, TO_64_FLOAT )
135 ATTRIB( R64G64_FLOAT,         2, float, double, TO_64_FLOAT )
136 ATTRIB( R64_FLOAT,            1, float, double, TO_64_FLOAT )
137 
138 ATTRIB( R32G32B32A32_FLOAT,   4, float, float, TO_32_FLOAT )
139 ATTRIB( R32G32B32_FLOAT,      3, float, float, TO_32_FLOAT )
140 ATTRIB( R32G32_FLOAT,         2, float, float, TO_32_FLOAT )
141 ATTRIB( R32_FLOAT,            1, float, float, TO_32_FLOAT )
142 
143 ATTRIB( R16G16B16A16_FLOAT,   4, float, ushort, TO_16_FLOAT )
144 ATTRIB( R16G16B16_FLOAT,      3, float, ushort, TO_16_FLOAT )
145 ATTRIB( R16G16_FLOAT,         2, float, ushort, TO_16_FLOAT )
146 ATTRIB( R16_FLOAT,            1, float, ushort, TO_16_FLOAT )
147 
148 ATTRIB( R32G32B32A32_USCALED, 4, float, unsigned, TO_32_USCALED )
149 ATTRIB( R32G32B32_USCALED,    3, float, unsigned, TO_32_USCALED )
150 ATTRIB( R32G32_USCALED,       2, float, unsigned, TO_32_USCALED )
151 ATTRIB( R32_USCALED,          1, float, unsigned, TO_32_USCALED )
152 
153 ATTRIB( R32G32B32A32_SSCALED, 4, float, int, TO_32_SSCALED )
154 ATTRIB( R32G32B32_SSCALED,    3, float, int, TO_32_SSCALED )
155 ATTRIB( R32G32_SSCALED,       2, float, int, TO_32_SSCALED )
156 ATTRIB( R32_SSCALED,          1, float, int, TO_32_SSCALED )
157 
158 ATTRIB( R32G32B32A32_UNORM, 4, float, unsigned, TO_32_UNORM )
159 ATTRIB( R32G32B32_UNORM,    3, float, unsigned, TO_32_UNORM )
160 ATTRIB( R32G32_UNORM,       2, float, unsigned, TO_32_UNORM )
161 ATTRIB( R32_UNORM,          1, float, unsigned, TO_32_UNORM )
162 
163 ATTRIB( R32G32B32A32_SNORM, 4, float, int, TO_32_SNORM )
164 ATTRIB( R32G32B32_SNORM,    3, float, int, TO_32_SNORM )
165 ATTRIB( R32G32_SNORM,       2, float, int, TO_32_SNORM )
166 ATTRIB( R32_SNORM,          1, float, int, TO_32_SNORM )
167 
168 ATTRIB( R16G16B16A16_USCALED, 4, float, ushort, TO_16_USCALED )
169 ATTRIB( R16G16B16_USCALED,    3, float, ushort, TO_16_USCALED )
170 ATTRIB( R16G16_USCALED,       2, float, ushort, TO_16_USCALED )
171 ATTRIB( R16_USCALED,          1, float, ushort, TO_16_USCALED )
172 
173 ATTRIB( R16G16B16A16_SSCALED, 4, float, short, TO_16_SSCALED )
174 ATTRIB( R16G16B16_SSCALED,    3, float, short, TO_16_SSCALED )
175 ATTRIB( R16G16_SSCALED,       2, float, short, TO_16_SSCALED )
176 ATTRIB( R16_SSCALED,          1, float, short, TO_16_SSCALED )
177 
178 ATTRIB( R16G16B16A16_UNORM, 4, float, ushort, TO_16_UNORM )
179 ATTRIB( R16G16B16_UNORM,    3, float, ushort, TO_16_UNORM )
180 ATTRIB( R16G16_UNORM,       2, float, ushort, TO_16_UNORM )
181 ATTRIB( R16_UNORM,          1, float, ushort, TO_16_UNORM )
182 
183 ATTRIB( R16G16B16A16_SNORM, 4, float, short, TO_16_SNORM )
184 ATTRIB( R16G16B16_SNORM,    3, float, short, TO_16_SNORM )
185 ATTRIB( R16G16_SNORM,       2, float, short, TO_16_SNORM )
186 ATTRIB( R16_SNORM,          1, float, short, TO_16_SNORM )
187 
188 ATTRIB( R8G8B8A8_USCALED,   4, float, ubyte, TO_8_USCALED )
189 ATTRIB( R8G8B8_USCALED,     3, float, ubyte, TO_8_USCALED )
190 ATTRIB( R8G8_USCALED,       2, float, ubyte, TO_8_USCALED )
191 ATTRIB( R8_USCALED,         1, float, ubyte, TO_8_USCALED )
192 
193 ATTRIB( R8G8B8A8_SSCALED,  4, float, char, TO_8_SSCALED )
194 ATTRIB( R8G8B8_SSCALED,    3, float, char, TO_8_SSCALED )
195 ATTRIB( R8G8_SSCALED,      2, float, char, TO_8_SSCALED )
196 ATTRIB( R8_SSCALED,        1, float, char, TO_8_SSCALED )
197 
198 ATTRIB( R8G8B8A8_UNORM,  4, float, ubyte, TO_8_UNORM )
199 ATTRIB( R8G8B8_UNORM,    3, float, ubyte, TO_8_UNORM )
200 ATTRIB( R8G8_UNORM,      2, float, ubyte, TO_8_UNORM )
201 ATTRIB( R8_UNORM,        1, float, ubyte, TO_8_UNORM )
202 
203 ATTRIB( R8G8B8A8_SNORM,  4, float, char, TO_8_SNORM )
204 ATTRIB( R8G8B8_SNORM,    3, float, char, TO_8_SNORM )
205 ATTRIB( R8G8_SNORM,      2, float, char, TO_8_SNORM )
206 ATTRIB( R8_SNORM,        1, float, char, TO_8_SNORM )
207 
208 ATTRIB( R32G32B32A32_UINT, 4, uint32_t, unsigned, TO_INT )
209 ATTRIB( R32G32B32_UINT,    3, uint32_t, unsigned, TO_INT )
210 ATTRIB( R32G32_UINT,       2, uint32_t, unsigned, TO_INT )
211 ATTRIB( R32_UINT,          1, uint32_t, unsigned, TO_INT )
212 
213 ATTRIB( R16G16B16A16_UINT, 4, uint32_t, ushort, TO_INT )
214 ATTRIB( R16G16B16_UINT,    3, uint32_t, ushort, TO_INT )
215 ATTRIB( R16G16_UINT,       2, uint32_t, ushort, TO_INT )
216 ATTRIB( R16_UINT,          1, uint32_t, ushort, TO_INT )
217 
218 ATTRIB( R8G8B8A8_UINT,   4, uint32_t, ubyte, TO_INT )
219 ATTRIB( R8G8B8_UINT,     3, uint32_t, ubyte, TO_INT )
220 ATTRIB( R8G8_UINT,       2, uint32_t, ubyte, TO_INT )
221 ATTRIB( R8_UINT,         1, uint32_t, ubyte, TO_INT )
222 
223 ATTRIB( R32G32B32A32_SINT, 4, int32_t, int, TO_INT )
224 ATTRIB( R32G32B32_SINT,    3, int32_t, int, TO_INT )
225 ATTRIB( R32G32_SINT,       2, int32_t, int, TO_INT )
226 ATTRIB( R32_SINT,          1, int32_t, int, TO_INT )
227 
228 ATTRIB( R16G16B16A16_SINT, 4, int32_t, short, TO_INT )
229 ATTRIB( R16G16B16_SINT,    3, int32_t, short, TO_INT )
230 ATTRIB( R16G16_SINT,       2, int32_t, short, TO_INT )
231 ATTRIB( R16_SINT,          1, int32_t, short, TO_INT )
232 
233 ATTRIB( R8G8B8A8_SINT,   4, int32_t, char, TO_INT )
234 ATTRIB( R8G8B8_SINT,     3, int32_t, char, TO_INT )
235 ATTRIB( R8G8_SINT,       2, int32_t, char, TO_INT )
236 ATTRIB( R8_SINT,         1, int32_t, char, TO_INT )
237 
238 static void
emit_A8R8G8B8_UNORM(const void * attrib,void * ptr)239 emit_A8R8G8B8_UNORM( const void *attrib, void *ptr)
240 {
241    float *in = (float *)attrib;
242    ubyte *out = (ubyte *)ptr;
243    out[0] = TO_8_UNORM(in[3]);
244    out[1] = TO_8_UNORM(in[0]);
245    out[2] = TO_8_UNORM(in[1]);
246    out[3] = TO_8_UNORM(in[2]);
247 }
248 
249 static void
emit_B8G8R8A8_UNORM(const void * attrib,void * ptr)250 emit_B8G8R8A8_UNORM( const void *attrib, void *ptr)
251 {
252    float *in = (float *)attrib;
253    ubyte *out = (ubyte *)ptr;
254    out[2] = TO_8_UNORM(in[0]);
255    out[1] = TO_8_UNORM(in[1]);
256    out[0] = TO_8_UNORM(in[2]);
257    out[3] = TO_8_UNORM(in[3]);
258 }
259 
260 static void
emit_B10G10R10A2_UNORM(const void * attrib,void * ptr)261 emit_B10G10R10A2_UNORM( const void *attrib, void *ptr )
262 {
263    float *src = (float *)ptr;
264    uint32_t value = 0;
265    value |= ((uint32_t)(CLAMP(src[2], 0, 1) * 0x3ff)) & 0x3ff;
266    value |= (((uint32_t)(CLAMP(src[1], 0, 1) * 0x3ff)) & 0x3ff) << 10;
267    value |= (((uint32_t)(CLAMP(src[0], 0, 1) * 0x3ff)) & 0x3ff) << 20;
268    value |= ((uint32_t)(CLAMP(src[3], 0, 1) * 0x3)) << 30;
269    *(uint32_t *)attrib = util_le32_to_cpu(value);
270 }
271 
272 static void
emit_B10G10R10A2_USCALED(const void * attrib,void * ptr)273 emit_B10G10R10A2_USCALED( const void *attrib, void *ptr )
274 {
275    float *src = (float *)ptr;
276    uint32_t value = 0;
277    value |= ((uint32_t)CLAMP(src[2], 0, 1023)) & 0x3ff;
278    value |= (((uint32_t)CLAMP(src[1], 0, 1023)) & 0x3ff) << 10;
279    value |= (((uint32_t)CLAMP(src[0], 0, 1023)) & 0x3ff) << 20;
280    value |= ((uint32_t)CLAMP(src[3], 0, 3)) << 30;
281    *(uint32_t *)attrib = util_le32_to_cpu(value);
282 }
283 
284 static void
emit_B10G10R10A2_SNORM(const void * attrib,void * ptr)285 emit_B10G10R10A2_SNORM( const void *attrib, void *ptr )
286 {
287    float *src = (float *)ptr;
288    uint32_t value = 0;
289    value |= (uint32_t)(((uint32_t)(CLAMP(src[2], -1, 1) * 0x1ff)) & 0x3ff) ;
290    value |= (uint32_t)((((uint32_t)(CLAMP(src[1], -1, 1) * 0x1ff)) & 0x3ff) << 10) ;
291    value |= (uint32_t)((((uint32_t)(CLAMP(src[0], -1, 1) * 0x1ff)) & 0x3ff) << 20) ;
292    value |= (uint32_t)(((uint32_t)(CLAMP(src[3], -1, 1) * 0x1)) << 30) ;
293    *(uint32_t *)attrib = util_le32_to_cpu(value);
294 }
295 
296 static void
emit_B10G10R10A2_SSCALED(const void * attrib,void * ptr)297 emit_B10G10R10A2_SSCALED( const void *attrib, void *ptr )
298 {
299    float *src = (float *)ptr;
300    uint32_t value = 0;
301    value |= (uint32_t)(((uint32_t)CLAMP(src[2], -512, 511)) & 0x3ff) ;
302    value |= (uint32_t)((((uint32_t)CLAMP(src[1], -512, 511)) & 0x3ff) << 10) ;
303    value |= (uint32_t)((((uint32_t)CLAMP(src[0], -512, 511)) & 0x3ff) << 20) ;
304    value |= (uint32_t)(((uint32_t)CLAMP(src[3], -2, 1)) << 30) ;
305    *(uint32_t *)attrib = util_le32_to_cpu(value);
306 }
307 
308 static void
emit_R10G10B10A2_UNORM(const void * attrib,void * ptr)309 emit_R10G10B10A2_UNORM( const void *attrib, void *ptr )
310 {
311    float *src = (float *)ptr;
312    uint32_t value = 0;
313    value |= ((uint32_t)(CLAMP(src[0], 0, 1) * 0x3ff)) & 0x3ff;
314    value |= (((uint32_t)(CLAMP(src[1], 0, 1) * 0x3ff)) & 0x3ff) << 10;
315    value |= (((uint32_t)(CLAMP(src[2], 0, 1) * 0x3ff)) & 0x3ff) << 20;
316    value |= ((uint32_t)(CLAMP(src[3], 0, 1) * 0x3)) << 30;
317    *(uint32_t *)attrib = util_le32_to_cpu(value);
318 }
319 
320 static void
emit_R10G10B10A2_USCALED(const void * attrib,void * ptr)321 emit_R10G10B10A2_USCALED( const void *attrib, void *ptr )
322 {
323    float *src = (float *)ptr;
324    uint32_t value = 0;
325    value |= ((uint32_t)CLAMP(src[0], 0, 1023)) & 0x3ff;
326    value |= (((uint32_t)CLAMP(src[1], 0, 1023)) & 0x3ff) << 10;
327    value |= (((uint32_t)CLAMP(src[2], 0, 1023)) & 0x3ff) << 20;
328    value |= ((uint32_t)CLAMP(src[3], 0, 3)) << 30;
329    *(uint32_t *)attrib = util_le32_to_cpu(value);
330 }
331 
332 static void
emit_R10G10B10A2_SNORM(const void * attrib,void * ptr)333 emit_R10G10B10A2_SNORM( const void *attrib, void *ptr )
334 {
335    float *src = (float *)ptr;
336    uint32_t value = 0;
337    value |= (uint32_t)(((uint32_t)(CLAMP(src[0], -1, 1) * 0x1ff)) & 0x3ff) ;
338    value |= (uint32_t)((((uint32_t)(CLAMP(src[1], -1, 1) * 0x1ff)) & 0x3ff) << 10) ;
339    value |= (uint32_t)((((uint32_t)(CLAMP(src[2], -1, 1) * 0x1ff)) & 0x3ff) << 20) ;
340    value |= (uint32_t)(((uint32_t)(CLAMP(src[3], -1, 1) * 0x1)) << 30) ;
341    *(uint32_t *)attrib = util_le32_to_cpu(value);
342 }
343 
344 static void
emit_R10G10B10A2_SSCALED(const void * attrib,void * ptr)345 emit_R10G10B10A2_SSCALED( const void *attrib, void *ptr)
346 {
347    float *src = (float *)ptr;
348    uint32_t value = 0;
349    value |= (uint32_t)(((uint32_t)CLAMP(src[0], -512, 511)) & 0x3ff) ;
350    value |= (uint32_t)((((uint32_t)CLAMP(src[1], -512, 511)) & 0x3ff) << 10) ;
351    value |= (uint32_t)((((uint32_t)CLAMP(src[2], -512, 511)) & 0x3ff) << 20) ;
352    value |= (uint32_t)(((uint32_t)CLAMP(src[3], -2, 1)) << 30) ;
353    *(uint32_t *)attrib = util_le32_to_cpu(value);
354 }
355 
356 static void
emit_NULL(const void * attrib,void * ptr)357 emit_NULL( const void *attrib, void *ptr )
358 {
359    /* do nothing is the only sensible option */
360 }
361 
get_emit_func(enum pipe_format format)362 static emit_func get_emit_func( enum pipe_format format )
363 {
364    switch (format) {
365    case PIPE_FORMAT_R64_FLOAT:
366       return &emit_R64_FLOAT;
367    case PIPE_FORMAT_R64G64_FLOAT:
368       return &emit_R64G64_FLOAT;
369    case PIPE_FORMAT_R64G64B64_FLOAT:
370       return &emit_R64G64B64_FLOAT;
371    case PIPE_FORMAT_R64G64B64A64_FLOAT:
372       return &emit_R64G64B64A64_FLOAT;
373 
374    case PIPE_FORMAT_R32_FLOAT:
375       return &emit_R32_FLOAT;
376    case PIPE_FORMAT_R32G32_FLOAT:
377       return &emit_R32G32_FLOAT;
378    case PIPE_FORMAT_R32G32B32_FLOAT:
379       return &emit_R32G32B32_FLOAT;
380    case PIPE_FORMAT_R32G32B32A32_FLOAT:
381       return &emit_R32G32B32A32_FLOAT;
382 
383    case PIPE_FORMAT_R16_FLOAT:
384       return &emit_R16_FLOAT;
385    case PIPE_FORMAT_R16G16_FLOAT:
386       return &emit_R16G16_FLOAT;
387    case PIPE_FORMAT_R16G16B16_FLOAT:
388       return &emit_R16G16B16_FLOAT;
389    case PIPE_FORMAT_R16G16B16A16_FLOAT:
390       return &emit_R16G16B16A16_FLOAT;
391 
392    case PIPE_FORMAT_R32_UNORM:
393       return &emit_R32_UNORM;
394    case PIPE_FORMAT_R32G32_UNORM:
395       return &emit_R32G32_UNORM;
396    case PIPE_FORMAT_R32G32B32_UNORM:
397       return &emit_R32G32B32_UNORM;
398    case PIPE_FORMAT_R32G32B32A32_UNORM:
399       return &emit_R32G32B32A32_UNORM;
400 
401    case PIPE_FORMAT_R32_USCALED:
402       return &emit_R32_USCALED;
403    case PIPE_FORMAT_R32G32_USCALED:
404       return &emit_R32G32_USCALED;
405    case PIPE_FORMAT_R32G32B32_USCALED:
406       return &emit_R32G32B32_USCALED;
407    case PIPE_FORMAT_R32G32B32A32_USCALED:
408       return &emit_R32G32B32A32_USCALED;
409 
410    case PIPE_FORMAT_R32_SNORM:
411       return &emit_R32_SNORM;
412    case PIPE_FORMAT_R32G32_SNORM:
413       return &emit_R32G32_SNORM;
414    case PIPE_FORMAT_R32G32B32_SNORM:
415       return &emit_R32G32B32_SNORM;
416    case PIPE_FORMAT_R32G32B32A32_SNORM:
417       return &emit_R32G32B32A32_SNORM;
418 
419    case PIPE_FORMAT_R32_SSCALED:
420       return &emit_R32_SSCALED;
421    case PIPE_FORMAT_R32G32_SSCALED:
422       return &emit_R32G32_SSCALED;
423    case PIPE_FORMAT_R32G32B32_SSCALED:
424       return &emit_R32G32B32_SSCALED;
425    case PIPE_FORMAT_R32G32B32A32_SSCALED:
426       return &emit_R32G32B32A32_SSCALED;
427 
428    case PIPE_FORMAT_R16_UNORM:
429       return &emit_R16_UNORM;
430    case PIPE_FORMAT_R16G16_UNORM:
431       return &emit_R16G16_UNORM;
432    case PIPE_FORMAT_R16G16B16_UNORM:
433       return &emit_R16G16B16_UNORM;
434    case PIPE_FORMAT_R16G16B16A16_UNORM:
435       return &emit_R16G16B16A16_UNORM;
436 
437    case PIPE_FORMAT_R16_USCALED:
438       return &emit_R16_USCALED;
439    case PIPE_FORMAT_R16G16_USCALED:
440       return &emit_R16G16_USCALED;
441    case PIPE_FORMAT_R16G16B16_USCALED:
442       return &emit_R16G16B16_USCALED;
443    case PIPE_FORMAT_R16G16B16A16_USCALED:
444       return &emit_R16G16B16A16_USCALED;
445 
446    case PIPE_FORMAT_R16_SNORM:
447       return &emit_R16_SNORM;
448    case PIPE_FORMAT_R16G16_SNORM:
449       return &emit_R16G16_SNORM;
450    case PIPE_FORMAT_R16G16B16_SNORM:
451       return &emit_R16G16B16_SNORM;
452    case PIPE_FORMAT_R16G16B16A16_SNORM:
453       return &emit_R16G16B16A16_SNORM;
454 
455    case PIPE_FORMAT_R16_SSCALED:
456       return &emit_R16_SSCALED;
457    case PIPE_FORMAT_R16G16_SSCALED:
458       return &emit_R16G16_SSCALED;
459    case PIPE_FORMAT_R16G16B16_SSCALED:
460       return &emit_R16G16B16_SSCALED;
461    case PIPE_FORMAT_R16G16B16A16_SSCALED:
462       return &emit_R16G16B16A16_SSCALED;
463 
464    case PIPE_FORMAT_R8_UNORM:
465       return &emit_R8_UNORM;
466    case PIPE_FORMAT_R8G8_UNORM:
467       return &emit_R8G8_UNORM;
468    case PIPE_FORMAT_R8G8B8_UNORM:
469       return &emit_R8G8B8_UNORM;
470    case PIPE_FORMAT_R8G8B8A8_UNORM:
471       return &emit_R8G8B8A8_UNORM;
472 
473    case PIPE_FORMAT_R8_USCALED:
474       return &emit_R8_USCALED;
475    case PIPE_FORMAT_R8G8_USCALED:
476       return &emit_R8G8_USCALED;
477    case PIPE_FORMAT_R8G8B8_USCALED:
478       return &emit_R8G8B8_USCALED;
479    case PIPE_FORMAT_R8G8B8A8_USCALED:
480       return &emit_R8G8B8A8_USCALED;
481 
482    case PIPE_FORMAT_R8_SNORM:
483       return &emit_R8_SNORM;
484    case PIPE_FORMAT_R8G8_SNORM:
485       return &emit_R8G8_SNORM;
486    case PIPE_FORMAT_R8G8B8_SNORM:
487       return &emit_R8G8B8_SNORM;
488    case PIPE_FORMAT_R8G8B8A8_SNORM:
489       return &emit_R8G8B8A8_SNORM;
490 
491    case PIPE_FORMAT_R8_SSCALED:
492       return &emit_R8_SSCALED;
493    case PIPE_FORMAT_R8G8_SSCALED:
494       return &emit_R8G8_SSCALED;
495    case PIPE_FORMAT_R8G8B8_SSCALED:
496       return &emit_R8G8B8_SSCALED;
497    case PIPE_FORMAT_R8G8B8A8_SSCALED:
498       return &emit_R8G8B8A8_SSCALED;
499 
500    case PIPE_FORMAT_B8G8R8A8_UNORM:
501       return &emit_B8G8R8A8_UNORM;
502 
503    case PIPE_FORMAT_A8R8G8B8_UNORM:
504       return &emit_A8R8G8B8_UNORM;
505 
506    case PIPE_FORMAT_R32_UINT:
507       return &emit_R32_UINT;
508    case PIPE_FORMAT_R32G32_UINT:
509       return &emit_R32G32_UINT;
510    case PIPE_FORMAT_R32G32B32_UINT:
511       return &emit_R32G32B32_UINT;
512    case PIPE_FORMAT_R32G32B32A32_UINT:
513       return &emit_R32G32B32A32_UINT;
514 
515    case PIPE_FORMAT_R16_UINT:
516       return &emit_R16_UINT;
517    case PIPE_FORMAT_R16G16_UINT:
518       return &emit_R16G16_UINT;
519    case PIPE_FORMAT_R16G16B16_UINT:
520       return &emit_R16G16B16_UINT;
521    case PIPE_FORMAT_R16G16B16A16_UINT:
522       return &emit_R16G16B16A16_UINT;
523 
524    case PIPE_FORMAT_R8_UINT:
525       return &emit_R8_UINT;
526    case PIPE_FORMAT_R8G8_UINT:
527       return &emit_R8G8_UINT;
528    case PIPE_FORMAT_R8G8B8_UINT:
529       return &emit_R8G8B8_UINT;
530    case PIPE_FORMAT_R8G8B8A8_UINT:
531       return &emit_R8G8B8A8_UINT;
532 
533    case PIPE_FORMAT_R32_SINT:
534       return &emit_R32_SINT;
535    case PIPE_FORMAT_R32G32_SINT:
536       return &emit_R32G32_SINT;
537    case PIPE_FORMAT_R32G32B32_SINT:
538       return &emit_R32G32B32_SINT;
539    case PIPE_FORMAT_R32G32B32A32_SINT:
540       return &emit_R32G32B32A32_SINT;
541 
542    case PIPE_FORMAT_R16_SINT:
543       return &emit_R16_SINT;
544    case PIPE_FORMAT_R16G16_SINT:
545       return &emit_R16G16_SINT;
546    case PIPE_FORMAT_R16G16B16_SINT:
547       return &emit_R16G16B16_SINT;
548    case PIPE_FORMAT_R16G16B16A16_SINT:
549       return &emit_R16G16B16A16_SINT;
550 
551    case PIPE_FORMAT_R8_SINT:
552       return &emit_R8_SINT;
553    case PIPE_FORMAT_R8G8_SINT:
554       return &emit_R8G8_SINT;
555    case PIPE_FORMAT_R8G8B8_SINT:
556       return &emit_R8G8B8_SINT;
557    case PIPE_FORMAT_R8G8B8A8_SINT:
558       return &emit_R8G8B8A8_SINT;
559 
560    case PIPE_FORMAT_B10G10R10A2_UNORM:
561       return &emit_B10G10R10A2_UNORM;
562    case PIPE_FORMAT_B10G10R10A2_USCALED:
563       return &emit_B10G10R10A2_USCALED;
564    case PIPE_FORMAT_B10G10R10A2_SNORM:
565       return &emit_B10G10R10A2_SNORM;
566    case PIPE_FORMAT_B10G10R10A2_SSCALED:
567       return &emit_B10G10R10A2_SSCALED;
568 
569    case PIPE_FORMAT_R10G10B10A2_UNORM:
570       return &emit_R10G10B10A2_UNORM;
571    case PIPE_FORMAT_R10G10B10A2_USCALED:
572       return &emit_R10G10B10A2_USCALED;
573    case PIPE_FORMAT_R10G10B10A2_SNORM:
574       return &emit_R10G10B10A2_SNORM;
575    case PIPE_FORMAT_R10G10B10A2_SSCALED:
576       return &emit_R10G10B10A2_SSCALED;
577 
578    default:
579       assert(0);
580       return &emit_NULL;
581    }
582 }
583 
generic_run_one(struct translate_generic * tg,unsigned elt,unsigned start_instance,unsigned instance_id,void * vert)584 static ALWAYS_INLINE void PIPE_CDECL generic_run_one( struct translate_generic *tg,
585                                          unsigned elt,
586                                          unsigned start_instance,
587                                          unsigned instance_id,
588                                          void *vert )
589 {
590    unsigned nr_attrs = tg->nr_attrib;
591    unsigned attr;
592 
593    for (attr = 0; attr < nr_attrs; attr++) {
594       float data[4];
595       uint8_t *dst = (uint8_t *)vert + tg->attrib[attr].output_offset;
596 
597       if (tg->attrib[attr].type == TRANSLATE_ELEMENT_NORMAL) {
598          const uint8_t *src;
599          unsigned index;
600          int copy_size;
601 
602          if (tg->attrib[attr].instance_divisor) {
603             index = start_instance;
604             index += (instance_id  / tg->attrib[attr].instance_divisor);
605             /* XXX we need to clamp the index here too, but to a
606              * per-array max value, not the draw->pt.max_index value
607              * that's being given to us via translate->set_buffer().
608              */
609          }
610          else {
611             index = elt;
612             /* clamp to avoid going out of bounds */
613             index = MIN2(index, tg->attrib[attr].max_index);
614          }
615 
616          src = tg->attrib[attr].input_ptr +
617                (ptrdiff_t)tg->attrib[attr].input_stride * index;
618 
619          copy_size = tg->attrib[attr].copy_size;
620          if(likely(copy_size >= 0))
621             memcpy(dst, src, copy_size);
622          else
623          {
624             tg->attrib[attr].fetch( data, src, 0, 0 );
625 
626             if (0)
627                debug_printf("Fetch linear attr %d  from %p  stride %d  index %d: "
628                          " %f, %f, %f, %f \n",
629                          attr,
630                          tg->attrib[attr].input_ptr,
631                          tg->attrib[attr].input_stride,
632                          index,
633                          data[0], data[1],data[2], data[3]);
634 
635             tg->attrib[attr].emit( data, dst );
636          }
637       } else {
638          if(likely(tg->attrib[attr].copy_size >= 0))
639             memcpy(data, &instance_id, 4);
640          else
641          {
642             data[0] = (float)instance_id;
643             tg->attrib[attr].emit( data, dst );
644          }
645       }
646    }
647 }
648 
649 /**
650  * Fetch vertex attributes for 'count' vertices.
651  */
generic_run_elts(struct translate * translate,const unsigned * elts,unsigned count,unsigned start_instance,unsigned instance_id,void * output_buffer)652 static void PIPE_CDECL generic_run_elts( struct translate *translate,
653                                          const unsigned *elts,
654                                          unsigned count,
655                                          unsigned start_instance,
656                                          unsigned instance_id,
657                                          void *output_buffer )
658 {
659    struct translate_generic *tg = translate_generic(translate);
660    char *vert = output_buffer;
661    unsigned i;
662 
663    for (i = 0; i < count; i++) {
664       generic_run_one(tg, *elts++, start_instance, instance_id, vert);
665       vert += tg->translate.key.output_stride;
666    }
667 }
668 
generic_run_elts16(struct translate * translate,const uint16_t * elts,unsigned count,unsigned start_instance,unsigned instance_id,void * output_buffer)669 static void PIPE_CDECL generic_run_elts16( struct translate *translate,
670                                          const uint16_t *elts,
671                                          unsigned count,
672                                          unsigned start_instance,
673                                          unsigned instance_id,
674                                          void *output_buffer )
675 {
676    struct translate_generic *tg = translate_generic(translate);
677    char *vert = output_buffer;
678    unsigned i;
679 
680    for (i = 0; i < count; i++) {
681       generic_run_one(tg, *elts++, start_instance, instance_id, vert);
682       vert += tg->translate.key.output_stride;
683    }
684 }
685 
generic_run_elts8(struct translate * translate,const uint8_t * elts,unsigned count,unsigned start_instance,unsigned instance_id,void * output_buffer)686 static void PIPE_CDECL generic_run_elts8( struct translate *translate,
687                                          const uint8_t *elts,
688                                          unsigned count,
689                                          unsigned start_instance,
690                                          unsigned instance_id,
691                                          void *output_buffer )
692 {
693    struct translate_generic *tg = translate_generic(translate);
694    char *vert = output_buffer;
695    unsigned i;
696 
697    for (i = 0; i < count; i++) {
698       generic_run_one(tg, *elts++, start_instance, instance_id, vert);
699       vert += tg->translate.key.output_stride;
700    }
701 }
702 
generic_run(struct translate * translate,unsigned start,unsigned count,unsigned start_instance,unsigned instance_id,void * output_buffer)703 static void PIPE_CDECL generic_run( struct translate *translate,
704                                     unsigned start,
705                                     unsigned count,
706                                     unsigned start_instance,
707                                     unsigned instance_id,
708                                     void *output_buffer )
709 {
710    struct translate_generic *tg = translate_generic(translate);
711    char *vert = output_buffer;
712    unsigned i;
713 
714    for (i = 0; i < count; i++) {
715       generic_run_one(tg, start + i, start_instance, instance_id, vert);
716       vert += tg->translate.key.output_stride;
717    }
718 }
719 
720 
721 
generic_set_buffer(struct translate * translate,unsigned buf,const void * ptr,unsigned stride,unsigned max_index)722 static void generic_set_buffer( struct translate *translate,
723 				unsigned buf,
724 				const void *ptr,
725 				unsigned stride,
726 				unsigned max_index )
727 {
728    struct translate_generic *tg = translate_generic(translate);
729    unsigned i;
730 
731    for (i = 0; i < tg->nr_attrib; i++) {
732       if (tg->attrib[i].buffer == buf) {
733 	 tg->attrib[i].input_ptr = ((const uint8_t *)ptr +
734 				    tg->attrib[i].input_offset);
735 	 tg->attrib[i].input_stride = stride;
736          tg->attrib[i].max_index = max_index;
737       }
738    }
739 }
740 
741 
generic_release(struct translate * translate)742 static void generic_release( struct translate *translate )
743 {
744    /* Refcount?
745     */
746    FREE(translate);
747 }
748 
749 static boolean
is_legal_int_format_combo(const struct util_format_description * src,const struct util_format_description * dst)750 is_legal_int_format_combo( const struct util_format_description *src,
751                            const struct util_format_description *dst )
752 {
753    unsigned i;
754    unsigned nr = MIN2(src->nr_channels, dst->nr_channels);
755 
756    for (i = 0; i < nr; i++) {
757       /* The signs must match. */
758       if (src->channel[i].type != dst->channel[i].type) {
759          return FALSE;
760       }
761 
762       /* Integers must not lose precision at any point in the pipeline. */
763       if (src->channel[i].size > dst->channel[i].size) {
764          return FALSE;
765       }
766    }
767    return TRUE;
768 }
769 
translate_generic_create(const struct translate_key * key)770 struct translate *translate_generic_create( const struct translate_key *key )
771 {
772    struct translate_generic *tg = CALLOC_STRUCT(translate_generic);
773    unsigned i;
774 
775    if (!tg)
776       return NULL;
777 
778    assert(key->nr_elements <= TRANSLATE_MAX_ATTRIBS);
779 
780    tg->translate.key = *key;
781    tg->translate.release = generic_release;
782    tg->translate.set_buffer = generic_set_buffer;
783    tg->translate.run_elts = generic_run_elts;
784    tg->translate.run_elts16 = generic_run_elts16;
785    tg->translate.run_elts8 = generic_run_elts8;
786    tg->translate.run = generic_run;
787 
788    for (i = 0; i < key->nr_elements; i++) {
789       const struct util_format_description *format_desc =
790             util_format_description(key->element[i].input_format);
791 
792       assert(format_desc);
793 
794       tg->attrib[i].type = key->element[i].type;
795 
796       if (format_desc->channel[0].pure_integer) {
797          const struct util_format_description *out_format_desc =
798                util_format_description(key->element[i].output_format);
799 
800          if (!is_legal_int_format_combo(format_desc, out_format_desc)) {
801             FREE(tg);
802             return NULL;
803          }
804 
805          if (format_desc->channel[0].type == UTIL_FORMAT_TYPE_SIGNED) {
806             assert(format_desc->fetch_rgba_sint);
807             tg->attrib[i].fetch = (fetch_func)format_desc->fetch_rgba_sint;
808          } else {
809             assert(format_desc->fetch_rgba_uint);
810             tg->attrib[i].fetch = (fetch_func)format_desc->fetch_rgba_uint;
811          }
812       } else {
813          assert(format_desc->fetch_rgba_float);
814          tg->attrib[i].fetch = (fetch_func)format_desc->fetch_rgba_float;
815       }
816 
817       tg->attrib[i].buffer = key->element[i].input_buffer;
818       tg->attrib[i].input_offset = key->element[i].input_offset;
819       tg->attrib[i].instance_divisor = key->element[i].instance_divisor;
820 
821       tg->attrib[i].output_offset = key->element[i].output_offset;
822 
823       tg->attrib[i].copy_size = -1;
824       if (tg->attrib[i].type == TRANSLATE_ELEMENT_INSTANCE_ID)
825       {
826             if(key->element[i].output_format == PIPE_FORMAT_R32_USCALED
827                   || key->element[i].output_format == PIPE_FORMAT_R32_SSCALED)
828                tg->attrib[i].copy_size = 4;
829       }
830       else
831       {
832          if(key->element[i].input_format == key->element[i].output_format
833                && format_desc->block.width == 1
834                && format_desc->block.height == 1
835                && !(format_desc->block.bits & 7))
836             tg->attrib[i].copy_size = format_desc->block.bits >> 3;
837       }
838 
839       if(tg->attrib[i].copy_size < 0)
840 	      tg->attrib[i].emit = get_emit_func(key->element[i].output_format);
841       else
842 	      tg->attrib[i].emit  = NULL;
843    }
844 
845    tg->nr_attrib = key->nr_elements;
846 
847 
848    return &tg->translate;
849 }
850 
translate_generic_is_output_format_supported(enum pipe_format format)851 boolean translate_generic_is_output_format_supported(enum pipe_format format)
852 {
853    switch(format)
854    {
855    case PIPE_FORMAT_R64G64B64A64_FLOAT: return TRUE;
856    case PIPE_FORMAT_R64G64B64_FLOAT: return TRUE;
857    case PIPE_FORMAT_R64G64_FLOAT: return TRUE;
858    case PIPE_FORMAT_R64_FLOAT: return TRUE;
859 
860    case PIPE_FORMAT_R32G32B32A32_FLOAT: return TRUE;
861    case PIPE_FORMAT_R32G32B32_FLOAT: return TRUE;
862    case PIPE_FORMAT_R32G32_FLOAT: return TRUE;
863    case PIPE_FORMAT_R32_FLOAT: return TRUE;
864 
865    case PIPE_FORMAT_R16G16B16A16_FLOAT: return TRUE;
866    case PIPE_FORMAT_R16G16B16_FLOAT: return TRUE;
867    case PIPE_FORMAT_R16G16_FLOAT: return TRUE;
868    case PIPE_FORMAT_R16_FLOAT: return TRUE;
869 
870    case PIPE_FORMAT_R32G32B32A32_USCALED: return TRUE;
871    case PIPE_FORMAT_R32G32B32_USCALED: return TRUE;
872    case PIPE_FORMAT_R32G32_USCALED: return TRUE;
873    case PIPE_FORMAT_R32_USCALED: return TRUE;
874 
875    case PIPE_FORMAT_R32G32B32A32_SSCALED: return TRUE;
876    case PIPE_FORMAT_R32G32B32_SSCALED: return TRUE;
877    case PIPE_FORMAT_R32G32_SSCALED: return TRUE;
878    case PIPE_FORMAT_R32_SSCALED: return TRUE;
879 
880    case PIPE_FORMAT_R32G32B32A32_UNORM: return TRUE;
881    case PIPE_FORMAT_R32G32B32_UNORM: return TRUE;
882    case PIPE_FORMAT_R32G32_UNORM: return TRUE;
883    case PIPE_FORMAT_R32_UNORM: return TRUE;
884 
885    case PIPE_FORMAT_R32G32B32A32_SNORM: return TRUE;
886    case PIPE_FORMAT_R32G32B32_SNORM: return TRUE;
887    case PIPE_FORMAT_R32G32_SNORM: return TRUE;
888    case PIPE_FORMAT_R32_SNORM: return TRUE;
889 
890    case PIPE_FORMAT_R16G16B16A16_USCALED: return TRUE;
891    case PIPE_FORMAT_R16G16B16_USCALED: return TRUE;
892    case PIPE_FORMAT_R16G16_USCALED: return TRUE;
893    case PIPE_FORMAT_R16_USCALED: return TRUE;
894 
895    case PIPE_FORMAT_R16G16B16A16_SSCALED: return TRUE;
896    case PIPE_FORMAT_R16G16B16_SSCALED: return TRUE;
897    case PIPE_FORMAT_R16G16_SSCALED: return TRUE;
898    case PIPE_FORMAT_R16_SSCALED: return TRUE;
899 
900    case PIPE_FORMAT_R16G16B16A16_UNORM: return TRUE;
901    case PIPE_FORMAT_R16G16B16_UNORM: return TRUE;
902    case PIPE_FORMAT_R16G16_UNORM: return TRUE;
903    case PIPE_FORMAT_R16_UNORM: return TRUE;
904 
905    case PIPE_FORMAT_R16G16B16A16_SNORM: return TRUE;
906    case PIPE_FORMAT_R16G16B16_SNORM: return TRUE;
907    case PIPE_FORMAT_R16G16_SNORM: return TRUE;
908    case PIPE_FORMAT_R16_SNORM: return TRUE;
909 
910    case PIPE_FORMAT_R8G8B8A8_USCALED: return TRUE;
911    case PIPE_FORMAT_R8G8B8_USCALED: return TRUE;
912    case PIPE_FORMAT_R8G8_USCALED: return TRUE;
913    case PIPE_FORMAT_R8_USCALED: return TRUE;
914 
915    case PIPE_FORMAT_R8G8B8A8_SSCALED: return TRUE;
916    case PIPE_FORMAT_R8G8B8_SSCALED: return TRUE;
917    case PIPE_FORMAT_R8G8_SSCALED: return TRUE;
918    case PIPE_FORMAT_R8_SSCALED: return TRUE;
919 
920    case PIPE_FORMAT_R8G8B8A8_UNORM: return TRUE;
921    case PIPE_FORMAT_R8G8B8_UNORM: return TRUE;
922    case PIPE_FORMAT_R8G8_UNORM: return TRUE;
923    case PIPE_FORMAT_R8_UNORM: return TRUE;
924 
925    case PIPE_FORMAT_R8G8B8A8_SNORM: return TRUE;
926    case PIPE_FORMAT_R8G8B8_SNORM: return TRUE;
927    case PIPE_FORMAT_R8G8_SNORM: return TRUE;
928    case PIPE_FORMAT_R8_SNORM: return TRUE;
929 
930    case PIPE_FORMAT_A8R8G8B8_UNORM: return TRUE;
931    case PIPE_FORMAT_B8G8R8A8_UNORM: return TRUE;
932 
933    case PIPE_FORMAT_R32G32B32A32_UINT: return TRUE;
934    case PIPE_FORMAT_R32G32B32_UINT: return TRUE;
935    case PIPE_FORMAT_R32G32_UINT: return TRUE;
936    case PIPE_FORMAT_R32_UINT: return TRUE;
937 
938    case PIPE_FORMAT_R16G16B16A16_UINT: return TRUE;
939    case PIPE_FORMAT_R16G16B16_UINT: return TRUE;
940    case PIPE_FORMAT_R16G16_UINT: return TRUE;
941    case PIPE_FORMAT_R16_UINT: return TRUE;
942 
943    case PIPE_FORMAT_R8G8B8A8_UINT: return TRUE;
944    case PIPE_FORMAT_R8G8B8_UINT: return TRUE;
945    case PIPE_FORMAT_R8G8_UINT: return TRUE;
946    case PIPE_FORMAT_R8_UINT: return TRUE;
947 
948    case PIPE_FORMAT_R32G32B32A32_SINT: return TRUE;
949    case PIPE_FORMAT_R32G32B32_SINT: return TRUE;
950    case PIPE_FORMAT_R32G32_SINT: return TRUE;
951    case PIPE_FORMAT_R32_SINT: return TRUE;
952 
953    case PIPE_FORMAT_R16G16B16A16_SINT: return TRUE;
954    case PIPE_FORMAT_R16G16B16_SINT: return TRUE;
955    case PIPE_FORMAT_R16G16_SINT: return TRUE;
956    case PIPE_FORMAT_R16_SINT: return TRUE;
957 
958    case PIPE_FORMAT_R8G8B8A8_SINT: return TRUE;
959    case PIPE_FORMAT_R8G8B8_SINT: return TRUE;
960    case PIPE_FORMAT_R8G8_SINT: return TRUE;
961    case PIPE_FORMAT_R8_SINT: return TRUE;
962 
963    case PIPE_FORMAT_B10G10R10A2_UNORM: return TRUE;
964    case PIPE_FORMAT_B10G10R10A2_USCALED: return TRUE;
965    case PIPE_FORMAT_B10G10R10A2_SNORM: return TRUE;
966    case PIPE_FORMAT_B10G10R10A2_SSCALED: return TRUE;
967 
968    case PIPE_FORMAT_R10G10B10A2_UNORM: return TRUE;
969    case PIPE_FORMAT_R10G10B10A2_USCALED: return TRUE;
970    case PIPE_FORMAT_R10G10B10A2_SNORM: return TRUE;
971    case PIPE_FORMAT_R10G10B10A2_SSCALED: return TRUE;
972 
973    default: return FALSE;
974    }
975 }
976