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"
40 #include "st_context.h"
41 #include "st_cb_bufferobjects.h"
43 #include "st_program.h"
45 #include "pipe/p_context.h"
46 #include "pipe/p_defines.h"
47 #include "pipe/p_winsys.h"
48 #include "pipe/p_inlines.h"
50 #include "draw/draw_private.h"
51 #include "draw/draw_context.h"
54 static GLuint double_types
[4] = {
55 PIPE_FORMAT_R64_FLOAT
,
56 PIPE_FORMAT_R64G64_FLOAT
,
57 PIPE_FORMAT_R64G64B64_FLOAT
,
58 PIPE_FORMAT_R64G64B64A64_FLOAT
61 static GLuint float_types
[4] = {
62 PIPE_FORMAT_R32_FLOAT
,
63 PIPE_FORMAT_R32G32_FLOAT
,
64 PIPE_FORMAT_R32G32B32_FLOAT
,
65 PIPE_FORMAT_R32G32B32A32_FLOAT
68 static GLuint uint_types_norm
[4] = {
69 PIPE_FORMAT_R32_UNORM
,
70 PIPE_FORMAT_R32G32_UNORM
,
71 PIPE_FORMAT_R32G32B32_UNORM
,
72 PIPE_FORMAT_R32G32B32A32_UNORM
75 static GLuint uint_types_scale
[4] = {
76 PIPE_FORMAT_R32_USCALED
,
77 PIPE_FORMAT_R32G32_USCALED
,
78 PIPE_FORMAT_R32G32B32_USCALED
,
79 PIPE_FORMAT_R32G32B32A32_USCALED
82 static GLuint int_types_norm
[4] = {
83 PIPE_FORMAT_R32_SNORM
,
84 PIPE_FORMAT_R32G32_SNORM
,
85 PIPE_FORMAT_R32G32B32_SNORM
,
86 PIPE_FORMAT_R32G32B32A32_SNORM
89 static GLuint int_types_scale
[4] = {
90 PIPE_FORMAT_R32_SSCALED
,
91 PIPE_FORMAT_R32G32_SSCALED
,
92 PIPE_FORMAT_R32G32B32_SSCALED
,
93 PIPE_FORMAT_R32G32B32A32_SSCALED
96 static GLuint ushort_types_norm
[4] = {
97 PIPE_FORMAT_R16_UNORM
,
98 PIPE_FORMAT_R16G16_UNORM
,
99 PIPE_FORMAT_R16G16B16_UNORM
,
100 PIPE_FORMAT_R16G16B16A16_UNORM
103 static GLuint ushort_types_scale
[4] = {
104 PIPE_FORMAT_R16_USCALED
,
105 PIPE_FORMAT_R16G16_USCALED
,
106 PIPE_FORMAT_R16G16B16_USCALED
,
107 PIPE_FORMAT_R16G16B16A16_USCALED
110 static GLuint short_types_norm
[4] = {
111 PIPE_FORMAT_R16_SNORM
,
112 PIPE_FORMAT_R16G16_SNORM
,
113 PIPE_FORMAT_R16G16B16_SNORM
,
114 PIPE_FORMAT_R16G16B16A16_SNORM
117 static GLuint short_types_scale
[4] = {
118 PIPE_FORMAT_R16_SSCALED
,
119 PIPE_FORMAT_R16G16_SSCALED
,
120 PIPE_FORMAT_R16G16B16_SSCALED
,
121 PIPE_FORMAT_R16G16B16A16_SSCALED
124 static GLuint ubyte_types_norm
[4] = {
125 PIPE_FORMAT_R8_UNORM
,
126 PIPE_FORMAT_R8G8_UNORM
,
127 PIPE_FORMAT_R8G8B8_UNORM
,
128 PIPE_FORMAT_R8G8B8A8_UNORM
131 static GLuint ubyte_types_scale
[4] = {
132 PIPE_FORMAT_R8_USCALED
,
133 PIPE_FORMAT_R8G8_USCALED
,
134 PIPE_FORMAT_R8G8B8_USCALED
,
135 PIPE_FORMAT_R8G8B8A8_USCALED
138 static GLuint byte_types_norm
[4] = {
139 PIPE_FORMAT_R8_SNORM
,
140 PIPE_FORMAT_R8G8_SNORM
,
141 PIPE_FORMAT_R8G8B8_SNORM
,
142 PIPE_FORMAT_R8G8B8A8_SNORM
145 static GLuint byte_types_scale
[4] = {
146 PIPE_FORMAT_R8_SSCALED
,
147 PIPE_FORMAT_R8G8_SSCALED
,
148 PIPE_FORMAT_R8G8B8_SSCALED
,
149 PIPE_FORMAT_R8G8B8A8_SSCALED
154 * Return a PIPE_FORMAT_x for the given GL datatype and size.
157 pipe_vertex_format(GLenum type
, GLuint size
, GLboolean normalized
)
159 assert(type
>= GL_BYTE
);
160 assert(type
<= GL_DOUBLE
);
166 case GL_DOUBLE
: return double_types
[size
-1];
167 case GL_FLOAT
: return float_types
[size
-1];
168 case GL_INT
: return int_types_norm
[size
-1];
169 case GL_SHORT
: return short_types_norm
[size
-1];
170 case GL_BYTE
: return byte_types_norm
[size
-1];
171 case GL_UNSIGNED_INT
: return uint_types_norm
[size
-1];
172 case GL_UNSIGNED_SHORT
: return ushort_types_norm
[size
-1];
173 case GL_UNSIGNED_BYTE
: return ubyte_types_norm
[size
-1];
174 default: assert(0); return 0;
179 case GL_DOUBLE
: return double_types
[size
-1];
180 case GL_FLOAT
: return float_types
[size
-1];
181 case GL_INT
: return int_types_scale
[size
-1];
182 case GL_SHORT
: return short_types_scale
[size
-1];
183 case GL_BYTE
: return byte_types_scale
[size
-1];
184 case GL_UNSIGNED_INT
: return uint_types_scale
[size
-1];
185 case GL_UNSIGNED_SHORT
: return ushort_types_scale
[size
-1];
186 case GL_UNSIGNED_BYTE
: return ubyte_types_scale
[size
-1];
187 default: assert(0); return 0;
190 return 0; /* silence compiler warning */
195 * This function gets plugged into the VBO module and is called when
196 * we have something to render.
197 * Basically, translate the information into the format expected by pipe.
200 st_draw_vbo(GLcontext
*ctx
,
201 const struct gl_client_array
**arrays
,
202 const struct _mesa_prim
*prims
,
204 const struct _mesa_index_buffer
*ib
,
208 struct pipe_context
*pipe
= ctx
->st
->pipe
;
209 struct pipe_winsys
*winsys
= pipe
->winsys
;
210 const struct st_vertex_program
*vp
;
211 const struct pipe_shader_state
*vs
;
212 struct pipe_vertex_buffer vbuffer
[PIPE_MAX_SHADER_INPUTS
];
215 /* sanity check for pointer arithmetic below */
216 assert(sizeof(arrays
[0]->Ptr
[0]) == 1);
218 st_validate_state(ctx
->st
);
220 /* must get these after state validation! */
222 vs
= &ctx
->st
->state
.vs
->cso
->state
;
224 /* loop over TGSI shader inputs to determine vertex buffer
227 for (attr
= 0; attr
< vs
->num_inputs
; attr
++) {
228 const GLuint mesaAttr
= vp
->index_to_input
[attr
];
229 struct gl_buffer_object
*bufobj
= arrays
[mesaAttr
]->BufferObj
;
230 struct pipe_vertex_element velement
;
232 if (bufobj
&& bufobj
->Name
) {
233 /* Attribute data is in a VBO.
234 * Recall that for VBOs, the gl_client_array->Ptr field is
235 * really an offset from the start of the VBO, not a pointer.
237 struct st_buffer_object
*stobj
= st_buffer_object(bufobj
);
238 assert(stobj
->buffer
);
240 vbuffer
[attr
].buffer
= NULL
;
241 pipe_buffer_reference(winsys
, &vbuffer
[attr
].buffer
, stobj
->buffer
);
242 vbuffer
[attr
].buffer_offset
= (unsigned) arrays
[0]->Ptr
;/* in bytes */
243 velement
.src_offset
= arrays
[mesaAttr
]->Ptr
- arrays
[0]->Ptr
;
244 assert(velement
.src_offset
<= 2048); /* 11-bit field */
247 /* attribute data is in user-space memory, not a VBO */
250 if (!arrays
[mesaAttr
]->StrideB
) {
251 bytes
= arrays
[mesaAttr
]->Size
252 * _mesa_sizeof_type(arrays
[mesaAttr
]->Type
);
254 bytes
= arrays
[mesaAttr
]->StrideB
* (max_index
+ 1);
259 = winsys
->user_buffer_create(winsys
,
260 (void *) arrays
[mesaAttr
]->Ptr
,
262 vbuffer
[attr
].buffer_offset
= 0;
263 velement
.src_offset
= 0;
266 /* common-case setup */
267 vbuffer
[attr
].pitch
= arrays
[mesaAttr
]->StrideB
; /* in bytes */
268 vbuffer
[attr
].max_index
= max_index
;
269 velement
.vertex_buffer_index
= attr
;
270 velement
.nr_components
= arrays
[mesaAttr
]->Size
;
271 velement
.src_format
= pipe_vertex_format(arrays
[mesaAttr
]->Type
,
272 arrays
[mesaAttr
]->Size
,
273 arrays
[mesaAttr
]->Normalized
);
274 assert(velement
.src_format
);
276 /* tell pipe about this attribute */
277 pipe
->set_vertex_buffer(pipe
, attr
, &vbuffer
[attr
]);
278 pipe
->set_vertex_element(pipe
, attr
, &velement
);
282 /* do actual drawing */
284 /* indexed primitive */
285 struct gl_buffer_object
*bufobj
= ib
->obj
;
286 struct pipe_buffer
*indexBuf
= NULL
;
287 unsigned indexSize
, indexOffset
, i
;
290 case GL_UNSIGNED_INT
:
293 case GL_UNSIGNED_SHORT
:
296 case GL_UNSIGNED_BYTE
:
304 /* get/create the index buffer object */
305 if (bufobj
&& bufobj
->Name
) {
306 /* elements/indexes are in a real VBO */
307 struct st_buffer_object
*stobj
= st_buffer_object(bufobj
);
308 pipe_buffer_reference(winsys
, &indexBuf
, stobj
->buffer
);
309 indexOffset
= (unsigned) ib
->ptr
/ indexSize
;
312 /* element/indicies are in user space memory */
313 indexBuf
= winsys
->user_buffer_create(winsys
,
315 ib
->count
* indexSize
);
320 for (i
= 0; i
< nr_prims
; i
++) {
321 pipe
->draw_elements(pipe
, indexBuf
, indexSize
,
323 prims
[i
].start
+ indexOffset
, prims
[i
].count
);
326 pipe_buffer_reference(winsys
, &indexBuf
, NULL
);
331 for (i
= 0; i
< nr_prims
; i
++) {
332 pipe
->draw_arrays(pipe
, prims
[i
].mode
, prims
[i
].start
, prims
[i
].count
);
336 /* unreference buffers (frees wrapped user-space buffer objects) */
337 for (attr
= 0; attr
< vs
->num_inputs
; attr
++) {
338 pipe_buffer_reference(winsys
, &vbuffer
[attr
].buffer
, NULL
);
339 assert(!vbuffer
[attr
].buffer
);
340 pipe
->set_vertex_buffer(pipe
, attr
, &vbuffer
[attr
]);
347 * Utility function for drawing simple primitives (such as quads for
348 * glClear and glDrawPixels). Coordinates are in screen space.
349 * \param mode one of PIPE_PRIM_x
350 * \param numVertex number of vertices
351 * \param verts vertex data (all attributes are float[4])
352 * \param numAttribs number of attributes per vertex
355 st_draw_vertices(GLcontext
*ctx
, unsigned prim
,
356 unsigned numVertex
, float *verts
,
358 GLboolean inClipCoords
)
360 const float width
= ctx
->DrawBuffer
->Width
;
361 const float height
= ctx
->DrawBuffer
->Height
;
362 const unsigned vertex_bytes
= numVertex
* numAttribs
* 4 * sizeof(float);
363 struct pipe_context
*pipe
= ctx
->st
->pipe
;
364 struct pipe_buffer
*vbuf
;
365 struct pipe_vertex_buffer vbuffer
;
366 struct pipe_vertex_element velement
;
369 assert(numAttribs
> 0);
372 /* convert to clip coords */
373 for (i
= 0; i
< numVertex
; i
++) {
374 float x
= verts
[i
* numAttribs
* 4 + 0];
375 float y
= verts
[i
* numAttribs
* 4 + 1];
376 x
= x
/ width
* 2.0 - 1.0;
377 y
= y
/ height
* 2.0 - 1.0;
378 verts
[i
* numAttribs
* 4 + 0] = x
;
379 verts
[i
* numAttribs
* 4 + 1] = y
;
383 /* XXX create one-time */
384 vbuf
= pipe
->winsys
->buffer_create(pipe
->winsys
, 32,
385 PIPE_BUFFER_USAGE_VERTEX
, vertex_bytes
);
388 memcpy(pipe
->winsys
->buffer_map(pipe
->winsys
, vbuf
,
389 PIPE_BUFFER_USAGE_CPU_WRITE
),
390 verts
, vertex_bytes
);
391 pipe
->winsys
->buffer_unmap(pipe
->winsys
, vbuf
);
393 /* tell pipe about the vertex buffer */
394 vbuffer
.buffer
= vbuf
;
395 vbuffer
.pitch
= numAttribs
* 4 * sizeof(float); /* vertex size */
396 vbuffer
.buffer_offset
= 0;
397 pipe
->set_vertex_buffer(pipe
, 0, &vbuffer
);
399 /* tell pipe about the vertex attributes */
400 for (i
= 0; i
< numAttribs
; i
++) {
401 velement
.src_offset
= i
* 4 * sizeof(GLfloat
);
402 velement
.vertex_buffer_index
= 0;
403 velement
.src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
404 velement
.nr_components
= 4;
405 pipe
->set_vertex_element(pipe
, i
, &velement
);
409 pipe
->draw_arrays(pipe
, prim
, 0, numVertex
);
411 /* XXX: do one-time */
412 pipe_buffer_reference(pipe
->winsys
, &vbuf
, NULL
);
417 * Set the (private) draw module's post-transformed vertex format when in
418 * GL_SELECT or GL_FEEDBACK mode or for glRasterPos.
421 set_feedback_vertex_format(GLcontext
*ctx
)
424 struct st_context
*st
= ctx
->st
;
425 struct vertex_info vinfo
;
428 memset(&vinfo
, 0, sizeof(vinfo
));
430 if (ctx
->RenderMode
== GL_SELECT
) {
431 assert(ctx
->RenderMode
== GL_SELECT
);
432 vinfo
.num_attribs
= 1;
433 vinfo
.format
[0] = FORMAT_4F
;
434 vinfo
.interp_mode
[0] = INTERP_LINEAR
;
437 /* GL_FEEDBACK, or glRasterPos */
438 /* emit all attribs (pos, color, texcoord) as GLfloat[4] */
439 vinfo
.num_attribs
= st
->state
.vs
->cso
->state
.num_outputs
;
440 for (i
= 0; i
< vinfo
.num_attribs
; i
++) {
441 vinfo
.format
[i
] = FORMAT_4F
;
442 vinfo
.interp_mode
[i
] = INTERP_LINEAR
;
446 draw_set_vertex_info(st
->draw
, &vinfo
);
452 * Called by VBO to draw arrays when in selection or feedback mode and
453 * to implement glRasterPos.
454 * This is very much like the normal draw_vbo() function above.
455 * Look at code refactoring some day.
456 * Might move this into the failover module some day.
459 st_feedback_draw_vbo(GLcontext
*ctx
,
460 const struct gl_client_array
**arrays
,
461 const struct _mesa_prim
*prims
,
463 const struct _mesa_index_buffer
*ib
,
467 struct st_context
*st
= ctx
->st
;
468 struct pipe_context
*pipe
= st
->pipe
;
469 struct draw_context
*draw
= st
->draw
;
470 struct pipe_winsys
*winsys
= pipe
->winsys
;
471 const struct st_vertex_program
*vp
;
472 const struct pipe_shader_state
*vs
;
473 struct pipe_buffer
*index_buffer_handle
= 0;
474 struct pipe_vertex_buffer vbuffer
[PIPE_MAX_SHADER_INPUTS
];
476 ubyte
*mapped_constants
;
480 st_validate_state(ctx
->st
);
482 /* must get these after state validation! */
484 vs
= &ctx
->st
->state
.vs
->cso
->state
;
486 if (!st
->state
.vs
->draw_shader
) {
487 st
->state
.vs
->draw_shader
= draw_create_vertex_shader(draw
, vs
);
491 * Set up the draw module's state.
493 * We'd like to do this less frequently, but the normal state-update
494 * code sends state updates to the pipe, not to our private draw module.
497 draw_set_viewport_state(draw
, &st
->state
.viewport
);
498 draw_set_clip_state(draw
, &st
->state
.clip
);
499 draw_set_rasterizer_state(draw
, &st
->state
.rasterizer
->state
);
500 draw_bind_vertex_shader(draw
, st
->state
.vs
->draw_shader
);
501 set_feedback_vertex_format(ctx
);
503 /* loop over TGSI shader inputs to determine vertex buffer
506 for (attr
= 0; attr
< vs
->num_inputs
; attr
++) {
507 const GLuint mesaAttr
= vp
->index_to_input
[attr
];
508 struct gl_buffer_object
*bufobj
= arrays
[mesaAttr
]->BufferObj
;
509 struct pipe_vertex_element velement
;
512 if (bufobj
&& bufobj
->Name
) {
513 /* Attribute data is in a VBO.
514 * Recall that for VBOs, the gl_client_array->Ptr field is
515 * really an offset from the start of the VBO, not a pointer.
517 struct st_buffer_object
*stobj
= st_buffer_object(bufobj
);
518 assert(stobj
->buffer
);
520 vbuffer
[attr
].buffer
= NULL
;
521 pipe_buffer_reference(winsys
, &vbuffer
[attr
].buffer
, stobj
->buffer
);
522 vbuffer
[attr
].buffer_offset
= (unsigned) arrays
[0]->Ptr
;/* in bytes */
523 velement
.src_offset
= arrays
[mesaAttr
]->Ptr
- arrays
[0]->Ptr
;
526 /* attribute data is in user-space memory, not a VBO */
527 uint bytes
= (arrays
[mesaAttr
]->Size
528 * _mesa_sizeof_type(arrays
[mesaAttr
]->Type
)
533 = winsys
->user_buffer_create(winsys
,
534 (void *) arrays
[mesaAttr
]->Ptr
,
536 vbuffer
[attr
].buffer_offset
= 0;
537 velement
.src_offset
= 0;
540 /* common-case setup */
541 vbuffer
[attr
].pitch
= arrays
[mesaAttr
]->StrideB
; /* in bytes */
542 vbuffer
[attr
].max_index
= max_index
;
543 velement
.vertex_buffer_index
= attr
;
544 velement
.nr_components
= arrays
[mesaAttr
]->Size
;
545 velement
.src_format
= pipe_vertex_format(arrays
[mesaAttr
]->Type
,
546 arrays
[mesaAttr
]->Size
,
547 arrays
[mesaAttr
]->Normalized
);
548 assert(velement
.src_format
);
550 /* tell draw about this attribute */
551 draw_set_vertex_buffer(draw
, attr
, &vbuffer
[attr
]);
552 draw_set_vertex_element(draw
, attr
, &velement
);
554 /* map the attrib buffer */
555 map
= pipe
->winsys
->buffer_map(pipe
->winsys
,
556 vbuffer
[attr
].buffer
,
557 PIPE_BUFFER_USAGE_CPU_READ
);
558 draw_set_mapped_vertex_buffer(draw
, attr
, map
);
563 struct gl_buffer_object
*bufobj
= ib
->obj
;
564 struct st_buffer_object
*stobj
= st_buffer_object(bufobj
);
565 index_buffer_handle
= stobj
->buffer
;
569 case GL_UNSIGNED_INT
:
572 case GL_UNSIGNED_SHORT
:
580 map
= pipe
->winsys
->buffer_map(pipe
->winsys
,
582 PIPE_BUFFER_USAGE_CPU_READ
);
583 draw_set_mapped_element_buffer(draw
, indexSize
, map
);
586 /* no index/element buffer */
587 draw_set_mapped_element_buffer(draw
, 0, NULL
);
591 /* map constant buffers */
592 mapped_constants
= winsys
->buffer_map(winsys
,
593 st
->state
.constants
[PIPE_SHADER_VERTEX
].buffer
,
594 PIPE_BUFFER_USAGE_CPU_READ
);
595 draw_set_mapped_constant_buffer(st
->draw
, mapped_constants
);
599 for (i
= 0; i
< nr_prims
; i
++) {
600 draw_arrays(draw
, prims
[i
].mode
, prims
[i
].start
, prims
[i
].count
);
604 /* unmap constant buffers */
605 winsys
->buffer_unmap(winsys
, st
->state
.constants
[PIPE_SHADER_VERTEX
].buffer
);
608 * unmap vertex/index buffers
610 for (i
= 0; i
< PIPE_ATTRIB_MAX
; i
++) {
611 if (draw
->vertex_buffer
[i
].buffer
) {
612 pipe
->winsys
->buffer_unmap(pipe
->winsys
,
613 draw
->vertex_buffer
[i
].buffer
);
614 pipe_buffer_reference(winsys
, &draw
->vertex_buffer
[i
].buffer
, NULL
);
615 draw_set_mapped_vertex_buffer(draw
, i
, NULL
);
619 pipe
->winsys
->buffer_unmap(pipe
->winsys
, index_buffer_handle
);
620 draw_set_mapped_element_buffer(draw
, 0, NULL
);
626 void st_init_draw( struct st_context
*st
)
628 GLcontext
*ctx
= st
->ctx
;
630 vbo_set_draw_func(ctx
, st_draw_vbo
);
634 void st_destroy_draw( struct st_context
*st
)