2 * Copyright 2008 Ben Skeggs
3 * Copyright 2010 Christoph Bumiller
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
20 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 #include "pipe/p_context.h"
25 #include "pipe/p_defines.h"
26 #include "pipe/p_state.h"
27 #include "util/u_inlines.h"
29 #include "nv50_context.h"
30 #include "nv50_transfer.h"
33 nv50_transfer_constbuf(struct nv50_context
*nv50
,
34 struct pipe_resource
*buf
, unsigned size
, unsigned cbi
)
36 struct pipe_context
*pipe
= &nv50
->pipe
;
37 struct pipe_transfer
*transfer
;
38 struct nouveau_channel
*chan
= nv50
->screen
->base
.channel
;
39 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
41 unsigned count
, start
;
43 map
= pipe_buffer_map(pipe
, buf
, PIPE_TRANSFER_READ
, &transfer
);
47 count
= (buf
->width0
+ 3) / 4;
54 /* FIXME: emit relocs for unsuiTed MM */
55 BEGIN_RING(chan
, tesla
, NV50TCL_CB_ADDR
, 1);
56 OUT_RING (chan
, (start
<< 8) | cbi
);
57 BEGIN_RING_NI(chan
, tesla
, NV50TCL_CB_DATA(0), nr
);
58 OUT_RINGp (chan
, map
, nr
);
65 pipe_buffer_unmap(pipe
, buf
, transfer
);
69 nv50_program_validate_data(struct nv50_context
*nv50
, struct nv50_program
*p
)
71 struct nouveau_channel
*chan
= nv50
->screen
->base
.channel
;
72 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
76 uint32_t *data
= p
->immd
;
77 unsigned count
= p
->immd_size
/ 4;
84 BEGIN_RING(chan
, tesla
, NV50TCL_CB_ADDR
, 1);
85 OUT_RING (chan
, (start
<< 8) | NV50_CB_PMISC
);
86 BEGIN_RING_NI(chan
, tesla
, NV50TCL_CB_DATA(0), nr
);
87 OUT_RINGp (chan
, data
, nr
);
95 /* If the state tracker doesn't change the constbuf, and it is first
96 * validated with a program that doesn't use it, this check prevents
97 * it from even being uploaded. */
99 if (p->parm_size == 0)
104 case PIPE_SHADER_VERTEX
:
107 case PIPE_SHADER_FRAGMENT
:
110 case PIPE_SHADER_GEOMETRY
:
118 nv50_transfer_constbuf(nv50
, nv50
->constbuf
[p
->type
], p
->parm_size
, cbi
);
122 nv50_program_validate_code(struct nv50_context
*nv50
, struct nv50_program
*p
)
124 struct nouveau_channel
*chan
= nv50
->screen
->base
.channel
;
125 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
126 struct nouveau_grobj
*eng2d
= nv50
->screen
->eng2d
;
129 unsigned size
= p
->code_size
;
130 uint32_t *data
= p
->code
;
132 assert(p
->translated
);
134 /* TODO: use a single bo (for each type) for shader code */
137 ret
= nouveau_bo_new(chan
->device
, NOUVEAU_BO_VRAM
, 0x100, size
, &p
->bo
);
140 offset
= p
->code_start
= 0;
142 BEGIN_RING(chan
, eng2d
, NV50_2D_DST_FORMAT
, 2);
143 OUT_RING (chan
, NV50_2D_DST_FORMAT_R8_UNORM
);
145 BEGIN_RING(chan
, eng2d
, NV50_2D_DST_PITCH
, 1);
146 OUT_RING (chan
, 0x40000);
147 BEGIN_RING(chan
, eng2d
, NV50_2D_DST_WIDTH
, 2);
148 OUT_RING (chan
, 0x10000);
152 unsigned nr
= size
/ 4;
154 if (AVAIL_RING(chan
) < 32)
157 nr
= MIN2(nr
, AVAIL_RING(chan
) - 18);
163 BEGIN_RING(chan
, eng2d
, NV50_2D_DST_ADDRESS_HIGH
, 2);
164 OUT_RELOCh(chan
, p
->bo
, offset
, NOUVEAU_BO_VRAM
| NOUVEAU_BO_WR
);
165 OUT_RELOCl(chan
, p
->bo
, offset
, NOUVEAU_BO_VRAM
| NOUVEAU_BO_WR
);
166 BEGIN_RING(chan
, eng2d
, NV50_2D_SIFC_BITMAP_ENABLE
, 2);
168 OUT_RING (chan
, NV50_2D_SIFC_FORMAT_R8_UNORM
);
169 BEGIN_RING(chan
, eng2d
, NV50_2D_SIFC_WIDTH
, 10);
170 OUT_RING (chan
, nr
* 4);
181 BEGIN_RING_NI(chan
, eng2d
, NV50_2D_SIFC_DATA
, nr
);
182 OUT_RINGp (chan
, data
, nr
);
189 BEGIN_RING(chan
, tesla
, NV50TCL_CODE_CB_FLUSH
, 1);
194 nv50_vp_update_stateobj(struct nv50_context
*nv50
, struct nv50_program
*p
)
196 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
197 struct nouveau_stateobj
*so
= so_new(5, 7, 2);
199 nv50_program_validate_code(nv50
, p
);
201 so_method(so
, tesla
, NV50TCL_VP_ADDRESS_HIGH
, 2);
202 so_reloc (so
, p
->bo
, 0, NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
|
203 NOUVEAU_BO_HIGH
, 0, 0);
204 so_reloc (so
, p
->bo
, 0, NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
|
205 NOUVEAU_BO_LOW
, 0, 0);
206 so_method(so
, tesla
, NV50TCL_VP_ATTR_EN_0
, 2);
207 so_data (so
, p
->vp
.attrs
[0]);
208 so_data (so
, p
->vp
.attrs
[1]);
209 so_method(so
, tesla
, NV50TCL_VP_REG_ALLOC_RESULT
, 1);
210 so_data (so
, p
->max_out
);
211 so_method(so
, tesla
, NV50TCL_VP_REG_ALLOC_TEMP
, 1);
212 so_data (so
, p
->max_gpr
);
213 so_method(so
, tesla
, NV50TCL_VP_START_ID
, 1);
214 so_data (so
, p
->code_start
);
221 nv50_fp_update_stateobj(struct nv50_context
*nv50
, struct nv50_program
*p
)
223 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
224 struct nouveau_stateobj
*so
= so_new(6, 7, 2);
226 nv50_program_validate_code(nv50
, p
);
228 so_method(so
, tesla
, NV50TCL_FP_ADDRESS_HIGH
, 2);
229 so_reloc (so
, p
->bo
, 0, NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
|
230 NOUVEAU_BO_HIGH
, 0, 0);
231 so_reloc (so
, p
->bo
, 0, NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
|
232 NOUVEAU_BO_LOW
, 0, 0);
233 so_method(so
, tesla
, NV50TCL_FP_REG_ALLOC_TEMP
, 1);
234 so_data (so
, p
->max_gpr
);
235 so_method(so
, tesla
, NV50TCL_FP_RESULT_COUNT
, 1);
236 so_data (so
, p
->max_out
);
237 so_method(so
, tesla
, NV50TCL_FP_CONTROL
, 1);
238 so_data (so
, p
->fp
.flags
[0]);
239 so_method(so
, tesla
, NV50TCL_FP_CTRL_UNK196C
, 1);
240 so_data (so
, p
->fp
.flags
[1]);
241 so_method(so
, tesla
, NV50TCL_FP_START_ID
, 1);
242 so_data (so
, p
->code_start
);
249 nv50_gp_update_stateobj(struct nv50_context
*nv50
, struct nv50_program
*p
)
251 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
252 struct nouveau_stateobj
*so
= so_new(6, 7, 2);
254 nv50_program_validate_code(nv50
, p
);
256 so_method(so
, tesla
, NV50TCL_GP_ADDRESS_HIGH
, 2);
257 so_reloc (so
, p
->bo
, 0, NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
|
258 NOUVEAU_BO_HIGH
, 0, 0);
259 so_reloc (so
, p
->bo
, 0, NOUVEAU_BO_VRAM
| NOUVEAU_BO_RD
|
260 NOUVEAU_BO_LOW
, 0, 0);
261 so_method(so
, tesla
, NV50TCL_GP_REG_ALLOC_TEMP
, 1);
262 so_data (so
, p
->max_gpr
);
263 so_method(so
, tesla
, NV50TCL_GP_REG_ALLOC_RESULT
, 1);
264 so_data (so
, p
->max_out
);
265 so_method(so
, tesla
, NV50TCL_GP_OUTPUT_PRIMITIVE_TYPE
, 1);
266 so_data (so
, p
->gp
.prim_type
);
267 so_method(so
, tesla
, NV50TCL_GP_VERTEX_OUTPUT_COUNT
, 1);
268 so_data (so
, p
->gp
.vert_count
);
269 so_method(so
, tesla
, NV50TCL_GP_START_ID
, 1);
270 so_data (so
, p
->code_start
);
277 nv50_program_validate(struct nv50_program
*p
)
279 p
->translated
= nv50_program_tx(p
);
280 assert(p
->translated
);
281 return p
->translated
;
284 struct nouveau_stateobj
*
285 nv50_vertprog_validate(struct nv50_context
*nv50
)
287 struct nv50_program
*p
= nv50
->vertprog
;
288 struct nouveau_stateobj
*so
= NULL
;
290 if (!p
->translated
) {
291 if (nv50_program_validate(p
))
292 nv50_vp_update_stateobj(nv50
, p
);
297 if (nv50
->dirty
& NV50_NEW_VERTPROG_CB
)
298 nv50_program_validate_data(nv50
, p
);
300 if (!(nv50
->dirty
& NV50_NEW_VERTPROG
))
303 nv50_program_validate_code(nv50
, p
);
309 struct nouveau_stateobj
*
310 nv50_fragprog_validate(struct nv50_context
*nv50
)
312 struct nv50_program
*p
= nv50
->fragprog
;
313 struct nouveau_stateobj
*so
= NULL
;
315 if (!p
->translated
) {
316 if (nv50_program_validate(p
))
317 nv50_fp_update_stateobj(nv50
, p
);
322 if (nv50
->dirty
& NV50_NEW_FRAGPROG_CB
)
323 nv50_program_validate_data(nv50
, p
);
325 if (!(nv50
->dirty
& NV50_NEW_FRAGPROG
))
328 nv50_program_validate_code(nv50
, p
);
334 struct nouveau_stateobj
*
335 nv50_geomprog_validate(struct nv50_context
*nv50
)
337 struct nv50_program
*p
= nv50
->geomprog
;
338 struct nouveau_stateobj
*so
= NULL
;
340 if (!p
->translated
) {
341 if (nv50_program_validate(p
))
342 nv50_gp_update_stateobj(nv50
, p
);
347 if (nv50
->dirty
& NV50_NEW_GEOMPROG_CB
)
348 nv50_program_validate_data(nv50
, p
);
350 if (!(nv50
->dirty
& NV50_NEW_GEOMPROG
))
353 nv50_program_validate_code(nv50
, p
);
359 /* XXX: this might not work correctly in all cases yet: we assume that
360 * an FP generic input that is not written in the VP is gl_PointCoord.
363 nv50_pntc_replace(struct nv50_context
*nv50
, uint32_t pntc
[8], unsigned m
)
365 struct nv50_program
*vp
= nv50
->vertprog
;
366 struct nv50_program
*fp
= nv50
->fragprog
;
369 memset(pntc
, 0, 8 * sizeof(uint32_t));
374 for (i
= 0; i
< fp
->in_nr
; i
++) {
375 unsigned j
, n
= util_bitcount(fp
->in
[i
].mask
);
377 if (fp
->in
[i
].sn
!= TGSI_SEMANTIC_GENERIC
) {
382 for (j
= 0; j
< vp
->out_nr
; ++j
)
383 if (vp
->out
[j
].sn
== fp
->in
[i
].sn
&& vp
->out
[j
].si
== fp
->in
[i
].si
)
386 if (j
< vp
->out_nr
) {
387 uint32_t en
= nv50
->rasterizer
->pipe
.sprite_coord_enable
;
389 if (!(en
& (1 << vp
->out
[j
].si
))) {
395 /* this is either PointCoord or replaced by sprite coords */
396 for (c
= 0; c
< 4; c
++) {
397 if (!(fp
->in
[i
].mask
& (1 << c
)))
399 pntc
[m
/ 8] |= (c
+ 1) << ((m
% 8) * 4);
403 if (nv50
->rasterizer
->pipe
.sprite_coord_mode
== PIPE_SPRITE_COORD_LOWER_LEFT
)
409 nv50_vec4_map(uint32_t *map32
, int mid
, uint32_t lin
[4],
410 struct nv50_varying
*in
, struct nv50_varying
*out
)
413 uint8_t mv
= out
->mask
, mf
= in
->mask
, oid
= out
->hw
;
414 uint8_t *map
= (uint8_t *)map32
;
416 for (c
= 0; c
< 4; ++c
) {
419 lin
[mid
/ 32] |= 1 << (mid
% 32);
436 struct nouveau_stateobj
*
437 nv50_fp_linkage_validate(struct nv50_context
*nv50
)
439 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
440 struct nv50_program
*vp
;
441 struct nv50_program
*fp
= nv50
->fragprog
;
442 struct nouveau_stateobj
*so
;
443 struct nv50_varying dummy
;
446 uint32_t map
[16], lin
[4], pntc
[8];
448 uint32_t interp
= fp
->fp
.interp
;
449 uint32_t colors
= fp
->fp
.colors
;
450 uint32_t clip
= 0x04;
451 uint32_t psiz
= 0x000;
455 if (nv50
->geomprog
) {
457 memset(map
, 0x80, sizeof(map
));
460 memset(map
, 0x40, sizeof(map
));
462 memset(lin
, 0, sizeof(lin
));
465 dummy
.mask
= 0xf; /* map all components of HPOS */
466 m
= nv50_vec4_map(map
, 0, lin
, &dummy
, &vp
->out
[0]);
468 if (vp
->vp
.clpd
< 0x40) {
469 for (c
= 0; c
< vp
->vp
.clpd_nr
; ++c
) {
470 map
[m
/ 4] |= (vp
->vp
.clpd
+ c
) << ((m
% 4) * 8);
473 clip
|= vp
->vp
.clpd_nr
<< 8;
476 colors
|= m
<< 8; /* adjust BFC0 id */
478 /* if light_twoside is active, it seems FFC0_ID == BFC0_ID is bad */
479 if (nv50
->rasterizer
->pipe
.light_twoside
) {
480 for (i
= 0; i
< 2; ++i
)
481 m
= nv50_vec4_map(map
, m
, lin
,
482 &fp
->in
[fp
->vp
.bfc
[i
]],
483 &vp
->out
[vp
->vp
.bfc
[i
]]);
486 colors
+= m
- 4; /* adjust FFC0 id */
487 interp
|= m
<< 8; /* set mid where 'normal' FP inputs start */
490 for (i
= 0; i
< fp
->in_nr
; i
++) {
491 for (n
= 0; n
< vp
->out_nr
; ++n
)
492 if (vp
->out
[n
].sn
== fp
->in
[i
].sn
&&
493 vp
->out
[n
].si
== fp
->in
[i
].si
)
496 m
= nv50_vec4_map(map
, m
, lin
,
497 &fp
->in
[i
], (n
< vp
->out_nr
) ? &vp
->out
[n
] : &dummy
);
500 /* PrimitiveID either is replaced by the system value, or
501 * written by the geometry shader into an output register
503 if (fp
->gp
.primid
< 0x40) {
505 map
[m
/ 4] = (map
[m
/ 4] & ~(0xff << i
)) | (vp
->gp
.primid
<< i
);
509 if (nv50
->rasterizer
->pipe
.point_size_per_vertex
) {
511 map
[m
/ 4] = (map
[m
/ 4] & ~(0xff << i
)) | (vp
->vp
.psiz
<< i
);
512 psiz
= (m
++ << 4) | 1;
515 /* now fill the stateobj (at most 28 so_data) */
516 so
= so_new(10, 54, 0);
520 if (vp
->type
== PIPE_SHADER_GEOMETRY
) {
521 so_method(so
, tesla
, NV50TCL_GP_RESULT_MAP_SIZE
, 1);
523 so_method(so
, tesla
, NV50TCL_GP_RESULT_MAP(0), n
);
524 so_datap (so
, map
, n
);
526 so_method(so
, tesla
, NV50TCL_VP_GP_BUILTIN_ATTR_EN
, 1);
527 so_data (so
, vp
->vp
.attrs
[2]);
529 so_method(so
, tesla
, NV50TCL_MAP_SEMANTIC_4
, 1);
530 so_data (so
, primid
);
532 so_method(so
, tesla
, NV50TCL_VP_RESULT_MAP_SIZE
, 1);
534 so_method(so
, tesla
, NV50TCL_VP_RESULT_MAP(0), n
);
535 so_datap (so
, map
, n
);
538 so_method(so
, tesla
, NV50TCL_MAP_SEMANTIC_0
, 4);
539 so_data (so
, colors
);
541 so_data (so
, sysval
);
544 so_method(so
, tesla
, NV50TCL_FP_INTERPOLANT_CTRL
, 1);
545 so_data (so
, interp
);
547 so_method(so
, tesla
, NV50TCL_NOPERSPECTIVE_BITMAP(0), 4);
548 so_datap (so
, lin
, 4);
550 if (nv50
->rasterizer
->pipe
.point_quad_rasterization
) {
551 so_method(so
, tesla
, NV50TCL_POINT_SPRITE_CTRL
, 1);
553 nv50_pntc_replace(nv50
, pntc
, (interp
>> 8) & 0xff));
555 so_method(so
, tesla
, NV50TCL_POINT_COORD_REPLACE_MAP(0), 8);
556 so_datap (so
, pntc
, 8);
559 so_method(so
, tesla
, NV50TCL_GP_ENABLE
, 1);
560 so_data (so
, (vp
->type
== PIPE_SHADER_GEOMETRY
) ? 1 : 0);
566 nv50_vp_gp_mapping(uint32_t *map32
, int m
,
567 struct nv50_program
*vp
, struct nv50_program
*gp
)
569 uint8_t *map
= (uint8_t *)map32
;
572 for (i
= 0; i
< gp
->in_nr
; ++i
) {
573 uint8_t oid
= 0, mv
= 0, mg
= gp
->in
[i
].mask
;
575 for (j
= 0; j
< vp
->out_nr
; ++j
) {
576 if (vp
->out
[j
].sn
== gp
->in
[i
].sn
&&
577 vp
->out
[j
].si
== gp
->in
[i
].si
) {
578 mv
= vp
->out
[j
].mask
;
584 for (c
= 0; c
< 4; ++c
, mv
>>= 1, mg
>>= 1) {
589 map
[m
++] = (c
== 3) ? 0x41 : 0x40;
596 struct nouveau_stateobj
*
597 nv50_gp_linkage_validate(struct nv50_context
*nv50
)
599 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
600 struct nouveau_stateobj
*so
;
601 struct nv50_program
*vp
= nv50
->vertprog
;
602 struct nv50_program
*gp
= nv50
->geomprog
;
608 memset(map
, 0, sizeof(map
));
610 m
= nv50_vp_gp_mapping(map
, m
, vp
, gp
);
612 so
= so_new(3, 24 - 3, 0);
614 so_method(so
, tesla
, NV50TCL_VP_GP_BUILTIN_ATTR_EN
, 1);
615 so_data (so
, vp
->vp
.attrs
[2] | gp
->vp
.attrs
[2]);
618 so_method(so
, tesla
, NV50TCL_VP_RESULT_MAP_SIZE
, 1);
622 so_method(so
, tesla
, NV50TCL_VP_RESULT_MAP(0), m
);
623 so_datap (so
, map
, m
);