Sync with trunk r63647.
[reactos.git] / dll / opengl / mesa / tnl / t_draw.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions 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 MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Keith Whitwell <keith@tungstengraphics.com>
26 */
27
28 #include <precomp.h>
29
30 static GLubyte *get_space(struct gl_context *ctx, GLuint bytes)
31 {
32 TNLcontext *tnl = TNL_CONTEXT(ctx);
33 GLubyte *space = malloc(bytes);
34
35 tnl->block[tnl->nr_blocks++] = space;
36 return space;
37 }
38
39
40 static void free_space(struct gl_context *ctx)
41 {
42 TNLcontext *tnl = TNL_CONTEXT(ctx);
43 GLuint i;
44 for (i = 0; i < tnl->nr_blocks; i++)
45 free(tnl->block[i]);
46 tnl->nr_blocks = 0;
47 }
48
49
50 /* Convert the incoming array to GLfloats. Understands the
51 * array->Normalized flag and selects the correct conversion method.
52 */
53 #define CONVERT( TYPE, MACRO ) do { \
54 GLuint i, j; \
55 if (input->Normalized) { \
56 for (i = 0; i < count; i++) { \
57 const TYPE *in = (TYPE *)ptr; \
58 for (j = 0; j < sz; j++) { \
59 *fptr++ = MACRO(*in); \
60 in++; \
61 } \
62 ptr += input->StrideB; \
63 } \
64 } else { \
65 for (i = 0; i < count; i++) { \
66 const TYPE *in = (TYPE *)ptr; \
67 for (j = 0; j < sz; j++) { \
68 *fptr++ = (GLfloat)(*in); \
69 in++; \
70 } \
71 ptr += input->StrideB; \
72 } \
73 } \
74 } while (0)
75
76 /**
77 * \brief Convert fixed-point to floating-point.
78 *
79 * In OpenGL, a fixed-point number is a "signed 2's complement 16.16 scaled
80 * integer" (Table 2.2 of the OpenGL ES 2.0 spec).
81 *
82 * If the buffer has the \c normalized flag set, the formula
83 * \code normalize(x) := (2*x + 1) / (2^16 - 1) \endcode
84 * is used to map the fixed-point numbers into the range [-1, 1].
85 */
86 static void
87 convert_fixed_to_float(const struct gl_client_array *input,
88 const GLubyte *ptr, GLfloat *fptr,
89 GLuint count)
90 {
91 GLuint i, j;
92 const GLint size = input->Size;
93
94 if (input->Normalized) {
95 for (i = 0; i < count; ++i) {
96 const GLfixed *in = (GLfixed *) ptr;
97 for (j = 0; j < size; ++j) {
98 *fptr++ = (GLfloat) (2 * in[j] + 1) / (GLfloat) ((1 << 16) - 1);
99 }
100 ptr += input->StrideB;
101 }
102 } else {
103 for (i = 0; i < count; ++i) {
104 const GLfixed *in = (GLfixed *) ptr;
105 for (j = 0; j < size; ++j) {
106 *fptr++ = in[j] / (GLfloat) (1 << 16);
107 }
108 ptr += input->StrideB;
109 }
110 }
111 }
112
113 /* Adjust pointer to point at first requested element, convert to
114 * floating point, populate VB->AttribPtr[].
115 */
116 static void _tnl_import_array( struct gl_context *ctx,
117 GLuint attrib,
118 GLuint count,
119 const struct gl_client_array *input,
120 const GLubyte *ptr )
121 {
122 TNLcontext *tnl = TNL_CONTEXT(ctx);
123 struct vertex_buffer *VB = &tnl->vb;
124 GLuint stride = input->StrideB;
125
126 if (input->Type != GL_FLOAT) {
127 const GLuint sz = input->Size;
128 GLubyte *buf = get_space(ctx, count * sz * sizeof(GLfloat));
129 GLfloat *fptr = (GLfloat *)buf;
130
131 switch (input->Type) {
132 case GL_BYTE:
133 CONVERT(GLbyte, BYTE_TO_FLOAT);
134 break;
135 case GL_UNSIGNED_BYTE:
136 CONVERT(GLubyte, UBYTE_TO_FLOAT);
137 break;
138 case GL_SHORT:
139 CONVERT(GLshort, SHORT_TO_FLOAT);
140 break;
141 case GL_UNSIGNED_SHORT:
142 CONVERT(GLushort, USHORT_TO_FLOAT);
143 break;
144 case GL_INT:
145 CONVERT(GLint, INT_TO_FLOAT);
146 break;
147 case GL_UNSIGNED_INT:
148 CONVERT(GLuint, UINT_TO_FLOAT);
149 break;
150 case GL_DOUBLE:
151 CONVERT(GLdouble, (GLfloat));
152 break;
153 case GL_FIXED:
154 convert_fixed_to_float(input, ptr, fptr, count);
155 break;
156 default:
157 assert(0);
158 break;
159 }
160
161 ptr = buf;
162 stride = sz * sizeof(GLfloat);
163 }
164
165 VB->AttribPtr[attrib] = &tnl->tmp_inputs[attrib];
166 VB->AttribPtr[attrib]->data = (GLfloat (*)[4])ptr;
167 VB->AttribPtr[attrib]->start = (GLfloat *)ptr;
168 VB->AttribPtr[attrib]->count = count;
169 VB->AttribPtr[attrib]->stride = stride;
170 VB->AttribPtr[attrib]->size = input->Size;
171
172 /* This should die, but so should the whole GLvector4f concept:
173 */
174 VB->AttribPtr[attrib]->flags = (((1<<input->Size)-1) |
175 VEC_NOT_WRITEABLE |
176 (stride == 4*sizeof(GLfloat) ? 0 : VEC_BAD_STRIDE));
177
178 VB->AttribPtr[attrib]->storage = NULL;
179 }
180
181 #define CLIPVERTS ((6 + MAX_CLIP_PLANES) * 2)
182
183
184 static GLboolean *_tnl_import_edgeflag( struct gl_context *ctx,
185 const GLvector4f *input,
186 GLuint count)
187 {
188 const GLubyte *ptr = (const GLubyte *)input->data;
189 const GLuint stride = input->stride;
190 GLboolean *space = (GLboolean *)get_space(ctx, count + CLIPVERTS);
191 GLboolean *bptr = space;
192 GLuint i;
193
194 for (i = 0; i < count; i++) {
195 *bptr++ = ((GLfloat *)ptr)[0] == 1.0;
196 ptr += stride;
197 }
198
199 return space;
200 }
201
202 static const GLfloat zero_floats[4] = {0.0, 0.0, 0.0, 0.0};
203
204
205 static void bind_inputs( struct gl_context *ctx,
206 const struct gl_client_array *inputs[],
207 GLint count,
208 struct gl_buffer_object **bo,
209 GLuint *nr_bo )
210 {
211 TNLcontext *tnl = TNL_CONTEXT(ctx);
212 struct vertex_buffer *VB = &tnl->vb;
213 GLuint i;
214
215 /* Map all the VBOs
216 */
217 for (i = 0; i < _TNL_ATTRIB_MAX; i++) {
218 const void *ptr;
219
220 if (inputs[i]->BufferObj->Name) {
221 if (!inputs[i]->BufferObj->Pointer) {
222 bo[*nr_bo] = inputs[i]->BufferObj;
223 (*nr_bo)++;
224 ctx->Driver.MapBufferRange(ctx, 0, inputs[i]->BufferObj->Size,
225 GL_MAP_READ_BIT,
226 inputs[i]->BufferObj);
227
228 assert(inputs[i]->BufferObj->Pointer);
229 }
230
231 ptr = ADD_POINTERS(inputs[i]->BufferObj->Pointer,
232 inputs[i]->Ptr);
233 }
234 else
235 ptr = inputs[i]->Ptr;
236
237 /* Just make sure the array is floating point, otherwise convert to
238 * temporary storage.
239 *
240 * XXX: remove the GLvector4f type at some stage and just use
241 * client arrays.
242 */
243 _tnl_import_array(ctx, i, count, inputs[i], ptr);
244 }
245
246 /* We process only the vertices between min & max index:
247 */
248 VB->Count = count;
249
250 /* These should perhaps be part of _TNL_ATTRIB_* */
251 VB->BackfaceColorPtr = NULL;
252 VB->BackfaceIndexPtr = NULL;
253
254 /* Clipping and drawing code still requires this to be a packed
255 * array of ubytes which can be written into. TODO: Fix and
256 * remove.
257 */
258 if (ctx->Polygon.FrontMode != GL_FILL ||
259 ctx->Polygon.BackMode != GL_FILL)
260 {
261 VB->EdgeFlag = _tnl_import_edgeflag( ctx,
262 VB->AttribPtr[_TNL_ATTRIB_EDGEFLAG],
263 VB->Count );
264 }
265 else {
266 /* the data previously pointed to by EdgeFlag may have been freed */
267 VB->EdgeFlag = NULL;
268 }
269 }
270
271
272 /* Translate indices to GLuints and store in VB->Elts.
273 */
274 static void bind_indices( struct gl_context *ctx,
275 const struct _mesa_index_buffer *ib,
276 struct gl_buffer_object **bo,
277 GLuint *nr_bo)
278 {
279 TNLcontext *tnl = TNL_CONTEXT(ctx);
280 struct vertex_buffer *VB = &tnl->vb;
281 GLuint i;
282 const void *ptr;
283
284 if (!ib) {
285 VB->Elts = NULL;
286 return;
287 }
288
289 if (_mesa_is_bufferobj(ib->obj) && !_mesa_bufferobj_mapped(ib->obj)) {
290 /* if the buffer object isn't mapped yet, map it now */
291 bo[*nr_bo] = ib->obj;
292 (*nr_bo)++;
293 ptr = ctx->Driver.MapBufferRange(ctx, (GLsizeiptr) ib->ptr,
294 ib->count * vbo_sizeof_ib_type(ib->type),
295 GL_MAP_READ_BIT, ib->obj);
296 assert(ib->obj->Pointer);
297 } else {
298 /* user-space elements, or buffer already mapped */
299 ptr = ADD_POINTERS(ib->obj->Pointer, ib->ptr);
300 }
301
302 if (ib->type == GL_UNSIGNED_INT) {
303 VB->Elts = (GLuint *) ptr;
304 }
305 else {
306 GLuint *elts = (GLuint *)get_space(ctx, ib->count * sizeof(GLuint));
307 VB->Elts = elts;
308
309 if (ib->type == GL_UNSIGNED_SHORT) {
310 const GLushort *in = (GLushort *)ptr;
311 for (i = 0; i < ib->count; i++)
312 *elts++ = (GLuint)(*in++);
313 }
314 else {
315 const GLubyte *in = (GLubyte *)ptr;
316 for (i = 0; i < ib->count; i++)
317 *elts++ = (GLuint)(*in++);
318 }
319 }
320 }
321
322 static void bind_prims( struct gl_context *ctx,
323 const struct _mesa_prim *prim,
324 GLuint nr_prims )
325 {
326 TNLcontext *tnl = TNL_CONTEXT(ctx);
327 struct vertex_buffer *VB = &tnl->vb;
328
329 VB->Primitive = prim;
330 VB->PrimitiveCount = nr_prims;
331 }
332
333 static void unmap_vbos( struct gl_context *ctx,
334 struct gl_buffer_object **bo,
335 GLuint nr_bo )
336 {
337 GLuint i;
338 for (i = 0; i < nr_bo; i++) {
339 ctx->Driver.UnmapBuffer(ctx, bo[i]);
340 }
341 }
342
343
344 void _tnl_vbo_draw_prims(struct gl_context *ctx,
345 const struct gl_client_array *arrays[],
346 const struct _mesa_prim *prim,
347 GLuint nr_prims,
348 const struct _mesa_index_buffer *ib,
349 GLboolean index_bounds_valid,
350 GLuint min_index,
351 GLuint max_index)
352 {
353 if (!index_bounds_valid)
354 vbo_get_minmax_index(ctx, prim, ib, &min_index, &max_index);
355
356 _tnl_draw_prims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
357 }
358
359 /* This is the main entrypoint into the slimmed-down software tnl
360 * module. In a regular swtnl driver, this can be plugged straight
361 * into the vbo->Driver.DrawPrims() callback.
362 */
363 void _tnl_draw_prims( struct gl_context *ctx,
364 const struct gl_client_array *arrays[],
365 const struct _mesa_prim *prim,
366 GLuint nr_prims,
367 const struct _mesa_index_buffer *ib,
368 GLuint min_index,
369 GLuint max_index)
370 {
371 TNLcontext *tnl = TNL_CONTEXT(ctx);
372 const GLuint TEST_SPLIT = 0;
373 const GLint max = TEST_SPLIT ? 8 : tnl->vb.Size - MAX_CLIPPED_VERTICES;
374
375 /* Mesa core state should have been validated already */
376 assert(ctx->NewState == 0x0);
377
378 if (0)
379 {
380 GLuint i;
381 printf("%s %d..%d\n", __FUNCTION__, min_index, max_index);
382 for (i = 0; i < nr_prims; i++)
383 printf("prim %d: %s start %d count %d\n", i,
384 _mesa_lookup_enum_by_nr(prim[i].mode),
385 prim[i].start,
386 prim[i].count);
387 }
388
389 if (min_index) {
390 /* We always translate away calls with min_index != 0.
391 */
392 vbo_rebase_prims( ctx, arrays, prim, nr_prims, ib,
393 min_index, max_index,
394 _tnl_vbo_draw_prims );
395 return;
396 }
397 else if (max_index > max) {
398 /* The software TNL pipeline has a fixed amount of storage for
399 * vertices and it is necessary to split incoming drawing commands
400 * if they exceed that limit.
401 */
402 struct split_limits limits;
403 limits.max_verts = max;
404 limits.max_vb_size = ~0;
405 limits.max_indices = ~0;
406
407 /* This will split the buffers one way or another and
408 * recursively call back into this function.
409 */
410 vbo_split_prims( ctx, arrays, prim, nr_prims, ib,
411 0, max_index,
412 _tnl_vbo_draw_prims,
413 &limits );
414 }
415 else {
416 /* May need to map a vertex buffer object for every attribute plus
417 * one for the index buffer.
418 */
419 struct gl_buffer_object *bo[_TNL_ATTRIB_MAX];
420 GLuint nr_bo = 0;
421 GLuint inst;
422
423 /* Binding inputs may imply mapping some vertex buffer objects.
424 * They will need to be unmapped below.
425 */
426 for (inst = 0; inst < prim[0].num_instances; inst++) {
427
428 bind_prims(ctx, prim, nr_prims);
429 bind_inputs(ctx, arrays, max_index + 1,
430 bo, &nr_bo);
431 bind_indices(ctx, ib, bo, &nr_bo);
432
433 tnl->CurInstance = inst;
434 TNL_CONTEXT(ctx)->Driver.RunPipeline(ctx);
435
436 unmap_vbos(ctx, bo, nr_bo);
437 free_space(ctx);
438 }
439 }
440 }
441