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 "pipe/nouveau/nouveau_channel.h"
9 #include "pipe/nouveau/nouveau_pushbuf.h"
12 nv40_vbo_ncomp(uint format
)
16 if (pf_size_x(format
)) ncomp
++;
17 if (pf_size_y(format
)) ncomp
++;
18 if (pf_size_z(format
)) ncomp
++;
19 if (pf_size_w(format
)) ncomp
++;
25 nv40_vbo_type(uint format
)
27 switch (pf_type(format
)) {
28 case PIPE_FORMAT_TYPE_FLOAT
:
29 return NV40TCL_VTXFMT_TYPE_FLOAT
;
30 case PIPE_FORMAT_TYPE_UNORM
:
31 return NV40TCL_VTXFMT_TYPE_UBYTE
;
38 nv40_vbo_static_attrib(struct nv40_context
*nv40
, int attrib
,
39 struct pipe_vertex_element
*ve
,
40 struct pipe_vertex_buffer
*vb
)
42 struct pipe_winsys
*ws
= nv40
->pipe
.winsys
;
46 type
= nv40_vbo_type(ve
->src_format
);
47 ncomp
= nv40_vbo_ncomp(ve
->src_format
);
49 map
= ws
->buffer_map(ws
, vb
->buffer
, PIPE_BUFFER_USAGE_CPU_READ
);
50 map
+= vb
->buffer_offset
+ ve
->src_offset
;
53 case NV40TCL_VTXFMT_TYPE_FLOAT
:
57 BEGIN_RING(curie
, NV40TCL_VTX_ATTR_4F_X(attrib
), 4);
84 ws
->buffer_unmap(ws
, vb
->buffer
);
90 ws
->buffer_unmap(ws
, vb
->buffer
);
94 ws
->buffer_unmap(ws
, vb
->buffer
);
100 nv40_vbo_arrays_update(struct nv40_context
*nv40
, struct pipe_buffer
*ib
,
103 struct nv40_vertex_program
*vp
= nv40
->vertprog
.active
;
104 struct nouveau_stateobj
*vtxbuf
, *vtxfmt
;
105 unsigned inputs
, hw
, num_hw
;
106 unsigned vb_flags
= NOUVEAU_BO_VRAM
| NOUVEAU_BO_GART
| NOUVEAU_BO_RD
;
109 for (hw
= 0; hw
< 16 && inputs
; hw
++) {
110 if (inputs
& (1 << hw
)) {
112 inputs
&= ~(1 << hw
);
117 vtxbuf
= so_new(20, 18);
118 so_method(vtxbuf
, nv40
->hw
->curie
, NV40TCL_VTXBUF_ADDRESS(0), num_hw
);
119 vtxfmt
= so_new(17, 0);
120 so_method(vtxfmt
, nv40
->hw
->curie
, NV40TCL_VTXFMT(0), num_hw
);
123 for (hw
= 0; hw
< num_hw
; hw
++) {
124 struct pipe_vertex_element
*ve
;
125 struct pipe_vertex_buffer
*vb
;
127 if (!(inputs
& (1 << hw
))) {
129 so_data(vtxfmt
, NV40TCL_VTXFMT_TYPE_FLOAT
);
133 ve
= &nv40
->vtxelt
[hw
];
134 vb
= &nv40
->vtxbuf
[ve
->vertex_buffer_index
];
136 if (!vb
->pitch
&& nv40_vbo_static_attrib(nv40
, hw
, ve
, vb
)) {
138 so_data(vtxfmt
, NV40TCL_VTXFMT_TYPE_FLOAT
);
142 so_reloc(vtxbuf
, vb
->buffer
, vb
->buffer_offset
+ ve
->src_offset
,
143 vb_flags
| NOUVEAU_BO_LOW
| NOUVEAU_BO_OR
,
144 0, NV40TCL_VTXBUF_ADDRESS_DMA1
);
145 so_data (vtxfmt
, ((vb
->pitch
<< NV40TCL_VTXFMT_STRIDE_SHIFT
) |
146 (nv40_vbo_ncomp(ve
->src_format
) <<
147 NV40TCL_VTXFMT_SIZE_SHIFT
) |
148 nv40_vbo_type(ve
->src_format
)));
152 so_method(vtxbuf
, nv40
->hw
->curie
, NV40TCL_IDXBUF_ADDRESS
, 2);
153 so_reloc (vtxbuf
, ib
, 0, vb_flags
| NOUVEAU_BO_LOW
, 0, 0);
154 so_reloc (vtxbuf
, ib
, ib_format
, vb_flags
| NOUVEAU_BO_OR
,
155 0, NV40TCL_IDXBUF_FORMAT_DMA1
);
158 so_emit(nv40
->nvws
, vtxfmt
);
159 so_emit(nv40
->nvws
, vtxbuf
);
160 so_ref (vtxbuf
, &nv40
->so_vtxbuf
);
161 so_ref (NULL
, &vtxbuf
);
162 so_ref (NULL
, &vtxfmt
);
166 nv40_vbo_validate_state(struct nv40_context
*nv40
,
167 struct pipe_buffer
*ib
, unsigned ib_format
)
169 unsigned vdn
= nv40
->dirty
& NV40_NEW_ARRAYS
;
171 nv40_emit_hw_state(nv40
);
173 nv40_vbo_arrays_update(nv40
, ib
, ib_format
);
174 nv40
->dirty
&= ~NV40_NEW_ARRAYS
;
177 so_emit_reloc_markers(nv40
->nvws
, nv40
->so_vtxbuf
);
179 BEGIN_RING(curie
, 0x1710, 1);
180 OUT_RING (0); /* vtx cache flush */
186 nv40_draw_arrays(struct pipe_context
*pipe
, unsigned mode
, unsigned start
,
189 struct nv40_context
*nv40
= nv40_context(pipe
);
192 assert(nv40_vbo_validate_state(nv40
, NULL
, 0));
194 BEGIN_RING(curie
, NV40TCL_BEGIN_END
, 1);
195 OUT_RING (nvgl_primitive(mode
));
199 BEGIN_RING(curie
, NV40TCL_VB_VERTEX_BATCH
, 1);
200 OUT_RING (((nr
- 1) << 24) | start
);
206 unsigned push
= nr
> 2047 ? 2047 : nr
;
210 BEGIN_RING_NI(curie
, NV40TCL_VB_VERTEX_BATCH
, push
);
212 OUT_RING(((0x100 - 1) << 24) | start
);
217 BEGIN_RING(curie
, NV40TCL_BEGIN_END
, 1);
220 pipe
->flush(pipe
, 0);
225 nv40_draw_elements_u08(struct nv40_context
*nv40
, void *ib
,
226 unsigned start
, unsigned count
)
228 uint8_t *elts
= (uint8_t *)ib
+ start
;
232 BEGIN_RING(curie
, NV40TCL_VB_ELEMENT_U32
, 1);
238 push
= MIN2(count
, 2047 * 2);
240 BEGIN_RING_NI(curie
, NV40TCL_VB_ELEMENT_U16
, push
>> 1);
241 for (i
= 0; i
< push
; i
+=2)
242 OUT_RING((elts
[i
+1] << 16) | elts
[i
]);
250 nv40_draw_elements_u16(struct nv40_context
*nv40
, void *ib
,
251 unsigned start
, unsigned count
)
253 uint16_t *elts
= (uint16_t *)ib
+ start
;
257 BEGIN_RING(curie
, NV40TCL_VB_ELEMENT_U32
, 1);
263 push
= MIN2(count
, 2047 * 2);
265 BEGIN_RING_NI(curie
, NV40TCL_VB_ELEMENT_U16
, push
>> 1);
266 for (i
= 0; i
< push
; i
+=2)
267 OUT_RING((elts
[i
+1] << 16) | elts
[i
]);
275 nv40_draw_elements_u32(struct nv40_context
*nv40
, void *ib
,
276 unsigned start
, unsigned count
)
278 uint32_t *elts
= (uint32_t *)ib
+ start
;
282 push
= MIN2(count
, 2047);
284 BEGIN_RING_NI(curie
, NV40TCL_VB_ELEMENT_U32
, push
);
285 OUT_RINGp (elts
, push
);
293 nv40_draw_elements_inline(struct pipe_context
*pipe
,
294 struct pipe_buffer
*ib
, unsigned ib_size
,
295 unsigned mode
, unsigned start
, unsigned count
)
297 struct nv40_context
*nv40
= nv40_context(pipe
);
298 struct pipe_winsys
*ws
= pipe
->winsys
;
301 assert(nv40_vbo_validate_state(nv40
, NULL
, 0));
303 map
= ws
->buffer_map(ws
, ib
, PIPE_BUFFER_USAGE_CPU_READ
);
307 BEGIN_RING(curie
, NV40TCL_BEGIN_END
, 1);
308 OUT_RING (nvgl_primitive(mode
));
312 nv40_draw_elements_u08(nv40
, map
, start
, count
);
315 nv40_draw_elements_u16(nv40
, map
, start
, count
);
318 nv40_draw_elements_u32(nv40
, map
, start
, count
);
325 BEGIN_RING(curie
, NV40TCL_BEGIN_END
, 1);
328 ws
->buffer_unmap(ws
, ib
);
334 nv40_draw_elements_vbo(struct pipe_context
*pipe
,
335 struct pipe_buffer
*ib
, unsigned ib_size
,
336 unsigned mode
, unsigned start
, unsigned count
)
338 struct nv40_context
*nv40
= nv40_context(pipe
);
343 type
= NV40TCL_IDXBUF_FORMAT_TYPE_U16
;
346 type
= NV40TCL_IDXBUF_FORMAT_TYPE_U32
;
352 assert(nv40_vbo_validate_state(nv40
, ib
, type
));
354 BEGIN_RING(curie
, NV40TCL_BEGIN_END
, 1);
355 OUT_RING (nvgl_primitive(mode
));
359 BEGIN_RING(curie
, NV40TCL_VB_INDEX_BATCH
, 1);
360 OUT_RING (((nr
- 1) << 24) | start
);
366 unsigned push
= nr
> 2047 ? 2047 : nr
;
370 BEGIN_RING_NI(curie
, NV40TCL_VB_INDEX_BATCH
, push
);
372 OUT_RING(((0x100 - 1) << 24) | start
);
377 BEGIN_RING(curie
, NV40TCL_BEGIN_END
, 1);
384 nv40_draw_elements(struct pipe_context
*pipe
,
385 struct pipe_buffer
*indexBuffer
, unsigned indexSize
,
386 unsigned mode
, unsigned start
, unsigned count
)
388 struct nv40_context
*nv40
= nv40_context(pipe
);
390 /* 0x4497 doesn't support real index buffers, and there doesn't appear
391 * to be support on any chipset for 8-bit indices.
393 if (nv40
->hw
->curie
->grclass
== NV44TCL
|| indexSize
== 1) {
394 nv40_draw_elements_inline(pipe
, indexBuffer
, indexSize
,
397 nv40_draw_elements_vbo(pipe
, indexBuffer
, indexSize
,
401 pipe
->flush(pipe
, 0);