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 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 #include "pipe/p_defines.h"
25 #include "nvc0_context.h"
27 #include "nv50/codegen/nv50_ir_driver.h"
29 /* If only they told use the actual semantic instead of just GENERIC ... */
31 nvc0_mesa_varying_hack(struct nv50_ir_varying
*var
)
35 if (var
->sn
!= TGSI_SEMANTIC_GENERIC
)
38 if (var
->si
<= 7) /* gl_TexCoord */
39 for (c
= 0; c
< 4; ++c
)
40 var
->slot
[c
] = (0x300 + var
->si
* 0x10 + c
* 0x4) / 4;
42 if (var
->si
== 9) /* gl_PointCoord */
43 for (c
= 0; c
< 4; ++c
)
44 var
->slot
[c
] = (0x2e0 + c
* 0x4) / 4;
46 for (c
= 0; c
< 4; ++c
) /* move down user varyings (first has index 8) */
47 var
->slot
[c
] -= 0x80 / 4;
51 nvc0_shader_input_address(unsigned sn
, unsigned si
, unsigned ubase
)
54 /* case TGSI_SEMANTIC_TESSFACTOR: return 0x000 + si * 0x4; */
55 case TGSI_SEMANTIC_PRIMID
: return 0x060;
56 case TGSI_SEMANTIC_PSIZE
: return 0x06c;
57 case TGSI_SEMANTIC_POSITION
: return 0x070;
58 case TGSI_SEMANTIC_GENERIC
: return ubase
+ si
* 0x10;
59 case TGSI_SEMANTIC_FOG
: return 0x270;
60 case TGSI_SEMANTIC_COLOR
: return 0x280 + si
* 0x10;
61 case TGSI_SEMANTIC_BCOLOR
: return 0x2a0 + si
* 0x10;
62 /* case TGSI_SEMANTIC_CLIP: return 0x2c0 + si * 0x10; */
63 /* case TGSI_SEMANTIC_POINTCOORD: return 0x2e0; */
64 /* case TGSI_SEMANTIC_TESSCOORD: return ~0; */ /* 0x2f0, but special load */
65 case TGSI_SEMANTIC_INSTANCEID
: return 0x2f8;
66 /* case TGSI_SEMANTIC_VERTEXID: return 0x2fc; */
67 /* case TGSI_SEMANTIC_TEXCOORD: return 0x300 + si * 0x10; */
68 case TGSI_SEMANTIC_FACE
: return 0x3fc;
69 /* case TGSI_SEMANTIC_INVOCATIONID: return ~0; */
71 assert(!"invalid TGSI input semantic");
77 nvc0_shader_output_address(unsigned sn
, unsigned si
, unsigned ubase
)
80 /* case TGSI_SEMANTIC_TESSFACTOR: return 0x000 + si * 0x4; */
81 case TGSI_SEMANTIC_PRIMID
: return 0x040;
82 /* case TGSI_SEMANTIC_LAYER: return 0x064; */
83 /* case TGSI_SEMANTIC_VIEWPORTINDEX: return 0x068; */
84 case TGSI_SEMANTIC_PSIZE
: return 0x06c;
85 case TGSI_SEMANTIC_POSITION
: return 0x070;
86 case TGSI_SEMANTIC_GENERIC
: return ubase
+ si
* 0x10;
87 case TGSI_SEMANTIC_FOG
: return 0x270;
88 case TGSI_SEMANTIC_COLOR
: return 0x280 + si
* 0x10;
89 case TGSI_SEMANTIC_BCOLOR
: return 0x2a0 + si
* 0x10;
90 /* case TGSI_SEMANTIC_CLIP: return 0x2c0 + si * 0x10; */
91 /* case TGSI_SEMANTIC_TEXCOORD: return 0x300 + si * 0x10; */
92 case TGSI_SEMANTIC_EDGEFLAG
: return ~0;
94 assert(!"invalid TGSI output semantic");
100 nvc0_vp_assign_input_slots(struct nv50_ir_prog_info
*info
)
104 for (i
= 0; i
< info
->numInputs
; ++i
)
105 for (c
= 0; c
< 4; ++c
)
106 info
->in
[i
].slot
[c
] = (0x80 + i
* 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 for (c
= 0; c
< 4; ++c
)
125 info
->in
[i
].slot
[c
] = (offset
+ c
* 0x4) / 4;
127 nvc0_mesa_varying_hack(&info
->in
[i
]);
134 nvc0_fp_assign_output_slots(struct nv50_ir_prog_info
*info
)
136 unsigned last
= info
->prop
.fp
.numColourResults
* 4;
139 for (i
= 0; i
< info
->numOutputs
; ++i
)
140 if (info
->out
[i
].sn
== TGSI_SEMANTIC_COLOR
)
141 for (c
= 0; c
< 4; ++c
)
142 info
->out
[i
].slot
[c
] = info
->out
[i
].si
* 4 + c
;
144 if (info
->io
.sampleMask
< PIPE_MAX_SHADER_OUTPUTS
)
145 info
->out
[info
->io
.sampleMask
].slot
[0] = last
++;
147 if (info
->io
.fragDepth
< PIPE_MAX_SHADER_OUTPUTS
)
148 info
->out
[info
->io
.fragDepth
].slot
[2] = last
;
154 nvc0_sp_assign_output_slots(struct nv50_ir_prog_info
*info
)
156 unsigned ubase
= MAX2(0x80, 0x20 + info
->numPatchConstants
* 0x10);
160 for (i
= 0; i
< info
->numOutputs
; ++i
) {
161 offset
= nvc0_shader_output_address(info
->out
[i
].sn
,
162 info
->out
[i
].si
, ubase
);
163 if (info
->out
[i
].patch
&& offset
>= 0x20)
164 offset
= 0x20 + info
->out
[i
].si
* 0x10;
166 for (c
= 0; c
< 4; ++c
)
167 info
->out
[i
].slot
[c
] = (offset
+ c
* 0x4) / 4;
169 nvc0_mesa_varying_hack(&info
->out
[i
]);
176 nvc0_program_assign_varying_slots(struct nv50_ir_prog_info
*info
)
180 if (info
->type
== PIPE_SHADER_VERTEX
)
181 ret
= nvc0_vp_assign_input_slots(info
);
183 ret
= nvc0_sp_assign_input_slots(info
);
187 if (info
->type
== PIPE_SHADER_FRAGMENT
)
188 ret
= nvc0_fp_assign_output_slots(info
);
190 ret
= nvc0_sp_assign_output_slots(info
);
195 nvc0_vtgp_hdr_update_oread(struct nvc0_program
*vp
, uint8_t slot
)
197 uint8_t min
= (vp
->hdr
[4] >> 12) & 0xff;
198 uint8_t max
= (vp
->hdr
[4] >> 24);
200 min
= MIN2(min
, slot
);
201 max
= MAX2(max
, slot
);
203 vp
->hdr
[4] = (max
<< 24) | (min
<< 12);
206 /* Common part of header generation for VP, TCP, TEP and GP. */
208 nvc0_vtgp_gen_header(struct nvc0_program
*vp
, struct nv50_ir_prog_info
*info
)
212 for (i
= 0; i
< info
->numInputs
; ++i
) {
213 if (info
->in
[i
].patch
)
215 for (c
= 0; c
< 4; ++c
) {
216 a
= info
->in
[i
].slot
[c
];
217 if (info
->in
[i
].mask
& (1 << c
))
218 vp
->hdr
[5 + a
/ 32] |= 1 << (a
% 32);
222 for (i
= 0; i
< info
->numOutputs
; ++i
) {
223 if (info
->out
[i
].patch
)
225 for (c
= 0; c
< 4; ++c
) {
226 if (!(info
->out
[i
].mask
& (1 << c
)))
228 assert(info
->out
[i
].slot
[c
] >= 0x40 / 4);
229 a
= info
->out
[i
].slot
[c
] - 0x40 / 4;
230 vp
->hdr
[13 + a
/ 32] |= 1 << (a
% 32);
231 if (info
->out
[i
].oread
)
232 nvc0_vtgp_hdr_update_oread(vp
, info
->out
[i
].slot
[c
]);
236 for (i
= 0; i
< info
->numSysVals
; ++i
) {
237 switch (info
->sv
[i
].sn
) {
238 case TGSI_SEMANTIC_PRIMID
:
239 vp
->hdr
[5] |= 1 << 24;
241 case TGSI_SEMANTIC_INSTANCEID
:
242 vp
->hdr
[10] |= 1 << 30;
245 case TGSI_SEMANTIC_VERTEXID:
246 vp->hdr[10] |= 1 << 31;
258 nvc0_vp_gen_header(struct nvc0_program
*vp
, struct nv50_ir_prog_info
*info
)
260 vp
->hdr
[0] = 0x20061 | (1 << 10);
261 vp
->hdr
[4] = 0xff000;
263 vp
->hdr
[18] = (1 << info
->io
.clipDistanceCount
) - 1;
265 return nvc0_vtgp_gen_header(vp
, info
);
268 #if defined(PIPE_SHADER_HULL) || defined(PIPE_SHADER_DOMAIN)
270 nvc0_tp_get_tess_mode(struct nvc0_program
*tp
, struct nv50_ir_prog_info
*info
)
272 switch (info
->prop
.tp
.domain
) {
273 case PIPE_PRIM_LINES
:
274 tp
->tp
.tess_mode
= NVC0_3D_TESS_MODE_PRIM_ISOLINES
;
276 case PIPE_PRIM_TRIANGLES
:
277 tp
->tp
.tess_mode
= NVC0_3D_TESS_MODE_PRIM_TRIANGLES
;
278 if (info
->prop
.tp
.winding
> 0)
279 tp
->tp
.tess_mode
|= NVC0_3D_TESS_MODE_CW
;
281 case PIPE_PRIM_QUADS
:
282 tp
->tp
.tess_mode
= NVC0_3D_TESS_MODE_PRIM_QUADS
;
285 tp
->tp
.tess_mode
= ~0;
288 if (info
->prop
.tp
.outputPrim
!= PIPE_PRIM_POINTS
)
289 tp
->tp
.tess_mode
|= NVC0_3D_TESS_MODE_CONNECTED
;
291 switch (info
->prop
.tp
.partitioning
) {
292 case PIPE_TESS_PART_INTEGER
:
293 case PIPE_TESS_PART_POW2
:
294 tp
->tp
.tess_mode
|= NVC0_3D_TESS_MODE_SPACING_EQUAL
;
296 case PIPE_TESS_PART_FRACT_ODD
:
297 tp
->tp
.tess_mode
|= NVC0_3D_TESS_MODE_SPACING_FRACTIONAL_ODD
;
299 case PIPE_TESS_PART_FRACT_EVEN
:
300 tp
->tp
.tess_mode
|= NVC0_3D_TESS_MODE_SPACING_FRACTIONAL_EVEN
;
303 assert(!"invalid tessellator partitioning");
309 #ifdef PIPE_SHADER_HULL
311 nvc0_tcp_gen_header(struct nvc0_program
*tcp
, struct nv50_ir_prog_info
*info
)
313 unsigned opcs
= 6; /* output patch constants (at least the TessFactors) */
315 tcp
->tp
.input_patch_size
= info
->prop
.tp
.inputPatchSize
;
317 if (info
->numPatchConstants
)
318 opcs
= 8 + info
->numPatchConstants
* 4;
320 tcp
->hdr
[0] = 0x20061 | (2 << 10);
322 tcp
->hdr
[1] = opcs
<< 24;
323 tcp
->hdr
[2] = info
->prop
.tp
.outputPatchSize
<< 24;
325 tcp
->hdr
[4] = 0xff000; /* initial min/max parallel output read address */
327 nvc0_vtgp_gen_header(tcp
, info
);
329 nvc0_tp_get_tess_mode(tcp
, info
);
335 #ifdef PIPE_SHADER_DOMAIN
337 nvc0_tep_gen_header(struct nvc0_program
*tep
, struct nv50_ir_prog_info
*info
)
339 tep
->hdr
[0] = 0x20061 | (3 << 10);
340 tep
->hdr
[4] = 0xff000;
342 nvc0_vtgp_gen_header(tep
, info
);
344 nvc0_tp_get_tess_mode(tep
, info
);
346 tep
->hdr
[18] |= 0x3 << 12; /* ? */
353 nvc0_gp_gen_header(struct nvc0_program
*gp
, struct nv50_ir_prog_info
*info
)
355 gp
->hdr
[0] = 0x20061 | (4 << 10);
357 gp
->hdr
[2] = MIN2(info
->prop
.gp
.instanceCount
, 32) << 24;
359 switch (info
->prop
.gp
.outputPrim
) {
360 case PIPE_PRIM_POINTS
:
361 gp
->hdr
[3] = 0x01000000;
362 gp
->hdr
[0] |= 0xf0000000;
364 case PIPE_PRIM_LINE_STRIP
:
365 gp
->hdr
[3] = 0x06000000;
366 gp
->hdr
[0] |= 0x10000000;
368 case PIPE_PRIM_TRIANGLE_STRIP
:
369 gp
->hdr
[3] = 0x07000000;
370 gp
->hdr
[0] |= 0x10000000;
377 gp
->hdr
[4] = info
->prop
.gp
.maxVertices
& 0x1ff;
379 return nvc0_vtgp_gen_header(gp
, info
);
382 #define NVC0_INTERP_FLAT (1 << 0)
383 #define NVC0_INTERP_PERSPECTIVE (2 << 0)
384 #define NVC0_INTERP_LINEAR (3 << 0)
385 #define NVC0_INTERP_CENTROID (1 << 2)
388 nvc0_hdr_interp_mode(const struct nv50_ir_varying
*var
)
391 return NVC0_INTERP_LINEAR
;
393 return NVC0_INTERP_FLAT
;
394 return NVC0_INTERP_PERSPECTIVE
;
398 nvc0_fp_gen_header(struct nvc0_program
*fp
, struct nv50_ir_prog_info
*info
)
402 fp
->hdr
[0] = 0x20062 | (5 << 10);
403 fp
->hdr
[5] = 0x80000000; /* getting a trap if FRAG_COORD_UMASK.w = 0 */
405 if (info
->prop
.fp
.usesDiscard
)
406 fp
->hdr
[0] |= 0x8000;
407 if (info
->prop
.fp
.numColourResults
> 1)
408 fp
->hdr
[0] |= 0x4000;
409 if (info
->io
.sampleMask
< PIPE_MAX_SHADER_OUTPUTS
)
411 if (info
->prop
.fp
.writesDepth
) {
413 fp
->flags
[0] = 0x11; /* deactivate ZCULL */
416 for (i
= 0; i
< info
->numInputs
; ++i
) {
417 m
= nvc0_hdr_interp_mode(&info
->in
[i
]);
418 for (c
= 0; c
< 4; ++c
) {
419 if (!(info
->in
[i
].mask
& (1 << c
)))
421 if (info
->in
[i
].slot
[0] == (0x070 / 4)) {
422 fp
->hdr
[5] |= 1 << (28 + c
);
424 if (info
->in
[i
].slot
[0] == (0x2e0 / 4)) {
426 fp
->hdr
[14] |= 1 << (24 + c
);
428 if (info
->in
[i
].slot
[c
] < (0x040 / 4) ||
429 info
->in
[i
].slot
[c
] > (0x380 / 4))
431 a
= info
->in
[i
].slot
[c
] * 2;
432 if (info
->in
[i
].slot
[0] >= (0x2c0 / 4))
434 fp
->hdr
[4 + a
/ 32] |= m
<< (a
% 32);
439 for (i
= 0; i
< info
->numOutputs
; ++i
) {
440 if (info
->out
[i
].sn
== TGSI_SEMANTIC_COLOR
)
441 fp
->hdr
[18] |= info
->out
[i
].mask
<< info
->out
[i
].slot
[0];
444 fp
->fp
.early_z
= info
->prop
.fp
.earlyFragTests
;
445 if (fp
->fp
.early_z
== FALSE
&& fp
->code_size
>= 0x400)
446 fp
->fp
.early_z
= !(info
->prop
.fp
.writesDepth
||
447 info
->prop
.fp
.usesDiscard
||
448 (info
->io
.globalAccess
& 2));
455 nvc0_program_dump(struct nvc0_program
*prog
)
459 for (pos
= 0; pos
< sizeof(prog
->hdr
) / sizeof(prog
->hdr
[0]); ++pos
)
460 debug_printf("HDR[%02lx] = 0x%08x\n",
461 pos
* sizeof(prog
->hdr
[0]), prog
->hdr
[pos
]);
463 debug_printf("shader binary code (0x%x bytes):", prog
->code_size
);
464 for (pos
= 0; pos
< prog
->code_size
/ 4; ++pos
) {
467 debug_printf("%08x ", prog
->code
[pos
]);
474 nvc0_program_translate(struct nvc0_program
*prog
)
476 struct nv50_ir_prog_info
*info
;
479 info
= CALLOC_STRUCT(nv50_ir_prog_info
);
483 info
->type
= prog
->type
;
485 info
->bin
.sourceRep
= NV50_PROGRAM_IR_TGSI
;
486 info
->bin
.source
= (void *)prog
->pipe
.tokens
;
488 info
->io
.clipDistanceCount
= prog
->vp
.num_ucps
;
490 info
->assignSlots
= nvc0_program_assign_varying_slots
;
493 info
->optLevel
= debug_get_num_option("NV50_PROG_OPTIMIZE", 3);
494 info
->dbgFlags
= debug_get_num_option("NV50_PROG_DEBUG", 0);
499 ret
= nv50_ir_generate_code(info
);
501 NOUVEAU_ERR("shader translation failed: %i\n", ret
);
505 prog
->code
= info
->bin
.code
;
506 prog
->code_size
= info
->bin
.codeSize
;
507 prog
->immd_data
= info
->immd
.buf
;
508 prog
->immd_size
= info
->immd
.bufSize
;
509 prog
->relocs
= info
->bin
.relocData
;
510 prog
->max_gpr
= MAX2(4, (info
->bin
.maxGPR
+ 1));
512 prog
->vp
.edgeflag
= PIPE_MAX_ATTRIBS
;
514 switch (prog
->type
) {
515 case PIPE_SHADER_VERTEX
:
516 ret
= nvc0_vp_gen_header(prog
, info
);
518 #ifdef PIPE_SHADER_HULL
519 case PIPE_SHADER_HULL
:
520 ret
= nvc0_tcp_gen_header(prog
, info
);
523 #ifdef PIPE_SHADER_DOMAIN
524 case PIPE_SHADER_DOMAIN
:
525 ret
= nvc0_tep_gen_header(prog
, info
);
528 case PIPE_SHADER_GEOMETRY
:
529 ret
= nvc0_gp_gen_header(prog
, info
);
531 case PIPE_SHADER_FRAGMENT
:
532 ret
= nvc0_fp_gen_header(prog
, info
);
536 NOUVEAU_ERR("unknown program type: %u\n", prog
->type
);
542 if (info
->bin
.tlsSpace
) {
543 assert(info
->bin
.tlsSpace
< (1 << 24));
544 prog
->hdr
[0] |= 1 << 26;
545 prog
->hdr
[1] |= info
->bin
.tlsSpace
; /* l[] size */
547 if (info
->io
.globalAccess
)
548 prog
->hdr
[0] |= 1 << 16;
556 nvc0_program_upload_code(struct nvc0_context
*nvc0
, struct nvc0_program
*prog
)
558 struct nvc0_screen
*screen
= nvc0
->screen
;
560 uint32_t size
= prog
->code_size
+ NVC0_SHADER_HEADER_SIZE
;
561 uint32_t lib_pos
= screen
->lib_code
->start
;
564 /* c[] bindings need to be aligned to 0x100, but we could use relocations
566 if (prog
->immd_size
) {
567 prog
->immd_base
= size
;
568 size
= align(size
, 0x40);
569 size
+= prog
->immd_size
+ 0xc0; /* add 0xc0 for align 0x40 -> 0x100 */
571 size
= align(size
, 0x40); /* required by SP_START_ID */
573 ret
= nouveau_resource_alloc(screen
->text_heap
, size
, prog
, &prog
->res
);
575 NOUVEAU_ERR("out of code space\n");
578 prog
->code_base
= prog
->res
->start
;
579 prog
->immd_base
= align(prog
->res
->start
+ prog
->immd_base
, 0x100);
580 assert((prog
->immd_size
== 0) || (prog
->immd_base
+ prog
->immd_size
<
581 prog
->res
->start
+ prog
->res
->size
));
583 code_pos
= prog
->code_base
+ NVC0_SHADER_HEADER_SIZE
;
586 nv50_ir_relocate_code(prog
->relocs
, prog
->code
, code_pos
, lib_pos
, 0);
589 if (debug_get_bool_option("NV50_PROG_DEBUG", FALSE
))
590 nvc0_program_dump(prog
);
593 nvc0_m2mf_push_linear(&nvc0
->base
, screen
->text
, prog
->code_base
,
594 NOUVEAU_BO_VRAM
, NVC0_SHADER_HEADER_SIZE
, prog
->hdr
);
595 nvc0_m2mf_push_linear(&nvc0
->base
, screen
->text
,
596 prog
->code_base
+ NVC0_SHADER_HEADER_SIZE
,
597 NOUVEAU_BO_VRAM
, prog
->code_size
, prog
->code
);
599 nvc0_m2mf_push_linear(&nvc0
->base
,
600 screen
->text
, prog
->immd_base
, NOUVEAU_BO_VRAM
,
601 prog
->immd_size
, prog
->immd_data
);
603 BEGIN_RING(screen
->base
.channel
, RING_3D(MEM_BARRIER
), 1);
604 OUT_RING (screen
->base
.channel
, 0x1111);
609 /* Upload code for builtin functions like integer division emulation. */
611 nvc0_program_library_upload(struct nvc0_context
*nvc0
)
613 struct nvc0_screen
*screen
= nvc0
->screen
;
616 const uint32_t *code
;
618 if (screen
->lib_code
)
621 nv50_ir_get_target_library(screen
->base
.device
->chipset
, &code
, &size
);
625 ret
= nouveau_resource_alloc(screen
->text_heap
, align(size
, 0x100), NULL
,
630 nvc0_m2mf_push_linear(&nvc0
->base
,
631 screen
->text
, screen
->lib_code
->start
, NOUVEAU_BO_VRAM
,
633 /* no need for a memory barrier, will be emitted with first program */
637 nvc0_program_destroy(struct nvc0_context
*nvc0
, struct nvc0_program
*prog
)
640 nouveau_resource_free(&prog
->res
);
645 FREE(prog
->immd_data
);
649 memset(prog
->hdr
, 0, sizeof(prog
->hdr
));
651 prog
->translated
= FALSE
;