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 "nvc0/nvc0_context.h"
27 #include "codegen/nv50_ir_driver.h"
28 #include "nvc0/nve4_compute.h"
30 /* NOTE: Using a[0x270] in FP may cause an error even if we're using less than
31 * 124 scalar varying values.
34 nvc0_shader_input_address(unsigned sn
, unsigned si
, unsigned ubase
)
37 case NV50_SEMANTIC_TESSFACTOR
: return 0x000 + si
* 0x4;
38 case TGSI_SEMANTIC_PRIMID
: return 0x060;
39 case TGSI_SEMANTIC_LAYER
: return 0x064;
40 case TGSI_SEMANTIC_VIEWPORT_INDEX
:return 0x068;
41 case TGSI_SEMANTIC_PSIZE
: return 0x06c;
42 case TGSI_SEMANTIC_POSITION
: return 0x070;
43 case TGSI_SEMANTIC_GENERIC
: return ubase
+ si
* 0x10;
44 case TGSI_SEMANTIC_FOG
: return 0x2e8;
45 case TGSI_SEMANTIC_COLOR
: return 0x280 + si
* 0x10;
46 case TGSI_SEMANTIC_BCOLOR
: return 0x2a0 + si
* 0x10;
47 case NV50_SEMANTIC_CLIPDISTANCE
: return 0x2c0 + si
* 0x4;
48 case TGSI_SEMANTIC_CLIPDIST
: return 0x2c0 + si
* 0x10;
49 case TGSI_SEMANTIC_CLIPVERTEX
: return 0x270;
50 case TGSI_SEMANTIC_PCOORD
: return 0x2e0;
51 case NV50_SEMANTIC_TESSCOORD
: return 0x2f0;
52 case TGSI_SEMANTIC_INSTANCEID
: return 0x2f8;
53 case TGSI_SEMANTIC_VERTEXID
: return 0x2fc;
54 case TGSI_SEMANTIC_TEXCOORD
: return 0x300 + si
* 0x10;
55 case TGSI_SEMANTIC_FACE
: return 0x3fc;
57 assert(!"invalid TGSI input semantic");
63 nvc0_shader_output_address(unsigned sn
, unsigned si
, unsigned ubase
)
66 case NV50_SEMANTIC_TESSFACTOR
: return 0x000 + si
* 0x4;
67 case TGSI_SEMANTIC_PRIMID
: return 0x060;
68 case TGSI_SEMANTIC_LAYER
: return 0x064;
69 case TGSI_SEMANTIC_VIEWPORT_INDEX
:return 0x068;
70 case TGSI_SEMANTIC_PSIZE
: return 0x06c;
71 case TGSI_SEMANTIC_POSITION
: return 0x070;
72 case TGSI_SEMANTIC_GENERIC
: return ubase
+ si
* 0x10;
73 case TGSI_SEMANTIC_FOG
: return 0x2e8;
74 case TGSI_SEMANTIC_COLOR
: return 0x280 + si
* 0x10;
75 case TGSI_SEMANTIC_BCOLOR
: return 0x2a0 + si
* 0x10;
76 case NV50_SEMANTIC_CLIPDISTANCE
: return 0x2c0 + si
* 0x4;
77 case TGSI_SEMANTIC_CLIPDIST
: return 0x2c0 + si
* 0x10;
78 case TGSI_SEMANTIC_CLIPVERTEX
: return 0x270;
79 case TGSI_SEMANTIC_TEXCOORD
: return 0x300 + si
* 0x10;
80 case TGSI_SEMANTIC_EDGEFLAG
: return ~0;
82 assert(!"invalid TGSI output semantic");
88 nvc0_vp_assign_input_slots(struct nv50_ir_prog_info
*info
)
92 for (n
= 0, i
= 0; i
< info
->numInputs
; ++i
) {
93 switch (info
->in
[i
].sn
) {
94 case TGSI_SEMANTIC_INSTANCEID
: /* for SM4 only, in TGSI they're SVs */
95 case TGSI_SEMANTIC_VERTEXID
:
96 info
->in
[i
].mask
= 0x1;
98 nvc0_shader_input_address(info
->in
[i
].sn
, 0, 0) / 4;
103 for (c
= 0; c
< 4; ++c
)
104 info
->in
[i
].slot
[c
] = (0x80 + n
* 0x10 + c
* 0x4) / 4;
112 nvc0_sp_assign_input_slots(struct nv50_ir_prog_info
*info
)
114 unsigned ubase
= MAX2(0x80, 0x20 + info
->numPatchConstants
* 0x10);
118 for (i
= 0; i
< info
->numInputs
; ++i
) {
119 offset
= nvc0_shader_input_address(info
->in
[i
].sn
,
120 info
->in
[i
].si
, ubase
);
121 if (info
->in
[i
].patch
&& offset
>= 0x20)
122 offset
= 0x20 + info
->in
[i
].si
* 0x10;
124 if (info
->in
[i
].sn
== NV50_SEMANTIC_TESSCOORD
)
125 info
->in
[i
].mask
&= 3;
127 for (c
= 0; c
< 4; ++c
)
128 info
->in
[i
].slot
[c
] = (offset
+ c
* 0x4) / 4;
135 nvc0_fp_assign_output_slots(struct nv50_ir_prog_info
*info
)
137 unsigned count
= info
->prop
.fp
.numColourResults
* 4;
140 for (i
= 0; i
< info
->numOutputs
; ++i
)
141 if (info
->out
[i
].sn
== TGSI_SEMANTIC_COLOR
)
142 for (c
= 0; c
< 4; ++c
)
143 info
->out
[i
].slot
[c
] = info
->out
[i
].si
* 4 + c
;
145 if (info
->io
.sampleMask
< PIPE_MAX_SHADER_OUTPUTS
)
146 info
->out
[info
->io
.sampleMask
].slot
[0] = count
++;
148 if (info
->target
>= 0xe0)
149 count
++; /* on Kepler, depth is always last colour reg + 2 */
151 if (info
->io
.fragDepth
< PIPE_MAX_SHADER_OUTPUTS
)
152 info
->out
[info
->io
.fragDepth
].slot
[2] = count
;
158 nvc0_sp_assign_output_slots(struct nv50_ir_prog_info
*info
)
160 unsigned ubase
= MAX2(0x80, 0x20 + info
->numPatchConstants
* 0x10);
164 for (i
= 0; i
< info
->numOutputs
; ++i
) {
165 offset
= nvc0_shader_output_address(info
->out
[i
].sn
,
166 info
->out
[i
].si
, ubase
);
167 if (info
->out
[i
].patch
&& offset
>= 0x20)
168 offset
= 0x20 + info
->out
[i
].si
* 0x10;
170 for (c
= 0; c
< 4; ++c
)
171 info
->out
[i
].slot
[c
] = (offset
+ c
* 0x4) / 4;
178 nvc0_program_assign_varying_slots(struct nv50_ir_prog_info
*info
)
182 if (info
->type
== PIPE_SHADER_VERTEX
)
183 ret
= nvc0_vp_assign_input_slots(info
);
185 ret
= nvc0_sp_assign_input_slots(info
);
189 if (info
->type
== PIPE_SHADER_FRAGMENT
)
190 ret
= nvc0_fp_assign_output_slots(info
);
192 ret
= nvc0_sp_assign_output_slots(info
);
197 nvc0_vtgp_hdr_update_oread(struct nvc0_program
*vp
, uint8_t slot
)
199 uint8_t min
= (vp
->hdr
[4] >> 12) & 0xff;
200 uint8_t max
= (vp
->hdr
[4] >> 24);
202 min
= MIN2(min
, slot
);
203 max
= MAX2(max
, slot
);
205 vp
->hdr
[4] = (max
<< 24) | (min
<< 12);
208 /* Common part of header generation for VP, TCP, TEP and GP. */
210 nvc0_vtgp_gen_header(struct nvc0_program
*vp
, struct nv50_ir_prog_info
*info
)
214 for (i
= 0; i
< info
->numInputs
; ++i
) {
215 if (info
->in
[i
].patch
)
217 for (c
= 0; c
< 4; ++c
) {
218 a
= info
->in
[i
].slot
[c
];
219 if (info
->in
[i
].mask
& (1 << c
)) {
220 if (info
->in
[i
].sn
!= NV50_SEMANTIC_TESSCOORD
)
221 vp
->hdr
[5 + a
/ 32] |= 1 << (a
% 32);
223 nvc0_vtgp_hdr_update_oread(vp
, info
->in
[i
].slot
[c
]);
228 for (i
= 0; i
< info
->numOutputs
; ++i
) {
229 if (info
->out
[i
].patch
)
231 for (c
= 0; c
< 4; ++c
) {
232 if (!(info
->out
[i
].mask
& (1 << c
)))
234 assert(info
->out
[i
].slot
[c
] >= 0x40 / 4);
235 a
= info
->out
[i
].slot
[c
] - 0x40 / 4;
236 vp
->hdr
[13 + a
/ 32] |= 1 << (a
% 32);
237 if (info
->out
[i
].oread
)
238 nvc0_vtgp_hdr_update_oread(vp
, info
->out
[i
].slot
[c
]);
242 for (i
= 0; i
< info
->numSysVals
; ++i
) {
243 switch (info
->sv
[i
].sn
) {
244 case TGSI_SEMANTIC_PRIMID
:
245 vp
->hdr
[5] |= 1 << 24;
247 case TGSI_SEMANTIC_INSTANCEID
:
248 vp
->hdr
[10] |= 1 << 30;
250 case TGSI_SEMANTIC_VERTEXID
:
251 vp
->hdr
[10] |= 1 << 31;
258 vp
->vp
.clip_enable
= info
->io
.clipDistanceMask
;
259 for (i
= 0; i
< 8; ++i
)
260 if (info
->io
.cullDistanceMask
& (1 << i
))
261 vp
->vp
.clip_mode
|= 1 << (i
* 4);
263 if (info
->io
.genUserClip
< 0)
264 vp
->vp
.num_ucps
= PIPE_MAX_CLIP_PLANES
+ 1; /* prevent rebuilding */
270 nvc0_vp_gen_header(struct nvc0_program
*vp
, struct nv50_ir_prog_info
*info
)
272 vp
->hdr
[0] = 0x20061 | (1 << 10);
273 vp
->hdr
[4] = 0xff000;
275 vp
->hdr
[18] = info
->io
.clipDistanceMask
;
277 return nvc0_vtgp_gen_header(vp
, info
);
280 #if defined(PIPE_SHADER_HULL) || defined(PIPE_SHADER_DOMAIN)
282 nvc0_tp_get_tess_mode(struct nvc0_program
*tp
, struct nv50_ir_prog_info
*info
)
284 if (info
->prop
.tp
.outputPrim
== PIPE_PRIM_MAX
) {
285 tp
->tp
.tess_mode
= ~0;
288 switch (info
->prop
.tp
.domain
) {
289 case PIPE_PRIM_LINES
:
290 tp
->tp
.tess_mode
= NVC0_3D_TESS_MODE_PRIM_ISOLINES
;
292 case PIPE_PRIM_TRIANGLES
:
293 tp
->tp
.tess_mode
= NVC0_3D_TESS_MODE_PRIM_TRIANGLES
;
294 if (info
->prop
.tp
.winding
> 0)
295 tp
->tp
.tess_mode
|= NVC0_3D_TESS_MODE_CW
;
297 case PIPE_PRIM_QUADS
:
298 tp
->tp
.tess_mode
= NVC0_3D_TESS_MODE_PRIM_QUADS
;
301 tp
->tp
.tess_mode
= ~0;
304 if (info
->prop
.tp
.outputPrim
!= PIPE_PRIM_POINTS
)
305 tp
->tp
.tess_mode
|= NVC0_3D_TESS_MODE_CONNECTED
;
307 switch (info
->prop
.tp
.partitioning
) {
308 case PIPE_TESS_PART_INTEGER
:
309 case PIPE_TESS_PART_POW2
:
310 tp
->tp
.tess_mode
|= NVC0_3D_TESS_MODE_SPACING_EQUAL
;
312 case PIPE_TESS_PART_FRACT_ODD
:
313 tp
->tp
.tess_mode
|= NVC0_3D_TESS_MODE_SPACING_FRACTIONAL_ODD
;
315 case PIPE_TESS_PART_FRACT_EVEN
:
316 tp
->tp
.tess_mode
|= NVC0_3D_TESS_MODE_SPACING_FRACTIONAL_EVEN
;
319 assert(!"invalid tessellator partitioning");
325 #ifdef PIPE_SHADER_HULL
327 nvc0_tcp_gen_header(struct nvc0_program
*tcp
, struct nv50_ir_prog_info
*info
)
329 unsigned opcs
= 6; /* output patch constants (at least the TessFactors) */
331 tcp
->tp
.input_patch_size
= info
->prop
.tp
.inputPatchSize
;
333 if (info
->numPatchConstants
)
334 opcs
= 8 + info
->numPatchConstants
* 4;
336 tcp
->hdr
[0] = 0x20061 | (2 << 10);
338 tcp
->hdr
[1] = opcs
<< 24;
339 tcp
->hdr
[2] = info
->prop
.tp
.outputPatchSize
<< 24;
341 tcp
->hdr
[4] = 0xff000; /* initial min/max parallel output read address */
343 nvc0_vtgp_gen_header(tcp
, info
);
345 nvc0_tp_get_tess_mode(tcp
, info
);
351 #ifdef PIPE_SHADER_DOMAIN
353 nvc0_tep_gen_header(struct nvc0_program
*tep
, struct nv50_ir_prog_info
*info
)
355 tep
->tp
.input_patch_size
= ~0;
357 tep
->hdr
[0] = 0x20061 | (3 << 10);
358 tep
->hdr
[4] = 0xff000;
360 nvc0_vtgp_gen_header(tep
, info
);
362 nvc0_tp_get_tess_mode(tep
, info
);
364 tep
->hdr
[18] |= 0x3 << 12; /* ? */
371 nvc0_gp_gen_header(struct nvc0_program
*gp
, struct nv50_ir_prog_info
*info
)
373 gp
->hdr
[0] = 0x20061 | (4 << 10);
375 gp
->hdr
[2] = MIN2(info
->prop
.gp
.instanceCount
, 32) << 24;
377 switch (info
->prop
.gp
.outputPrim
) {
378 case PIPE_PRIM_POINTS
:
379 gp
->hdr
[3] = 0x01000000;
380 gp
->hdr
[0] |= 0xf0000000;
382 case PIPE_PRIM_LINE_STRIP
:
383 gp
->hdr
[3] = 0x06000000;
384 gp
->hdr
[0] |= 0x10000000;
386 case PIPE_PRIM_TRIANGLE_STRIP
:
387 gp
->hdr
[3] = 0x07000000;
388 gp
->hdr
[0] |= 0x10000000;
395 gp
->hdr
[4] = MIN2(info
->prop
.gp
.maxVertices
, 1024);
397 return nvc0_vtgp_gen_header(gp
, info
);
400 #define NVC0_INTERP_FLAT (1 << 0)
401 #define NVC0_INTERP_PERSPECTIVE (2 << 0)
402 #define NVC0_INTERP_LINEAR (3 << 0)
403 #define NVC0_INTERP_CENTROID (1 << 2)
406 nvc0_hdr_interp_mode(const struct nv50_ir_varying
*var
)
409 return NVC0_INTERP_LINEAR
;
411 return NVC0_INTERP_FLAT
;
412 return NVC0_INTERP_PERSPECTIVE
;
416 nvc0_fp_gen_header(struct nvc0_program
*fp
, struct nv50_ir_prog_info
*info
)
420 /* just 00062 on Kepler */
421 fp
->hdr
[0] = 0x20062 | (5 << 10);
422 fp
->hdr
[5] = 0x80000000; /* getting a trap if FRAG_COORD_UMASK.w = 0 */
424 if (info
->prop
.fp
.usesDiscard
)
425 fp
->hdr
[0] |= 0x8000;
426 if (info
->prop
.fp
.numColourResults
> 1)
427 fp
->hdr
[0] |= 0x4000;
428 if (info
->io
.sampleMask
< PIPE_MAX_SHADER_OUTPUTS
)
430 if (info
->prop
.fp
.writesDepth
) {
432 fp
->flags
[0] = 0x11; /* deactivate ZCULL */
435 for (i
= 0; i
< info
->numInputs
; ++i
) {
436 m
= nvc0_hdr_interp_mode(&info
->in
[i
]);
437 for (c
= 0; c
< 4; ++c
) {
438 if (!(info
->in
[i
].mask
& (1 << c
)))
440 a
= info
->in
[i
].slot
[c
];
441 if (info
->in
[i
].slot
[0] >= (0x060 / 4) &&
442 info
->in
[i
].slot
[0] <= (0x07c / 4)) {
443 fp
->hdr
[5] |= 1 << (24 + (a
- 0x060 / 4));
445 if (info
->in
[i
].slot
[0] >= (0x2c0 / 4) &&
446 info
->in
[i
].slot
[0] <= (0x2fc / 4)) {
447 fp
->hdr
[14] |= (1 << (a
- 0x280 / 4)) & 0x07ff0000;
449 if (info
->in
[i
].slot
[c
] < (0x040 / 4) ||
450 info
->in
[i
].slot
[c
] > (0x380 / 4))
453 if (info
->in
[i
].slot
[0] >= (0x300 / 4))
455 fp
->hdr
[4 + a
/ 32] |= m
<< (a
% 32);
460 for (i
= 0; i
< info
->numOutputs
; ++i
) {
461 if (info
->out
[i
].sn
== TGSI_SEMANTIC_COLOR
)
462 fp
->hdr
[18] |= info
->out
[i
].mask
<< info
->out
[i
].slot
[0];
465 fp
->fp
.early_z
= info
->prop
.fp
.earlyFragTests
;
470 static struct nvc0_transform_feedback_state
*
471 nvc0_program_create_tfb_state(const struct nv50_ir_prog_info
*info
,
472 const struct pipe_stream_output_info
*pso
)
474 struct nvc0_transform_feedback_state
*tfb
;
477 tfb
= MALLOC_STRUCT(nvc0_transform_feedback_state
);
480 for (b
= 0; b
< 4; ++b
) {
481 tfb
->stride
[b
] = pso
->stride
[b
] * 4;
482 tfb
->varying_count
[b
] = 0;
484 memset(tfb
->varying_index
, 0xff, sizeof(tfb
->varying_index
)); /* = skip */
486 for (i
= 0; i
< pso
->num_outputs
; ++i
) {
487 unsigned s
= pso
->output
[i
].start_component
;
488 unsigned p
= pso
->output
[i
].dst_offset
;
489 b
= pso
->output
[i
].output_buffer
;
491 for (c
= 0; c
< pso
->output
[i
].num_components
; ++c
)
492 tfb
->varying_index
[b
][p
++] =
493 info
->out
[pso
->output
[i
].register_index
].slot
[s
+ c
];
495 tfb
->varying_count
[b
] = MAX2(tfb
->varying_count
[b
], p
);
496 tfb
->stream
[b
] = pso
->output
[i
].stream
;
498 for (b
= 0; b
< 4; ++b
) // zero unused indices (looks nicer)
499 for (c
= tfb
->varying_count
[b
]; c
& 3; ++c
)
500 tfb
->varying_index
[b
][c
] = 0;
507 nvc0_program_dump(struct nvc0_program
*prog
)
511 if (prog
->type
!= PIPE_SHADER_COMPUTE
) {
512 for (pos
= 0; pos
< sizeof(prog
->hdr
) / sizeof(prog
->hdr
[0]); ++pos
)
513 debug_printf("HDR[%02"PRIxPTR
"] = 0x%08x\n",
514 pos
* sizeof(prog
->hdr
[0]), prog
->hdr
[pos
]);
516 debug_printf("shader binary code (0x%x bytes):", prog
->code_size
);
517 for (pos
= 0; pos
< prog
->code_size
/ 4; ++pos
) {
520 debug_printf("%08x ", prog
->code
[pos
]);
527 nvc0_program_translate(struct nvc0_program
*prog
, uint16_t chipset
)
529 struct nv50_ir_prog_info
*info
;
532 info
= CALLOC_STRUCT(nv50_ir_prog_info
);
536 info
->type
= prog
->type
;
537 info
->target
= chipset
;
538 info
->bin
.sourceRep
= NV50_PROGRAM_IR_TGSI
;
539 info
->bin
.source
= (void *)prog
->pipe
.tokens
;
541 info
->io
.genUserClip
= prog
->vp
.num_ucps
;
542 info
->io
.ucpBase
= 256;
543 info
->io
.ucpCBSlot
= 15;
544 info
->io
.sampleInterp
= prog
->fp
.sample_interp
;
546 if (prog
->type
== PIPE_SHADER_COMPUTE
) {
547 if (chipset
>= NVISA_GK104_CHIPSET
) {
548 info
->io
.resInfoCBSlot
= 0;
549 info
->io
.texBindBase
= NVE4_CP_INPUT_TEX(0);
550 info
->io
.suInfoBase
= NVE4_CP_INPUT_SUF(0);
551 info
->prop
.cp
.gridInfoBase
= NVE4_CP_INPUT_GRID_INFO(0);
553 info
->io
.msInfoCBSlot
= 0;
554 info
->io
.msInfoBase
= NVE4_CP_INPUT_MS_OFFSETS
;
556 if (chipset
>= NVISA_GK104_CHIPSET
) {
557 info
->io
.texBindBase
= 0x20;
558 info
->io
.suInfoBase
= 0; /* TODO */
560 info
->io
.resInfoCBSlot
= 15;
561 info
->io
.sampleInfoBase
= 256 + 128;
562 info
->io
.msInfoCBSlot
= 15;
563 info
->io
.msInfoBase
= 0; /* TODO */
566 info
->assignSlots
= nvc0_program_assign_varying_slots
;
569 info
->optLevel
= debug_get_num_option("NV50_PROG_OPTIMIZE", 3);
570 info
->dbgFlags
= debug_get_num_option("NV50_PROG_DEBUG", 0);
575 ret
= nv50_ir_generate_code(info
);
577 NOUVEAU_ERR("shader translation failed: %i\n", ret
);
580 if (prog
->type
!= PIPE_SHADER_COMPUTE
)
581 FREE(info
->bin
.syms
);
583 prog
->code
= info
->bin
.code
;
584 prog
->code_size
= info
->bin
.codeSize
;
585 prog
->immd_data
= info
->immd
.buf
;
586 prog
->immd_size
= info
->immd
.bufSize
;
587 prog
->relocs
= info
->bin
.relocData
;
588 prog
->num_gprs
= MAX2(4, (info
->bin
.maxGPR
+ 1));
589 prog
->num_barriers
= info
->numBarriers
;
591 prog
->vp
.need_vertex_id
= info
->io
.vertexId
< PIPE_MAX_SHADER_INPUTS
;
593 if (info
->io
.edgeFlagOut
< PIPE_MAX_ATTRIBS
)
594 info
->out
[info
->io
.edgeFlagOut
].mask
= 0; /* for headergen */
595 prog
->vp
.edgeflag
= info
->io
.edgeFlagIn
;
597 switch (prog
->type
) {
598 case PIPE_SHADER_VERTEX
:
599 ret
= nvc0_vp_gen_header(prog
, info
);
601 #ifdef PIPE_SHADER_HULL
602 case PIPE_SHADER_HULL
:
603 ret
= nvc0_tcp_gen_header(prog
, info
);
606 #ifdef PIPE_SHADER_DOMAIN
607 case PIPE_SHADER_DOMAIN
:
608 ret
= nvc0_tep_gen_header(prog
, info
);
611 case PIPE_SHADER_GEOMETRY
:
612 ret
= nvc0_gp_gen_header(prog
, info
);
614 case PIPE_SHADER_FRAGMENT
:
615 ret
= nvc0_fp_gen_header(prog
, info
);
617 case PIPE_SHADER_COMPUTE
:
618 prog
->cp
.syms
= info
->bin
.syms
;
619 prog
->cp
.num_syms
= info
->bin
.numSyms
;
623 NOUVEAU_ERR("unknown program type: %u\n", prog
->type
);
629 if (info
->bin
.tlsSpace
) {
630 assert(info
->bin
.tlsSpace
< (1 << 24));
631 prog
->hdr
[0] |= 1 << 26;
632 prog
->hdr
[1] |= align(info
->bin
.tlsSpace
, 0x10); /* l[] size */
633 prog
->need_tls
= true;
635 /* TODO: factor 2 only needed where joinat/precont is used,
636 * and we only have to count non-uniform branches
639 if ((info->maxCFDepth * 2) > 16) {
640 prog->hdr[2] |= (((info->maxCFDepth * 2) + 47) / 48) * 0x200;
641 prog->need_tls = true;
644 if (info
->io
.globalAccess
)
645 prog
->hdr
[0] |= 1 << 16;
647 prog
->hdr
[0] |= 1 << 27;
649 if (prog
->pipe
.stream_output
.num_outputs
)
650 prog
->tfb
= nvc0_program_create_tfb_state(info
,
651 &prog
->pipe
.stream_output
);
659 nvc0_program_upload_code(struct nvc0_context
*nvc0
, struct nvc0_program
*prog
)
661 struct nvc0_screen
*screen
= nvc0
->screen
;
662 const bool is_cp
= prog
->type
== PIPE_SHADER_COMPUTE
;
664 uint32_t size
= prog
->code_size
+ (is_cp
? 0 : NVC0_SHADER_HEADER_SIZE
);
665 uint32_t lib_pos
= screen
->lib_code
->start
;
668 /* c[] bindings need to be aligned to 0x100, but we could use relocations
670 if (prog
->immd_size
) {
671 prog
->immd_base
= size
;
672 size
= align(size
, 0x40);
673 size
+= prog
->immd_size
+ 0xc0; /* add 0xc0 for align 0x40 -> 0x100 */
675 /* On Fermi, SP_START_ID must be aligned to 0x40.
676 * On Kepler, the first instruction must be aligned to 0x80 because
677 * latency information is expected only at certain positions.
679 if (screen
->base
.class_3d
>= NVE4_3D_CLASS
)
680 size
= size
+ (is_cp
? 0x40 : 0x70);
681 size
= align(size
, 0x40);
683 ret
= nouveau_heap_alloc(screen
->text_heap
, size
, prog
, &prog
->mem
);
685 struct nouveau_heap
*heap
= screen
->text_heap
;
686 /* Note that the code library, which is allocated before anything else,
687 * does not have a priv pointer. We can stop once we hit it.
689 while (heap
->next
&& heap
->next
->priv
) {
690 struct nvc0_program
*evict
= heap
->next
->priv
;
691 nouveau_heap_free(&evict
->mem
);
693 debug_printf("WARNING: out of code space, evicting all shaders.\n");
694 ret
= nouveau_heap_alloc(heap
, size
, prog
, &prog
->mem
);
696 NOUVEAU_ERR("shader too large (0x%x) to fit in code space ?\n", size
);
699 IMMED_NVC0(nvc0
->base
.pushbuf
, NVC0_3D(SERIALIZE
), 0);
701 prog
->code_base
= prog
->mem
->start
;
702 prog
->immd_base
= align(prog
->mem
->start
+ prog
->immd_base
, 0x100);
703 assert((prog
->immd_size
== 0) || (prog
->immd_base
+ prog
->immd_size
<=
704 prog
->mem
->start
+ prog
->mem
->size
));
707 if (screen
->base
.class_3d
>= NVE4_3D_CLASS
) {
708 switch (prog
->mem
->start
& 0xff) {
709 case 0x40: prog
->code_base
+= 0x70; break;
710 case 0x80: prog
->code_base
+= 0x30; break;
711 case 0xc0: prog
->code_base
+= 0x70; break;
713 prog
->code_base
+= 0x30;
714 assert((prog
->mem
->start
& 0xff) == 0x00);
718 code_pos
= prog
->code_base
+ NVC0_SHADER_HEADER_SIZE
;
720 if (screen
->base
.class_3d
>= NVE4_3D_CLASS
) {
721 if (prog
->mem
->start
& 0x40)
722 prog
->code_base
+= 0x40;
723 assert((prog
->code_base
& 0x7f) == 0x00);
725 code_pos
= prog
->code_base
;
729 nv50_ir_relocate_code(prog
->relocs
, prog
->code
, code_pos
, lib_pos
, 0);
732 if (debug_get_bool_option("NV50_PROG_DEBUG", false))
733 nvc0_program_dump(prog
);
737 nvc0
->base
.push_data(&nvc0
->base
, screen
->text
, prog
->code_base
,
738 NV_VRAM_DOMAIN(&screen
->base
), NVC0_SHADER_HEADER_SIZE
, prog
->hdr
);
739 nvc0
->base
.push_data(&nvc0
->base
, screen
->text
, code_pos
,
740 NV_VRAM_DOMAIN(&screen
->base
), prog
->code_size
, prog
->code
);
742 nvc0
->base
.push_data(&nvc0
->base
,
743 screen
->text
, prog
->immd_base
, NV_VRAM_DOMAIN(&screen
->base
),
744 prog
->immd_size
, prog
->immd_data
);
746 BEGIN_NVC0(nvc0
->base
.pushbuf
, NVC0_3D(MEM_BARRIER
), 1);
747 PUSH_DATA (nvc0
->base
.pushbuf
, 0x1011);
752 /* Upload code for builtin functions like integer division emulation. */
754 nvc0_program_library_upload(struct nvc0_context
*nvc0
)
756 struct nvc0_screen
*screen
= nvc0
->screen
;
759 const uint32_t *code
;
761 if (screen
->lib_code
)
764 nv50_ir_get_target_library(screen
->base
.device
->chipset
, &code
, &size
);
768 ret
= nouveau_heap_alloc(screen
->text_heap
, align(size
, 0x100), NULL
,
773 nvc0
->base
.push_data(&nvc0
->base
,
774 screen
->text
, screen
->lib_code
->start
, NV_VRAM_DOMAIN(&screen
->base
),
776 /* no need for a memory barrier, will be emitted with first program */
780 nvc0_program_destroy(struct nvc0_context
*nvc0
, struct nvc0_program
*prog
)
782 const struct pipe_shader_state pipe
= prog
->pipe
;
783 const ubyte type
= prog
->type
;
786 nouveau_heap_free(&prog
->mem
);
787 FREE(prog
->code
); /* may be 0 for hardcoded shaders */
788 FREE(prog
->immd_data
);
790 if (prog
->type
== PIPE_SHADER_COMPUTE
&& prog
->cp
.syms
)
793 if (nvc0
->state
.tfb
== prog
->tfb
)
794 nvc0
->state
.tfb
= NULL
;
798 memset(prog
, 0, sizeof(*prog
));
805 nvc0_program_symbol_offset(const struct nvc0_program
*prog
, uint32_t label
)
807 const struct nv50_ir_prog_symbol
*syms
=
808 (const struct nv50_ir_prog_symbol
*)prog
->cp
.syms
;
811 if (prog
->type
!= PIPE_SHADER_COMPUTE
)
812 base
= NVC0_SHADER_HEADER_SIZE
;
813 for (i
= 0; i
< prog
->cp
.num_syms
; ++i
)
814 if (syms
[i
].label
== label
)
815 return prog
->code_base
+ base
+ syms
[i
].offset
;
816 return prog
->code_base
; /* no symbols or symbol not found */