summaryrefslogtreecommitdiff
path: root/src/mesa/pipe/nv30/nv30_vbo.c
blob: 173a6e8fd7b5c5ea8316cc566be8d8fb4dceec5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include "pipe/p_context.h"
#include "pipe/p_state.h"
#include "pipe/p_util.h"

#include "nv30_context.h"
#include "nv30_state.h"

#include "pipe/nouveau/nouveau_channel.h"
#include "pipe/nouveau/nouveau_pushbuf.h"

static INLINE int
nv30_vbo_ncomp(uint format)
{
	int ncomp = 0;

	if (pf_size_x(format)) ncomp++;
	if (pf_size_y(format)) ncomp++;
	if (pf_size_z(format)) ncomp++;
	if (pf_size_w(format)) ncomp++;

	return ncomp;
}

static INLINE int
nv30_vbo_type(uint format)
{
	switch (pf_type(format)) {
	case PIPE_FORMAT_TYPE_FLOAT:
		return NV34TCL_VERTEX_ARRAY_FORMAT_TYPE_FLOAT;
	case PIPE_FORMAT_TYPE_UNORM:
		return NV34TCL_VERTEX_ARRAY_FORMAT_TYPE_UBYTE;
	default:
		assert(0);
	}
}

static boolean
nv30_vbo_static_attrib(struct nv30_context *nv30, int attrib,
		       struct pipe_vertex_element *ve,
		       struct pipe_vertex_buffer *vb)
{
	struct pipe_winsys *ws = nv30->pipe.winsys;
	int type, ncomp;
	void *map;

	type = nv30_vbo_type(ve->src_format);
	ncomp = nv30_vbo_ncomp(ve->src_format);

	map  = ws->buffer_map(ws, vb->buffer, PIPE_BUFFER_USAGE_CPU_READ);
	map += vb->buffer_offset + ve->src_offset;

	switch (type) {
	case NV34TCL_VERTEX_ARRAY_FORMAT_TYPE_FLOAT:
	{
		float *v = map;

		BEGIN_RING(rankine, NV34TCL_VERTEX_ATTR_4F_X(attrib), 4);
		switch (ncomp) {
		case 4:
			OUT_RINGf(v[0]);
			OUT_RINGf(v[1]);
			OUT_RINGf(v[2]);
			OUT_RINGf(v[3]);
			break;
		case 3:
			OUT_RINGf(v[0]);
			OUT_RINGf(v[1]);
			OUT_RINGf(v[2]);
			OUT_RINGf(1.0);
			break;
		case 2:
			OUT_RINGf(v[0]);
			OUT_RINGf(v[1]);
			OUT_RINGf(0.0);
			OUT_RINGf(1.0);
			break;
		case 1:
			OUT_RINGf(v[0]);
			OUT_RINGf(0.0);
			OUT_RINGf(0.0);
			OUT_RINGf(1.0);
			break;
		default:
			ws->buffer_unmap(ws, vb->buffer);
			return FALSE;
		}
	}
		break;
	default:
		ws->buffer_unmap(ws, vb->buffer);
		return FALSE;
	}

	ws->buffer_unmap(ws, vb->buffer);

	return TRUE;
}

static void
nv30_vbo_arrays_update(struct nv30_context *nv30)
{
	struct nv30_vertex_program *vp = nv30->vertprog.active;
	uint32_t inputs, vtxfmt[16];
	int hw, num_hw;

	nv30->vb_enable = 0;

	inputs = vp->ir;
	for (hw = 0; hw < 16 && inputs; hw++) {
		if (inputs & (1 << hw)) {
			num_hw = hw;
			inputs &= ~(1 << hw);
		}
	}
	num_hw++;

	inputs = vp->ir;
	for (hw = 0; hw < num_hw; hw++) {
		struct pipe_vertex_element *ve;
		struct pipe_vertex_buffer *vb;

		if (!(inputs & (1 << hw))) {
			vtxfmt[hw] = NV34TCL_VERTEX_ARRAY_FORMAT_TYPE_FLOAT;
			continue;
		}

		ve = &nv30->vtxelt[hw];
		vb = &nv30->vtxbuf[ve->vertex_buffer_index];

		if (vb->pitch == 0) {
			vtxfmt[hw] = NV34TCL_VERTEX_ARRAY_FORMAT_TYPE_FLOAT;
			if (nv30_vbo_static_attrib(nv30, hw, ve, vb) == TRUE)
				continue;
		}

		nv30->vb_enable |= (1 << hw);
		nv30->vb[hw].delta = vb->buffer_offset + ve->src_offset;
		nv30->vb[hw].buffer = vb->buffer;

		vtxfmt[hw] = ((vb->pitch << NV34TCL_VERTEX_ARRAY_FORMAT_STRIDE_SHIFT) |
			      (nv30_vbo_ncomp(ve->src_format) <<
			       NV34TCL_VERTEX_ARRAY_FORMAT_SIZE_SHIFT) |
			      nv30_vbo_type(ve->src_format));
	}

	BEGIN_RING(rankine, NV34TCL_VERTEX_ARRAY_FORMAT(0), num_hw);
	OUT_RINGp (vtxfmt, num_hw);
}

