Some changed for non-C99 compilers
[mesa.git] / src / mesa / state_tracker / st_draw.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 <keith@tungstengraphics.com>
31 */
32
33 #include "main/imports.h"
34 #include "main/image.h"
35
36 #include "vbo/vbo.h"
37
38 #include "st_context.h"
39 #include "st_atom.h"
40 #include "st_cb_bufferobjects.h"
41 #include "st_draw.h"
42 #include "st_program.h"
43
44 #include "pipe/p_context.h"
45 #include "pipe/p_defines.h"
46 #include "pipe/p_inlines.h"
47
48 #include "draw/draw_private.h"
49 #include "draw/draw_context.h"
50
51
52 static GLuint double_types[4] = {
53 PIPE_FORMAT_R64_FLOAT,
54 PIPE_FORMAT_R64G64_FLOAT,
55 PIPE_FORMAT_R64G64B64_FLOAT,
56 PIPE_FORMAT_R64G64B64A64_FLOAT
57 };
58
59 static GLuint float_types[4] = {
60 PIPE_FORMAT_R32_FLOAT,
61 PIPE_FORMAT_R32G32_FLOAT,
62 PIPE_FORMAT_R32G32B32_FLOAT,
63 PIPE_FORMAT_R32G32B32A32_FLOAT
64 };
65
66 static GLuint uint_types_norm[4] = {
67 PIPE_FORMAT_R32_UNORM,
68 PIPE_FORMAT_R32G32_UNORM,
69 PIPE_FORMAT_R32G32B32_UNORM,
70 PIPE_FORMAT_R32G32B32A32_UNORM
71 };
72
73 static GLuint uint_types_scale[4] = {
74 PIPE_FORMAT_R32_USCALED,
75 PIPE_FORMAT_R32G32_USCALED,
76 PIPE_FORMAT_R32G32B32_USCALED,
77 PIPE_FORMAT_R32G32B32A32_USCALED
78 };
79
80 static GLuint int_types_norm[4] = {
81 PIPE_FORMAT_R32_SNORM,
82 PIPE_FORMAT_R32G32_SNORM,
83 PIPE_FORMAT_R32G32B32_SNORM,
84 PIPE_FORMAT_R32G32B32A32_SNORM
85 };
86
87 static GLuint int_types_scale[4] = {
88 PIPE_FORMAT_R32_SSCALED,
89 PIPE_FORMAT_R32G32_SSCALED,
90 PIPE_FORMAT_R32G32B32_SSCALED,
91 PIPE_FORMAT_R32G32B32A32_SSCALED
92 };
93
94 static GLuint ushort_types_norm[4] = {
95 PIPE_FORMAT_R16_UNORM,
96 PIPE_FORMAT_R16G16_UNORM,
97 PIPE_FORMAT_R16G16B16_UNORM,
98 PIPE_FORMAT_R16G16B16A16_UNORM
99 };
100
101 static GLuint ushort_types_scale[4] = {
102 PIPE_FORMAT_R16_USCALED,
103 PIPE_FORMAT_R16G16_USCALED,
104 PIPE_FORMAT_R16G16B16_USCALED,
105 PIPE_FORMAT_R16G16B16A16_USCALED
106 };
107
108 static GLuint short_types_norm[4] = {
109 PIPE_FORMAT_R16_SNORM,
110 PIPE_FORMAT_R16G16_SNORM,
111 PIPE_FORMAT_R16G16B16_SNORM,
112 PIPE_FORMAT_R16G16B16A16_SNORM
113 };
114
115 static GLuint short_types_scale[4] = {
116 PIPE_FORMAT_R16_SSCALED,
117 PIPE_FORMAT_R16G16_SSCALED,
118 PIPE_FORMAT_R16G16B16_SSCALED,
119 PIPE_FORMAT_R16G16B16A16_SSCALED
120 };
121
122 static GLuint ubyte_types_norm[4] = {
123 PIPE_FORMAT_R8_UNORM,
124 PIPE_FORMAT_R8G8_UNORM,
125 PIPE_FORMAT_R8G8B8_UNORM,
126 PIPE_FORMAT_R8G8B8A8_UNORM
127 };
128
129 static GLuint ubyte_types_scale[4] = {
130 PIPE_FORMAT_R8_USCALED,
131 PIPE_FORMAT_R8G8_USCALED,
132 PIPE_FORMAT_R8G8B8_USCALED,
133 PIPE_FORMAT_R8G8B8A8_USCALED
134 };
135
136 static GLuint byte_types_norm[4] = {
137 PIPE_FORMAT_R8_SNORM,
138 PIPE_FORMAT_R8G8_SNORM,
139 PIPE_FORMAT_R8G8B8_SNORM,
140 PIPE_FORMAT_R8G8B8A8_SNORM
141 };
142
143 static GLuint byte_types_scale[4] = {
144 PIPE_FORMAT_R8_SSCALED,
145 PIPE_FORMAT_R8G8_SSCALED,
146 PIPE_FORMAT_R8G8B8_SSCALED,
147 PIPE_FORMAT_R8G8B8A8_SSCALED
148 };
149
150
151 /**
152 * Return a PIPE_FORMAT_x for the given GL datatype and size.
153 */
154 static GLuint
155 pipe_vertex_format(GLenum type, GLuint size, GLboolean normalized)
156 {
157 assert(type >= GL_BYTE);
158 assert(type <= GL_DOUBLE);
159 assert(size >= 1);
160 assert(size <= 4);
161
162 if (normalized) {
163 switch (type) {
164 case GL_DOUBLE: return double_types[size-1];
165 case GL_FLOAT: return float_types[size-1];
166 case GL_INT: return int_types_norm[size-1];
167 case GL_SHORT: return short_types_norm[size-1];
168 case GL_BYTE: return byte_types_norm[size-1];
169 case GL_UNSIGNED_INT: return uint_types_norm[size-1];
170 case GL_UNSIGNED_SHORT: return ushort_types_norm[size-1];
171 case GL_UNSIGNED_BYTE: return ubyte_types_norm[size-1];
172 default: assert(0); return 0;
173 }
174 }
175 else {
176 switch (type) {
177 case GL_DOUBLE: return double_types[size-1];
178 case GL_FLOAT: return float_types[size-1];
179 case GL_INT: return int_types_scale[size-1];
180 case GL_SHORT: return short_types_scale[size-1];
181 case GL_BYTE: return byte_types_scale[size-1];
182 case GL_UNSIGNED_INT: return uint_types_scale[size-1];
183 case GL_UNSIGNED_SHORT: return ushort_types_scale[size-1];
184 case GL_UNSIGNED_BYTE: return ubyte_types_scale[size-1];
185 default: assert(0); return 0;
186 }
187 }
188 return 0; /* silence compiler warning */
189 }
190
191
192 /*
193 * If edge flags are needed, setup an bitvector of flags and call
194 * pipe->set_edgeflags().
195 * XXX memleak: need to free the returned pointer at some point
196 */
197 static void *
198 setup_edgeflags(GLcontext *ctx, GLenum primMode, GLint start, GLint count,
199 const struct gl_client_array *array)
200 {
201 struct pipe_context *pipe = ctx->st->pipe;
202
203 if ((primMode == GL_TRIANGLES ||
204 primMode == GL_QUADS ||
205 primMode == GL_POLYGON) &&
206 (ctx->Polygon.FrontMode != GL_FILL ||
207 ctx->Polygon.BackMode != GL_FILL)) {
208 /* need edge flags */
209 GLuint i;
210 unsigned *vec;
211 struct st_buffer_object *stobj = st_buffer_object(array->BufferObj);
212 ubyte *map;
213
214 if (!stobj)
215 return NULL;
216
217 vec = (unsigned *) calloc(sizeof(unsigned), (count + 31) / 32);
218 if (!vec)
219 return NULL;
220
221 map = pipe_buffer_map(pipe, stobj->buffer, PIPE_BUFFER_USAGE_CPU_READ);
222 map = ADD_POINTERS(map, array->Ptr);
223
224 for (i = 0; i < count; i++) {
225 if (*((float *) map))
226 vec[i/32] |= 1 << (i % 32);
227
228 map += array->StrideB;
229 }
230
231 pipe_buffer_unmap(pipe, stobj->buffer);
232
233 pipe->set_edgeflags(pipe, vec);
234
235 return vec;
236 }
237 else {
238 /* edge flags not needed */
239 pipe->set_edgeflags(pipe, NULL);
240 return NULL;
241 }
242 }
243
244
245
246 /**
247 * This function gets plugged into the VBO module and is called when
248 * we have something to render.
249 * Basically, translate the information into the format expected by pipe.
250 */
251 void
252 st_draw_vbo(GLcontext *ctx,
253 const struct gl_client_array **arrays,
254 const struct _mesa_prim *prims,
255 GLuint nr_prims,
256 const struct _mesa_index_buffer *ib,
257 GLuint min_index,
258 GLuint max_index)
259 {
260 struct pipe_context *pipe = ctx->st->pipe;
261 const struct st_vertex_program *vp;
262 const struct pipe_shader_state *vs;
263 struct pipe_vertex_buffer vbuffer[PIPE_MAX_SHADER_INPUTS];
264 GLuint attr;
265 struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS];
266
267 /* sanity check for pointer arithmetic below */
268 assert(sizeof(arrays[0]->Ptr[0]) == 1);
269
270 st_validate_state(ctx->st);
271
272 /* must get these after state validation! */
273 vp = ctx->st->vp;
274 vs = &ctx->st->vp->state;
275
276 /* loop over TGSI shader inputs to determine vertex buffer
277 * and attribute info
278 */
279 for (attr = 0; attr < vp->num_inputs; attr++) {
280 const GLuint mesaAttr = vp->index_to_input[attr];
281 struct gl_buffer_object *bufobj = arrays[mesaAttr]->BufferObj;
282
283 if (bufobj && bufobj->Name) {
284 /* Attribute data is in a VBO.
285 * Recall that for VBOs, the gl_client_array->Ptr field is
286 * really an offset from the start of the VBO, not a pointer.
287 */
288 struct st_buffer_object *stobj = st_buffer_object(bufobj);
289 assert(stobj->buffer);
290
291 vbuffer[attr].buffer = NULL;
292 pipe_reference_buffer(pipe, &vbuffer[attr].buffer, stobj->buffer);
293 vbuffer[attr].buffer_offset = (unsigned) arrays[0]->Ptr;/* in bytes */
294 velements[attr].src_offset = arrays[mesaAttr]->Ptr - arrays[0]->Ptr;
295 assert(velements[attr].src_offset <= 2048); /* 11-bit field */
296 }
297 else {
298 /* attribute data is in user-space memory, not a VBO */
299 uint bytes;
300
301 if (!arrays[mesaAttr]->StrideB) {
302 bytes = arrays[mesaAttr]->Size
303 * _mesa_sizeof_type(arrays[mesaAttr]->Type);
304 } else {
305 bytes = arrays[mesaAttr]->StrideB * (max_index + 1);
306 }
307
308 /* wrap user data */
309 vbuffer[attr].buffer
310 = pipe_user_buffer_create(pipe, (void *) arrays[mesaAttr]->Ptr,
311 bytes);
312 vbuffer[attr].buffer_offset = 0;
313 velements[attr].src_offset = 0;
314 }
315
316 /* common-case setup */
317 vbuffer[attr].pitch = arrays[mesaAttr]->StrideB; /* in bytes */
318 vbuffer[attr].max_index = max_index;
319 velements[attr].vertex_buffer_index = attr;
320 velements[attr].nr_components = arrays[mesaAttr]->Size;
321 velements[attr].src_format
322 = pipe_vertex_format(arrays[mesaAttr]->Type,
323 arrays[mesaAttr]->Size,
324 arrays[mesaAttr]->Normalized);
325 assert(velements[attr].src_format);
326 }
327
328 pipe->set_vertex_buffers(pipe, vp->num_inputs, vbuffer);
329 pipe->set_vertex_elements(pipe, vp->num_inputs, velements);
330
331 /* do actual drawing */
332 if (ib) {
333 /* indexed primitive */
334 struct gl_buffer_object *bufobj = ib->obj;
335 struct pipe_buffer *indexBuf = NULL;
336 unsigned indexSize, indexOffset, i;
337
338 switch (ib->type) {
339 case GL_UNSIGNED_INT:
340 indexSize = 4;
341 break;
342 case GL_UNSIGNED_SHORT:
343 indexSize = 2;
344 break;
345 case GL_UNSIGNED_BYTE:
346 indexSize = 1;
347 break;
348 default:
349 assert(0);
350 return;
351 }
352
353 /* get/create the index buffer object */
354 if (bufobj && bufobj->Name) {
355 /* elements/indexes are in a real VBO */
356 struct st_buffer_object *stobj = st_buffer_object(bufobj);
357 pipe_reference_buffer(pipe, &indexBuf, stobj->buffer);
358 indexOffset = (unsigned) ib->ptr / indexSize;
359 }
360 else {
361 /* element/indicies are in user space memory */
362 indexBuf = pipe_user_buffer_create(pipe, (void *) ib->ptr,
363 ib->count * indexSize);
364 indexOffset = 0;
365 }
366
367 /* draw */
368 for (i = 0; i < nr_prims; i++) {
369 setup_edgeflags(ctx, prims[i].mode,
370 prims[i].start + indexOffset, prims[i].count,
371 arrays[VERT_ATTRIB_EDGEFLAG]);
372
373 pipe->draw_elements(pipe, indexBuf, indexSize,
374 prims[i].mode,
375 prims[i].start + indexOffset, prims[i].count);
376 }
377
378 pipe_reference_buffer(pipe, &indexBuf, NULL);
379 }
380 else {
381 /* non-indexed */
382 GLuint i;
383 for (i = 0; i < nr_prims; i++) {
384 setup_edgeflags(ctx, prims[i].mode,
385 prims[i].start, prims[i].count,
386 arrays[VERT_ATTRIB_EDGEFLAG]);
387
388 pipe->draw_arrays(pipe, prims[i].mode, prims[i].start, prims[i].count);
389 }
390 }
391
392 /* unreference buffers (frees wrapped user-space buffer objects) */
393 for (attr = 0; attr < vp->num_inputs; attr++) {
394 pipe_reference_buffer(pipe, &vbuffer[attr].buffer, NULL);
395 assert(!vbuffer[attr].buffer);
396 }
397 pipe->set_vertex_buffers(pipe, vp->num_inputs, vbuffer);
398 }
399
400
401
402 /**
403 * Set the (private) draw module's post-transformed vertex format when in
404 * GL_SELECT or GL_FEEDBACK mode or for glRasterPos.
405 */
406 static void
407 set_feedback_vertex_format(GLcontext *ctx)
408 {
409 #if 0
410 struct st_context *st = ctx->st;
411 struct vertex_info vinfo;
412 GLuint i;
413
414 memset(&vinfo, 0, sizeof(vinfo));
415
416 if (ctx->RenderMode == GL_SELECT) {
417 assert(ctx->RenderMode == GL_SELECT);
418 vinfo.num_attribs = 1;
419 vinfo.format[0] = FORMAT_4F;
420 vinfo.interp_mode[0] = INTERP_LINEAR;
421 }
422 else {
423 /* GL_FEEDBACK, or glRasterPos */
424 /* emit all attribs (pos, color, texcoord) as GLfloat[4] */
425 vinfo.num_attribs = st->state.vs->cso->state.num_outputs;
426 for (i = 0; i < vinfo.num_attribs; i++) {
427 vinfo.format[i] = FORMAT_4F;
428 vinfo.interp_mode[i] = INTERP_LINEAR;
429 }
430 }
431
432 draw_set_vertex_info(st->draw, &vinfo);
433 #endif
434 }
435
436
437 /**
438 * Called by VBO to draw arrays when in selection or feedback mode and
439 * to implement glRasterPos.
440 * This is very much like the normal draw_vbo() function above.
441 * Look at code refactoring some day.
442 * Might move this into the failover module some day.
443 */
444 void
445 st_feedback_draw_vbo(GLcontext *ctx,
446 const struct gl_client_array **arrays,
447 const struct _mesa_prim *prims,
448 GLuint nr_prims,
449 const struct _mesa_index_buffer *ib,
450 GLuint min_index,
451 GLuint max_index)
452 {
453 struct st_context *st = ctx->st;
454 struct pipe_context *pipe = st->pipe;
455 struct draw_context *draw = st->draw;
456 const struct st_vertex_program *vp;
457 const struct pipe_shader_state *vs;
458 struct pipe_buffer *index_buffer_handle = 0;
459 struct pipe_vertex_buffer vbuffers[PIPE_MAX_SHADER_INPUTS];
460 struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS];
461 GLuint attr, i;
462 ubyte *mapped_constants;
463
464 assert(draw);
465
466 st_validate_state(ctx->st);
467
468 /* must get these after state validation! */
469 vp = ctx->st->vp;
470 vs = &st->vp->state;
471
472 if (!st->vp->draw_shader) {
473 st->vp->draw_shader = draw_create_vertex_shader(draw, vs);
474 }
475
476 /*
477 * Set up the draw module's state.
478 *
479 * We'd like to do this less frequently, but the normal state-update
480 * code sends state updates to the pipe, not to our private draw module.
481 */
482 assert(draw);
483 draw_set_viewport_state(draw, &st->state.viewport);
484 draw_set_clip_state(draw, &st->state.clip);
485 draw_set_rasterizer_state(draw, &st->state.rasterizer);
486 draw_bind_vertex_shader(draw, st->vp->draw_shader);
487 set_feedback_vertex_format(ctx);
488
489 /* loop over TGSI shader inputs to determine vertex buffer
490 * and attribute info
491 */
492 for (attr = 0; attr < vp->num_inputs; attr++) {
493 const GLuint mesaAttr = vp->index_to_input[attr];
494 struct gl_buffer_object *bufobj = arrays[mesaAttr]->BufferObj;
495 void *map;
496
497 if (bufobj && bufobj->Name) {
498 /* Attribute data is in a VBO.
499 * Recall that for VBOs, the gl_client_array->Ptr field is
500 * really an offset from the start of the VBO, not a pointer.
501 */
502 struct st_buffer_object *stobj = st_buffer_object(bufobj);
503 assert(stobj->buffer);
504
505 vbuffers[attr].buffer = NULL;
506 pipe_reference_buffer(pipe, &vbuffers[attr].buffer, stobj->buffer);
507 vbuffers[attr].buffer_offset = (unsigned) arrays[0]->Ptr;/* in bytes */
508 velements[attr].src_offset = arrays[mesaAttr]->Ptr - arrays[0]->Ptr;
509 }
510 else {
511 /* attribute data is in user-space memory, not a VBO */
512 uint bytes = (arrays[mesaAttr]->Size
513 * _mesa_sizeof_type(arrays[mesaAttr]->Type)
514 * (max_index + 1));
515
516 /* wrap user data */
517 vbuffers[attr].buffer
518 = pipe_user_buffer_create(pipe, (void *) arrays[mesaAttr]->Ptr,
519 bytes);
520 vbuffers[attr].buffer_offset = 0;
521 velements[attr].src_offset = 0;
522 }
523
524 /* common-case setup */
525 vbuffers[attr].pitch = arrays[mesaAttr]->StrideB; /* in bytes */
526 vbuffers[attr].max_index = max_index;
527 velements[attr].vertex_buffer_index = attr;
528 velements[attr].nr_components = arrays[mesaAttr]->Size;
529 velements[attr].src_format = pipe_vertex_format(arrays[mesaAttr]->Type,
530 arrays[mesaAttr]->Size,
531 arrays[mesaAttr]->Normalized);
532 assert(velements[attr].src_format);
533
534 /* tell draw about this attribute */
535 #if 0
536 draw_set_vertex_buffer(draw, attr, &vbuffer[attr]);
537 #endif
538
539 /* map the attrib buffer */
540 map = pipe_buffer_map(pipe, vbuffers[attr].buffer,
541 PIPE_BUFFER_USAGE_CPU_READ);
542 draw_set_mapped_vertex_buffer(draw, attr, map);
543 }
544
545 draw_set_vertex_buffers(draw, vp->num_inputs, vbuffers);
546 draw_set_vertex_elements(draw, vp->num_inputs, velements);
547
548 if (ib) {
549 unsigned indexSize;
550 struct gl_buffer_object *bufobj = ib->obj;
551 struct st_buffer_object *stobj = st_buffer_object(bufobj);
552 void *map;
553
554 index_buffer_handle = stobj->buffer;
555
556 switch (ib->type) {
557 case GL_UNSIGNED_INT:
558 indexSize = 4;
559 break;
560 case GL_UNSIGNED_SHORT:
561 indexSize = 2;
562 break;
563 default:
564 assert(0);
565 return;
566 }
567
568 map = pipe_buffer_map(pipe, index_buffer_handle,
569 PIPE_BUFFER_USAGE_CPU_READ);
570 draw_set_mapped_element_buffer(draw, indexSize, map);
571 }
572 else {
573 /* no index/element buffer */
574 draw_set_mapped_element_buffer(draw, 0, NULL);
575 }
576
577
578 /* map constant buffers */
579 mapped_constants = pipe_buffer_map(pipe,
580 st->state.constants[PIPE_SHADER_VERTEX].buffer,
581 PIPE_BUFFER_USAGE_CPU_READ);
582 draw_set_mapped_constant_buffer(st->draw, mapped_constants);
583
584
585 /* draw here */
586 for (i = 0; i < nr_prims; i++) {
587 draw_arrays(draw, prims[i].mode, prims[i].start, prims[i].count);
588 }
589
590
591 /* unmap constant buffers */
592 pipe_buffer_unmap(pipe, st->state.constants[PIPE_SHADER_VERTEX].buffer);
593
594 /*
595 * unmap vertex/index buffers
596 */
597 for (i = 0; i < PIPE_MAX_ATTRIBS; i++) {
598 if (draw->pt.vertex_buffer[i].buffer) {
599 pipe_buffer_unmap(pipe, draw->pt.vertex_buffer[i].buffer);
600 pipe_reference_buffer(pipe, &draw->pt.vertex_buffer[i].buffer, NULL);
601 draw_set_mapped_vertex_buffer(draw, i, NULL);
602 }
603 }
604 if (ib) {
605 pipe_buffer_unmap(pipe, index_buffer_handle);
606 draw_set_mapped_element_buffer(draw, 0, NULL);
607 }
608 }
609
610
611
612 void st_init_draw( struct st_context *st )
613 {
614 GLcontext *ctx = st->ctx;
615
616 vbo_set_draw_func(ctx, st_draw_vbo);
617 }
618
619
620 void st_destroy_draw( struct st_context *st )
621 {
622 }
623
624