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