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_EDGEFLAG
: return ~0;
85 assert(!"invalid TGSI output semantic");
91 nvc0_vp_assign_input_slots(struct nv50_ir_prog_info
*info
)
95 for (n
= 0, i
= 0; i
< info
->numInputs
; ++i
) {
96 switch (info
->in
[i
].sn
) {
97 case TGSI_SEMANTIC_INSTANCEID
: /* for SM4 only, in TGSI they're SVs */
98 case TGSI_SEMANTIC_VERTEXID
:
99 info
->in
[i
].mask
= 0x1;
100 info
->in
[i
].slot
[0] =
101 nvc0_shader_input_address(info
->in
[i
].sn
, 0) / 4;
106 for (c
= 0; c
< 4; ++c
)
107 info
->in
[i
].slot
[c
] = (0x80 + n
* 0x10 + c
* 0x4) / 4;
115 nvc0_sp_assign_input_slots(struct nv50_ir_prog_info
*info
)
120 for (i
= 0; i
< info
->numInputs
; ++i
) {
121 offset
= nvc0_shader_input_address(info
->in
[i
].sn
, info
->in
[i
].si
);
123 for (c
= 0; c
< 4; ++c
)
124 info
->in
[i
].slot
[c
] = (offset
+ c
* 0x4) / 4;
131 nvc0_fp_assign_output_slots(struct nv50_ir_prog_info
*info
)
133 unsigned count
= info
->prop
.fp
.numColourResults
* 4;
136 for (i
= 0; i
< info
->numOutputs
; ++i
)
137 if (info
->out
[i
].sn
== TGSI_SEMANTIC_COLOR
)
138 for (c
= 0; c
< 4; ++c
)
139 info
->out
[i
].slot
[c
] = info
->out
[i
].si
* 4 + c
;
141 if (info
->io
.sampleMask
< PIPE_MAX_SHADER_OUTPUTS
)
142 info
->out
[info
->io
.sampleMask
].slot
[0] = count
++;
144 if (info
->target
>= 0xe0)
145 count
++; /* on Kepler, depth is always last colour reg + 2 */
147 if (info
->io
.fragDepth
< PIPE_MAX_SHADER_OUTPUTS
)
148 info
->out
[info
->io
.fragDepth
].slot
[2] = count
;
154 nvc0_sp_assign_output_slots(struct nv50_ir_prog_info
*info
)
159 for (i
= 0; i
< info
->numOutputs
; ++i
) {
160 offset
= nvc0_shader_output_address(info
->out
[i
].sn
, info
->out
[i
].si
);
162 for (c
= 0; c
< 4; ++c
)
163 info
->out
[i
].slot
[c
] = (offset
+ c
* 0x4) / 4;
170 nvc0_program_assign_varying_slots(struct nv50_ir_prog_info
*info
)
174 if (info
->type
== PIPE_SHADER_VERTEX
)
175 ret
= nvc0_vp_assign_input_slots(info
);
177 ret
= nvc0_sp_assign_input_slots(info
);
181 if (info
->type
== PIPE_SHADER_FRAGMENT
)
182 ret
= nvc0_fp_assign_output_slots(info
);
184 ret
= nvc0_sp_assign_output_slots(info
);
189 nvc0_vtgp_hdr_update_oread(struct nvc0_program
*vp
, uint8_t slot
)
191 uint8_t min
= (vp
->hdr
[4] >> 12) & 0xff;
192 uint8_t max
= (vp
->hdr
[4] >> 24);
194 min
= MIN2(min
, slot
);
195 max
= MAX2(max
, slot
);
197 vp
->hdr
[4] = (max
<< 24) | (min
<< 12);
200 /* Common part of header generation for VP, TCP, TEP and GP. */
202 nvc0_vtgp_gen_header(struct nvc0_program
*vp
, struct nv50_ir_prog_info
*info
)
206 for (i
= 0; i
< info
->numInputs
; ++i
) {
207 if (info
->in
[i
].patch
)
209 for (c
= 0; c
< 4; ++c
) {
210 a
= info
->in
[i
].slot
[c
];
211 if (info
->in
[i
].mask
& (1 << c
))
212 vp
->hdr
[5 + a
/ 32] |= 1 << (a
% 32);
216 for (i
= 0; i
< info
->numOutputs
; ++i
) {
217 if (info
->out
[i
].patch
)
219 for (c
= 0; c
< 4; ++c
) {
220 if (!(info
->out
[i
].mask
& (1 << c
)))
222 assert(info
->out
[i
].slot
[c
] >= 0x40 / 4);
223 a
= info
->out
[i
].slot
[c
] - 0x40 / 4;
224 vp
->hdr
[13 + a
/ 32] |= 1 << (a
% 32);
225 if (info
->out
[i
].oread
)
226 nvc0_vtgp_hdr_update_oread(vp
, info
->out
[i
].slot
[c
]);
230 for (i
= 0; i
< info
->numSysVals
; ++i
) {
231 switch (info
->sv
[i
].sn
) {
232 case TGSI_SEMANTIC_PRIMID
:
233 vp
->hdr
[5] |= 1 << 24;
235 case TGSI_SEMANTIC_INSTANCEID
:
236 vp
->hdr
[10] |= 1 << 30;
238 case TGSI_SEMANTIC_VERTEXID
:
239 vp
->hdr
[10] |= 1 << 31;
241 case TGSI_SEMANTIC_TESSCOORD
:
242 /* We don't have the mask, nor the slots populated. While this could
243 * be achieved, the vast majority of the time if either of the coords
244 * are read, then both will be read.
246 nvc0_vtgp_hdr_update_oread(vp
, 0x2f0 / 4);
247 nvc0_vtgp_hdr_update_oread(vp
, 0x2f4 / 4);
254 vp
->vp
.clip_enable
= (1 << info
->io
.clipDistances
) - 1;
256 ((1 << info
->io
.cullDistances
) - 1) << info
->io
.clipDistances
;
257 for (i
= 0; i
< info
->io
.cullDistances
; ++i
)
258 vp
->vp
.clip_mode
|= 1 << ((info
->io
.clipDistances
+ i
) * 4);
260 if (info
->io
.genUserClip
< 0)
261 vp
->vp
.num_ucps
= PIPE_MAX_CLIP_PLANES
+ 1; /* prevent rebuilding */
267 nvc0_vp_gen_header(struct nvc0_program
*vp
, struct nv50_ir_prog_info
*info
)
269 vp
->hdr
[0] = 0x20061 | (1 << 10);
270 vp
->hdr
[4] = 0xff000;
272 return nvc0_vtgp_gen_header(vp
, info
);
276 nvc0_tp_get_tess_mode(struct nvc0_program
*tp
, struct nv50_ir_prog_info
*info
)
278 if (info
->prop
.tp
.outputPrim
== PIPE_PRIM_MAX
) {
279 tp
->tp
.tess_mode
= ~0;
282 switch (info
->prop
.tp
.domain
) {
283 case PIPE_PRIM_LINES
:
284 tp
->tp
.tess_mode
= NVC0_3D_TESS_MODE_PRIM_ISOLINES
;
286 case PIPE_PRIM_TRIANGLES
:
287 tp
->tp
.tess_mode
= NVC0_3D_TESS_MODE_PRIM_TRIANGLES
;
289 case PIPE_PRIM_QUADS
:
290 tp
->tp
.tess_mode
= NVC0_3D_TESS_MODE_PRIM_QUADS
;
293 tp
->tp
.tess_mode
= ~0;
297 if (info
->prop
.tp
.winding
> 0)
298 tp
->tp
.tess_mode
|= NVC0_3D_TESS_MODE_CW
;
300 if (info
->prop
.tp
.outputPrim
!= PIPE_PRIM_POINTS
)
301 tp
->tp
.tess_mode
|= NVC0_3D_TESS_MODE_CONNECTED
;
303 switch (info
->prop
.tp
.partitioning
) {
304 case PIPE_TESS_SPACING_EQUAL
:
305 tp
->tp
.tess_mode
|= NVC0_3D_TESS_MODE_SPACING_EQUAL
;
307 case PIPE_TESS_SPACING_FRACTIONAL_ODD
:
308 tp
->tp
.tess_mode
|= NVC0_3D_TESS_MODE_SPACING_FRACTIONAL_ODD
;
310 case PIPE_TESS_SPACING_FRACTIONAL_EVEN
:
311 tp
->tp
.tess_mode
|= NVC0_3D_TESS_MODE_SPACING_FRACTIONAL_EVEN
;
314 assert(!"invalid tessellator partitioning");
320 nvc0_tcp_gen_header(struct nvc0_program
*tcp
, struct nv50_ir_prog_info
*info
)
322 unsigned opcs
= 6; /* output patch constants (at least the TessFactors) */
324 tcp
->tp
.input_patch_size
= info
->prop
.tp
.inputPatchSize
;
326 if (info
->numPatchConstants
)
327 opcs
= 8 + info
->numPatchConstants
* 4;
329 tcp
->hdr
[0] = 0x20061 | (2 << 10);
331 tcp
->hdr
[1] = opcs
<< 24;
332 tcp
->hdr
[2] = info
->prop
.tp
.outputPatchSize
<< 24;
334 tcp
->hdr
[4] = 0xff000; /* initial min/max parallel output read address */
336 nvc0_vtgp_gen_header(tcp
, info
);
338 nvc0_tp_get_tess_mode(tcp
, info
);
344 nvc0_tep_gen_header(struct nvc0_program
*tep
, struct nv50_ir_prog_info
*info
)
346 tep
->tp
.input_patch_size
= ~0;
348 tep
->hdr
[0] = 0x20061 | (3 << 10);
349 tep
->hdr
[4] = 0xff000;
351 nvc0_vtgp_gen_header(tep
, info
);
353 nvc0_tp_get_tess_mode(tep
, info
);
355 tep
->hdr
[18] |= 0x3 << 12; /* ? */
361 nvc0_gp_gen_header(struct nvc0_program
*gp
, struct nv50_ir_prog_info
*info
)
363 gp
->hdr
[0] = 0x20061 | (4 << 10);
365 gp
->hdr
[2] = MIN2(info
->prop
.gp
.instanceCount
, 32) << 24;
367 switch (info
->prop
.gp
.outputPrim
) {
368 case PIPE_PRIM_POINTS
:
369 gp
->hdr
[3] = 0x01000000;
370 gp
->hdr
[0] |= 0xf0000000;
372 case PIPE_PRIM_LINE_STRIP
:
373 gp
->hdr
[3] = 0x06000000;
374 gp
->hdr
[0] |= 0x10000000;
376 case PIPE_PRIM_TRIANGLE_STRIP
:
377 gp
->hdr
[3] = 0x07000000;
378 gp
->hdr
[0] |= 0x10000000;
385 gp
->hdr
[4] = MIN2(info
->prop
.gp
.maxVertices
, 1024);
387 return nvc0_vtgp_gen_header(gp
, info
);
390 #define NVC0_INTERP_FLAT (1 << 0)
391 #define NVC0_INTERP_PERSPECTIVE (2 << 0)
392 #define NVC0_INTERP_LINEAR (3 << 0)
393 #define NVC0_INTERP_CENTROID (1 << 2)
396 nvc0_hdr_interp_mode(const struct nv50_ir_varying
*var
)
399 return NVC0_INTERP_LINEAR
;
401 return NVC0_INTERP_FLAT
;
402 return NVC0_INTERP_PERSPECTIVE
;
406 nvc0_fp_gen_header(struct nvc0_program
*fp
, struct nv50_ir_prog_info
*info
)
410 /* just 00062 on Kepler */
411 fp
->hdr
[0] = 0x20062 | (5 << 10);
412 fp
->hdr
[5] = 0x80000000; /* getting a trap if FRAG_COORD_UMASK.w = 0 */
414 if (info
->prop
.fp
.usesDiscard
)
415 fp
->hdr
[0] |= 0x8000;
416 if (info
->prop
.fp
.numColourResults
> 1)
417 fp
->hdr
[0] |= 0x4000;
418 if (info
->io
.sampleMask
< PIPE_MAX_SHADER_OUTPUTS
)
420 if (info
->prop
.fp
.writesDepth
) {
422 fp
->flags
[0] = 0x11; /* deactivate ZCULL */
425 for (i
= 0; i
< info
->numInputs
; ++i
) {
426 m
= nvc0_hdr_interp_mode(&info
->in
[i
]);
427 if (info
->in
[i
].sn
== TGSI_SEMANTIC_COLOR
) {
428 fp
->fp
.colors
|= 1 << info
->in
[i
].si
;
430 fp
->fp
.color_interp
[info
->in
[i
].si
] = m
| (info
->in
[i
].mask
<< 4);
432 for (c
= 0; c
< 4; ++c
) {
433 if (!(info
->in
[i
].mask
& (1 << c
)))
435 a
= info
->in
[i
].slot
[c
];
436 if (info
->in
[i
].slot
[0] >= (0x060 / 4) &&
437 info
->in
[i
].slot
[0] <= (0x07c / 4)) {
438 fp
->hdr
[5] |= 1 << (24 + (a
- 0x060 / 4));
440 if (info
->in
[i
].slot
[0] >= (0x2c0 / 4) &&
441 info
->in
[i
].slot
[0] <= (0x2fc / 4)) {
442 fp
->hdr
[14] |= (1 << (a
- 0x280 / 4)) & 0x07ff0000;
444 if (info
->in
[i
].slot
[c
] < (0x040 / 4) ||
445 info
->in
[i
].slot
[c
] > (0x380 / 4))
448 if (info
->in
[i
].slot
[0] >= (0x300 / 4))
450 fp
->hdr
[4 + a
/ 32] |= m
<< (a
% 32);
455 for (i
= 0; i
< info
->numOutputs
; ++i
) {
456 if (info
->out
[i
].sn
== TGSI_SEMANTIC_COLOR
)
457 fp
->hdr
[18] |= 0xf << info
->out
[i
].slot
[0];
460 /* There are no "regular" attachments, but the shader still needs to be
461 * executed. It seems like it wants to think that it has some color
462 * outputs in order to actually run.
464 if (info
->prop
.fp
.numColourResults
== 0 && !info
->prop
.fp
.writesDepth
)
467 fp
->fp
.early_z
= info
->prop
.fp
.earlyFragTests
;
468 fp
->fp
.sample_mask_in
= info
->prop
.fp
.usesSampleMaskIn
;
473 static struct nvc0_transform_feedback_state
*
474 nvc0_program_create_tfb_state(const struct nv50_ir_prog_info
*info
,
475 const struct pipe_stream_output_info
*pso
)
477 struct nvc0_transform_feedback_state
*tfb
;
480 tfb
= MALLOC_STRUCT(nvc0_transform_feedback_state
);
483 for (b
= 0; b
< 4; ++b
) {
484 tfb
->stride
[b
] = pso
->stride
[b
] * 4;
485 tfb
->varying_count
[b
] = 0;
487 memset(tfb
->varying_index
, 0xff, sizeof(tfb
->varying_index
)); /* = skip */
489 for (i
= 0; i
< pso
->num_outputs
; ++i
) {
490 unsigned s
= pso
->output
[i
].start_component
;
491 unsigned p
= pso
->output
[i
].dst_offset
;
492 b
= pso
->output
[i
].output_buffer
;
494 for (c
= 0; c
< pso
->output
[i
].num_components
; ++c
)
495 tfb
->varying_index
[b
][p
++] =
496 info
->out
[pso
->output
[i
].register_index
].slot
[s
+ c
];
498 tfb
->varying_count
[b
] = MAX2(tfb
->varying_count
[b
], p
);
499 tfb
->stream
[b
] = pso
->output
[i
].stream
;
501 for (b
= 0; b
< 4; ++b
) // zero unused indices (looks nicer)
502 for (c
= tfb
->varying_count
[b
]; c
& 3; ++c
)
503 tfb
->varying_index
[b
][c
] = 0;
510 nvc0_program_dump(struct nvc0_program
*prog
)
514 if (prog
->type
!= PIPE_SHADER_COMPUTE
) {
515 for (pos
= 0; pos
< ARRAY_SIZE(prog
->hdr
); ++pos
)
516 debug_printf("HDR[%02"PRIxPTR
"] = 0x%08x\n",
517 pos
* sizeof(prog
->hdr
[0]), prog
->hdr
[pos
]);
519 debug_printf("shader binary code (0x%x bytes):", prog
->code_size
);
520 for (pos
= 0; pos
< prog
->code_size
/ 4; ++pos
) {
523 debug_printf("%08x ", prog
->code
[pos
]);
530 nvc0_program_translate(struct nvc0_program
*prog
, uint16_t chipset
,
531 struct pipe_debug_callback
*debug
)
533 struct nv50_ir_prog_info
*info
;
536 info
= CALLOC_STRUCT(nv50_ir_prog_info
);
540 info
->type
= prog
->type
;
541 info
->target
= chipset
;
542 info
->bin
.sourceRep
= NV50_PROGRAM_IR_TGSI
;
543 info
->bin
.source
= (void *)prog
->pipe
.tokens
;
545 info
->io
.genUserClip
= prog
->vp
.num_ucps
;
546 info
->io
.auxCBSlot
= 15;
547 info
->io
.ucpBase
= NVC0_CB_AUX_UCP_INFO
;
548 info
->io
.drawInfoBase
= NVC0_CB_AUX_DRAW_INFO
;
550 if (prog
->type
== PIPE_SHADER_COMPUTE
) {
551 if (chipset
>= NVISA_GK104_CHIPSET
) {
552 info
->io
.auxCBSlot
= 7;
553 info
->io
.texBindBase
= NVC0_CB_AUX_TEX_INFO(0);
554 info
->prop
.cp
.gridInfoBase
= NVC0_CB_AUX_GRID_INFO
;
555 info
->io
.uboInfoBase
= NVC0_CB_AUX_UBO_INFO(0);
556 info
->io
.suInfoBase
= NVC0_CB_AUX_SU_INFO(0);
558 info
->io
.suInfoBase
= 0; /* TODO */
560 info
->io
.msInfoCBSlot
= 0;
561 info
->io
.msInfoBase
= NVC0_CB_AUX_MS_INFO
;
562 info
->io
.bufInfoBase
= NVC0_CB_AUX_BUF_INFO(0);
564 if (chipset
>= NVISA_GK104_CHIPSET
) {
565 info
->io
.texBindBase
= NVC0_CB_AUX_TEX_INFO(0);
566 info
->io
.suInfoBase
= NVC0_CB_AUX_SU_INFO(0);
568 info
->io
.suInfoBase
= 0; /* TODO */
570 info
->io
.sampleInfoBase
= NVC0_CB_AUX_SAMPLE_INFO
;
571 info
->io
.bufInfoBase
= NVC0_CB_AUX_BUF_INFO(0);
572 info
->io
.msInfoCBSlot
= 15;
573 info
->io
.msInfoBase
= 0; /* TODO */
576 info
->assignSlots
= nvc0_program_assign_varying_slots
;
579 info
->optLevel
= debug_get_num_option("NV50_PROG_OPTIMIZE", 3);
580 info
->dbgFlags
= debug_get_num_option("NV50_PROG_DEBUG", 0);
585 ret
= nv50_ir_generate_code(info
);
587 NOUVEAU_ERR("shader translation failed: %i\n", ret
);
590 if (prog
->type
!= PIPE_SHADER_COMPUTE
)
591 FREE(info
->bin
.syms
);
593 prog
->code
= info
->bin
.code
;
594 prog
->code_size
= info
->bin
.codeSize
;
595 prog
->immd_data
= info
->immd
.buf
;
596 prog
->immd_size
= info
->immd
.bufSize
;
597 prog
->relocs
= info
->bin
.relocData
;
598 prog
->fixups
= info
->bin
.fixupData
;
599 prog
->num_gprs
= MAX2(4, (info
->bin
.maxGPR
+ 1));
600 prog
->num_barriers
= info
->numBarriers
;
602 prog
->vp
.need_vertex_id
= info
->io
.vertexId
< PIPE_MAX_SHADER_INPUTS
;
603 prog
->vp
.need_draw_parameters
= info
->prop
.vp
.usesDrawParameters
;
605 if (info
->io
.edgeFlagOut
< PIPE_MAX_ATTRIBS
)
606 info
->out
[info
->io
.edgeFlagOut
].mask
= 0; /* for headergen */
607 prog
->vp
.edgeflag
= info
->io
.edgeFlagIn
;
609 switch (prog
->type
) {
610 case PIPE_SHADER_VERTEX
:
611 ret
= nvc0_vp_gen_header(prog
, info
);
613 case PIPE_SHADER_TESS_CTRL
:
614 ret
= nvc0_tcp_gen_header(prog
, info
);
616 case PIPE_SHADER_TESS_EVAL
:
617 ret
= nvc0_tep_gen_header(prog
, info
);
619 case PIPE_SHADER_GEOMETRY
:
620 ret
= nvc0_gp_gen_header(prog
, info
);
622 case PIPE_SHADER_FRAGMENT
:
623 ret
= nvc0_fp_gen_header(prog
, info
);
625 case PIPE_SHADER_COMPUTE
:
626 prog
->cp
.syms
= info
->bin
.syms
;
627 prog
->cp
.num_syms
= info
->bin
.numSyms
;
631 NOUVEAU_ERR("unknown program type: %u\n", prog
->type
);
637 if (info
->bin
.tlsSpace
) {
638 assert(info
->bin
.tlsSpace
< (1 << 24));
639 prog
->hdr
[0] |= 1 << 26;
640 prog
->hdr
[1] |= align(info
->bin
.tlsSpace
, 0x10); /* l[] size */
641 prog
->need_tls
= true;
643 /* TODO: factor 2 only needed where joinat/precont is used,
644 * and we only have to count non-uniform branches
647 if ((info->maxCFDepth * 2) > 16) {
648 prog->hdr[2] |= (((info->maxCFDepth * 2) + 47) / 48) * 0x200;
649 prog->need_tls = true;
652 if (info
->io
.globalAccess
)
653 prog
->hdr
[0] |= 1 << 26;
654 if (info
->io
.globalAccess
& 0x2)
655 prog
->hdr
[0] |= 1 << 16;
657 prog
->hdr
[0] |= 1 << 27;
659 if (prog
->pipe
.stream_output
.num_outputs
)
660 prog
->tfb
= nvc0_program_create_tfb_state(info
,
661 &prog
->pipe
.stream_output
);
663 pipe_debug_message(debug
, SHADER_INFO
,
664 "type: %d, local: %d, gpr: %d, inst: %d, bytes: %d",
665 prog
->type
, info
->bin
.tlsSpace
, prog
->num_gprs
,
666 info
->bin
.instructions
, info
->bin
.codeSize
);
674 nvc0_program_upload_code(struct nvc0_context
*nvc0
, struct nvc0_program
*prog
)
676 struct nvc0_screen
*screen
= nvc0
->screen
;
677 const bool is_cp
= prog
->type
== PIPE_SHADER_COMPUTE
;
679 uint32_t size
= prog
->code_size
+ (is_cp
? 0 : NVC0_SHADER_HEADER_SIZE
);
680 uint32_t lib_pos
= screen
->lib_code
->start
;
683 /* c[] bindings need to be aligned to 0x100, but we could use relocations
685 if (prog
->immd_size
) {
686 prog
->immd_base
= size
;
687 size
= align(size
, 0x40);
688 size
+= prog
->immd_size
+ 0xc0; /* add 0xc0 for align 0x40 -> 0x100 */
690 /* On Fermi, SP_START_ID must be aligned to 0x40.
691 * On Kepler, the first instruction must be aligned to 0x80 because
692 * latency information is expected only at certain positions.
694 if (screen
->base
.class_3d
>= NVE4_3D_CLASS
)
695 size
= size
+ (is_cp
? 0x40 : 0x70);
696 size
= align(size
, 0x40);
698 ret
= nouveau_heap_alloc(screen
->text_heap
, size
, prog
, &prog
->mem
);
700 struct nouveau_heap
*heap
= screen
->text_heap
;
701 /* Note that the code library, which is allocated before anything else,
702 * does not have a priv pointer. We can stop once we hit it.
704 while (heap
->next
&& heap
->next
->priv
) {
705 struct nvc0_program
*evict
= heap
->next
->priv
;
706 nouveau_heap_free(&evict
->mem
);
708 debug_printf("WARNING: out of code space, evicting all shaders.\n");
709 ret
= nouveau_heap_alloc(heap
, size
, prog
, &prog
->mem
);
711 NOUVEAU_ERR("shader too large (0x%x) to fit in code space ?\n", size
);
714 IMMED_NVC0(nvc0
->base
.pushbuf
, NVC0_3D(SERIALIZE
), 0);
716 prog
->code_base
= prog
->mem
->start
;
717 prog
->immd_base
= align(prog
->mem
->start
+ prog
->immd_base
, 0x100);
718 assert((prog
->immd_size
== 0) || (prog
->immd_base
+ prog
->immd_size
<=
719 prog
->mem
->start
+ prog
->mem
->size
));
722 if (screen
->base
.class_3d
>= NVE4_3D_CLASS
) {
723 switch (prog
->mem
->start
& 0xff) {
724 case 0x40: prog
->code_base
+= 0x70; break;
725 case 0x80: prog
->code_base
+= 0x30; break;
726 case 0xc0: prog
->code_base
+= 0x70; break;
728 prog
->code_base
+= 0x30;
729 assert((prog
->mem
->start
& 0xff) == 0x00);
733 code_pos
= prog
->code_base
+ NVC0_SHADER_HEADER_SIZE
;
735 if (screen
->base
.class_3d
>= NVE4_3D_CLASS
) {
736 if (prog
->mem
->start
& 0x40)
737 prog
->code_base
+= 0x40;
738 assert((prog
->code_base
& 0x7f) == 0x00);
740 code_pos
= prog
->code_base
;
744 nv50_ir_relocate_code(prog
->relocs
, prog
->code
, code_pos
, lib_pos
, 0);
746 nv50_ir_apply_fixups(prog
->fixups
, prog
->code
,
747 prog
->fp
.force_persample_interp
,
749 for (int i
= 0; i
< 2; i
++) {
750 unsigned mask
= prog
->fp
.color_interp
[i
] >> 4;
751 unsigned interp
= prog
->fp
.color_interp
[i
] & 3;
754 prog
->hdr
[14] &= ~(0xff << (8 * i
));
755 if (prog
->fp
.flatshade
)
756 interp
= NVC0_INTERP_FLAT
;
757 for (int c
= 0; c
< 4; c
++)
759 prog
->hdr
[14] |= interp
<< (2 * (4 * i
+ c
));
764 if (debug_get_bool_option("NV50_PROG_DEBUG", false))
765 nvc0_program_dump(prog
);
769 nvc0
->base
.push_data(&nvc0
->base
, screen
->text
, prog
->code_base
,
770 NV_VRAM_DOMAIN(&screen
->base
), NVC0_SHADER_HEADER_SIZE
, prog
->hdr
);
771 nvc0
->base
.push_data(&nvc0
->base
, screen
->text
, code_pos
,
772 NV_VRAM_DOMAIN(&screen
->base
), prog
->code_size
, prog
->code
);
774 nvc0
->base
.push_data(&nvc0
->base
,
775 screen
->text
, prog
->immd_base
, NV_VRAM_DOMAIN(&screen
->base
),
776 prog
->immd_size
, prog
->immd_data
);
778 BEGIN_NVC0(nvc0
->base
.pushbuf
, NVC0_3D(MEM_BARRIER
), 1);
779 PUSH_DATA (nvc0
->base
.pushbuf
, 0x1011);
784 /* Upload code for builtin functions like integer division emulation. */
786 nvc0_program_library_upload(struct nvc0_context
*nvc0
)
788 struct nvc0_screen
*screen
= nvc0
->screen
;
791 const uint32_t *code
;
793 if (screen
->lib_code
)
796 nv50_ir_get_target_library(screen
->base
.device
->chipset
, &code
, &size
);
800 ret
= nouveau_heap_alloc(screen
->text_heap
, align(size
, 0x100), NULL
,
805 nvc0
->base
.push_data(&nvc0
->base
,
806 screen
->text
, screen
->lib_code
->start
, NV_VRAM_DOMAIN(&screen
->base
),
808 /* no need for a memory barrier, will be emitted with first program */
812 nvc0_program_destroy(struct nvc0_context
*nvc0
, struct nvc0_program
*prog
)
814 const struct pipe_shader_state pipe
= prog
->pipe
;
815 const ubyte type
= prog
->type
;
818 nouveau_heap_free(&prog
->mem
);
819 FREE(prog
->code
); /* may be 0 for hardcoded shaders */
820 FREE(prog
->immd_data
);
823 if (prog
->type
== PIPE_SHADER_COMPUTE
&& prog
->cp
.syms
)
826 if (nvc0
->state
.tfb
== prog
->tfb
)
827 nvc0
->state
.tfb
= NULL
;
831 memset(prog
, 0, sizeof(*prog
));
838 nvc0_program_symbol_offset(const struct nvc0_program
*prog
, uint32_t label
)
840 const struct nv50_ir_prog_symbol
*syms
=
841 (const struct nv50_ir_prog_symbol
*)prog
->cp
.syms
;
844 if (prog
->type
!= PIPE_SHADER_COMPUTE
)
845 base
= NVC0_SHADER_HEADER_SIZE
;
846 for (i
= 0; i
< prog
->cp
.num_syms
; ++i
)
847 if (syms
[i
].label
== label
)
848 return prog
->code_base
+ base
+ syms
[i
].offset
;
849 return prog
->code_base
; /* no symbols or symbol not found */
853 nvc0_program_init_tcp_empty(struct nvc0_context
*nvc0
)
855 struct ureg_program
*ureg
;
857 ureg
= ureg_create(PIPE_SHADER_TESS_CTRL
);
861 ureg_property(ureg
, TGSI_PROPERTY_TCS_VERTICES_OUT
, 1);
864 nvc0
->tcp_empty
= ureg_create_shader_and_destroy(ureg
, &nvc0
->base
.pipe
);