Merge remote branch 'main/radeon-rewrite'
[mesa.git] / src / mesa / drivers / dri / r300 / r300_draw.c
1 /**************************************************************************
2 *
3 * Copyright 2009 Maciej Cencora
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL THE AUTHOR(S) AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 **************************************************************************/
26
27 #include <stdlib.h>
28
29 #include "main/glheader.h"
30 #include "main/context.h"
31 #include "main/state.h"
32 #include "main/api_validate.h"
33 #include "main/enums.h"
34
35 #include "r300_reg.h"
36 #include "r300_context.h"
37 #include "r300_emit.h"
38 #include "r300_render.h"
39 #include "r300_state.h"
40 #include "r300_tex.h"
41
42 #include "tnl/tnl.h"
43 #include "tnl/t_vp_build.h"
44 #include "vbo/vbo_context.h"
45 #include "swrast/swrast.h"
46 #include "swrast_setup/swrast_setup.h"
47
48 static void r300FixupIndexBuffer(GLcontext *ctx, const struct _mesa_index_buffer *mesa_ind_buf, struct gl_buffer_object **bo, GLuint *nr_bo)
49 {
50 r300ContextPtr r300 = R300_CONTEXT(ctx);
51 struct r300_index_buffer *ind_buf = &r300->ind_buf;
52 GLvoid *src_ptr;
53
54 if (!mesa_ind_buf) {
55 ind_buf->ptr = NULL;
56 return;
57 }
58
59 ind_buf->count = mesa_ind_buf->count;
60 if (mesa_ind_buf->obj->Name && !mesa_ind_buf->obj->Pointer) {
61 bo[*nr_bo] = mesa_ind_buf->obj;
62 (*nr_bo)++;
63 ctx->Driver.MapBuffer(ctx, GL_ELEMENT_ARRAY_BUFFER, GL_READ_ONLY_ARB, mesa_ind_buf->obj);
64 assert(mesa_ind_buf->obj->Pointer != NULL);
65 }
66 src_ptr = ADD_POINTERS(mesa_ind_buf->obj->Pointer, mesa_ind_buf->ptr);
67
68 if (mesa_ind_buf->type == GL_UNSIGNED_BYTE) {
69 GLubyte *in = (GLubyte *)src_ptr;
70 GLuint *out = _mesa_malloc(sizeof(GLushort) * ((mesa_ind_buf->count + 1) & ~1));
71 int i;
72
73 ind_buf->ptr = out;
74
75 for (i = 0; i + 1 < mesa_ind_buf->count; i += 2) {
76 *out++ = in[i] | in[i + 1] << 16;
77 }
78
79 if (i < mesa_ind_buf->count) {
80 *out++ = in[i];
81 }
82
83 ind_buf->free_needed = GL_TRUE;
84 ind_buf->is_32bit = GL_FALSE;
85 } else if (mesa_ind_buf->type == GL_UNSIGNED_SHORT) {
86 #if MESA_BIG_ENDIAN
87 GLushort *in = (GLushort *)src_ptr;
88 GLuint *out = _mesa_malloc(sizeof(GLushort) *
89 ((mesa_ind_buf->count + 1) & ~1));
90 int i;
91
92 ind_buf->ptr = out;
93
94 for (i = 0; i + 1 < mesa_ind_buf->count; i += 2) {
95 *out++ = in[i] | in[i + 1] << 16;
96 }
97
98 if (i < mesa_ind_buf->count) {
99 *out++ = in[i];
100 }
101
102 ind_buf->free_needed = GL_TRUE;
103 #else
104 ind_buf->ptr = src_ptr;
105 ind_buf->free_needed = GL_FALSE;
106 #endif
107 ind_buf->is_32bit = GL_FALSE;
108 } else {
109 ind_buf->ptr = src_ptr;
110 ind_buf->free_needed = GL_FALSE;
111 ind_buf->is_32bit = GL_TRUE;
112 }
113 }
114
115 static int getTypeSize(GLenum type)
116 {
117 switch (type) {
118 case GL_DOUBLE:
119 return sizeof(GLdouble);
120 case GL_FLOAT:
121 return sizeof(GLfloat);
122 case GL_INT:
123 return sizeof(GLint);
124 case GL_UNSIGNED_INT:
125 return sizeof(GLuint);
126 case GL_SHORT:
127 return sizeof(GLshort);
128 case GL_UNSIGNED_SHORT:
129 return sizeof(GLushort);
130 case GL_BYTE:
131 return sizeof(GLbyte);
132 case GL_UNSIGNED_BYTE:
133 return sizeof(GLubyte);
134 default:
135 assert(0);
136 return 0;
137 }
138 }
139
140 #define CONVERT( TYPE, MACRO ) do { \
141 GLuint i, j, sz; \
142 sz = input->Size; \
143 if (input->Normalized) { \
144 for (i = 0; i < count; i++) { \
145 const TYPE *in = (TYPE *)src_ptr; \
146 for (j = 0; j < sz; j++) { \
147 *dst_ptr++ = MACRO(*in); \
148 in++; \
149 } \
150 src_ptr += stride; \
151 } \
152 } else { \
153 for (i = 0; i < count; i++) { \
154 const TYPE *in = (TYPE *)src_ptr; \
155 for (j = 0; j < sz; j++) { \
156 *dst_ptr++ = (GLfloat)(*in); \
157 in++; \
158 } \
159 src_ptr += stride; \
160 } \
161 } \
162 } while (0)
163
164 static void r300TranslateAttrib(GLcontext *ctx, GLuint attr, int count, const struct gl_client_array *input, struct gl_buffer_object **bo, GLuint *nr_bo)
165 {
166 r300ContextPtr r300 = R300_CONTEXT(ctx);
167 struct r300_vertex_buffer *vbuf = &r300->vbuf;
168 struct vertex_attribute r300_attr;
169 const void *src_ptr;
170 GLenum type;
171 GLuint stride;
172
173 if (input->BufferObj->Name) {
174 if (!input->BufferObj->Pointer) {
175 bo[*nr_bo] = input->BufferObj;
176 (*nr_bo)++;
177 ctx->Driver.MapBuffer(ctx, GL_ARRAY_BUFFER, GL_READ_ONLY_ARB, input->BufferObj);
178 assert(input->BufferObj->Pointer != NULL);
179 }
180
181 src_ptr = ADD_POINTERS(input->BufferObj->Pointer, input->Ptr);
182 } else
183 src_ptr = input->Ptr;
184
185 stride = (input->StrideB == 0) ? getTypeSize(input->Type) * input->Size : input->StrideB;
186
187 if (input->Type == GL_DOUBLE || input->Type == GL_UNSIGNED_INT || input->Type == GL_INT ||
188 #if MESA_BIG_ENDIAN
189 getTypeSize(input->Type) != 4 ||
190 #endif
191 stride < 4) {
192 if (RADEON_DEBUG & DEBUG_FALLBACKS) {
193 fprintf(stderr, "%s: Converting vertex attributes, attribute data format %x,", __FUNCTION__, input->Type);
194 fprintf(stderr, "stride %d, components %d\n", stride, input->Size);
195 }
196
197 GLfloat *dst_ptr, *tmp;
198 tmp = dst_ptr = _mesa_malloc(sizeof(GLfloat) * input->Size * count);
199
200 switch (input->Type) {
201 case GL_DOUBLE:
202 CONVERT(GLdouble, (GLfloat));
203 break;
204 case GL_UNSIGNED_INT:
205 CONVERT(GLuint, UINT_TO_FLOAT);
206 break;
207 case GL_INT:
208 CONVERT(GLint, INT_TO_FLOAT);
209 break;
210 case GL_UNSIGNED_SHORT:
211 CONVERT(GLushort, USHORT_TO_FLOAT);
212 break;
213 case GL_SHORT:
214 CONVERT(GLshort, SHORT_TO_FLOAT);
215 break;
216 case GL_UNSIGNED_BYTE:
217 assert(input->Format != GL_BGRA);
218 CONVERT(GLubyte, UBYTE_TO_FLOAT);
219 break;
220 case GL_BYTE:
221 CONVERT(GLbyte, BYTE_TO_FLOAT);
222 break;
223 default:
224 assert(0);
225 break;
226 }
227
228 type = GL_FLOAT;
229 r300_attr.free_needed = GL_TRUE;
230 r300_attr.data = tmp;
231 r300_attr.stride = sizeof(GLfloat) * input->Size;
232 r300_attr.dwords = input->Size;
233 } else {
234 type = input->Type;
235 r300_attr.free_needed = GL_FALSE;
236 r300_attr.data = (GLvoid *)src_ptr;
237 r300_attr.stride = stride;
238 r300_attr.dwords = (getTypeSize(type) * input->Size + 3)/ 4;
239 }
240
241 r300_attr.size = input->Size;
242 r300_attr.element = attr;
243 r300_attr.dst_loc = vbuf->num_attribs;
244
245 switch (type) {
246 case GL_FLOAT:
247 switch (input->Size) {
248 case 1: r300_attr.data_type = R300_DATA_TYPE_FLOAT_1; break;
249 case 2: r300_attr.data_type = R300_DATA_TYPE_FLOAT_2; break;
250 case 3: r300_attr.data_type = R300_DATA_TYPE_FLOAT_3; break;
251 case 4: r300_attr.data_type = R300_DATA_TYPE_FLOAT_4; break;
252 }
253 r300_attr._signed = 0;
254 r300_attr.normalize = 0;
255 break;
256 case GL_SHORT:
257 r300_attr._signed = 1;
258 r300_attr.normalize = input->Normalized;
259 switch (input->Size) {
260 case 1:
261 case 2:
262 r300_attr.data_type = R300_DATA_TYPE_SHORT_2;
263 break;
264 case 3:
265 case 4:
266 r300_attr.data_type = R300_DATA_TYPE_SHORT_4;
267 break;
268 }
269 break;
270 case GL_BYTE:
271 r300_attr._signed = 1;
272 r300_attr.normalize = input->Normalized;
273 r300_attr.data_type = R300_DATA_TYPE_BYTE;
274 break;
275 case GL_UNSIGNED_SHORT:
276 r300_attr._signed = 0;
277 r300_attr.normalize = input->Normalized;
278 switch (input->Size) {
279 case 1:
280 case 2:
281 r300_attr.data_type = R300_DATA_TYPE_SHORT_2;
282 break;
283 case 3:
284 case 4:
285 r300_attr.data_type = R300_DATA_TYPE_SHORT_4;
286 break;
287 }
288 break;
289 case GL_UNSIGNED_BYTE:
290 r300_attr._signed = 0;
291 r300_attr.normalize = input->Normalized;
292 if (input->Format == GL_BGRA)
293 r300_attr.data_type = R300_DATA_TYPE_D3DCOLOR;
294 else
295 r300_attr.data_type = R300_DATA_TYPE_BYTE;
296 break;
297
298 default:
299 case GL_DOUBLE:
300 case GL_INT:
301 case GL_UNSIGNED_INT:
302 assert(0);
303 break;
304 }
305
306 switch (input->Size) {
307 case 4:
308 r300_attr.swizzle = SWIZZLE_XYZW;
309 break;
310 case 3:
311 r300_attr.swizzle = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_ONE);
312 break;
313 case 2:
314 r300_attr.swizzle = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_ZERO, SWIZZLE_ONE);
315 break;
316 case 1:
317 r300_attr.swizzle = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_ZERO, SWIZZLE_ZERO, SWIZZLE_ONE);
318 break;
319 }
320
321 r300_attr.write_mask = MASK_XYZW;
322
323 vbuf->attribs[vbuf->num_attribs] = r300_attr;
324 ++vbuf->num_attribs;
325 }
326
327 static void r300SetVertexFormat(GLcontext *ctx, const struct gl_client_array *arrays[], int count, struct gl_buffer_object **bo, GLuint *nr_bo)
328 {
329 r300ContextPtr r300 = R300_CONTEXT(ctx);
330 struct r300_vertex_buffer *vbuf = &r300->vbuf;
331
332 {
333 int i, tmp;
334
335 tmp = r300->selected_vp->key.InputsRead;
336 i = 0;
337 vbuf->num_attribs = 0;
338 while (tmp) {
339 /* find first enabled bit */
340 while (!(tmp & 1)) {
341 tmp >>= 1;
342 ++i;
343 }
344
345 r300TranslateAttrib(ctx, i, count, arrays[i], bo, nr_bo);
346
347 tmp >>= 1;
348 ++i;
349 }
350 }
351
352 r300SwitchFallback(ctx, R300_FALLBACK_AOS_LIMIT, vbuf->num_attribs > R300_MAX_AOS_ARRAYS);
353 if (r300->fallback)
354 return;
355
356 {
357 int i;
358
359 for (i = 0; i < vbuf->num_attribs; i++) {
360 rcommon_emit_vector(ctx, &r300->radeon.tcl.aos[i],
361 vbuf->attribs[i].data, vbuf->attribs[i].dwords,
362 vbuf->attribs[i].stride, count);
363 }
364
365 r300->radeon.tcl.aos_count = vbuf->num_attribs;
366 }
367 }
368
369 static void r300FreeData(GLcontext *ctx, struct gl_buffer_object **bo, GLuint nr_bo)
370 {
371 {
372 struct r300_vertex_buffer *vbuf = &R300_CONTEXT(ctx)->vbuf;
373 int i;
374
375 for (i = 0; i < vbuf->num_attribs; i++) {
376 if (vbuf->attribs[i].free_needed)
377 _mesa_free(vbuf->attribs[i].data);
378 }
379 }
380
381 {
382 struct r300_index_buffer *ind_buf = &R300_CONTEXT(ctx)->ind_buf;
383 if (ind_buf->free_needed)
384 _mesa_free(ind_buf->ptr);
385 }
386
387 {
388 int i;
389
390 for (i = 0; i < nr_bo; ++i) {
391 ctx->Driver.UnmapBuffer(ctx, 0, bo[i]);
392 }
393 }
394 }
395
396 static GLboolean r300TryDrawPrims(GLcontext *ctx,
397 const struct gl_client_array *arrays[],
398 const struct _mesa_prim *prim,
399 GLuint nr_prims,
400 const struct _mesa_index_buffer *ib,
401 GLuint min_index,
402 GLuint max_index )
403 {
404 struct r300_context *r300 = R300_CONTEXT(ctx);
405 struct gl_buffer_object *bo[VERT_ATTRIB_MAX+1];
406 GLuint i, nr_bo = 0;
407
408 if (ctx->NewState)
409 _mesa_update_state( ctx );
410
411 if (r300->options.hw_tcl_enabled)
412 _tnl_UpdateFixedFunctionProgram(ctx);
413
414 r300UpdateShaders(r300);
415
416 r300SwitchFallback(ctx, R300_FALLBACK_INVALID_BUFFERS, !r300ValidateBuffers(ctx));
417
418 r300FixupIndexBuffer(ctx, ib, bo, &nr_bo);
419
420 r300SetVertexFormat(ctx, arrays, max_index + 1, bo, &nr_bo);
421
422 if (r300->fallback)
423 return GL_FALSE;
424
425 r300SetupVAP(ctx, r300->selected_vp->key.InputsRead, r300->selected_vp->key.OutputsWritten);
426
427 r300UpdateShaderStates(r300);
428
429 r300EmitCacheFlush(r300);
430 radeonEmitState(&r300->radeon);
431
432 for (i = 0; i < nr_prims; ++i) {
433 r300RunRenderPrimitive(ctx, prim[i].start, prim[i].start + prim[i].count, prim[i].mode);
434 }
435
436 r300EmitCacheFlush(r300);
437
438 radeonReleaseArrays(ctx, ~0);
439
440 r300FreeData(ctx, bo, nr_bo);
441
442 return GL_TRUE;
443 }
444
445 /* TODO: rebase if number of indices in any of primitives is > 8192 for 32bit indices or 16384 for 16bit indices */
446
447 static void r300DrawPrims(GLcontext *ctx,
448 const struct gl_client_array *arrays[],
449 const struct _mesa_prim *prim,
450 GLuint nr_prims,
451 const struct _mesa_index_buffer *ib,
452 GLuint min_index,
453 GLuint max_index)
454 {
455 GLboolean retval;
456
457 if (min_index) {
458 vbo_rebase_prims( ctx, arrays, prim, nr_prims, ib, min_index, max_index, r300DrawPrims );
459 return;
460 }
461
462 /* Make an attempt at drawing */
463 retval = r300TryDrawPrims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
464
465 /* If failed run tnl pipeline - it should take care of fallbacks */
466 if (!retval)
467 _tnl_draw_prims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
468 }
469
470 void r300InitDraw(GLcontext *ctx)
471 {
472 struct vbo_context *vbo = vbo_context(ctx);
473
474 vbo->draw_prims = r300DrawPrims;
475 }