1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Keith Whitwell <keith@tungstengraphics.com>
33 #include "main/imports.h"
34 #include "main/image.h"
35 #include "main/macros.h"
36 #include "shader/prog_uniform.h"
40 #include "st_context.h"
42 #include "st_cb_bufferobjects.h"
44 #include "st_program.h"
46 #include "pipe/p_context.h"
47 #include "pipe/p_defines.h"
48 #include "pipe/p_inlines.h"
51 static GLuint double_types
[4] = {
52 PIPE_FORMAT_R64_FLOAT
,
53 PIPE_FORMAT_R64G64_FLOAT
,
54 PIPE_FORMAT_R64G64B64_FLOAT
,
55 PIPE_FORMAT_R64G64B64A64_FLOAT
58 static GLuint float_types
[4] = {
59 PIPE_FORMAT_R32_FLOAT
,
60 PIPE_FORMAT_R32G32_FLOAT
,
61 PIPE_FORMAT_R32G32B32_FLOAT
,
62 PIPE_FORMAT_R32G32B32A32_FLOAT
65 static GLuint uint_types_norm
[4] = {
66 PIPE_FORMAT_R32_UNORM
,
67 PIPE_FORMAT_R32G32_UNORM
,
68 PIPE_FORMAT_R32G32B32_UNORM
,
69 PIPE_FORMAT_R32G32B32A32_UNORM
72 static GLuint uint_types_scale
[4] = {
73 PIPE_FORMAT_R32_USCALED
,
74 PIPE_FORMAT_R32G32_USCALED
,
75 PIPE_FORMAT_R32G32B32_USCALED
,
76 PIPE_FORMAT_R32G32B32A32_USCALED
79 static GLuint int_types_norm
[4] = {
80 PIPE_FORMAT_R32_SNORM
,
81 PIPE_FORMAT_R32G32_SNORM
,
82 PIPE_FORMAT_R32G32B32_SNORM
,
83 PIPE_FORMAT_R32G32B32A32_SNORM
86 static GLuint int_types_scale
[4] = {
87 PIPE_FORMAT_R32_SSCALED
,
88 PIPE_FORMAT_R32G32_SSCALED
,
89 PIPE_FORMAT_R32G32B32_SSCALED
,
90 PIPE_FORMAT_R32G32B32A32_SSCALED
93 static GLuint ushort_types_norm
[4] = {
94 PIPE_FORMAT_R16_UNORM
,
95 PIPE_FORMAT_R16G16_UNORM
,
96 PIPE_FORMAT_R16G16B16_UNORM
,
97 PIPE_FORMAT_R16G16B16A16_UNORM
100 static GLuint ushort_types_scale
[4] = {
101 PIPE_FORMAT_R16_USCALED
,
102 PIPE_FORMAT_R16G16_USCALED
,
103 PIPE_FORMAT_R16G16B16_USCALED
,
104 PIPE_FORMAT_R16G16B16A16_USCALED
107 static GLuint short_types_norm
[4] = {
108 PIPE_FORMAT_R16_SNORM
,
109 PIPE_FORMAT_R16G16_SNORM
,
110 PIPE_FORMAT_R16G16B16_SNORM
,
111 PIPE_FORMAT_R16G16B16A16_SNORM
114 static GLuint short_types_scale
[4] = {
115 PIPE_FORMAT_R16_SSCALED
,
116 PIPE_FORMAT_R16G16_SSCALED
,
117 PIPE_FORMAT_R16G16B16_SSCALED
,
118 PIPE_FORMAT_R16G16B16A16_SSCALED
121 static GLuint ubyte_types_norm
[4] = {
122 PIPE_FORMAT_R8_UNORM
,
123 PIPE_FORMAT_R8G8_UNORM
,
124 PIPE_FORMAT_R8G8B8_UNORM
,
125 PIPE_FORMAT_R8G8B8A8_UNORM
128 static GLuint ubyte_types_scale
[4] = {
129 PIPE_FORMAT_R8_USCALED
,
130 PIPE_FORMAT_R8G8_USCALED
,
131 PIPE_FORMAT_R8G8B8_USCALED
,
132 PIPE_FORMAT_R8G8B8A8_USCALED
135 static GLuint byte_types_norm
[4] = {
136 PIPE_FORMAT_R8_SNORM
,
137 PIPE_FORMAT_R8G8_SNORM
,
138 PIPE_FORMAT_R8G8B8_SNORM
,
139 PIPE_FORMAT_R8G8B8A8_SNORM
142 static GLuint byte_types_scale
[4] = {
143 PIPE_FORMAT_R8_SSCALED
,
144 PIPE_FORMAT_R8G8_SSCALED
,
145 PIPE_FORMAT_R8G8B8_SSCALED
,
146 PIPE_FORMAT_R8G8B8A8_SSCALED
149 static GLuint fixed_types
[4] = {
150 PIPE_FORMAT_R32_FIXED
,
151 PIPE_FORMAT_R32G32_FIXED
,
152 PIPE_FORMAT_R32G32B32_FIXED
,
153 PIPE_FORMAT_R32G32B32A32_FIXED
159 * Return a PIPE_FORMAT_x for the given GL datatype and size.
162 st_pipe_vertex_format(GLenum type
, GLuint size
, GLboolean normalized
)
164 assert((type
>= GL_BYTE
&& type
<= GL_DOUBLE
) ||
171 case GL_DOUBLE
: return double_types
[size
-1];
172 case GL_FLOAT
: return float_types
[size
-1];
173 case GL_INT
: return int_types_norm
[size
-1];
174 case GL_SHORT
: return short_types_norm
[size
-1];
175 case GL_BYTE
: return byte_types_norm
[size
-1];
176 case GL_UNSIGNED_INT
: return uint_types_norm
[size
-1];
177 case GL_UNSIGNED_SHORT
: return ushort_types_norm
[size
-1];
178 case GL_UNSIGNED_BYTE
: return ubyte_types_norm
[size
-1];
179 case GL_FIXED
: return fixed_types
[size
-1];
180 default: assert(0); return 0;
185 case GL_DOUBLE
: return double_types
[size
-1];
186 case GL_FLOAT
: return float_types
[size
-1];
187 case GL_INT
: return int_types_scale
[size
-1];
188 case GL_SHORT
: return short_types_scale
[size
-1];
189 case GL_BYTE
: return byte_types_scale
[size
-1];
190 case GL_UNSIGNED_INT
: return uint_types_scale
[size
-1];
191 case GL_UNSIGNED_SHORT
: return ushort_types_scale
[size
-1];
192 case GL_UNSIGNED_BYTE
: return ubyte_types_scale
[size
-1];
193 case GL_FIXED
: return fixed_types
[size
-1];
194 default: assert(0); return 0;
197 return 0; /* silence compiler warning */
202 * If edge flags are needed, setup an bitvector of flags and call
203 * pipe->set_edgeflags().
204 * XXX memleak: need to free the returned pointer at some point
207 setup_edgeflags(GLcontext
*ctx
, GLenum primMode
, GLint start
, GLint count
,
208 const struct gl_client_array
*array
)
210 struct pipe_context
*pipe
= ctx
->st
->pipe
;
212 if ((primMode
== GL_TRIANGLES
||
213 primMode
== GL_QUADS
||
214 primMode
== GL_POLYGON
) &&
215 (ctx
->Polygon
.FrontMode
!= GL_FILL
||
216 ctx
->Polygon
.BackMode
!= GL_FILL
)) {
217 /* need edge flags */
220 struct st_buffer_object
*stobj
= st_buffer_object(array
->BufferObj
);
226 vec
= (unsigned *) _mesa_calloc(sizeof(unsigned) * ((count
+ 31) / 32));
230 map
= pipe_buffer_map(pipe
->screen
, stobj
->buffer
, PIPE_BUFFER_USAGE_CPU_READ
);
231 map
= ADD_POINTERS(map
, array
->Ptr
);
233 for (i
= 0; i
< count
; i
++) {
234 if (*((float *) map
))
235 vec
[i
/32] |= 1 << (i
% 32);
237 map
+= array
->StrideB
;
240 pipe_buffer_unmap(pipe
->screen
, stobj
->buffer
);
242 pipe
->set_edgeflags(pipe
, vec
);
247 /* edge flags not needed */
248 pipe
->set_edgeflags(pipe
, NULL
);
255 * Examine the active arrays to determine if we have interleaved
256 * vertex arrays all living in one VBO, or all living in user space.
257 * \param userSpace returns whether the arrays are in user space.
260 is_interleaved_arrays(const struct st_vertex_program
*vp
,
261 const struct gl_client_array
**arrays
,
262 GLboolean
*userSpace
)
265 const struct gl_buffer_object
*firstBufObj
= NULL
;
266 GLint firstStride
= -1;
267 GLuint num_client_arrays
= 0;
268 const GLubyte
*client_addr
= NULL
;
270 for (attr
= 0; attr
< vp
->num_inputs
; attr
++) {
271 const GLuint mesaAttr
= vp
->index_to_input
[attr
];
272 const struct gl_buffer_object
*bufObj
= arrays
[mesaAttr
]->BufferObj
;
273 const GLsizei stride
= arrays
[mesaAttr
]->StrideB
; /* in bytes */
275 if (firstStride
< 0) {
276 firstStride
= stride
;
278 else if (firstStride
!= stride
) {
282 if (!bufObj
|| !bufObj
->Name
) {
284 /* Try to detect if the client-space arrays are
285 * "close" to each other.
288 client_addr
= arrays
[mesaAttr
]->Ptr
;
290 else if (abs(arrays
[mesaAttr
]->Ptr
- client_addr
) > firstStride
) {
291 /* arrays start too far apart */
295 else if (!firstBufObj
) {
296 firstBufObj
= bufObj
;
298 else if (bufObj
!= firstBufObj
) {
303 *userSpace
= (num_client_arrays
== vp
->num_inputs
);
304 /* printf("user space: %d (%d %d)\n", (int) *userSpace,num_client_arrays,vp->num_inputs); */
311 * Compute the memory range occupied by the arrays.
314 get_arrays_bounds(const struct st_vertex_program
*vp
,
315 const struct gl_client_array
**arrays
,
317 const GLubyte
**low
, const GLubyte
**high
)
319 const GLubyte
*low_addr
= NULL
;
320 const GLubyte
*high_addr
= NULL
;
323 for (attr
= 0; attr
< vp
->num_inputs
; attr
++) {
324 const GLuint mesaAttr
= vp
->index_to_input
[attr
];
325 const GLint stride
= arrays
[mesaAttr
]->StrideB
;
326 const GLubyte
*start
= arrays
[mesaAttr
]->Ptr
;
327 const unsigned sz
= (arrays
[mesaAttr
]->Size
*
328 _mesa_sizeof_type(arrays
[mesaAttr
]->Type
));
329 const GLubyte
*end
= start
+ (max_index
* stride
) + sz
;
336 low_addr
= MIN2(low_addr
, start
);
337 high_addr
= MAX2(high_addr
, end
);
347 * Set up for drawing interleaved arrays that all live in one VBO
348 * or all live in user space.
349 * \param vbuffer returns vertex buffer info
350 * \param velements returns vertex element info
353 setup_interleaved_attribs(GLcontext
*ctx
,
354 const struct st_vertex_program
*vp
,
355 const struct gl_client_array
**arrays
,
358 struct pipe_vertex_buffer
*vbuffer
,
359 struct pipe_vertex_element velements
[])
361 struct pipe_context
*pipe
= ctx
->st
->pipe
;
363 const GLubyte
*offset0
;
365 for (attr
= 0; attr
< vp
->num_inputs
; attr
++) {
366 const GLuint mesaAttr
= vp
->index_to_input
[attr
];
367 struct gl_buffer_object
*bufobj
= arrays
[mesaAttr
]->BufferObj
;
368 struct st_buffer_object
*stobj
= st_buffer_object(bufobj
);
369 GLsizei stride
= arrays
[mesaAttr
]->StrideB
;
371 /*printf("stobj %u = %p\n", attr, (void*)stobj);*/
374 const GLubyte
*low
, *high
;
376 get_arrays_bounds(vp
, arrays
, max_index
, &low
, &high
);
377 /*printf("buffer range: %p %p %d\n", low, high, high-low);*/
382 pipe_user_buffer_create(pipe
->screen
, (void *) low
, high
- low
);
383 vbuffer
->buffer_offset
= 0;
386 vbuffer
->buffer
= NULL
;
387 pipe_buffer_reference(&vbuffer
->buffer
, stobj
->buffer
);
388 vbuffer
->buffer_offset
= pointer_to_offset(low
);
390 vbuffer
->stride
= stride
; /* in bytes */
391 vbuffer
->max_index
= max_index
;
394 velements
[attr
].src_offset
=
395 (unsigned) (arrays
[mesaAttr
]->Ptr
- offset0
);
396 velements
[attr
].vertex_buffer_index
= 0;
397 velements
[attr
].nr_components
= arrays
[mesaAttr
]->Size
;
398 velements
[attr
].src_format
=
399 st_pipe_vertex_format(arrays
[mesaAttr
]->Type
,
400 arrays
[mesaAttr
]->Size
,
401 arrays
[mesaAttr
]->Normalized
);
402 assert(velements
[attr
].src_format
);
408 * Set up a separate pipe_vertex_buffer and pipe_vertex_element for each
410 * \param vbuffer returns vertex buffer info
411 * \param velements returns vertex element info
414 setup_non_interleaved_attribs(GLcontext
*ctx
,
415 const struct st_vertex_program
*vp
,
416 const struct gl_client_array
**arrays
,
418 GLboolean
*userSpace
,
419 struct pipe_vertex_buffer vbuffer
[],
420 struct pipe_vertex_element velements
[])
422 struct pipe_context
*pipe
= ctx
->st
->pipe
;
425 for (attr
= 0; attr
< vp
->num_inputs
; attr
++) {
426 const GLuint mesaAttr
= vp
->index_to_input
[attr
];
427 struct gl_buffer_object
*bufobj
= arrays
[mesaAttr
]->BufferObj
;
428 GLsizei stride
= arrays
[mesaAttr
]->StrideB
;
430 *userSpace
= GL_FALSE
;
432 if (bufobj
&& bufobj
->Name
) {
433 /* Attribute data is in a VBO.
434 * Recall that for VBOs, the gl_client_array->Ptr field is
435 * really an offset from the start of the VBO, not a pointer.
437 struct st_buffer_object
*stobj
= st_buffer_object(bufobj
);
438 assert(stobj
->buffer
);
439 /*printf("stobj %u = %p\n", attr, (void*) stobj);*/
441 vbuffer
[attr
].buffer
= NULL
;
442 pipe_buffer_reference(&vbuffer
[attr
].buffer
, stobj
->buffer
);
443 vbuffer
[attr
].buffer_offset
= pointer_to_offset(arrays
[mesaAttr
]->Ptr
);
444 velements
[attr
].src_offset
= 0;
447 /* attribute data is in user-space memory, not a VBO */
449 /*printf("user-space array %d stride %d\n", attr, stride);*/
451 *userSpace
= GL_TRUE
;
454 if (arrays
[mesaAttr
]->Ptr
) {
455 /* user's vertex array */
456 if (arrays
[mesaAttr
]->StrideB
) {
457 bytes
= arrays
[mesaAttr
]->StrideB
* (max_index
+ 1);
460 bytes
= arrays
[mesaAttr
]->Size
461 * _mesa_sizeof_type(arrays
[mesaAttr
]->Type
);
463 vbuffer
[attr
].buffer
= pipe_user_buffer_create(pipe
->screen
,
464 (void *) arrays
[mesaAttr
]->Ptr
, bytes
);
467 /* no array, use ctx->Current.Attrib[] value */
468 bytes
= sizeof(ctx
->Current
.Attrib
[0]);
469 vbuffer
[attr
].buffer
= pipe_user_buffer_create(pipe
->screen
,
470 (void *) ctx
->Current
.Attrib
[mesaAttr
], bytes
);
474 vbuffer
[attr
].buffer_offset
= 0;
475 velements
[attr
].src_offset
= 0;
478 assert(velements
[attr
].src_offset
<= 2048); /* 11-bit field */
480 /* common-case setup */
481 vbuffer
[attr
].stride
= stride
; /* in bytes */
482 vbuffer
[attr
].max_index
= max_index
;
483 velements
[attr
].vertex_buffer_index
= attr
;
484 velements
[attr
].nr_components
= arrays
[mesaAttr
]->Size
;
485 velements
[attr
].src_format
486 = st_pipe_vertex_format(arrays
[mesaAttr
]->Type
,
487 arrays
[mesaAttr
]->Size
,
488 arrays
[mesaAttr
]->Normalized
);
489 assert(velements
[attr
].src_format
);
496 * Prior to drawing, check that any uniforms referenced by the
497 * current shader have been set. If a uniform has not been set,
501 check_uniforms(GLcontext
*ctx
)
503 const struct gl_shader_program
*shProg
= ctx
->Shader
.CurrentProgram
;
504 if (shProg
&& shProg
->LinkStatus
) {
506 for (i
= 0; i
< shProg
->Uniforms
->NumUniforms
; i
++) {
507 const struct gl_uniform
*u
= &shProg
->Uniforms
->Uniforms
[i
];
508 if (!u
->Initialized
) {
510 "Using shader with uninitialized uniform: %s",
519 * This function gets plugged into the VBO module and is called when
520 * we have something to render.
521 * Basically, translate the information into the format expected by gallium.
524 st_draw_vbo(GLcontext
*ctx
,
525 const struct gl_client_array
**arrays
,
526 const struct _mesa_prim
*prims
,
528 const struct _mesa_index_buffer
*ib
,
532 struct pipe_context
*pipe
= ctx
->st
->pipe
;
533 const struct st_vertex_program
*vp
;
534 const struct pipe_shader_state
*vs
;
535 struct pipe_vertex_buffer vbuffer
[PIPE_MAX_SHADER_INPUTS
];
537 struct pipe_vertex_element velements
[PIPE_MAX_ATTRIBS
];
538 unsigned num_vbuffers
, num_velements
;
541 /* sanity check for pointer arithmetic below */
542 assert(sizeof(arrays
[0]->Ptr
[0]) == 1);
544 st_validate_state(ctx
->st
);
546 /* must get these after state validation! */
548 vs
= &ctx
->st
->vp
->state
;
551 if (MESA_VERBOSE
& VERBOSE_GLSL
) {
555 (void) check_uniforms
;
559 * Setup the vbuffer[] and velements[] arrays.
561 if (is_interleaved_arrays(vp
, arrays
, &userSpace
)) {
562 /*printf("Draw interleaved\n");*/
563 setup_interleaved_attribs(ctx
, vp
, arrays
, max_index
, userSpace
,
566 num_velements
= vp
->num_inputs
;
567 if (num_velements
== 0)
571 /*printf("Draw non-interleaved\n");*/
572 setup_non_interleaved_attribs(ctx
, vp
, arrays
, max_index
,
573 &userSpace
, vbuffer
, velements
);
574 num_vbuffers
= vp
->num_inputs
;
575 num_velements
= vp
->num_inputs
;
581 for (i
= 0; i
< num_vbuffers
; i
++) {
582 printf("buffers[%d].stride = %u\n", i
, vbuffer
[i
].stride
);
583 printf("buffers[%d].max_index = %u\n", i
, vbuffer
[i
].max_index
);
584 printf("buffers[%d].buffer_offset = %u\n", i
, vbuffer
[i
].buffer_offset
);
585 printf("buffers[%d].buffer = %p\n", i
, (void*) vbuffer
[i
].buffer
);
587 for (i
= 0; i
< num_velements
; i
++) {
588 printf("vlements[%d].vbuffer_index = %u\n", i
, velements
[i
].vertex_buffer_index
);
589 printf("vlements[%d].src_offset = %u\n", i
, velements
[i
].src_offset
);
590 printf("vlements[%d].nr_comps = %u\n", i
, velements
[i
].nr_components
);
591 printf("vlements[%d].format = %s\n", i
, pf_name(velements
[i
].src_format
));
596 pipe
->set_vertex_buffers(pipe
, num_vbuffers
, vbuffer
);
597 pipe
->set_vertex_elements(pipe
, num_velements
, velements
);
599 if (num_vbuffers
== 0 || num_velements
== 0)
602 /* do actual drawing */
604 /* indexed primitive */
605 struct gl_buffer_object
*bufobj
= ib
->obj
;
606 struct pipe_buffer
*indexBuf
= NULL
;
607 unsigned indexSize
, indexOffset
, i
;
610 case GL_UNSIGNED_INT
:
613 case GL_UNSIGNED_SHORT
:
616 case GL_UNSIGNED_BYTE
:
624 /* get/create the index buffer object */
625 if (bufobj
&& bufobj
->Name
) {
626 /* elements/indexes are in a real VBO */
627 struct st_buffer_object
*stobj
= st_buffer_object(bufobj
);
628 pipe_buffer_reference(&indexBuf
, stobj
->buffer
);
629 indexOffset
= pointer_to_offset(ib
->ptr
) / indexSize
;
632 /* element/indicies are in user space memory */
633 indexBuf
= pipe_user_buffer_create(pipe
->screen
, (void *) ib
->ptr
,
634 ib
->count
* indexSize
);
639 if (nr_prims
== 1 && pipe
->draw_range_elements
!= NULL
) {
642 /* XXX: exercise temporary path to pass min/max directly
643 * through to driver & draw module. These interfaces still
644 * need a bit of work...
646 setup_edgeflags(ctx
, prims
[i
].mode
,
647 prims
[i
].start
+ indexOffset
, prims
[i
].count
,
648 arrays
[VERT_ATTRIB_EDGEFLAG
]);
650 pipe
->draw_range_elements(pipe
, indexBuf
, indexSize
,
654 prims
[i
].start
+ indexOffset
, prims
[i
].count
);
657 for (i
= 0; i
< nr_prims
; i
++) {
658 setup_edgeflags(ctx
, prims
[i
].mode
,
659 prims
[i
].start
+ indexOffset
, prims
[i
].count
,
660 arrays
[VERT_ATTRIB_EDGEFLAG
]);
662 pipe
->draw_elements(pipe
, indexBuf
, indexSize
,
664 prims
[i
].start
+ indexOffset
, prims
[i
].count
);
668 pipe_buffer_reference(&indexBuf
, NULL
);
673 for (i
= 0; i
< nr_prims
; i
++) {
674 setup_edgeflags(ctx
, prims
[i
].mode
,
675 prims
[i
].start
, prims
[i
].count
,
676 arrays
[VERT_ATTRIB_EDGEFLAG
]);
678 pipe
->draw_arrays(pipe
, prims
[i
].mode
, prims
[i
].start
, prims
[i
].count
);
682 /* unreference buffers (frees wrapped user-space buffer objects) */
683 for (attr
= 0; attr
< num_vbuffers
; attr
++) {
684 pipe_buffer_reference(&vbuffer
[attr
].buffer
, NULL
);
685 assert(!vbuffer
[attr
].buffer
);
690 pipe
->set_vertex_buffers(pipe
, 0, NULL
);
695 void st_init_draw( struct st_context
*st
)
697 GLcontext
*ctx
= st
->ctx
;
699 vbo_set_draw_func(ctx
, st_draw_vbo
);
703 void st_destroy_draw( struct st_context
*st
)