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