1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 **************************************************************************/
29 * This file implements the st_draw_vbo() function which is called from
30 * Mesa's VBO module. All point/line/triangle rendering is done through
31 * this function whether the user called glBegin/End, glDrawArrays,
32 * glDrawElements, glEvalMesh, or glCalList, etc.
35 * Keith Whitwell <keithw@vmware.com>
39 #include "main/imports.h"
40 #include "main/image.h"
41 #include "main/bufferobj.h"
42 #include "main/macros.h"
46 #include "st_context.h"
48 #include "st_cb_bufferobjects.h"
49 #include "st_cb_xformfb.h"
52 #include "st_program.h"
54 #include "pipe/p_context.h"
55 #include "pipe/p_defines.h"
56 #include "util/u_inlines.h"
57 #include "util/u_format.h"
58 #include "util/u_prim.h"
59 #include "util/u_draw_quad.h"
60 #include "util/u_upload_mgr.h"
61 #include "draw/draw_context.h"
62 #include "cso_cache/cso_context.h"
64 #include "../glsl/ir_uniform.h"
68 * This is very similar to vbo_all_varyings_in_vbos() but we are
69 * only interested in per-vertex data. See bug 38626.
72 all_varyings_in_vbos(const struct gl_client_array
*arrays
[])
76 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++)
77 if (arrays
[i
]->StrideB
&&
78 !arrays
[i
]->InstanceDivisor
&&
79 !_mesa_is_bufferobj(arrays
[i
]->BufferObj
))
87 * Basically, translate Mesa's index buffer information into
88 * a pipe_index_buffer object.
89 * \return TRUE or FALSE for success/failure
92 setup_index_buffer(struct st_context
*st
,
93 const struct _mesa_index_buffer
*ib
,
94 struct pipe_index_buffer
*ibuffer
)
96 struct gl_buffer_object
*bufobj
= ib
->obj
;
98 ibuffer
->index_size
= vbo_sizeof_ib_type(ib
->type
);
100 /* get/create the index buffer object */
101 if (_mesa_is_bufferobj(bufobj
)) {
102 /* indices are in a real VBO */
103 ibuffer
->buffer
= st_buffer_object(bufobj
)->buffer
;
104 ibuffer
->offset
= pointer_to_offset(ib
->ptr
);
106 else if (st
->indexbuf_uploader
) {
107 /* upload indexes from user memory into a real buffer */
108 if (u_upload_data(st
->indexbuf_uploader
, 0,
109 ib
->count
* ibuffer
->index_size
, ib
->ptr
,
110 &ibuffer
->offset
, &ibuffer
->buffer
) != PIPE_OK
) {
114 u_upload_unmap(st
->indexbuf_uploader
);
117 /* indices are in user space memory */
118 ibuffer
->user_buffer
= ib
->ptr
;
121 cso_set_index_buffer(st
->cso_context
, ibuffer
);
127 * Prior to drawing, check that any uniforms referenced by the
128 * current shader have been set. If a uniform has not been set,
132 check_uniforms(struct gl_context
*ctx
)
134 struct gl_shader_program
**shProg
= ctx
->_Shader
->CurrentProgram
;
137 for (j
= 0; j
< 3; j
++) {
140 if (shProg
[j
] == NULL
|| !shProg
[j
]->LinkStatus
)
143 for (i
= 0; i
< shProg
[j
]->NumUserUniformStorage
; i
++) {
144 const struct gl_uniform_storage
*u
= &shProg
[j
]->UniformStorage
[i
];
145 if (!u
->initialized
) {
147 "Using shader with uninitialized uniform: %s",
156 * Translate OpenGL primtive type (GL_POINTS, GL_TRIANGLE_STRIP, etc) to
157 * the corresponding Gallium type.
160 translate_prim(const struct gl_context
*ctx
, unsigned prim
)
162 /* GL prims should match Gallium prims, spot-check a few */
163 STATIC_ASSERT(GL_POINTS
== PIPE_PRIM_POINTS
);
164 STATIC_ASSERT(GL_QUADS
== PIPE_PRIM_QUADS
);
165 STATIC_ASSERT(GL_TRIANGLE_STRIP_ADJACENCY
== PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY
);
172 * This function gets plugged into the VBO module and is called when
173 * we have something to render.
174 * Basically, translate the information into the format expected by gallium.
177 st_draw_vbo(struct gl_context
*ctx
,
178 const struct _mesa_prim
*prims
,
180 const struct _mesa_index_buffer
*ib
,
181 GLboolean index_bounds_valid
,
184 struct gl_transform_feedback_object
*tfb_vertcount
,
185 struct gl_buffer_object
*indirect
)
187 struct st_context
*st
= st_context(ctx
);
188 struct pipe_index_buffer ibuffer
= {0};
189 struct pipe_draw_info info
;
190 const struct gl_client_array
**arrays
= ctx
->Array
._DrawArrays
;
193 /* Mesa core state should have been validated already */
194 assert(ctx
->NewState
== 0x0);
196 /* Validate state. */
197 if (st
->dirty
.st
|| ctx
->NewDriverState
) {
198 st_validate_state(st
);
201 if (MESA_VERBOSE
& VERBOSE_GLSL
) {
205 (void) check_uniforms
;
209 if (st
->vertex_array_out_of_memory
) {
213 util_draw_init_info(&info
);
216 /* Get index bounds for user buffers. */
217 if (!index_bounds_valid
)
218 if (!all_varyings_in_vbos(arrays
))
219 vbo_get_minmax_indices(ctx
, prims
, ib
, &min_index
, &max_index
,
222 if (!setup_index_buffer(st
, ib
, &ibuffer
)) {
223 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBegin/DrawElements/DrawArray");
228 if (min_index
!= ~0 && max_index
!= ~0) {
229 info
.min_index
= min_index
;
230 info
.max_index
= max_index
;
233 /* The VBO module handles restart for the non-indexed GLDrawArrays
234 * so we only set these fields for indexed drawing:
236 info
.primitive_restart
= ctx
->Array
._PrimitiveRestart
;
237 info
.restart_index
= ctx
->Array
.RestartIndex
;
240 /* Transform feedback drawing is always non-indexed. */
241 /* Set info.count_from_stream_output. */
243 st_transform_feedback_draw_init(tfb_vertcount
, &info
);
248 info
.indirect
= st_buffer_object(indirect
)->buffer
;
250 /* Primitive restart is not handled by the VBO module in this case. */
251 info
.primitive_restart
= ctx
->Array
._PrimitiveRestart
;
252 info
.restart_index
= ctx
->Array
.RestartIndex
;
255 /* do actual drawing */
256 for (i
= 0; i
< nr_prims
; i
++) {
257 info
.mode
= translate_prim(ctx
, prims
[i
].mode
);
258 info
.start
= prims
[i
].start
;
259 info
.count
= prims
[i
].count
;
260 info
.start_instance
= prims
[i
].base_instance
;
261 info
.instance_count
= prims
[i
].num_instances
;
262 info
.index_bias
= prims
[i
].basevertex
;
264 info
.min_index
= info
.start
;
265 info
.max_index
= info
.start
+ info
.count
- 1;
267 info
.indirect_offset
= prims
[i
].indirect_offset
;
269 if (ST_DEBUG
& DEBUG_DRAW
) {
270 debug_printf("st/draw: mode %s start %u count %u indexed %d\n",
271 u_prim_name(info
.mode
),
277 if (info
.count_from_stream_output
|| info
.indirect
) {
278 cso_draw_vbo(st
->cso_context
, &info
);
280 else if (info
.primitive_restart
) {
281 /* don't trim, restarts might be inside index list */
282 cso_draw_vbo(st
->cso_context
, &info
);
284 else if (u_trim_pipe_prim(prims
[i
].mode
, &info
.count
)) {
285 cso_draw_vbo(st
->cso_context
, &info
);
289 if (ib
&& st
->indexbuf_uploader
&& !_mesa_is_bufferobj(ib
->obj
)) {
290 pipe_resource_reference(&ibuffer
.buffer
, NULL
);
296 st_init_draw(struct st_context
*st
)
298 struct gl_context
*ctx
= st
->ctx
;
300 vbo_set_draw_func(ctx
, st_draw_vbo
);
302 st
->draw
= draw_create(st
->pipe
); /* for selection/feedback */
304 /* Disable draw options that might convert points/lines to tris, etc.
305 * as that would foul-up feedback/selection mode.
307 draw_wide_line_threshold(st
->draw
, 1000.0f
);
308 draw_wide_point_threshold(st
->draw
, 1000.0f
);
309 draw_enable_line_stipple(st
->draw
, FALSE
);
310 draw_enable_point_sprites(st
->draw
, FALSE
);
315 st_destroy_draw(struct st_context
*st
)
317 draw_destroy(st
->draw
);