1 #include "pipe/p_context.h"
2 #include "pipe/p_state.h"
3 #include "pipe/p_util.h"
5 #include "nv40_context.h"
6 #include "nv40_state.h"
8 #include "nouveau/nouveau_channel.h"
9 #include "nouveau/nouveau_pushbuf.h"
14 nv40_vbo_format_to_hw(enum pipe_format pipe
, unsigned *fmt
, unsigned *ncomp
)
19 case PIPE_FORMAT_R32_FLOAT
:
20 case PIPE_FORMAT_R32G32_FLOAT
:
21 case PIPE_FORMAT_R32G32B32_FLOAT
:
22 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
23 *fmt
= NV40TCL_VTXFMT_TYPE_FLOAT
;
25 case PIPE_FORMAT_R8_UNORM
:
26 case PIPE_FORMAT_R8G8_UNORM
:
27 case PIPE_FORMAT_R8G8B8_UNORM
:
28 case PIPE_FORMAT_R8G8B8A8_UNORM
:
29 *fmt
= NV40TCL_VTXFMT_TYPE_UBYTE
;
32 pf_sprint_name(fs
, pipe
);
33 NOUVEAU_ERR("Unknown format %s\n", fs
);
38 case PIPE_FORMAT_R8_UNORM
:
39 case PIPE_FORMAT_R32_FLOAT
:
42 case PIPE_FORMAT_R8G8_UNORM
:
43 case PIPE_FORMAT_R32G32_FLOAT
:
46 case PIPE_FORMAT_R8G8B8_UNORM
:
47 case PIPE_FORMAT_R32G32B32_FLOAT
:
50 case PIPE_FORMAT_R8G8B8A8_UNORM
:
51 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
55 pf_sprint_name(fs
, pipe
);
56 NOUVEAU_ERR("Unknown format %s\n", fs
);
64 nv40_vbo_set_idxbuf(struct nv40_context
*nv40
, struct pipe_buffer
*ib
,
71 nv40
->idxbuf_format
= 0xdeadbeef;
75 /* No support for 8bit indices, no support at all on 0x4497 chips */
76 if (nv40
->screen
->curie
->grclass
== NV44TCL
|| ib_size
== 1)
81 type
= NV40TCL_IDXBUF_FORMAT_TYPE_U16
;
84 type
= NV40TCL_IDXBUF_FORMAT_TYPE_U32
;
90 if (ib
!= nv40
->idxbuf
||
91 type
!= nv40
->idxbuf_format
) {
92 nv40
->dirty
|= NV40_NEW_ARRAYS
;
94 nv40
->idxbuf_format
= type
;
101 nv40_vbo_static_attrib(struct nv40_context
*nv40
, int attrib
,
102 struct pipe_vertex_element
*ve
,
103 struct pipe_vertex_buffer
*vb
)
105 struct pipe_winsys
*ws
= nv40
->pipe
.winsys
;
106 unsigned type
, ncomp
;
109 if (nv40_vbo_format_to_hw(ve
->src_format
, &type
, &ncomp
))
112 map
= ws
->buffer_map(ws
, vb
->buffer
, PIPE_BUFFER_USAGE_CPU_READ
);
113 map
+= vb
->buffer_offset
+ ve
->src_offset
;
116 case NV40TCL_VTXFMT_TYPE_FLOAT
:
120 BEGIN_RING(curie
, NV40TCL_VTX_ATTR_4F_X(attrib
), 4);
147 ws
->buffer_unmap(ws
, vb
->buffer
);
153 ws
->buffer_unmap(ws
, vb
->buffer
);
157 ws
->buffer_unmap(ws
, vb
->buffer
);
163 nv40_draw_arrays(struct pipe_context
*pipe
, unsigned mode
, unsigned start
,
166 struct nv40_context
*nv40
= nv40_context(pipe
);
169 nv40_vbo_set_idxbuf(nv40
, NULL
, 0);
170 if (FORCE_SWTNL
|| !nv40_state_validate(nv40
)) {
171 return nv40_draw_elements_swtnl(pipe
, NULL
, 0,
174 nv40_state_emit(nv40
);
176 BEGIN_RING(curie
, NV40TCL_BEGIN_END
, 1);
177 OUT_RING (nvgl_primitive(mode
));
181 BEGIN_RING(curie
, NV40TCL_VB_VERTEX_BATCH
, 1);
182 OUT_RING (((nr
- 1) << 24) | start
);
188 unsigned push
= nr
> 2047 ? 2047 : nr
;
192 BEGIN_RING_NI(curie
, NV40TCL_VB_VERTEX_BATCH
, push
);
194 OUT_RING(((0x100 - 1) << 24) | start
);
199 BEGIN_RING(curie
, NV40TCL_BEGIN_END
, 1);
202 pipe
->flush(pipe
, 0);
207 nv40_draw_elements_u08(struct nv40_context
*nv40
, void *ib
,
208 unsigned start
, unsigned count
)
210 uint8_t *elts
= (uint8_t *)ib
+ start
;
214 BEGIN_RING(curie
, NV40TCL_VB_ELEMENT_U32
, 1);
220 push
= MIN2(count
, 2047 * 2);
222 BEGIN_RING_NI(curie
, NV40TCL_VB_ELEMENT_U16
, push
>> 1);
223 for (i
= 0; i
< push
; i
+=2)
224 OUT_RING((elts
[i
+1] << 16) | elts
[i
]);
232 nv40_draw_elements_u16(struct nv40_context
*nv40
, void *ib
,
233 unsigned start
, unsigned count
)
235 uint16_t *elts
= (uint16_t *)ib
+ start
;
239 BEGIN_RING(curie
, NV40TCL_VB_ELEMENT_U32
, 1);
245 push
= MIN2(count
, 2047 * 2);
247 BEGIN_RING_NI(curie
, NV40TCL_VB_ELEMENT_U16
, push
>> 1);
248 for (i
= 0; i
< push
; i
+=2)
249 OUT_RING((elts
[i
+1] << 16) | elts
[i
]);
257 nv40_draw_elements_u32(struct nv40_context
*nv40
, void *ib
,
258 unsigned start
, unsigned count
)
260 uint32_t *elts
= (uint32_t *)ib
+ start
;
264 push
= MIN2(count
, 2047);
266 BEGIN_RING_NI(curie
, NV40TCL_VB_ELEMENT_U32
, push
);
267 OUT_RINGp (elts
, push
);
275 nv40_draw_elements_inline(struct pipe_context
*pipe
,
276 struct pipe_buffer
*ib
, unsigned ib_size
,
277 unsigned mode
, unsigned start
, unsigned count
)
279 struct nv40_context
*nv40
= nv40_context(pipe
);
280 struct pipe_winsys
*ws
= pipe
->winsys
;
283 nv40_state_emit(nv40
);
285 map
= ws
->buffer_map(ws
, ib
, PIPE_BUFFER_USAGE_CPU_READ
);
287 NOUVEAU_ERR("failed mapping ib\n");
291 BEGIN_RING(curie
, NV40TCL_BEGIN_END
, 1);
292 OUT_RING (nvgl_primitive(mode
));
296 nv40_draw_elements_u08(nv40
, map
, start
, count
);
299 nv40_draw_elements_u16(nv40
, map
, start
, count
);
302 nv40_draw_elements_u32(nv40
, map
, start
, count
);
305 NOUVEAU_ERR("invalid idxbuf fmt %d\n", ib_size
);
309 BEGIN_RING(curie
, NV40TCL_BEGIN_END
, 1);
312 ws
->buffer_unmap(ws
, ib
);
318 nv40_draw_elements_vbo(struct pipe_context
*pipe
,
319 unsigned mode
, unsigned start
, unsigned count
)
321 struct nv40_context
*nv40
= nv40_context(pipe
);
324 nv40_state_emit(nv40
);
326 BEGIN_RING(curie
, NV40TCL_BEGIN_END
, 1);
327 OUT_RING (nvgl_primitive(mode
));
331 BEGIN_RING(curie
, NV40TCL_VB_INDEX_BATCH
, 1);
332 OUT_RING (((nr
- 1) << 24) | start
);
338 unsigned push
= nr
> 2047 ? 2047 : nr
;
342 BEGIN_RING_NI(curie
, NV40TCL_VB_INDEX_BATCH
, push
);
344 OUT_RING(((0x100 - 1) << 24) | start
);
349 BEGIN_RING(curie
, NV40TCL_BEGIN_END
, 1);
356 nv40_draw_elements(struct pipe_context
*pipe
,
357 struct pipe_buffer
*indexBuffer
, unsigned indexSize
,
358 unsigned mode
, unsigned start
, unsigned count
)
360 struct nv40_context
*nv40
= nv40_context(pipe
);
363 idxbuf
= nv40_vbo_set_idxbuf(nv40
, indexBuffer
, indexSize
);
364 if (FORCE_SWTNL
|| !nv40_state_validate(nv40
)) {
365 return nv40_draw_elements_swtnl(pipe
, NULL
, 0,
368 nv40_state_emit(nv40
);
371 nv40_draw_elements_vbo(pipe
, mode
, start
, count
);
373 nv40_draw_elements_inline(pipe
, indexBuffer
, indexSize
,
377 pipe
->flush(pipe
, 0);
382 nv40_vbo_validate(struct nv40_context
*nv40
)
384 struct nv40_vertex_program
*vp
= nv40
->vertprog
;
385 struct nouveau_stateobj
*vtxbuf
, *vtxfmt
;
386 struct pipe_buffer
*ib
= nv40
->idxbuf
;
387 unsigned ib_format
= nv40
->idxbuf_format
;
388 unsigned inputs
, hw
, num_hw
;
389 unsigned vb_flags
= NOUVEAU_BO_VRAM
| NOUVEAU_BO_GART
| NOUVEAU_BO_RD
;
392 for (hw
= 0; hw
< 16 && inputs
; hw
++) {
393 if (inputs
& (1 << hw
)) {
395 inputs
&= ~(1 << hw
);
400 vtxbuf
= so_new(20, 18);
401 so_method(vtxbuf
, nv40
->screen
->curie
, NV40TCL_VTXBUF_ADDRESS(0), num_hw
);
402 vtxfmt
= so_new(17, 0);
403 so_method(vtxfmt
, nv40
->screen
->curie
, NV40TCL_VTXFMT(0), num_hw
);
406 for (hw
= 0; hw
< num_hw
; hw
++) {
407 struct pipe_vertex_element
*ve
;
408 struct pipe_vertex_buffer
*vb
;
409 unsigned type
, ncomp
;
411 if (!(inputs
& (1 << hw
))) {
413 so_data(vtxfmt
, NV40TCL_VTXFMT_TYPE_FLOAT
);
417 ve
= &nv40
->vtxelt
[hw
];
418 vb
= &nv40
->vtxbuf
[ve
->vertex_buffer_index
];
420 if (!vb
->pitch
&& nv40_vbo_static_attrib(nv40
, hw
, ve
, vb
)) {
422 so_data(vtxfmt
, NV40TCL_VTXFMT_TYPE_FLOAT
);
426 if (nv40_vbo_format_to_hw(ve
->src_format
, &type
, &ncomp
)) {
427 nv40
->fallback_swtnl
|= NV40_NEW_ARRAYS
;
428 so_ref(NULL
, &vtxbuf
);
429 so_ref(NULL
, &vtxfmt
);
433 so_reloc(vtxbuf
, vb
->buffer
, vb
->buffer_offset
+ ve
->src_offset
,
434 vb_flags
| NOUVEAU_BO_LOW
| NOUVEAU_BO_OR
,
435 0, NV40TCL_VTXBUF_ADDRESS_DMA1
);
436 so_data (vtxfmt
, ((vb
->pitch
<< NV40TCL_VTXFMT_STRIDE_SHIFT
) |
437 (ncomp
<< NV40TCL_VTXFMT_SIZE_SHIFT
) | type
));
441 so_method(vtxbuf
, nv40
->screen
->curie
, NV40TCL_IDXBUF_ADDRESS
, 2);
442 so_reloc (vtxbuf
, ib
, 0, vb_flags
| NOUVEAU_BO_LOW
, 0, 0);
443 so_reloc (vtxbuf
, ib
, ib_format
, vb_flags
| NOUVEAU_BO_OR
,
444 0, NV40TCL_IDXBUF_FORMAT_DMA1
);
447 so_method(vtxbuf
, nv40
->screen
->curie
, 0x1710, 1);
450 so_ref(vtxbuf
, &nv40
->state
.hw
[NV40_STATE_VTXBUF
]);
451 nv40
->state
.dirty
|= (1ULL << NV40_STATE_VTXBUF
);
452 so_ref(vtxfmt
, &nv40
->state
.hw
[NV40_STATE_VTXFMT
]);
453 nv40
->state
.dirty
|= (1ULL << NV40_STATE_VTXFMT
);
457 struct nv40_state_entry nv40_state_vbo
= {
458 .validate
= nv40_vbo_validate
,
460 .pipe
= NV40_NEW_ARRAYS
,