mesa/es3: Add support for GL_PRIMITIVE_RESTART_FIXED_INDEX
[mesa.git] / src / mesa / state_tracker / st_draw.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /*
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.
33 *
34 * Authors:
35 * Keith Whitwell <keith@tungstengraphics.com>
36 */
37
38
39 #include "main/imports.h"
40 #include "main/image.h"
41 #include "main/bufferobj.h"
42 #include "main/macros.h"
43 #include "main/mfeatures.h"
44
45 #include "vbo/vbo.h"
46
47 #include "st_context.h"
48 #include "st_atom.h"
49 #include "st_cb_bufferobjects.h"
50 #include "st_cb_xformfb.h"
51 #include "st_draw.h"
52 #include "st_program.h"
53
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"
63
64 #include "../glsl/ir_uniform.h"
65
66
67 /**
68 * This is very similar to vbo_all_varyings_in_vbos() but we are
69 * only interested in per-vertex data. See bug 38626.
70 */
71 static GLboolean
72 all_varyings_in_vbos(const struct gl_client_array *arrays[])
73 {
74 GLuint i;
75
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))
80 return GL_FALSE;
81
82 return GL_TRUE;
83 }
84
85
86 static void
87 setup_index_buffer(struct st_context *st,
88 const struct _mesa_index_buffer *ib,
89 struct pipe_index_buffer *ibuffer)
90 {
91 struct gl_buffer_object *bufobj = ib->obj;
92
93 ibuffer->index_size = vbo_sizeof_ib_type(ib->type);
94
95 /* get/create the index buffer object */
96 if (_mesa_is_bufferobj(bufobj)) {
97 /* indices are in a real VBO */
98 ibuffer->buffer = st_buffer_object(bufobj)->buffer;
99 ibuffer->offset = pointer_to_offset(ib->ptr);
100 }
101 else if (st->indexbuf_uploader) {
102 u_upload_data(st->indexbuf_uploader, 0, ib->count * ibuffer->index_size,
103 ib->ptr, &ibuffer->offset, &ibuffer->buffer);
104 u_upload_unmap(st->indexbuf_uploader);
105 }
106 else {
107 /* indices are in user space memory */
108 ibuffer->user_buffer = ib->ptr;
109 }
110
111 cso_set_index_buffer(st->cso_context, ibuffer);
112 }
113
114
115 /**
116 * Prior to drawing, check that any uniforms referenced by the
117 * current shader have been set. If a uniform has not been set,
118 * issue a warning.
119 */
120 static void
121 check_uniforms(struct gl_context *ctx)
122 {
123 struct gl_shader_program *shProg[3] = {
124 ctx->Shader.CurrentVertexProgram,
125 ctx->Shader.CurrentGeometryProgram,
126 ctx->Shader.CurrentFragmentProgram,
127 };
128 unsigned j;
129
130 for (j = 0; j < 3; j++) {
131 unsigned i;
132
133 if (shProg[j] == NULL || !shProg[j]->LinkStatus)
134 continue;
135
136 for (i = 0; i < shProg[j]->NumUserUniformStorage; i++) {
137 const struct gl_uniform_storage *u = &shProg[j]->UniformStorage[i];
138 if (!u->initialized) {
139 _mesa_warning(ctx,
140 "Using shader with uninitialized uniform: %s",
141 u->name);
142 }
143 }
144 }
145 }
146
147
148 /**
149 * Translate OpenGL primtive type (GL_POINTS, GL_TRIANGLE_STRIP, etc) to
150 * the corresponding Gallium type.
151 */
152 static unsigned
153 translate_prim(const struct gl_context *ctx, unsigned prim)
154 {
155 /* GL prims should match Gallium prims, spot-check a few */
156 STATIC_ASSERT(GL_POINTS == PIPE_PRIM_POINTS);
157 STATIC_ASSERT(GL_QUADS == PIPE_PRIM_QUADS);
158 STATIC_ASSERT(GL_TRIANGLE_STRIP_ADJACENCY == PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY);
159
160 /* Avoid quadstrips if it's easy to do so:
161 * Note: it's important to do the correct trimming if we change the
162 * prim type! We do that wherever this function is called.
163 */
164 if (prim == GL_QUAD_STRIP &&
165 ctx->Light.ShadeModel != GL_FLAT &&
166 ctx->Polygon.FrontMode == GL_FILL &&
167 ctx->Polygon.BackMode == GL_FILL)
168 prim = GL_TRIANGLE_STRIP;
169
170 return prim;
171 }
172
173
174 /**
175 * This function gets plugged into the VBO module and is called when
176 * we have something to render.
177 * Basically, translate the information into the format expected by gallium.
178 */
179 void
180 st_draw_vbo(struct gl_context *ctx,
181 const struct _mesa_prim *prims,
182 GLuint nr_prims,
183 const struct _mesa_index_buffer *ib,
184 GLboolean index_bounds_valid,
185 GLuint min_index,
186 GLuint max_index,
187 struct gl_transform_feedback_object *tfb_vertcount)
188 {
189 struct st_context *st = st_context(ctx);
190 struct pipe_index_buffer ibuffer = {0};
191 struct pipe_draw_info info;
192 const struct gl_client_array **arrays = ctx->Array._DrawArrays;
193 unsigned i;
194
195 /* Mesa core state should have been validated already */
196 assert(ctx->NewState == 0x0);
197
198 /* Validate state. */
199 if (st->dirty.st || ctx->NewDriverState) {
200 st_validate_state(st);
201
202 if (st->vertex_array_out_of_memory)
203 return;
204
205 #if 0
206 if (MESA_VERBOSE & VERBOSE_GLSL) {
207 check_uniforms(ctx);
208 }
209 #else
210 (void) check_uniforms;
211 #endif
212 }
213
214 util_draw_init_info(&info);
215 if (ib) {
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,
220 nr_prims);
221
222 setup_index_buffer(st, ib, &ibuffer);
223
224 info.indexed = TRUE;
225 if (min_index != ~0 && max_index != ~0) {
226 info.min_index = min_index;
227 info.max_index = max_index;
228 }
229
230 /* The VBO module handles restart for the non-indexed GLDrawArrays
231 * so we only set these fields for indexed drawing:
232 */
233 info.primitive_restart = ctx->Array._PrimitiveRestart;
234 info.restart_index = ctx->Array._RestartIndex;
235 }
236 else {
237 /* Transform feedback drawing is always non-indexed. */
238 /* Set info.count_from_stream_output. */
239 if (tfb_vertcount) {
240 st_transform_feedback_draw_init(tfb_vertcount, &info);
241 }
242 }
243
244 /* do actual drawing */
245 for (i = 0; i < nr_prims; i++) {
246 info.mode = translate_prim( ctx, prims[i].mode );
247 info.start = prims[i].start;
248 info.count = prims[i].count;
249 info.start_instance = prims[i].base_instance;
250 info.instance_count = prims[i].num_instances;
251 info.index_bias = prims[i].basevertex;
252 if (!ib) {
253 info.min_index = info.start;
254 info.max_index = info.start + info.count - 1;
255 }
256
257 if (info.count_from_stream_output) {
258 cso_draw_vbo(st->cso_context, &info);
259 }
260 else if (info.primitive_restart) {
261 /* don't trim, restarts might be inside index list */
262 cso_draw_vbo(st->cso_context, &info);
263 }
264 else if (u_trim_pipe_prim(info.mode, &info.count))
265 cso_draw_vbo(st->cso_context, &info);
266 }
267
268 if (ib && st->indexbuf_uploader && !_mesa_is_bufferobj(ib->obj)) {
269 pipe_resource_reference(&ibuffer.buffer, NULL);
270 }
271 }
272
273
274 void
275 st_init_draw(struct st_context *st)
276 {
277 struct gl_context *ctx = st->ctx;
278
279 vbo_set_draw_func(ctx, st_draw_vbo);
280
281 st->draw = draw_create(st->pipe); /* for selection/feedback */
282
283 /* Disable draw options that might convert points/lines to tris, etc.
284 * as that would foul-up feedback/selection mode.
285 */
286 draw_wide_line_threshold(st->draw, 1000.0f);
287 draw_wide_point_threshold(st->draw, 1000.0f);
288 draw_enable_line_stipple(st->draw, FALSE);
289 draw_enable_point_sprites(st->draw, FALSE);
290 }
291
292
293 void
294 st_destroy_draw(struct st_context *st)
295 {
296 draw_destroy(st->draw);
297 }