2 * Copyright 2010 Christoph Bumiller
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
23 #include "pipe/p_defines.h"
25 #include "tgsi/tgsi_ureg.h"
27 #include "nvc0/nvc0_context.h"
29 #include "codegen/nv50_ir_driver.h"
30 #include "nvc0/nve4_compute.h"
32 /* NOTE: Using a[0x270] in FP may cause an error even if we're using less than
33 * 124 scalar varying values.
36 nvc0_shader_input_address(unsigned sn
, unsigned si
)
39 case TGSI_SEMANTIC_TESSOUTER
: return 0x000 + si
* 0x4;
40 case TGSI_SEMANTIC_TESSINNER
: return 0x010 + si
* 0x4;
41 case TGSI_SEMANTIC_PATCH
: return 0x020 + si
* 0x10;
42 case TGSI_SEMANTIC_PRIMID
: return 0x060;
43 case TGSI_SEMANTIC_LAYER
: return 0x064;
44 case TGSI_SEMANTIC_VIEWPORT_INDEX
:return 0x068;
45 case TGSI_SEMANTIC_PSIZE
: return 0x06c;
46 case TGSI_SEMANTIC_POSITION
: return 0x070;
47 case TGSI_SEMANTIC_GENERIC
: return 0x080 + si
* 0x10;
48 case TGSI_SEMANTIC_FOG
: return 0x2e8;
49 case TGSI_SEMANTIC_COLOR
: return 0x280 + si
* 0x10;
50 case TGSI_SEMANTIC_BCOLOR
: return 0x2a0 + si
* 0x10;
51 case TGSI_SEMANTIC_CLIPDIST
: return 0x2c0 + si
* 0x10;
52 case TGSI_SEMANTIC_CLIPVERTEX
: return 0x270;
53 case TGSI_SEMANTIC_PCOORD
: return 0x2e0;
54 case TGSI_SEMANTIC_TESSCOORD
: return 0x2f0;
55 case TGSI_SEMANTIC_INSTANCEID
: return 0x2f8;
56 case TGSI_SEMANTIC_VERTEXID
: return 0x2fc;
57 case TGSI_SEMANTIC_TEXCOORD
: return 0x300 + si
* 0x10;
59 assert(!"invalid TGSI input semantic");
65 nvc0_shader_output_address(unsigned sn
, unsigned si
)
68 case TGSI_SEMANTIC_TESSOUTER
: return 0x000 + si
* 0x4;
69 case TGSI_SEMANTIC_TESSINNER
: return 0x010 + si
* 0x4;
70 case TGSI_SEMANTIC_PATCH
: return 0x020 + si
* 0x10;
71 case TGSI_SEMANTIC_PRIMID
: return 0x060;
72 case TGSI_SEMANTIC_LAYER
: return 0x064;
73 case TGSI_SEMANTIC_VIEWPORT_INDEX
:return 0x068;
74 case TGSI_SEMANTIC_PSIZE
: return 0x06c;
75 case TGSI_SEMANTIC_POSITION
: return 0x070;
76 case TGSI_SEMANTIC_GENERIC
: return 0x080 + si
* 0x10;
77 case TGSI_SEMANTIC_FOG
: return 0x2e8;
78 case TGSI_SEMANTIC_COLOR
: return 0x280 + si
* 0x10;
79 case TGSI_SEMANTIC_BCOLOR
: return 0x2a0 + si
* 0x10;
80 case TGSI_SEMANTIC_CLIPDIST
: return 0x2c0 + si
* 0x10;
81 case TGSI_SEMANTIC_CLIPVERTEX
: return 0x270;
82 case TGSI_SEMANTIC_TEXCOORD
: return 0x300 + si
* 0x10;
83 /* case TGSI_SEMANTIC_VIEWPORT_MASK: return 0x3a0; */
84 case TGSI_SEMANTIC_EDGEFLAG
: return ~0;
86 assert(!"invalid TGSI output semantic");
92 nvc0_vp_assign_input_slots(struct nv50_ir_prog_info
*info
)
96 for (n
= 0, i
= 0; i
< info
->numInputs
; ++i
) {
97 switch (info
->in
[i
].sn
) {
98 case TGSI_SEMANTIC_INSTANCEID
: /* for SM4 only, in TGSI they're SVs */
99 case TGSI_SEMANTIC_VERTEXID
:
100 info
->in
[i
].mask
= 0x1;
101 info
->in
[i
].slot
[0] =
102 nvc0_shader_input_address(info
->in
[i
].sn
, 0) / 4;
107 for (c
= 0; c
< 4; ++c
)
108 info
->in
[i
].slot
[c
] = (0x80 + n
* 0x10 + c
* 0x4) / 4;
116 nvc0_sp_assign_input_slots(struct nv50_ir_prog_info
*info
)
121 for (i
= 0; i
< info
->numInputs
; ++i
) {
122 offset
= nvc0_shader_input_address(info
->in
[i
].sn
, info
->in
[i
].si
);
124 for (c
= 0; c
< 4; ++c
)
125 info
->in
[i
].slot
[c
] = (offset
+ c
* 0x4) / 4;
132 nvc0_fp_assign_output_slots(struct nv50_ir_prog_info
*info
)
134 unsigned count
= info
->prop
.fp
.numColourResults
* 4;
137 /* Compute the relative position of each color output, since skipped MRT
138 * positions will not have registers allocated to them.
140 unsigned colors
[8] = {0};
141 for (i
= 0; i
< info
->numOutputs
; ++i
)
142 if (info
->out
[i
].sn
== TGSI_SEMANTIC_COLOR
)
143 colors
[info
->out
[i
].si
] = 1;
144 for (i
= 0, c
= 0; i
< 8; i
++)
147 for (i
= 0; i
< info
->numOutputs
; ++i
)
148 if (info
->out
[i
].sn
== TGSI_SEMANTIC_COLOR
)
149 for (c
= 0; c
< 4; ++c
)
150 info
->out
[i
].slot
[c
] = colors
[info
->out
[i
].si
] * 4 + c
;
152 if (info
->io
.sampleMask
< PIPE_MAX_SHADER_OUTPUTS
)
153 info
->out
[info
->io
.sampleMask
].slot
[0] = count
++;
155 if (info
->target
>= 0xe0)
156 count
++; /* on Kepler, depth is always last colour reg + 2 */
158 if (info
->io
.fragDepth
< PIPE_MAX_SHADER_OUTPUTS
)
159 info
->out
[info
->io
.fragDepth
].slot
[2] = count
;
165 nvc0_sp_assign_output_slots(struct nv50_ir_prog_info
*info
)
170 for (i
= 0; i
< info
->numOutputs
; ++i
) {
171 offset
= nvc0_shader_output_address(info
->out
[i
].sn
, info
->out
[i
].si
);
173 for (c
= 0; c
< 4; ++c
)
174 info
->out
[i
].slot
[c
] = (offset
+ c
* 0x4) / 4;
181 nvc0_program_assign_varying_slots(struct nv50_ir_prog_info
*info
)
185 if (info
->type
== PIPE_SHADER_VERTEX
)
186 ret
= nvc0_vp_assign_input_slots(info
);
188 ret
= nvc0_sp_assign_input_slots(info
);
192 if (info
->type
== PIPE_SHADER_FRAGMENT
)
193 ret
= nvc0_fp_assign_output_slots(info
);
195 ret
= nvc0_sp_assign_output_slots(info
);
200 nvc0_vtgp_hdr_update_oread(struct nvc0_program
*vp
, uint8_t slot
)
202 uint8_t min
= (vp
->hdr
[4] >> 12) & 0xff;
203 uint8_t max
= (vp
->hdr
[4] >> 24);
205 min
= MIN2(min
, slot
);
206 max
= MAX2(max
, slot
);
208 vp
->hdr
[4] = (max
<< 24) | (min
<< 12);
211 /* Common part of header generation for VP, TCP, TEP and GP. */
213 nvc0_vtgp_gen_header(struct nvc0_program
*vp
, struct nv50_ir_prog_info
*info
)
217 for (i
= 0; i
< info
->numInputs
; ++i
) {
218 if (info
->in
[i
].patch
)
220 for (c
= 0; c
< 4; ++c
) {
221 a
= info
->in
[i
].slot
[c
];
222 if (info
->in
[i
].mask
& (1 << c
))
223 vp
->hdr
[5 + a
/ 32] |= 1 << (a
% 32);
227 for (i
= 0; i
< info
->numOutputs
; ++i
) {
228 if (info
->out
[i
].patch
)
230 for (c
= 0; c
< 4; ++c
) {
231 if (!(info
->out
[i
].mask
& (1 << c
)))
233 assert(info
->out
[i
].slot
[c
] >= 0x40 / 4);
234 a
= info
->out
[i
].slot
[c
] - 0x40 / 4;
235 vp
->hdr
[13 + a
/ 32] |= 1 << (a
% 32);
236 if (info
->out
[i
].oread
)
237 nvc0_vtgp_hdr_update_oread(vp
, info
->out
[i
].slot
[c
]);
241 for (i
= 0; i
< info
->numSysVals
; ++i
) {
242 switch (info
->sv
[i
].sn
) {
243 case TGSI_SEMANTIC_PRIMID
:
244 vp
->hdr
[5] |= 1 << 24;
246 case TGSI_SEMANTIC_INSTANCEID
:
247 vp
->hdr
[10] |= 1 << 30;
249 case TGSI_SEMANTIC_VERTEXID
:
250 vp
->hdr
[10] |= 1 << 31;
252 case TGSI_SEMANTIC_TESSCOORD
:
253 /* We don't have the mask, nor the slots populated. While this could
254 * be achieved, the vast majority of the time if either of the coords
255 * are read, then both will be read.
257 nvc0_vtgp_hdr_update_oread(vp
, 0x2f0 / 4);
258 nvc0_vtgp_hdr_update_oread(vp
, 0x2f4 / 4);
265 vp
->vp
.clip_enable
= (1 << info
->io
.clipDistances
) - 1;
267 ((1 << info
->io
.cullDistances
) - 1) << info
->io
.clipDistances
;
268 for (i
= 0; i
< info
->io
.cullDistances
; ++i
)
269 vp
->vp
.clip_mode
|= 1 << ((info
->io
.clipDistances
+ i
) * 4);
271 if (info
->io
.genUserClip
< 0)
272 vp
->vp
.num_ucps
= PIPE_MAX_CLIP_PLANES
+ 1; /* prevent rebuilding */
278 nvc0_vp_gen_header(struct nvc0_program
*vp
, struct nv50_ir_prog_info
*info
)
280 vp
->hdr
[0] = 0x20061 | (1 << 10);
281 vp
->hdr
[4] = 0xff000;
283 return nvc0_vtgp_gen_header(vp
, info
);
287 nvc0_tp_get_tess_mode(struct nvc0_program
*tp
, struct nv50_ir_prog_info
*info
)
289 if (info
->prop
.tp
.outputPrim
== PIPE_PRIM_MAX
) {
290 tp
->tp
.tess_mode
= ~0;
293 switch (info
->prop
.tp
.domain
) {
294 case PIPE_PRIM_LINES
:
295 tp
->tp
.tess_mode
= NVC0_3D_TESS_MODE_PRIM_ISOLINES
;
297 case PIPE_PRIM_TRIANGLES
:
298 tp
->tp
.tess_mode
= NVC0_3D_TESS_MODE_PRIM_TRIANGLES
;
300 case PIPE_PRIM_QUADS
:
301 tp
->tp
.tess_mode
= NVC0_3D_TESS_MODE_PRIM_QUADS
;
304 tp
->tp
.tess_mode
= ~0;
308 /* It seems like lines want the "CW" bit to indicate they're connected, and
309 * spit out errors in dmesg when the "CONNECTED" bit is set.
311 if (info
->prop
.tp
.outputPrim
!= PIPE_PRIM_POINTS
) {
312 if (info
->prop
.tp
.domain
== PIPE_PRIM_LINES
)
313 tp
->tp
.tess_mode
|= NVC0_3D_TESS_MODE_CW
;
315 tp
->tp
.tess_mode
|= NVC0_3D_TESS_MODE_CONNECTED
;
318 /* Winding only matters for triangles/quads, not lines. */
319 if (info
->prop
.tp
.domain
!= PIPE_PRIM_LINES
&&
320 info
->prop
.tp
.outputPrim
!= PIPE_PRIM_POINTS
&&
321 info
->prop
.tp
.winding
> 0)
322 tp
->tp
.tess_mode
|= NVC0_3D_TESS_MODE_CW
;
324 switch (info
->prop
.tp
.partitioning
) {
325 case PIPE_TESS_SPACING_EQUAL
:
326 tp
->tp
.tess_mode
|= NVC0_3D_TESS_MODE_SPACING_EQUAL
;
328 case PIPE_TESS_SPACING_FRACTIONAL_ODD
:
329 tp
->tp
.tess_mode
|= NVC0_3D_TESS_MODE_SPACING_FRACTIONAL_ODD
;
331 case PIPE_TESS_SPACING_FRACTIONAL_EVEN
:
332 tp
->tp
.tess_mode
|= NVC0_3D_TESS_MODE_SPACING_FRACTIONAL_EVEN
;
335 assert(!"invalid tessellator partitioning");
341 nvc0_tcp_gen_header(struct nvc0_program
*tcp
, struct nv50_ir_prog_info
*info
)
343 unsigned opcs
= 6; /* output patch constants (at least the TessFactors) */
345 tcp
->tp
.input_patch_size
= info
->prop
.tp
.inputPatchSize
;
347 if (info
->numPatchConstants
)
348 opcs
= 8 + info
->numPatchConstants
* 4;
350 tcp
->hdr
[0] = 0x20061 | (2 << 10);
352 tcp
->hdr
[1] = opcs
<< 24;
353 tcp
->hdr
[2] = info
->prop
.tp
.outputPatchSize
<< 24;
355 tcp
->hdr
[4] = 0xff000; /* initial min/max parallel output read address */
357 nvc0_vtgp_gen_header(tcp
, info
);
359 if (info
->target
>= NVISA_GM107_CHIPSET
) {
360 /* On GM107+, the number of output patch components has moved in the TCP
361 * header, but it seems like blob still also uses the old position.
362 * Also, the high 8-bits are located inbetween the min/max parallel
363 * field and has to be set after updating the outputs. */
364 tcp
->hdr
[3] = (opcs
& 0x0f) << 28;
365 tcp
->hdr
[4] |= (opcs
& 0xf0) << 16;
368 nvc0_tp_get_tess_mode(tcp
, info
);
374 nvc0_tep_gen_header(struct nvc0_program
*tep
, struct nv50_ir_prog_info
*info
)
376 tep
->tp
.input_patch_size
= ~0;
378 tep
->hdr
[0] = 0x20061 | (3 << 10);
379 tep
->hdr
[4] = 0xff000;
381 nvc0_vtgp_gen_header(tep
, info
);
383 nvc0_tp_get_tess_mode(tep
, info
);
385 tep
->hdr
[18] |= 0x3 << 12; /* ? */
391 nvc0_gp_gen_header(struct nvc0_program
*gp
, struct nv50_ir_prog_info
*info
)
393 gp
->hdr
[0] = 0x20061 | (4 << 10);
395 gp
->hdr
[2] = MIN2(info
->prop
.gp
.instanceCount
, 32) << 24;
397 switch (info
->prop
.gp
.outputPrim
) {
398 case PIPE_PRIM_POINTS
:
399 gp
->hdr
[3] = 0x01000000;
400 gp
->hdr
[0] |= 0xf0000000;
402 case PIPE_PRIM_LINE_STRIP
:
403 gp
->hdr
[3] = 0x06000000;
404 gp
->hdr
[0] |= 0x10000000;
406 case PIPE_PRIM_TRIANGLE_STRIP
:
407 gp
->hdr
[3] = 0x07000000;
408 gp
->hdr
[0] |= 0x10000000;
415 gp
->hdr
[4] = CLAMP(info
->prop
.gp
.maxVertices
, 1, 1024);
417 return nvc0_vtgp_gen_header(gp
, info
);
420 #define NVC0_INTERP_FLAT (1 << 0)
421 #define NVC0_INTERP_PERSPECTIVE (2 << 0)
422 #define NVC0_INTERP_LINEAR (3 << 0)
423 #define NVC0_INTERP_CENTROID (1 << 2)
426 nvc0_hdr_interp_mode(const struct nv50_ir_varying
*var
)
429 return NVC0_INTERP_LINEAR
;
431 return NVC0_INTERP_FLAT
;
432 return NVC0_INTERP_PERSPECTIVE
;
436 nvc0_fp_gen_header(struct nvc0_program
*fp
, struct nv50_ir_prog_info
*info
)
440 /* just 00062 on Kepler */
441 fp
->hdr
[0] = 0x20062 | (5 << 10);
442 fp
->hdr
[5] = 0x80000000; /* getting a trap if FRAG_COORD_UMASK.w = 0 */
444 if (info
->prop
.fp
.usesDiscard
)
445 fp
->hdr
[0] |= 0x8000;
446 if (info
->prop
.fp
.numColourResults
> 1)
447 fp
->hdr
[0] |= 0x4000;
448 if (info
->io
.sampleMask
< PIPE_MAX_SHADER_OUTPUTS
)
450 if (info
->prop
.fp
.writesDepth
) {
452 fp
->flags
[0] = 0x11; /* deactivate ZCULL */
455 for (i
= 0; i
< info
->numInputs
; ++i
) {
456 m
= nvc0_hdr_interp_mode(&info
->in
[i
]);
457 if (info
->in
[i
].sn
== TGSI_SEMANTIC_COLOR
) {
458 fp
->fp
.colors
|= 1 << info
->in
[i
].si
;
460 fp
->fp
.color_interp
[info
->in
[i
].si
] = m
| (info
->in
[i
].mask
<< 4);
462 for (c
= 0; c
< 4; ++c
) {
463 if (!(info
->in
[i
].mask
& (1 << c
)))
465 a
= info
->in
[i
].slot
[c
];
466 if (info
->in
[i
].slot
[0] >= (0x060 / 4) &&
467 info
->in
[i
].slot
[0] <= (0x07c / 4)) {
468 fp
->hdr
[5] |= 1 << (24 + (a
- 0x060 / 4));
470 if (info
->in
[i
].slot
[0] >= (0x2c0 / 4) &&
471 info
->in
[i
].slot
[0] <= (0x2fc / 4)) {
472 fp
->hdr
[14] |= (1 << (a
- 0x280 / 4)) & 0x07ff0000;
474 if (info
->in
[i
].slot
[c
] < (0x040 / 4) ||
475 info
->in
[i
].slot
[c
] > (0x380 / 4))
478 if (info
->in
[i
].slot
[0] >= (0x300 / 4))
480 fp
->hdr
[4 + a
/ 32] |= m
<< (a
% 32);
485 for (i
= 0; i
< info
->numOutputs
; ++i
) {
486 if (info
->out
[i
].sn
== TGSI_SEMANTIC_COLOR
)
487 fp
->hdr
[18] |= 0xf << (4 * info
->out
[i
].si
);
490 /* There are no "regular" attachments, but the shader still needs to be
491 * executed. It seems like it wants to think that it has some color
492 * outputs in order to actually run.
494 if (info
->prop
.fp
.numColourResults
== 0 && !info
->prop
.fp
.writesDepth
)
497 fp
->fp
.early_z
= info
->prop
.fp
.earlyFragTests
;
498 fp
->fp
.sample_mask_in
= info
->prop
.fp
.usesSampleMaskIn
;
499 fp
->fp
.reads_framebuffer
= info
->prop
.fp
.readsFramebuffer
;
500 fp
->fp
.post_depth_coverage
= info
->prop
.fp
.postDepthCoverage
;
502 /* Mark position xy and layer as read */
503 if (fp
->fp
.reads_framebuffer
)
504 fp
->hdr
[5] |= 0x32000000;
509 static struct nvc0_transform_feedback_state
*
510 nvc0_program_create_tfb_state(const struct nv50_ir_prog_info
*info
,
511 const struct pipe_stream_output_info
*pso
)
513 struct nvc0_transform_feedback_state
*tfb
;
516 tfb
= MALLOC_STRUCT(nvc0_transform_feedback_state
);
519 for (b
= 0; b
< 4; ++b
) {
520 tfb
->stride
[b
] = pso
->stride
[b
] * 4;
521 tfb
->varying_count
[b
] = 0;
523 memset(tfb
->varying_index
, 0xff, sizeof(tfb
->varying_index
)); /* = skip */
525 for (i
= 0; i
< pso
->num_outputs
; ++i
) {
526 unsigned s
= pso
->output
[i
].start_component
;
527 unsigned p
= pso
->output
[i
].dst_offset
;
528 const unsigned r
= pso
->output
[i
].register_index
;
529 b
= pso
->output
[i
].output_buffer
;
531 if (r
>= info
->numOutputs
)
534 for (c
= 0; c
< pso
->output
[i
].num_components
; ++c
)
535 tfb
->varying_index
[b
][p
++] = info
->out
[r
].slot
[s
+ c
];
537 tfb
->varying_count
[b
] = MAX2(tfb
->varying_count
[b
], p
);
538 tfb
->stream
[b
] = pso
->output
[i
].stream
;
540 for (b
= 0; b
< 4; ++b
) // zero unused indices (looks nicer)
541 for (c
= tfb
->varying_count
[b
]; c
& 3; ++c
)
542 tfb
->varying_index
[b
][c
] = 0;
549 nvc0_program_dump(struct nvc0_program
*prog
)
553 if (prog
->type
!= PIPE_SHADER_COMPUTE
) {
554 for (pos
= 0; pos
< ARRAY_SIZE(prog
->hdr
); ++pos
)
555 debug_printf("HDR[%02"PRIxPTR
"] = 0x%08x\n",
556 pos
* sizeof(prog
->hdr
[0]), prog
->hdr
[pos
]);
558 debug_printf("shader binary code (0x%x bytes):", prog
->code_size
);
559 for (pos
= 0; pos
< prog
->code_size
/ 4; ++pos
) {
562 debug_printf("%08x ", prog
->code
[pos
]);
569 nvc0_program_translate(struct nvc0_program
*prog
, uint16_t chipset
,
570 struct pipe_debug_callback
*debug
)
572 struct nv50_ir_prog_info
*info
;
575 info
= CALLOC_STRUCT(nv50_ir_prog_info
);
579 info
->type
= prog
->type
;
580 info
->target
= chipset
;
581 info
->bin
.sourceRep
= PIPE_SHADER_IR_TGSI
;
582 info
->bin
.source
= (void *)prog
->pipe
.tokens
;
585 info
->target
= debug_get_num_option("NV50_PROG_CHIPSET", chipset
);
586 info
->optLevel
= debug_get_num_option("NV50_PROG_OPTIMIZE", 3);
587 info
->dbgFlags
= debug_get_num_option("NV50_PROG_DEBUG", 0);
588 info
->omitLineNum
= debug_get_num_option("NV50_PROG_DEBUG_OMIT_LINENUM", 0);
593 info
->bin
.smemSize
= prog
->cp
.smem_size
;
594 info
->io
.genUserClip
= prog
->vp
.num_ucps
;
595 info
->io
.auxCBSlot
= 15;
596 info
->io
.msInfoCBSlot
= 15;
597 info
->io
.ucpBase
= NVC0_CB_AUX_UCP_INFO
;
598 info
->io
.drawInfoBase
= NVC0_CB_AUX_DRAW_INFO
;
599 info
->io
.msInfoBase
= NVC0_CB_AUX_MS_INFO
;
600 info
->io
.bufInfoBase
= NVC0_CB_AUX_BUF_INFO(0);
601 info
->io
.suInfoBase
= NVC0_CB_AUX_SU_INFO(0);
602 if (info
->target
>= NVISA_GK104_CHIPSET
) {
603 info
->io
.texBindBase
= NVC0_CB_AUX_TEX_INFO(0);
604 info
->io
.fbtexBindBase
= NVC0_CB_AUX_FB_TEX_INFO
;
605 info
->io
.bindlessBase
= NVC0_CB_AUX_BINDLESS_INFO(0);
608 if (prog
->type
== PIPE_SHADER_COMPUTE
) {
609 if (info
->target
>= NVISA_GK104_CHIPSET
) {
610 info
->io
.auxCBSlot
= 7;
611 info
->io
.msInfoCBSlot
= 7;
612 info
->io
.uboInfoBase
= NVC0_CB_AUX_UBO_INFO(0);
614 info
->prop
.cp
.gridInfoBase
= NVC0_CB_AUX_GRID_INFO(0);
616 info
->io
.sampleInfoBase
= NVC0_CB_AUX_SAMPLE_INFO
;
619 info
->assignSlots
= nvc0_program_assign_varying_slots
;
621 ret
= nv50_ir_generate_code(info
);
623 NOUVEAU_ERR("shader translation failed: %i\n", ret
);
626 if (prog
->type
!= PIPE_SHADER_COMPUTE
)
627 FREE(info
->bin
.syms
);
629 prog
->code
= info
->bin
.code
;
630 prog
->code_size
= info
->bin
.codeSize
;
631 prog
->relocs
= info
->bin
.relocData
;
632 prog
->fixups
= info
->bin
.fixupData
;
633 prog
->num_gprs
= MAX2(4, (info
->bin
.maxGPR
+ 1));
634 prog
->cp
.smem_size
= info
->bin
.smemSize
;
635 prog
->num_barriers
= info
->numBarriers
;
637 prog
->vp
.need_vertex_id
= info
->io
.vertexId
< PIPE_MAX_SHADER_INPUTS
;
638 prog
->vp
.need_draw_parameters
= info
->prop
.vp
.usesDrawParameters
;
640 if (info
->io
.edgeFlagOut
< PIPE_MAX_ATTRIBS
)
641 info
->out
[info
->io
.edgeFlagOut
].mask
= 0; /* for headergen */
642 prog
->vp
.edgeflag
= info
->io
.edgeFlagIn
;
644 switch (prog
->type
) {
645 case PIPE_SHADER_VERTEX
:
646 ret
= nvc0_vp_gen_header(prog
, info
);
648 case PIPE_SHADER_TESS_CTRL
:
649 ret
= nvc0_tcp_gen_header(prog
, info
);
651 case PIPE_SHADER_TESS_EVAL
:
652 ret
= nvc0_tep_gen_header(prog
, info
);
654 case PIPE_SHADER_GEOMETRY
:
655 ret
= nvc0_gp_gen_header(prog
, info
);
657 case PIPE_SHADER_FRAGMENT
:
658 ret
= nvc0_fp_gen_header(prog
, info
);
660 case PIPE_SHADER_COMPUTE
:
661 prog
->cp
.syms
= info
->bin
.syms
;
662 prog
->cp
.num_syms
= info
->bin
.numSyms
;
666 NOUVEAU_ERR("unknown program type: %u\n", prog
->type
);
672 if (info
->bin
.tlsSpace
) {
673 assert(info
->bin
.tlsSpace
< (1 << 24));
674 prog
->hdr
[0] |= 1 << 26;
675 prog
->hdr
[1] |= align(info
->bin
.tlsSpace
, 0x10); /* l[] size */
676 prog
->need_tls
= true;
678 /* TODO: factor 2 only needed where joinat/precont is used,
679 * and we only have to count non-uniform branches
682 if ((info->maxCFDepth * 2) > 16) {
683 prog->hdr[2] |= (((info->maxCFDepth * 2) + 47) / 48) * 0x200;
684 prog->need_tls = true;
687 if (info
->io
.globalAccess
)
688 prog
->hdr
[0] |= 1 << 26;
689 if (info
->io
.globalAccess
& 0x2)
690 prog
->hdr
[0] |= 1 << 16;
692 prog
->hdr
[0] |= 1 << 27;
694 if (prog
->pipe
.stream_output
.num_outputs
)
695 prog
->tfb
= nvc0_program_create_tfb_state(info
,
696 &prog
->pipe
.stream_output
);
698 pipe_debug_message(debug
, SHADER_INFO
,
699 "type: %d, local: %d, shared: %d, gpr: %d, inst: %d, bytes: %d",
700 prog
->type
, info
->bin
.tlsSpace
, info
->bin
.smemSize
,
701 prog
->num_gprs
, info
->bin
.instructions
,
705 if (debug_get_option("NV50_PROG_CHIPSET", NULL
) && info
->dbgFlags
)
706 nvc0_program_dump(prog
);
715 nvc0_program_alloc_code(struct nvc0_context
*nvc0
, struct nvc0_program
*prog
)
717 struct nvc0_screen
*screen
= nvc0
->screen
;
718 const bool is_cp
= prog
->type
== PIPE_SHADER_COMPUTE
;
720 uint32_t size
= prog
->code_size
+ (is_cp
? 0 : NVC0_SHADER_HEADER_SIZE
);
722 /* On Fermi, SP_START_ID must be aligned to 0x40.
723 * On Kepler, the first instruction must be aligned to 0x80 because
724 * latency information is expected only at certain positions.
726 if (screen
->base
.class_3d
>= NVE4_3D_CLASS
)
727 size
= size
+ (is_cp
? 0x40 : 0x70);
728 size
= align(size
, 0x40);
730 ret
= nouveau_heap_alloc(screen
->text_heap
, size
, prog
, &prog
->mem
);
733 prog
->code_base
= prog
->mem
->start
;
736 if (screen
->base
.class_3d
>= NVE4_3D_CLASS
) {
737 switch (prog
->mem
->start
& 0xff) {
738 case 0x40: prog
->code_base
+= 0x70; break;
739 case 0x80: prog
->code_base
+= 0x30; break;
740 case 0xc0: prog
->code_base
+= 0x70; break;
742 prog
->code_base
+= 0x30;
743 assert((prog
->mem
->start
& 0xff) == 0x00);
748 if (screen
->base
.class_3d
>= NVE4_3D_CLASS
) {
749 if (prog
->mem
->start
& 0x40)
750 prog
->code_base
+= 0x40;
751 assert((prog
->code_base
& 0x7f) == 0x00);
759 nvc0_program_upload_code(struct nvc0_context
*nvc0
, struct nvc0_program
*prog
)
761 struct nvc0_screen
*screen
= nvc0
->screen
;
762 const bool is_cp
= prog
->type
== PIPE_SHADER_COMPUTE
;
763 uint32_t code_pos
= prog
->code_base
+ (is_cp
? 0 : NVC0_SHADER_HEADER_SIZE
);
766 nv50_ir_relocate_code(prog
->relocs
, prog
->code
, code_pos
,
767 screen
->lib_code
->start
, 0);
769 nv50_ir_apply_fixups(prog
->fixups
, prog
->code
,
770 prog
->fp
.force_persample_interp
,
773 for (int i
= 0; i
< 2; i
++) {
774 unsigned mask
= prog
->fp
.color_interp
[i
] >> 4;
775 unsigned interp
= prog
->fp
.color_interp
[i
] & 3;
778 prog
->hdr
[14] &= ~(0xff << (8 * i
));
779 if (prog
->fp
.flatshade
)
780 interp
= NVC0_INTERP_FLAT
;
781 for (int c
= 0; c
< 4; c
++)
783 prog
->hdr
[14] |= interp
<< (2 * (4 * i
+ c
));
788 nvc0
->base
.push_data(&nvc0
->base
, screen
->text
, prog
->code_base
,
789 NV_VRAM_DOMAIN(&screen
->base
),
790 NVC0_SHADER_HEADER_SIZE
, prog
->hdr
);
792 nvc0
->base
.push_data(&nvc0
->base
, screen
->text
, code_pos
,
793 NV_VRAM_DOMAIN(&screen
->base
), prog
->code_size
,
798 nvc0_program_upload(struct nvc0_context
*nvc0
, struct nvc0_program
*prog
)
800 struct nvc0_screen
*screen
= nvc0
->screen
;
801 const bool is_cp
= prog
->type
== PIPE_SHADER_COMPUTE
;
803 uint32_t size
= prog
->code_size
+ (is_cp
? 0 : NVC0_SHADER_HEADER_SIZE
);
805 ret
= nvc0_program_alloc_code(nvc0
, prog
);
807 struct nouveau_heap
*heap
= screen
->text_heap
;
808 struct nvc0_program
*progs
[] = { /* Sorted accordingly to SP_START_ID */
809 nvc0
->compprog
, nvc0
->vertprog
, nvc0
->tctlprog
,
810 nvc0
->tevlprog
, nvc0
->gmtyprog
, nvc0
->fragprog
813 /* Note that the code library, which is allocated before anything else,
814 * does not have a priv pointer. We can stop once we hit it.
816 while (heap
->next
&& heap
->next
->priv
) {
817 struct nvc0_program
*evict
= heap
->next
->priv
;
818 nouveau_heap_free(&evict
->mem
);
820 debug_printf("WARNING: out of code space, evicting all shaders.\n");
822 /* Make sure to synchronize before deleting the code segment. */
823 IMMED_NVC0(nvc0
->base
.pushbuf
, NVC0_3D(SERIALIZE
), 0);
825 if ((screen
->text
->size
<< 1) <= (1 << 23)) {
826 ret
= nvc0_screen_resize_text_area(screen
, screen
->text
->size
<< 1);
828 NOUVEAU_ERR("Error allocating TEXT area: %d\n", ret
);
831 nouveau_bufctx_reset(nvc0
->bufctx_3d
, NVC0_BIND_3D_TEXT
);
832 BCTX_REFN_bo(nvc0
->bufctx_3d
, 3D_TEXT
,
833 NV_VRAM_DOMAIN(&screen
->base
) | NOUVEAU_BO_RD
,
835 if (screen
->compute
) {
836 nouveau_bufctx_reset(nvc0
->bufctx_cp
, NVC0_BIND_CP_TEXT
);
837 BCTX_REFN_bo(nvc0
->bufctx_cp
, CP_TEXT
,
838 NV_VRAM_DOMAIN(&screen
->base
) | NOUVEAU_BO_RD
,
842 /* Re-upload the builtin function into the new code segment. */
843 nvc0_program_library_upload(nvc0
);
846 ret
= nvc0_program_alloc_code(nvc0
, prog
);
848 NOUVEAU_ERR("shader too large (0x%x) to fit in code space ?\n", size
);
852 /* All currently bound shaders have to be reuploaded. */
853 for (int i
= 0; i
< ARRAY_SIZE(progs
); i
++) {
854 if (!progs
[i
] || progs
[i
] == prog
)
857 ret
= nvc0_program_alloc_code(nvc0
, progs
[i
]);
859 NOUVEAU_ERR("failed to re-upload a shader after code eviction.\n");
862 nvc0_program_upload_code(nvc0
, progs
[i
]);
864 if (progs
[i
]->type
== PIPE_SHADER_COMPUTE
) {
865 /* Caches have to be invalidated but the CP_START_ID will be
866 * updated in the launch_grid functions. */
867 BEGIN_NVC0(nvc0
->base
.pushbuf
, NVC0_CP(FLUSH
), 1);
868 PUSH_DATA (nvc0
->base
.pushbuf
, NVC0_COMPUTE_FLUSH_CODE
);
870 BEGIN_NVC0(nvc0
->base
.pushbuf
, NVC0_3D(SP_START_ID(i
)), 1);
871 PUSH_DATA (nvc0
->base
.pushbuf
, progs
[i
]->code_base
);
876 nvc0_program_upload_code(nvc0
, prog
);
879 if (debug_get_bool_option("NV50_PROG_DEBUG", false))
880 nvc0_program_dump(prog
);
883 BEGIN_NVC0(nvc0
->base
.pushbuf
, NVC0_3D(MEM_BARRIER
), 1);
884 PUSH_DATA (nvc0
->base
.pushbuf
, 0x1011);
889 /* Upload code for builtin functions like integer division emulation. */
891 nvc0_program_library_upload(struct nvc0_context
*nvc0
)
893 struct nvc0_screen
*screen
= nvc0
->screen
;
896 const uint32_t *code
;
898 if (screen
->lib_code
)
901 nv50_ir_get_target_library(screen
->base
.device
->chipset
, &code
, &size
);
905 ret
= nouveau_heap_alloc(screen
->text_heap
, align(size
, 0x100), NULL
,
910 nvc0
->base
.push_data(&nvc0
->base
,
911 screen
->text
, screen
->lib_code
->start
, NV_VRAM_DOMAIN(&screen
->base
),
913 /* no need for a memory barrier, will be emitted with first program */
917 nvc0_program_destroy(struct nvc0_context
*nvc0
, struct nvc0_program
*prog
)
919 const struct pipe_shader_state pipe
= prog
->pipe
;
920 const ubyte type
= prog
->type
;
923 nouveau_heap_free(&prog
->mem
);
924 FREE(prog
->code
); /* may be 0 for hardcoded shaders */
927 if (prog
->type
== PIPE_SHADER_COMPUTE
&& prog
->cp
.syms
)
930 if (nvc0
->state
.tfb
== prog
->tfb
)
931 nvc0
->state
.tfb
= NULL
;
935 memset(prog
, 0, sizeof(*prog
));
942 nvc0_program_symbol_offset(const struct nvc0_program
*prog
, uint32_t label
)
944 const struct nv50_ir_prog_symbol
*syms
=
945 (const struct nv50_ir_prog_symbol
*)prog
->cp
.syms
;
948 if (prog
->type
!= PIPE_SHADER_COMPUTE
)
949 base
= NVC0_SHADER_HEADER_SIZE
;
950 for (i
= 0; i
< prog
->cp
.num_syms
; ++i
)
951 if (syms
[i
].label
== label
)
952 return prog
->code_base
+ base
+ syms
[i
].offset
;
953 return prog
->code_base
; /* no symbols or symbol not found */
957 nvc0_program_init_tcp_empty(struct nvc0_context
*nvc0
)
959 struct ureg_program
*ureg
;
961 ureg
= ureg_create(PIPE_SHADER_TESS_CTRL
);
965 ureg_property(ureg
, TGSI_PROPERTY_TCS_VERTICES_OUT
, 1);
968 nvc0
->tcp_empty
= ureg_create_shader_and_destroy(ureg
, &nvc0
->base
.pipe
);