dri/nouveau: Split out array handling to its own file.
[mesa.git] / src / mesa / drivers / dri / nouveau / nouveau_vbo_t.c
1 /*
2 * Copyright (C) 2009-2010 Francisco Jerez.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 */
26
27 #include "nouveau_bufferobj.h"
28 #include "nouveau_util.h"
29
30 #include "main/bufferobj.h"
31 #include "main/image.h"
32
33 /* Arbitrary pushbuf length we can assume we can get with a single
34 * call to WAIT_RING. */
35 #define PUSHBUF_DWORDS 65536
36
37 /* Functions to turn GL arrays or index buffers into nouveau_array
38 * structures. */
39
40 static int
41 get_array_stride(struct gl_context *ctx, const struct gl_client_array *a)
42 {
43 struct nouveau_render_state *render = to_render_state(ctx);
44
45 if (render->mode == VBO && !_mesa_is_bufferobj(a->BufferObj))
46 /* Pack client buffers. */
47 return align(_mesa_sizeof_type(a->Type) * a->Size, 4);
48 else
49 return a->StrideB;
50 }
51
52 static void
53 vbo_init_arrays(struct gl_context *ctx, const struct _mesa_index_buffer *ib,
54 const struct gl_client_array **arrays)
55 {
56 struct nouveau_render_state *render = to_render_state(ctx);
57 GLboolean imm = (render->mode == IMM);
58 int i, attr;
59
60 if (ib)
61 nouveau_init_array(&render->ib, 0, 0, ib->count, ib->type,
62 ib->obj, ib->ptr, GL_TRUE);
63
64 FOR_EACH_BOUND_ATTR(render, i, attr) {
65 const struct gl_client_array *array = arrays[attr];
66
67 nouveau_init_array(&render->attrs[attr], attr,
68 get_array_stride(ctx, array),
69 array->Size, array->Type,
70 array->BufferObj,
71 array->Ptr, imm);
72 }
73 }
74
75 static void
76 vbo_deinit_arrays(struct gl_context *ctx, const struct _mesa_index_buffer *ib,
77 const struct gl_client_array **arrays)
78 {
79 struct nouveau_render_state *render = to_render_state(ctx);
80 int i, attr;
81
82 if (ib)
83 nouveau_cleanup_array(&render->ib);
84
85 FOR_EACH_BOUND_ATTR(render, i, attr) {
86 struct nouveau_array *a = &render->attrs[attr];
87
88 nouveau_deinit_array(a);
89 render->map[i] = -1;
90 }
91
92 render->attr_count = 0;
93 context_bctx(ctx, VERTEX);
94 }
95
96 /* Make some rendering decisions from the GL context. */
97
98 static void
99 vbo_choose_render_mode(struct gl_context *ctx, const struct gl_client_array **arrays)
100 {
101 struct nouveau_render_state *render = to_render_state(ctx);
102 int i;
103
104 render->mode = VBO;
105
106 if (ctx->Light.Enabled) {
107 for (i = 0; i < MAT_ATTRIB_MAX; i++) {
108 if (arrays[VERT_ATTRIB_GENERIC0 + i]->StrideB) {
109 render->mode = IMM;
110 break;
111 }
112 }
113 }
114 }
115
116 static void
117 vbo_emit_attr(struct gl_context *ctx, const struct gl_client_array **arrays,
118 int attr)
119 {
120 struct nouveau_channel *chan = context_chan(ctx);
121 struct nouveau_render_state *render = to_render_state(ctx);
122 const struct gl_client_array *array = arrays[attr];
123 struct nouveau_array *a = &render->attrs[attr];
124 RENDER_LOCALS(ctx);
125
126 if (!array->StrideB) {
127 if (attr >= VERT_ATTRIB_GENERIC0)
128 /* nouveau_update_state takes care of materials. */
129 return;
130
131 /* Constant attribute. */
132 nouveau_init_array(a, attr, array->StrideB, array->Size,
133 array->Type, array->BufferObj, array->Ptr,
134 GL_TRUE);
135 EMIT_IMM(ctx, a, 0);
136 nouveau_deinit_array(a);
137
138 } else {
139 /* Varying attribute. */
140 struct nouveau_attr_info *info = &TAG(vertex_attrs)[attr];
141
142 if (render->mode == VBO) {
143 render->map[info->vbo_index] = attr;
144 render->vertex_size += array->_ElementSize;
145 render->attr_count = MAX2(render->attr_count,
146 info->vbo_index + 1);
147 } else {
148 render->map[render->attr_count++] = attr;
149 render->vertex_size += 4 * info->imm_fields;
150 }
151
152 }
153 }
154
155 #define MAT(a) (VERT_ATTRIB_GENERIC0 + MAT_ATTRIB_##a)
156
157 static void
158 vbo_choose_attrs(struct gl_context *ctx, const struct gl_client_array **arrays)
159 {
160 struct nouveau_render_state *render = to_render_state(ctx);
161 int i;
162
163 /* Reset the vertex size. */
164 render->vertex_size = 0;
165 render->attr_count = 0;
166
167 vbo_emit_attr(ctx, arrays, VERT_ATTRIB_COLOR0);
168 if (ctx->Fog.ColorSumEnabled && !ctx->Light.Enabled)
169 vbo_emit_attr(ctx, arrays, VERT_ATTRIB_COLOR1);
170
171 for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) {
172 if (ctx->Texture._EnabledCoordUnits & (1 << i))
173 vbo_emit_attr(ctx, arrays, VERT_ATTRIB_TEX0 + i);
174 }
175
176 if (ctx->Fog.Enabled && ctx->Fog.FogCoordinateSource == GL_FOG_COORD)
177 vbo_emit_attr(ctx, arrays, VERT_ATTRIB_FOG);
178
179 if (ctx->Light.Enabled ||
180 (ctx->Texture._GenFlags & TEXGEN_NEED_NORMALS))
181 vbo_emit_attr(ctx, arrays, VERT_ATTRIB_NORMAL);
182
183 if (ctx->Light.Enabled) {
184 vbo_emit_attr(ctx, arrays, MAT(FRONT_AMBIENT));
185 vbo_emit_attr(ctx, arrays, MAT(FRONT_DIFFUSE));
186 vbo_emit_attr(ctx, arrays, MAT(FRONT_SPECULAR));
187 vbo_emit_attr(ctx, arrays, MAT(FRONT_SHININESS));
188
189 if (ctx->Light.Model.TwoSide) {
190 vbo_emit_attr(ctx, arrays, MAT(BACK_AMBIENT));
191 vbo_emit_attr(ctx, arrays, MAT(BACK_DIFFUSE));
192 vbo_emit_attr(ctx, arrays, MAT(BACK_SPECULAR));
193 vbo_emit_attr(ctx, arrays, MAT(BACK_SHININESS));
194 }
195 }
196
197 vbo_emit_attr(ctx, arrays, VERT_ATTRIB_POS);
198 }
199
200 static int
201 get_max_client_stride(struct gl_context *ctx, const struct gl_client_array **arrays)
202 {
203 struct nouveau_render_state *render = to_render_state(ctx);
204 int i, attr, s = 0;
205
206 FOR_EACH_BOUND_ATTR(render, i, attr) {
207 const struct gl_client_array *a = arrays[attr];
208
209 if (!_mesa_is_bufferobj(a->BufferObj))
210 s = MAX2(s, get_array_stride(ctx, a));
211 }
212
213 return s;
214 }
215
216 static void
217 TAG(vbo_render_prims)(struct gl_context *ctx, const struct gl_client_array **arrays,
218 const struct _mesa_prim *prims, GLuint nr_prims,
219 const struct _mesa_index_buffer *ib,
220 GLboolean index_bounds_valid,
221 GLuint min_index, GLuint max_index);
222
223 static GLboolean
224 vbo_maybe_split(struct gl_context *ctx, const struct gl_client_array **arrays,
225 const struct _mesa_prim *prims, GLuint nr_prims,
226 const struct _mesa_index_buffer *ib,
227 GLuint min_index, GLuint max_index)
228 {
229 struct nouveau_context *nctx = to_nouveau_context(ctx);
230 struct nouveau_render_state *render = to_render_state(ctx);
231 unsigned pushbuf_avail = PUSHBUF_DWORDS - 2 * (nctx->bo.count +
232 render->attr_count),
233 vert_avail = get_max_vertices(ctx, NULL, pushbuf_avail),
234 idx_avail = get_max_vertices(ctx, ib, pushbuf_avail);
235 int stride;
236
237 /* Try to keep client buffers smaller than the scratch BOs. */
238 if (render->mode == VBO &&
239 (stride = get_max_client_stride(ctx, arrays)))
240 vert_avail = MIN2(vert_avail,
241 NOUVEAU_SCRATCH_SIZE / stride);
242
243 if (max_index - min_index > vert_avail ||
244 (ib && ib->count > idx_avail)) {
245 struct split_limits limits = {
246 .max_verts = vert_avail,
247 .max_indices = idx_avail,
248 .max_vb_size = ~0,
249 };
250
251 vbo_split_prims(ctx, arrays, prims, nr_prims, ib, min_index,
252 max_index, TAG(vbo_render_prims), &limits);
253 return GL_TRUE;
254 }
255
256 return GL_FALSE;
257 }
258
259 /* VBO rendering path. */
260
261 static void
262 vbo_bind_vertices(struct gl_context *ctx, const struct gl_client_array **arrays,
263 GLint basevertex, GLuint min_index, GLuint max_index)
264 {
265 struct nouveau_render_state *render = to_render_state(ctx);
266 int i, attr;
267
268 FOR_EACH_BOUND_ATTR(render, i, attr) {
269 const struct gl_client_array *array = arrays[attr];
270 struct nouveau_array *a = &render->attrs[attr];
271 unsigned delta = (basevertex + min_index)
272 * array->StrideB;
273
274 if (a->bo) {
275 /* Array in a buffer obj. */
276 a->offset = (intptr_t)array->Ptr + delta;
277 } else {
278 int j, n = max_index - min_index + 1;
279 char *sp = (char *)array->Ptr + delta;
280 char *dp = nouveau_get_scratch(
281 ctx, n * a->stride, &a->bo, &a->offset);
282
283 /* Array in client memory, move it to
284 * a scratch buffer obj. */
285 for (j = 0; j < n; j++)
286 memcpy(dp + j * a->stride,
287 sp + j * array->StrideB,
288 a->stride);
289 }
290 }
291
292 TAG(render_bind_vertices)(ctx);
293 }
294
295 static void
296 vbo_draw_vbo(struct gl_context *ctx, const struct gl_client_array **arrays,
297 const struct _mesa_prim *prims, GLuint nr_prims,
298 const struct _mesa_index_buffer *ib, GLuint min_index,
299 GLuint max_index)
300 {
301 struct nouveau_channel *chan = context_chan(ctx);
302 dispatch_t dispatch = get_array_dispatch(&to_render_state(ctx)->ib);
303 int i, delta = -min_index, basevertex = 0;
304 RENDER_LOCALS(ctx);
305
306 TAG(render_set_format)(ctx);
307
308 for (i = 0; i < nr_prims; i++) {
309 unsigned start = prims[i].start,
310 count = prims[i].count;
311
312 if (i == 0 || basevertex != prims[i].basevertex) {
313 basevertex = prims[i].basevertex;
314 vbo_bind_vertices(ctx, arrays, basevertex,
315 min_index, max_index);
316 }
317
318 if (count > get_max_vertices(ctx, ib, AVAIL_RING(chan)))
319 WAIT_RING(chan, PUSHBUF_DWORDS);
320
321 BATCH_BEGIN(nvgl_primitive(prims[i].mode));
322 dispatch(ctx, start, delta, count);
323 BATCH_END();
324 }
325 }
326
327 /* Immediate rendering path. */
328
329 static unsigned
330 extract_id(struct nouveau_array *a, int i, int j)
331 {
332 return j;
333 }
334
335 static void
336 vbo_draw_imm(struct gl_context *ctx, const struct gl_client_array **arrays,
337 const struct _mesa_prim *prims, GLuint nr_prims,
338 const struct _mesa_index_buffer *ib, GLuint min_index,
339 GLuint max_index)
340 {
341 struct nouveau_render_state *render = to_render_state(ctx);
342 struct nouveau_channel *chan = context_chan(ctx);
343 extract_u_t extract = ib ? render->ib.extract_u : extract_id;
344 int i, j, k, attr;
345 RENDER_LOCALS(ctx);
346
347 for (i = 0; i < nr_prims; i++) {
348 unsigned start = prims[i].start,
349 end = start + prims[i].count;
350
351 if (prims[i].count > get_max_vertices(ctx, ib,
352 AVAIL_RING(chan)))
353 WAIT_RING(chan, PUSHBUF_DWORDS);
354
355 BATCH_BEGIN(nvgl_primitive(prims[i].mode));
356
357 for (; start < end; start++) {
358 j = prims[i].basevertex +
359 extract(&render->ib, 0, start);
360
361 FOR_EACH_BOUND_ATTR(render, k, attr)
362 EMIT_IMM(ctx, &render->attrs[attr], j);
363 }
364
365 BATCH_END();
366 }
367 }
368
369 /* draw_prims entry point when we're doing hw-tnl. */
370
371 static void
372 TAG(vbo_render_prims)(struct gl_context *ctx,
373 const struct gl_client_array **arrays,
374 const struct _mesa_prim *prims, GLuint nr_prims,
375 const struct _mesa_index_buffer *ib,
376 GLboolean index_bounds_valid,
377 GLuint min_index, GLuint max_index)
378 {
379 struct nouveau_render_state *render = to_render_state(ctx);
380
381 if (!index_bounds_valid)
382 vbo_get_minmax_index(ctx, prims, ib, &min_index, &max_index);
383
384 vbo_choose_render_mode(ctx, arrays);
385 vbo_choose_attrs(ctx, arrays);
386
387 if (vbo_maybe_split(ctx, arrays, prims, nr_prims, ib, min_index,
388 max_index))
389 return;
390
391 vbo_init_arrays(ctx, ib, arrays);
392
393 if (render->mode == VBO)
394 vbo_draw_vbo(ctx, arrays, prims, nr_prims, ib, min_index,
395 max_index);
396 else
397 vbo_draw_imm(ctx, arrays, prims, nr_prims, ib, min_index,
398 max_index);
399
400 vbo_deinit_arrays(ctx, ib, arrays);
401 }