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"
32 nv50_constbufs_validate(struct nv50_context
*nv50
)
34 struct nouveau_pushbuf
*push
= nv50
->base
.pushbuf
;
37 for (s
= 0; s
< 3; ++s
) {
38 struct nv04_resource
*res
;
42 if (s
== PIPE_SHADER_FRAGMENT
)
43 p
= NV50_3D_SET_PROGRAM_CB_PROGRAM_FRAGMENT
;
45 if (s
== PIPE_SHADER_GEOMETRY
)
46 p
= NV50_3D_SET_PROGRAM_CB_PROGRAM_GEOMETRY
;
48 p
= NV50_3D_SET_PROGRAM_CB_PROGRAM_VERTEX
;
50 while (nv50
->constbuf_dirty
[s
]) {
51 struct nouveau_bo
*bo
;
55 i
= ffs(nv50
->constbuf_dirty
[s
]) - 1;
56 nv50
->constbuf_dirty
[s
] &= ~(1 << i
);
58 res
= nv04_resource(nv50
->constbuf
[s
][i
]);
61 BEGIN_NV04(push
, NV50_3D(SET_PROGRAM_CB
), 1);
62 PUSH_DATA (push
, (i
<< 8) | p
| 0);
70 /* always upload GL uniforms through CB DATA */
71 bo
= nv50
->screen
->uniforms
;
72 words
= res
->base
.width0
/ 4;
78 if (!nouveau_resource_mapped_by_gpu(&res
->base
)) {
79 nouveau_buffer_migrate(&nv50
->base
, res
, NOUVEAU_BO_VRAM
);
81 BEGIN_NV04(push
, NV50_3D(CODE_CB_FLUSH
), 1);
84 BEGIN_NV04(push
, NV50_3D(CB_DEF_ADDRESS_HIGH
), 3);
85 PUSH_DATAh(push
, res
->address
);
86 PUSH_DATA (push
, res
->address
);
87 PUSH_DATA (push
, (b
<< 16) | (res
->base
.width0
& 0xffff));
88 BEGIN_NV04(push
, NV50_3D(SET_PROGRAM_CB
), 1);
89 PUSH_DATA (push
, (b
<< 12) | (i
<< 8) | p
| 1);
94 if (bo
!= nv50
->screen
->uniforms
)
95 BCTX_REFN(nv50
->bufctx_3d
, CB(s
, i
), res
, RD
);
100 if (!PUSH_SPACE(push
, 16))
102 nr
= PUSH_AVAIL(push
);
104 nr
= MIN2(MIN2(nr
- 3, words
), NV04_PFIFO_MAX_PACKET_LEN
);
106 BEGIN_NV04(push
, NV50_3D(CB_ADDR
), 1);
107 PUSH_DATA (push
, (start
<< 8) | b
);
108 BEGIN_NI04(push
, NV50_3D(CB_DATA(0)), nr
);
109 PUSH_DATAp(push
, &res
->data
[start
* 4], nr
);
119 nv50_program_validate(struct nv50_context
*nv50
, struct nv50_program
*prog
)
121 if (!prog
->translated
) {
122 prog
->translated
= nv50_program_translate(
123 prog
, nv50
->screen
->base
.device
->chipset
);
124 if (!prog
->translated
)
130 return nv50_program_upload_code(nv50
, prog
);
134 nv50_program_update_context_state(struct nv50_context
*nv50
,
135 struct nv50_program
*prog
, int stage
)
137 const unsigned flags
= NOUVEAU_BO_VRAM
| NOUVEAU_BO_RDWR
;
139 if (prog
&& prog
->uses_lmem
) {
140 if (!nv50
->state
.tls_required
)
141 BCTX_REFN_bo(nv50
->bufctx_3d
, TLS
, flags
, nv50
->screen
->tls_bo
);
142 nv50
->state
.tls_required
|= 1 << stage
;
144 if (nv50
->state
.tls_required
== (1 << stage
))
145 nouveau_bufctx_reset(nv50
->bufctx_3d
, NV50_BIND_TLS
);
146 nv50
->state
.tls_required
&= ~(1 << stage
);
151 nv50_vertprog_validate(struct nv50_context
*nv50
)
153 struct nouveau_pushbuf
*push
= nv50
->base
.pushbuf
;
154 struct nv50_program
*vp
= nv50
->vertprog
;
156 if (!nv50_program_validate(nv50
, vp
))
158 nv50_program_update_context_state(nv50
, vp
, 0);
160 BEGIN_NV04(push
, NV50_3D(VP_ATTR_EN(0)), 2);
161 PUSH_DATA (push
, vp
->vp
.attrs
[0]);
162 PUSH_DATA (push
, vp
->vp
.attrs
[1]);
163 BEGIN_NV04(push
, NV50_3D(VP_REG_ALLOC_RESULT
), 1);
164 PUSH_DATA (push
, vp
->max_out
);
165 BEGIN_NV04(push
, NV50_3D(VP_REG_ALLOC_TEMP
), 1);
166 PUSH_DATA (push
, vp
->max_gpr
);
167 BEGIN_NV04(push
, NV50_3D(VP_START_ID
), 1);
168 PUSH_DATA (push
, vp
->code_base
);
172 nv50_fragprog_validate(struct nv50_context
*nv50
)
174 struct nouveau_pushbuf
*push
= nv50
->base
.pushbuf
;
175 struct nv50_program
*fp
= nv50
->fragprog
;
177 if (!nv50_program_validate(nv50
, fp
))
179 nv50_program_update_context_state(nv50
, fp
, 1);
181 BEGIN_NV04(push
, NV50_3D(FP_REG_ALLOC_TEMP
), 1);
182 PUSH_DATA (push
, fp
->max_gpr
);
183 BEGIN_NV04(push
, NV50_3D(FP_RESULT_COUNT
), 1);
184 PUSH_DATA (push
, fp
->max_out
);
185 BEGIN_NV04(push
, NV50_3D(FP_CONTROL
), 1);
186 PUSH_DATA (push
, fp
->fp
.flags
[0]);
187 BEGIN_NV04(push
, NV50_3D(FP_CTRL_UNK196C
), 1);
188 PUSH_DATA (push
, fp
->fp
.flags
[1]);
189 BEGIN_NV04(push
, NV50_3D(FP_START_ID
), 1);
190 PUSH_DATA (push
, fp
->code_base
);
194 nv50_gmtyprog_validate(struct nv50_context
*nv50
)
196 struct nouveau_pushbuf
*push
= nv50
->base
.pushbuf
;
197 struct nv50_program
*gp
= nv50
->gmtyprog
;
200 BEGIN_NV04(push
, NV50_3D(GP_REG_ALLOC_TEMP
), 1);
201 PUSH_DATA (push
, gp
->max_gpr
);
202 BEGIN_NV04(push
, NV50_3D(GP_REG_ALLOC_RESULT
), 1);
203 PUSH_DATA (push
, gp
->max_out
);
204 BEGIN_NV04(push
, NV50_3D(GP_OUTPUT_PRIMITIVE_TYPE
), 1);
205 PUSH_DATA (push
, gp
->gp
.prim_type
);
206 BEGIN_NV04(push
, NV50_3D(GP_VERTEX_OUTPUT_COUNT
), 1);
207 PUSH_DATA (push
, gp
->gp
.vert_count
);
208 BEGIN_NV04(push
, NV50_3D(GP_START_ID
), 1);
209 PUSH_DATA (push
, gp
->code_base
);
211 nv50_program_update_context_state(nv50
, gp
, 2);
213 /* GP_ENABLE is updated in linkage validation */
217 nv50_sprite_coords_validate(struct nv50_context
*nv50
)
219 struct nouveau_pushbuf
*push
= nv50
->base
.pushbuf
;
220 uint32_t pntc
[8], mode
;
221 struct nv50_program
*fp
= nv50
->fragprog
;
223 unsigned m
= (nv50
->state
.interpolant_ctrl
>> 8) & 0xff;
225 if (!nv50
->rast
->pipe
.point_quad_rasterization
) {
226 if (nv50
->state
.point_sprite
) {
227 BEGIN_NV04(push
, NV50_3D(POINT_COORD_REPLACE_MAP(0)), 8);
228 for (i
= 0; i
< 8; ++i
)
231 nv50
->state
.point_sprite
= FALSE
;
235 nv50
->state
.point_sprite
= TRUE
;
238 memset(pntc
, 0, sizeof(pntc
));
240 for (i
= 0; i
< fp
->in_nr
; i
++) {
241 unsigned n
= util_bitcount(fp
->in
[i
].mask
);
243 if (fp
->in
[i
].sn
!= TGSI_SEMANTIC_GENERIC
) {
247 if (!(nv50
->rast
->pipe
.sprite_coord_enable
& (1 << fp
->in
[i
].si
))) {
252 for (c
= 0; c
< 4; ++c
) {
253 if (fp
->in
[i
].mask
& (1 << c
)) {
254 pntc
[m
/ 8] |= (c
+ 1) << ((m
% 8) * 4);
260 if (nv50
->rast
->pipe
.sprite_coord_mode
== PIPE_SPRITE_COORD_LOWER_LEFT
)
265 BEGIN_NV04(push
, NV50_3D(POINT_SPRITE_CTRL
), 1);
266 PUSH_DATA (push
, mode
);
268 BEGIN_NV04(push
, NV50_3D(POINT_COORD_REPLACE_MAP(0)), 8);
269 PUSH_DATAp(push
, pntc
, 8);
272 /* Validate state derived from shaders and the rasterizer cso. */
274 nv50_validate_derived_rs(struct nv50_context
*nv50
)
276 struct nouveau_pushbuf
*push
= nv50
->base
.pushbuf
;
277 uint32_t color
, psize
;
279 nv50_sprite_coords_validate(nv50
);
281 if (nv50
->dirty
& NV50_NEW_FRAGPROG
)
283 psize
= nv50
->state
.semantic_psize
& ~NV50_3D_SEMANTIC_PTSZ_PTSZ_EN__MASK
;
284 color
= nv50
->state
.semantic_color
& ~NV50_3D_SEMANTIC_COLOR_CLMP_EN
;
286 if (nv50
->rast
->pipe
.clamp_vertex_color
)
287 color
|= NV50_3D_SEMANTIC_COLOR_CLMP_EN
;
289 if (color
!= nv50
->state
.semantic_color
) {
290 nv50
->state
.semantic_color
= color
;
291 BEGIN_NV04(push
, NV50_3D(SEMANTIC_COLOR
), 1);
292 PUSH_DATA (push
, color
);
295 if (nv50
->rast
->pipe
.point_size_per_vertex
)
296 psize
|= NV50_3D_SEMANTIC_PTSZ_PTSZ_EN__MASK
;
298 if (psize
!= nv50
->state
.semantic_psize
) {
299 nv50
->state
.semantic_psize
= psize
;
300 BEGIN_NV04(push
, NV50_3D(SEMANTIC_PTSZ
), 1);
301 PUSH_DATA (push
, psize
);
306 nv50_vec4_map(uint8_t *map
, int mid
, uint32_t lin
[4],
307 struct nv50_varying
*in
, struct nv50_varying
*out
)
310 uint8_t mv
= out
->mask
, mf
= in
->mask
, oid
= out
->hw
;
312 for (c
= 0; c
< 4; ++c
) {
315 lin
[mid
/ 32] |= 1 << (mid
% 32);
333 nv50_fp_linkage_validate(struct nv50_context
*nv50
)
335 struct nouveau_pushbuf
*push
= nv50
->base
.pushbuf
;
336 struct nv50_program
*vp
= nv50
->gmtyprog
? nv50
->gmtyprog
: nv50
->vertprog
;
337 struct nv50_program
*fp
= nv50
->fragprog
;
338 struct nv50_varying dummy
;
341 uint32_t psiz
= 0x000;
342 uint32_t interp
= fp
->fp
.interp
;
343 uint32_t colors
= fp
->fp
.colors
;
347 if (!(nv50
->dirty
& (NV50_NEW_VERTPROG
|
349 NV50_NEW_GMTYPROG
))) {
351 ffc
= (nv50
->state
.semantic_color
& NV50_3D_SEMANTIC_COLOR_FFC0_ID__MASK
);
352 bfc
= (nv50
->state
.semantic_color
& NV50_3D_SEMANTIC_COLOR_BFC0_ID__MASK
)
354 if (nv50
->rast
->pipe
.light_twoside
== ((ffc
== bfc
) ? 0 : 1))
358 memset(lin
, 0x00, sizeof(lin
));
360 /* XXX: in buggy-endian mode, is the first element of map (u32)0x000000xx
361 * or is it the first byte ?
363 memset(map
, nv50
->gmtyprog
? 0x80 : 0x40, sizeof(map
));
365 dummy
.mask
= 0xf; /* map all components of HPOS */
367 m
= nv50_vec4_map(map
, 0, lin
, &dummy
, &vp
->out
[0]);
369 for (c
= 0; c
< vp
->vp
.clpd_nr
; ++c
)
370 map
[m
++] = vp
->vp
.clpd
[c
/ 4] + (c
% 4);
372 colors
|= m
<< 8; /* adjust BFC0 id */
376 /* if light_twoside is active, FFC0_ID == BFC0_ID is invalid */
377 if (nv50
->rast
->pipe
.light_twoside
) {
378 for (i
= 0; i
< 2; ++i
) {
380 if (fp
->vp
.bfc
[i
] >= fp
->in_nr
)
382 m
= nv50_vec4_map(map
, m
, lin
, &fp
->in
[fp
->vp
.bfc
[i
]],
383 (n
< vp
->out_nr
) ? &vp
->out
[n
] : &dummy
);
386 colors
+= m
- 4; /* adjust FFC0 id */
387 interp
|= m
<< 8; /* set map id where 'normal' FP inputs start */
389 for (i
= 0; i
< fp
->in_nr
; ++i
) {
390 for (n
= 0; n
< vp
->out_nr
; ++n
)
391 if (vp
->out
[n
].sn
== fp
->in
[i
].sn
&&
392 vp
->out
[n
].si
== fp
->in
[i
].si
)
394 m
= nv50_vec4_map(map
, m
, lin
,
395 &fp
->in
[i
], (n
< vp
->out_nr
) ? &vp
->out
[n
] : &dummy
);
398 /* PrimitiveID either is replaced by the system value, or
399 * written by the geometry shader into an output register
401 if (fp
->gp
.primid
< 0x80) {
403 map
[m
++] = vp
->gp
.primid
;
406 if (nv50
->rast
->pipe
.point_size_per_vertex
) {
408 map
[m
++] = vp
->vp
.psiz
;
411 if (nv50
->rast
->pipe
.clamp_vertex_color
)
412 colors
|= NV50_3D_SEMANTIC_COLOR_CLMP_EN
;
417 if (unlikely(nv50
->gmtyprog
)) {
418 BEGIN_NV04(push
, NV50_3D(GP_RESULT_MAP_SIZE
), 1);
420 BEGIN_NV04(push
, NV50_3D(GP_RESULT_MAP(0)), n
);
421 PUSH_DATAp(push
, map
, n
);
423 BEGIN_NV04(push
, NV50_3D(VP_GP_BUILTIN_ATTR_EN
), 1);
424 PUSH_DATA (push
, vp
->vp
.attrs
[2]);
426 BEGIN_NV04(push
, NV50_3D(SEMANTIC_PRIM_ID
), 1);
427 PUSH_DATA (push
, primid
);
429 BEGIN_NV04(push
, NV50_3D(VP_RESULT_MAP_SIZE
), 1);
431 BEGIN_NV04(push
, NV50_3D(VP_RESULT_MAP(0)), n
);
432 PUSH_DATAp(push
, map
, n
);
435 BEGIN_NV04(push
, NV50_3D(SEMANTIC_COLOR
), 4);
436 PUSH_DATA (push
, colors
);
437 PUSH_DATA (push
, (vp
->vp
.clpd_nr
<< 8) | 4);
439 PUSH_DATA (push
, psiz
);
441 BEGIN_NV04(push
, NV50_3D(FP_INTERPOLANT_CTRL
), 1);
442 PUSH_DATA (push
, interp
);
444 nv50
->state
.interpolant_ctrl
= interp
;
446 nv50
->state
.semantic_color
= colors
;
447 nv50
->state
.semantic_psize
= psiz
;
449 BEGIN_NV04(push
, NV50_3D(NOPERSPECTIVE_BITMAP(0)), 4);
450 PUSH_DATAp(push
, lin
, 4);
452 BEGIN_NV04(push
, NV50_3D(GP_ENABLE
), 1);
453 PUSH_DATA (push
, nv50
->gmtyprog
? 1 : 0);
457 nv50_vp_gp_mapping(uint8_t *map
, int m
,
458 struct nv50_program
*vp
, struct nv50_program
*gp
)
462 for (i
= 0; i
< gp
->in_nr
; ++i
) {
463 uint8_t oid
= 0, mv
= 0, mg
= gp
->in
[i
].mask
;
465 for (j
= 0; j
< vp
->out_nr
; ++j
) {
466 if (vp
->out
[j
].sn
== gp
->in
[i
].sn
&&
467 vp
->out
[j
].si
== gp
->in
[i
].si
) {
468 mv
= vp
->out
[j
].mask
;
474 for (c
= 0; c
< 4; ++c
, mv
>>= 1, mg
>>= 1) {
479 map
[m
++] = (c
== 3) ? 0x41 : 0x40;
487 nv50_gp_linkage_validate(struct nv50_context
*nv50
)
489 struct nouveau_pushbuf
*push
= nv50
->base
.pushbuf
;
490 struct nv50_program
*vp
= nv50
->vertprog
;
491 struct nv50_program
*gp
= nv50
->gmtyprog
;
498 memset(map
, 0, sizeof(map
));
500 m
= nv50_vp_gp_mapping(map
, m
, vp
, gp
);
504 BEGIN_NV04(push
, NV50_3D(VP_GP_BUILTIN_ATTR_EN
), 1);
505 PUSH_DATA (push
, vp
->vp
.attrs
[2] | gp
->vp
.attrs
[2]);
507 BEGIN_NV04(push
, NV50_3D(VP_RESULT_MAP_SIZE
), 1);
509 BEGIN_NV04(push
, NV50_3D(VP_RESULT_MAP(0)), n
);
510 PUSH_DATAp(push
, map
, n
);