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"
37 #include "vbo/vbo_context.h"
41 #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_winsys.h"
50 #include "pipe/draw/draw_private.h"
51 #include "pipe/draw/draw_context.h"
55 * Return a PIPE_FORMAT_x for the given GL datatype and size.
58 pipe_vertex_format(GLenum type
, GLuint size
)
60 static const GLuint float_fmts
[4] = {
61 PIPE_FORMAT_R32_FLOAT
,
62 PIPE_FORMAT_R32G32_FLOAT
,
63 PIPE_FORMAT_R32G32B32_FLOAT
,
64 PIPE_FORMAT_R32G32B32A32_FLOAT
,
66 static const GLuint int_fmts
[4] = {
67 PIPE_FORMAT_R32_SSCALED
,
68 PIPE_FORMAT_R32G32_SSCALED
,
69 PIPE_FORMAT_R32G32B32_SSCALED
,
70 PIPE_FORMAT_R32G32B32A32_SSCALED
,
73 assert(type
>= GL_BYTE
);
74 assert(type
<= GL_DOUBLE
);
80 return float_fmts
[size
- 1];
82 return int_fmts
[size
- 1];
86 return 0; /* silence compiler warning */
91 * The default attribute buffer is basically a copy of the
92 * ctx->Current.Attrib[] array. It's used when the vertex program
93 * references an attribute for which we don't have a VBO/array.
96 create_default_attribs_buffer(struct st_context
*st
)
98 struct pipe_context
*pipe
= st
->pipe
;
99 st
->default_attrib_buffer
= pipe
->winsys
->buffer_create( pipe
->winsys
, 32 );
104 destroy_default_attribs_buffer(struct st_context
*st
)
106 struct pipe_context
*pipe
= st
->pipe
;
107 pipe
->winsys
->buffer_reference(pipe
->winsys
,
108 &st
->default_attrib_buffer
, NULL
);
113 * This function gets plugged into the VBO module and is called when
114 * we have something to render.
115 * Basically, translate the information into the format expected by pipe.
118 st_draw_vbo(GLcontext
*ctx
,
119 const struct gl_client_array
**arrays
,
120 const struct _mesa_prim
*prims
,
122 const struct _mesa_index_buffer
*ib
,
126 struct pipe_context
*pipe
= ctx
->st
->pipe
;
127 struct pipe_winsys
*winsys
= pipe
->winsys
;
128 const struct st_vertex_program
*vp
;
129 const struct pipe_shader_state
*vs
;
130 struct pipe_vertex_buffer vbuffer
[PIPE_MAX_SHADER_INPUTS
];
133 /* sanity check for pointer arithmetic below */
134 assert(sizeof(arrays
[0]->Ptr
[0]) == 1);
136 st_validate_state(ctx
->st
);
138 /* must get these after state validation! */
140 vs
= &ctx
->st
->state
.vs
->state
;
142 /* loop over TGSI shader inputs to determine vertex buffer
145 for (attr
= 0; attr
< vs
->num_inputs
; attr
++) {
146 const GLuint mesaAttr
= vp
->index_to_input
[attr
];
147 struct gl_buffer_object
*bufobj
= arrays
[mesaAttr
]->BufferObj
;
148 struct pipe_vertex_element velement
;
150 if (bufobj
&& bufobj
->Name
) {
151 /* Attribute data is in a VBO.
152 * Recall that for VBOs, the gl_client_array->Ptr field is
153 * really an offset from the start of the VBO, not a pointer.
155 struct st_buffer_object
*stobj
= st_buffer_object(bufobj
);
156 assert(stobj
->buffer
);
158 vbuffer
[attr
].buffer
= NULL
;
159 winsys
->buffer_reference(winsys
, &vbuffer
[attr
].buffer
, stobj
->buffer
);
160 vbuffer
[attr
].buffer_offset
= (unsigned) arrays
[0]->Ptr
;/* in bytes */
161 velement
.src_offset
= arrays
[mesaAttr
]->Ptr
- arrays
[0]->Ptr
;
162 assert(velement
.src_offset
<= 2048); /* 11-bit field */
165 /* attribute data is in user-space memory, not a VBO */
166 uint bytes
= (arrays
[mesaAttr
]->Size
167 * _mesa_sizeof_type(arrays
[mesaAttr
]->Type
)
172 = winsys
->user_buffer_create(winsys
,
173 (void *) arrays
[mesaAttr
]->Ptr
,
175 vbuffer
[attr
].buffer_offset
= 0;
176 velement
.src_offset
= 0;
179 /* common-case setup */
180 vbuffer
[attr
].pitch
= arrays
[mesaAttr
]->StrideB
; /* in bytes */
181 vbuffer
[attr
].max_index
= 0; /* need this? */
182 velement
.vertex_buffer_index
= attr
;
183 velement
.dst_offset
= 0; /* need this? */
184 velement
.src_format
= pipe_vertex_format(arrays
[mesaAttr
]->Type
,
185 arrays
[mesaAttr
]->Size
);
186 assert(velement
.src_format
);
188 /* tell pipe about this attribute */
189 pipe
->set_vertex_buffer(pipe
, attr
, &vbuffer
[attr
]);
190 pipe
->set_vertex_element(pipe
, attr
, &velement
);
194 /* do actual drawing */
196 /* indexed primitive */
197 struct gl_buffer_object
*bufobj
= ib
->obj
;
198 struct pipe_buffer_handle
*indexBuf
= NULL
;
199 unsigned indexSize
, indexOffset
, i
;
202 case GL_UNSIGNED_INT
:
205 case GL_UNSIGNED_SHORT
:
208 case GL_UNSIGNED_BYTE
:
215 /* get/create the index buffer object */
216 if (bufobj
&& bufobj
->Name
) {
217 /* elements/indexes are in a real VBO */
218 struct st_buffer_object
*stobj
= st_buffer_object(bufobj
);
219 winsys
->buffer_reference(winsys
, &indexBuf
, stobj
->buffer
);
220 indexOffset
= (unsigned) ib
->ptr
/ indexSize
;
223 /* element/indicies are in user space memory */
224 indexBuf
= winsys
->user_buffer_create(winsys
,
226 ib
->count
* indexSize
);
231 for (i
= 0; i
< nr_prims
; i
++) {
232 pipe
->draw_elements(pipe
, indexBuf
, indexSize
,
234 prims
[i
].start
+ indexOffset
, prims
[i
].count
);
237 winsys
->buffer_reference(winsys
, &indexBuf
, NULL
);
242 for (i
= 0; i
< nr_prims
; i
++) {
243 pipe
->draw_arrays(pipe
, prims
[i
].mode
, prims
[i
].start
, prims
[i
].count
);
247 /* unreference buffers (frees wrapped user-space buffer objects) */
248 for (attr
= 0; attr
< vs
->num_inputs
; attr
++) {
249 winsys
->buffer_reference(winsys
, &vbuffer
[attr
].buffer
, NULL
);
250 assert(!vbuffer
[attr
].buffer
);
251 pipe
->set_vertex_buffer(pipe
, attr
, &vbuffer
[attr
]);
258 * Utility function for drawing simple primitives (such as quads for
259 * glClear and glDrawPixels). Coordinates are in screen space.
260 * \param mode one of PIPE_PRIM_x
261 * \param numVertex number of vertices
262 * \param verts vertex data (all attributes are float[4])
263 * \param numAttribs number of attributes per vertex
266 st_draw_vertices(GLcontext
*ctx
, unsigned prim
,
267 unsigned numVertex
, float *verts
,
270 const float width
= ctx
->DrawBuffer
->Width
;
271 const float height
= ctx
->DrawBuffer
->Height
;
272 const unsigned vertex_bytes
= numVertex
* numAttribs
* 4 * sizeof(float);
273 struct pipe_context
*pipe
= ctx
->st
->pipe
;
274 struct pipe_buffer_handle
*vbuf
;
275 struct pipe_vertex_buffer vbuffer
;
276 struct pipe_vertex_element velement
;
279 assert(numAttribs
> 0);
281 /* convert to clip coords */
282 for (i
= 0; i
< numVertex
; i
++) {
283 float x
= verts
[i
* numAttribs
* 4 + 0];
284 float y
= verts
[i
* numAttribs
* 4 + 1];
285 x
= x
/ width
* 2.0 - 1.0;
286 y
= y
/ height
* 2.0 - 1.0;
287 verts
[i
* numAttribs
* 4 + 0] = x
;
288 verts
[i
* numAttribs
* 4 + 1] = y
;
291 /* XXX create one-time */
292 vbuf
= pipe
->winsys
->buffer_create(pipe
->winsys
, 32);
293 pipe
->winsys
->buffer_data(pipe
->winsys
, vbuf
,
295 PIPE_BUFFER_USAGE_VERTEX
);
297 /* tell pipe about the vertex buffer */
298 vbuffer
.buffer
= vbuf
;
299 vbuffer
.pitch
= numAttribs
* 4 * sizeof(float); /* vertex size */
300 vbuffer
.buffer_offset
= 0;
301 pipe
->set_vertex_buffer(pipe
, 0, &vbuffer
);
303 /* tell pipe about the vertex attributes */
304 for (i
= 0; i
< numAttribs
; i
++) {
305 velement
.src_offset
= i
* 4 * sizeof(GLfloat
);
306 velement
.vertex_buffer_index
= 0;
307 velement
.src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
308 velement
.dst_offset
= 0;
309 pipe
->set_vertex_element(pipe
, i
, &velement
);
313 pipe
->draw_arrays(pipe
, prim
, 0, numVertex
);
315 /* XXX: do one-time */
316 pipe
->winsys
->buffer_reference(pipe
->winsys
, &vbuf
, NULL
);
321 * Set the (private) draw module's post-transformed vertex format when in
322 * GL_SELECT or GL_FEEDBACK mode or for glRasterPos.
325 set_feedback_vertex_format(GLcontext
*ctx
)
327 struct st_context
*st
= ctx
->st
;
328 struct vertex_info vinfo
;
331 if (ctx
->RenderMode
== GL_SELECT
) {
332 assert(ctx
->RenderMode
== GL_SELECT
);
333 vinfo
.num_attribs
= 1;
334 vinfo
.format
[0] = FORMAT_4F
;
335 vinfo
.interp_mode
[0] = INTERP_NONE
;
338 /* GL_FEEDBACK, or glRasterPos */
339 /* emit all attribs (pos, color, texcoord) as GLfloat[4] */
340 vinfo
.num_attribs
= st
->state
.vs
->state
.num_outputs
;
341 for (i
= 0; i
< vinfo
.num_attribs
; i
++) {
342 vinfo
.format
[i
] = FORMAT_4F
;
343 vinfo
.interp_mode
[i
] = INTERP_LINEAR
;
347 draw_set_vertex_info(st
->draw
, &vinfo
);
352 * Called by VBO to draw arrays when in selection or feedback mode and
353 * to implement glRasterPos.
354 * This is very much like the normal draw_vbo() function above.
355 * Look at code refactoring some day.
356 * Might move this into the failover module some day.
359 st_feedback_draw_vbo(GLcontext
*ctx
,
360 const struct gl_client_array
**arrays
,
361 const struct _mesa_prim
*prims
,
363 const struct _mesa_index_buffer
*ib
,
367 struct st_context
*st
= ctx
->st
;
368 struct pipe_context
*pipe
= st
->pipe
;
369 struct draw_context
*draw
= st
->draw
;
370 struct pipe_winsys
*winsys
= pipe
->winsys
;
371 const struct st_vertex_program
*vp
;
372 const struct pipe_shader_state
*vs
;
373 struct pipe_buffer_handle
*index_buffer_handle
= 0;
374 struct pipe_vertex_buffer vbuffer
[PIPE_MAX_SHADER_INPUTS
];
376 ubyte
*mapped_constants
;
380 st_validate_state(ctx
->st
);
382 /* must get these after state validation! */
384 vs
= &ctx
->st
->state
.vs
->state
;
387 * Set up the draw module's state.
389 * We'd like to do this less frequently, but the normal state-update
390 * code sends state updates to the pipe, not to our private draw module.
393 draw_set_viewport_state(draw
, &st
->state
.viewport
);
394 draw_set_clip_state(draw
, &st
->state
.clip
);
395 draw_set_rasterizer_state(draw
, &st
->state
.rasterizer
->state
);
396 draw_bind_vertex_shader(draw
, st
->state
.vs
->data
);
397 set_feedback_vertex_format(ctx
);
399 /* loop over TGSI shader inputs to determine vertex buffer
402 for (attr
= 0; attr
< vs
->num_inputs
; attr
++) {
403 const GLuint mesaAttr
= vp
->index_to_input
[attr
];
404 struct gl_buffer_object
*bufobj
= arrays
[mesaAttr
]->BufferObj
;
405 struct pipe_vertex_element velement
;
408 if (bufobj
&& bufobj
->Name
) {
409 /* Attribute data is in a VBO.
410 * Recall that for VBOs, the gl_client_array->Ptr field is
411 * really an offset from the start of the VBO, not a pointer.
413 struct st_buffer_object
*stobj
= st_buffer_object(bufobj
);
414 assert(stobj
->buffer
);
416 vbuffer
[attr
].buffer
= NULL
;
417 winsys
->buffer_reference(winsys
, &vbuffer
[attr
].buffer
, stobj
->buffer
);
418 vbuffer
[attr
].buffer_offset
= (unsigned) arrays
[0]->Ptr
;/* in bytes */
419 velement
.src_offset
= arrays
[mesaAttr
]->Ptr
- arrays
[0]->Ptr
;
422 /* attribute data is in user-space memory, not a VBO */
423 uint bytes
= (arrays
[mesaAttr
]->Size
424 * _mesa_sizeof_type(arrays
[mesaAttr
]->Type
)
429 = winsys
->user_buffer_create(winsys
,
430 (void *) arrays
[mesaAttr
]->Ptr
,
432 vbuffer
[attr
].buffer_offset
= 0;
433 velement
.src_offset
= 0;
436 /* common-case setup */
437 vbuffer
[attr
].pitch
= arrays
[mesaAttr
]->StrideB
; /* in bytes */
438 vbuffer
[attr
].max_index
= 0; /* need this? */
439 velement
.vertex_buffer_index
= attr
;
440 velement
.dst_offset
= 0; /* need this? */
441 velement
.src_format
= pipe_vertex_format(arrays
[mesaAttr
]->Type
,
442 arrays
[mesaAttr
]->Size
);
443 assert(velement
.src_format
);
445 /* tell draw about this attribute */
446 draw_set_vertex_buffer(draw
, attr
, &vbuffer
[attr
]);
447 draw_set_vertex_element(draw
, attr
, &velement
);
449 /* map the attrib buffer */
450 map
= pipe
->winsys
->buffer_map(pipe
->winsys
,
451 vbuffer
[attr
].buffer
,
452 PIPE_BUFFER_FLAG_READ
);
453 draw_set_mapped_vertex_buffer(draw
, attr
, map
);
458 struct gl_buffer_object
*bufobj
= ib
->obj
;
459 struct st_buffer_object
*stobj
= st_buffer_object(bufobj
);
460 index_buffer_handle
= stobj
->buffer
;
464 case GL_UNSIGNED_INT
:
467 case GL_UNSIGNED_SHORT
:
474 map
= pipe
->winsys
->buffer_map(pipe
->winsys
,
476 PIPE_BUFFER_FLAG_READ
);
477 draw_set_mapped_element_buffer(draw
, indexSize
, map
);
480 /* no index/element buffer */
481 draw_set_mapped_element_buffer(draw
, 0, NULL
);
485 /* map constant buffers */
486 mapped_constants
= winsys
->buffer_map(winsys
,
487 st
->state
.constants
[PIPE_SHADER_VERTEX
].buffer
,
488 PIPE_BUFFER_FLAG_READ
);
489 draw_set_mapped_constant_buffer(st
->draw
, mapped_constants
);
493 for (i
= 0; i
< nr_prims
; i
++) {
494 draw_arrays(draw
, prims
[i
].mode
, prims
[i
].start
, prims
[i
].count
);
498 /* unmap constant buffers */
499 winsys
->buffer_unmap(winsys
, st
->state
.constants
[PIPE_SHADER_VERTEX
].buffer
);
502 * unmap vertex/index buffers
504 for (i
= 0; i
< PIPE_ATTRIB_MAX
; i
++) {
505 if (draw
->vertex_buffer
[i
].buffer
) {
506 pipe
->winsys
->buffer_unmap(pipe
->winsys
,
507 draw
->vertex_buffer
[i
].buffer
);
508 draw_set_mapped_vertex_buffer(draw
, i
, NULL
);
512 pipe
->winsys
->buffer_unmap(pipe
->winsys
, index_buffer_handle
);
513 draw_set_mapped_element_buffer(draw
, 0, NULL
);
519 void st_init_draw( struct st_context
*st
)
521 GLcontext
*ctx
= st
->ctx
;
522 struct vbo_context
*vbo
= (struct vbo_context
*) ctx
->swtnl_im
;
524 /* actually, not used here, but elsewhere */
525 create_default_attribs_buffer(st
);
528 assert(vbo
->draw_prims
);
529 vbo
->draw_prims
= st_draw_vbo
;
533 void st_destroy_draw( struct st_context
*st
)
535 destroy_default_attribs_buffer(st
);