static boolean
nv30_vbo_validate_state(struct nv30_context *nv30,
			struct pipe_buffer *ib, unsigned ib_format)
{
	unsigned inputs;

	nv30_emit_hw_state(nv30);

	if (nv30->dirty & NV30_NEW_ARRAYS) {
		nv30_vbo_arrays_update(nv30);
		nv30->dirty &= ~NV30_NEW_ARRAYS;
	}

	inputs = nv30->vb_enable;
	while (inputs) {
		unsigned a = ffs(inputs) - 1;

		inputs &= ~(1 << a);

		BEGIN_RING(rankine, NV34TCL_VERTEX_BUFFER_ADDRESS(a), 1);
		OUT_RELOC (nv30->vb[a].buffer, nv30->vb[a].delta,
			   NOUVEAU_BO_VRAM | NOUVEAU_BO_GART | NOUVEAU_BO_LOW |
			   NOUVEAU_BO_OR | NOUVEAU_BO_RD, 0,
			   NV34TCL_VERTEX_BUFFER_ADDRESS_DMA1);
	}

	if (ib) {
		BEGIN_RING(rankine, NV40TCL_IDXBUF_ADDRESS, 2);
		OUT_RELOCl(ib, 0, NOUVEAU_BO_VRAM | NOUVEAU_BO_GART |
			   NOUVEAU_BO_RD);
		OUT_RELOCd(ib, ib_format, NOUVEAU_BO_VRAM | NOUVEAU_BO_GART |
			   NOUVEAU_BO_RD | NOUVEAU_BO_OR,
			   0, NV40TCL_IDXBUF_FORMAT_DMA1);
	}

	BEGIN_RING(rankine, 0x1710, 1);
	OUT_RING  (0); /* vtx cache flush */

	return TRUE;
}

boolean
nv30_draw_arrays(struct pipe_context *pipe, unsigned mode, unsigned start,
		 unsigned count)
{
	struct nv30_context *nv30 = nv30_context(pipe);
	unsigned nr;

	assert(nv30_vbo_validate_state(nv30, NULL, 0));

	BEGIN_RING(rankine, NV34TCL_VERTEX_BEGIN_END, 1);
	OUT_RING  (nvgl_primitive(mode));

	nr = (count & 0xff);
	if (nr) {
		BEGIN_RING(rankine, NV34TCL_VB_VERTEX_BATCH, 1);
		OUT_RING  (((nr - 1) << 24) | start);
		start += nr;
	}

	nr = count >> 8;
	while (nr) {
		unsigned push = nr > 2047 ? 2047 : nr;

		nr -= push;

		BEGIN_RING_NI(rankine, NV34TCL_VB_VERTEX_BATCH, push);
		while (push--) {
			OUT_RING(((0x100 - 1) << 24) | start);
			start += 0x100;
		}
	}

	BEGIN_RING(rankine, NV34TCL_VERTEX_BEGIN_END, 1);
	OUT_RING  (0);

	pipe->flush(pipe, 0);
	return TRUE;
}

static INLINE void
nv30_draw_elements_u08(struct nv30_context *nv30, void *ib,
		       unsigned start, unsigned count)
{
	uint8_t *elts = (uint8_t *)ib + start;
	int push, i;

	if (count & 1) {
		BEGIN_RING(rankine, NV40TCL_VB_ELEMENT_U32, 1);
		OUT_RING  (elts[0]);
		elts++; count--;
	}

	while (count) {
		push = MIN2(count, 2047 * 2);

		BEGIN_RING_NI(rankine, NV40TCL_VB_ELEMENT_U16, push >> 1);
		for (i = 0; i < push; i+=2)
			OUT_RING((elts[i+1] << 16) | elts[i]);

		count -= push;
		elts  += push;
	}
}

static INLINE void
nv30_draw_elements_u16(struct nv30_context *nv30, void *ib,
		       unsigned start, unsigned count)
{
	uint16_t *elts = (uint16_t *)ib + start;
	int push, i;

	if (count & 1) {
		BEGIN_RING(rankine, NV34TCL_VB_ELEMENT_U32, 1);
		OUT_RING  (elts[0]);
		elts++; count--;
	}

	while (count) {
		push = MIN2(count, 2047 * 2);

		BEGIN_RING_NI(rankine, NV34TCL_VB_ELEMENT_U16, push >> 1);
		for (i = 0; i < push; i+=2)
			OUT_RING((elts[i+1] << 16) | elts[i]);

		count -= push;
		elts  += push;
	}
}

static INLINE void
nv30_draw_elements_u32(struct nv30_context *nv30, void *ib,
		       unsigned start, unsigned count)
{
	uint32_t *elts = (uint32_t *)ib + start;
	int push;

	while (count) {
		push = MIN2(count, 2047);

		BEGIN_RING_NI(rankine, NV34TCL_VB_ELEMENT_U32, push);
		OUT_RINGp    (elts, push);

		count -= push;
		elts  += push;
	}
}

static boolean
nv30_draw_elements_inline(struct pipe_context *pipe,
			  struct pipe_buffer *ib, unsigned ib_size,
			  unsigned mode, unsigned start, unsigned count)
{
	struct nv30_context *nv30 = nv30_context(pipe);
	struct pipe_winsys *ws = pipe->winsys;
	void *map;

	assert(nv30_vbo_validate_state(nv30, NULL, 0));

	map = ws->buffer_map(ws, ib, PIPE_BUFFER_USAGE_CPU_READ);
	if (!ib)
		assert(0);

	BEGIN_RING(rankine, NV34TCL_VERTEX_BEGIN_END, 1);
	OUT_RING  (nvgl_primitive(mode));

	switch (ib_size) {
	case 1:
		nv30_draw_elements_u08(nv30, map, start, count);
		break;
	case 2:
		nv30_draw_elements_u16(nv30, map, start, count);
		break;
	case 4:
		nv30_draw_elements_u32(nv30, map, start, count);
		break;
	default:
		assert(0);
		break;
	}

	BEGIN_RING(rankine, NV34TCL_VERTEX_BEGIN_END, 1);
	OUT_RING  (0);

	ws->buffer_unmap(ws, ib);

	return TRUE;
}

static boolean
nv30_draw_elements_vbo(struct pipe_context *pipe,
		       struct pipe_buffer *ib, unsigned ib_size,
		       unsigned mode, unsigned start, unsigned count)
{
	struct nv30_context *nv30 = nv30_context(pipe);
	unsigned nr, type;

	switch (ib_size) {
	case 2:
		type = NV40TCL_IDXBUF_FORMAT_TYPE_U16;
		break;
	case 4:
		type = NV40TCL_IDXBUF_FORMAT_TYPE_U32;
		break;
	default:
		assert(0);
	}

	assert(nv30_vbo_validate_state(nv30, ib, type));

	BEGIN_RING(rankine, NV34TCL_VERTEX_BEGIN_END, 1);
	OUT_RING  (nvgl_primitive(mode));

	nr = (count & 0xff);
	if (nr) {
		BEGIN_RING(rankine, NV40TCL_VB_INDEX_BATCH, 1);
		OUT_RING  (((nr - 1) << 24) | start);
		start += nr;
	}

	nr = count >> 8;
	while (nr) {
		unsigned push = nr > 2047 ? 2047 : nr;

		nr -= push;

		BEGIN_RING_NI(rankine, NV40TCL_VB_INDEX_BATCH, push);
		while (push--) {
			OUT_RING(((0x100 - 1) << 24) | start);
			start += 0x100;
		}
	}

	BEGIN_RING(rankine, NV34TCL_VERTEX_BEGIN_END, 1);
	OUT_RING  (0);

	return TRUE;
}

boolean
nv30_draw_elements(struct pipe_context *pipe,
		   struct pipe_buffer *indexBuffer, unsigned indexSize,
		   unsigned mode, unsigned start, unsigned count)
{
	if (indexSize != 1) {
		nv30_draw_elements_vbo(pipe, indexBuffer, indexSize,
				       mode, start, count);
	} else {
		nv30_draw_elements_inline(pipe, indexBuffer, indexSize,
					  mode, start, count);
	}

	pipe->flush(pipe, 0);
	return TRUE;
}