2 * Copyright 2008 Ben Skeggs
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 "util/u_format.h"
25 #include "nv50_context.h"
26 #include "nouveau/nouveau_stateobj.h"
28 static struct nouveau_stateobj
*
29 validate_fb(struct nv50_context
*nv50
)
31 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
32 struct nouveau_stateobj
*so
= so_new(32, 79, 18);
33 struct pipe_framebuffer_state
*fb
= &nv50
->framebuffer
;
34 unsigned i
, w
, h
, gw
= 0;
36 /* Set nr of active RTs and select RT for each colour output.
37 * FP result 0 always goes to RT[0], bits 4 - 6 are ignored.
38 * Ambiguous assignment results in no rendering (no DATA_ERROR).
40 so_method(so
, tesla
, NV50TCL_RT_CONTROL
, 1);
41 so_data (so
, fb
->nr_cbufs
|
42 (0 << 4) | (1 << 7) | (2 << 10) | (3 << 13) |
43 (4 << 16) | (5 << 19) | (6 << 22) | (7 << 25));
45 for (i
= 0; i
< fb
->nr_cbufs
; i
++) {
46 struct pipe_texture
*pt
= fb
->cbufs
[i
]->texture
;
47 struct nouveau_bo
*bo
= nv50_miptree(pt
)->base
.bo
;
50 w
= fb
->cbufs
[i
]->width
;
51 h
= fb
->cbufs
[i
]->height
;
54 assert(w
== fb
->cbufs
[i
]->width
);
55 assert(h
== fb
->cbufs
[i
]->height
);
58 so_method(so
, tesla
, NV50TCL_RT_HORIZ(i
), 2);
59 so_data (so
, fb
->cbufs
[i
]->width
);
60 so_data (so
, fb
->cbufs
[i
]->height
);
62 so_method(so
, tesla
, NV50TCL_RT_ADDRESS_HIGH(i
), 5);
63 so_reloc (so
, bo
, fb
->cbufs
[i
]->offset
, NOUVEAU_BO_VRAM
|
64 NOUVEAU_BO_HIGH
| NOUVEAU_BO_RDWR
, 0, 0);
65 so_reloc (so
, bo
, fb
->cbufs
[i
]->offset
, NOUVEAU_BO_VRAM
|
66 NOUVEAU_BO_LOW
| NOUVEAU_BO_RDWR
, 0, 0);
67 switch (fb
->cbufs
[i
]->format
) {
68 case PIPE_FORMAT_B8G8R8A8_UNORM
:
69 so_data(so
, NV50TCL_RT_FORMAT_A8R8G8B8_UNORM
);
71 case PIPE_FORMAT_B8G8R8X8_UNORM
:
72 so_data(so
, NV50TCL_RT_FORMAT_X8R8G8B8_UNORM
);
74 case PIPE_FORMAT_B5G6R5_UNORM
:
75 so_data(so
, NV50TCL_RT_FORMAT_R5G6B5_UNORM
);
77 case PIPE_FORMAT_R16G16B16A16_SNORM
:
78 so_data(so
, NV50TCL_RT_FORMAT_R16G16B16A16_SNORM
);
80 case PIPE_FORMAT_R16G16B16A16_UNORM
:
81 so_data(so
, NV50TCL_RT_FORMAT_R16G16B16A16_UNORM
);
83 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
84 so_data(so
, NV50TCL_RT_FORMAT_R32G32B32A32_FLOAT
);
86 case PIPE_FORMAT_R16G16_SNORM
:
87 so_data(so
, NV50TCL_RT_FORMAT_R16G16_SNORM
);
89 case PIPE_FORMAT_R16G16_UNORM
:
90 so_data(so
, NV50TCL_RT_FORMAT_R16G16_UNORM
);
93 NOUVEAU_ERR("AIIII unknown format %s\n",
94 util_format_name(fb
->cbufs
[i
]->format
));
95 so_data(so
, NV50TCL_RT_FORMAT_X8R8G8B8_UNORM
);
98 so_data(so
, nv50_miptree(pt
)->
99 level
[fb
->cbufs
[i
]->level
].tile_mode
<< 4);
100 so_data(so
, 0x00000000);
102 so_method(so
, tesla
, NV50TCL_RT_ARRAY_MODE
, 1);
107 struct pipe_texture
*pt
= fb
->zsbuf
->texture
;
108 struct nouveau_bo
*bo
= nv50_miptree(pt
)->base
.bo
;
111 w
= fb
->zsbuf
->width
;
112 h
= fb
->zsbuf
->height
;
115 assert(w
== fb
->zsbuf
->width
);
116 assert(h
== fb
->zsbuf
->height
);
119 so_method(so
, tesla
, NV50TCL_ZETA_ADDRESS_HIGH
, 5);
120 so_reloc (so
, bo
, fb
->zsbuf
->offset
, NOUVEAU_BO_VRAM
|
121 NOUVEAU_BO_HIGH
| NOUVEAU_BO_RDWR
, 0, 0);
122 so_reloc (so
, bo
, fb
->zsbuf
->offset
, NOUVEAU_BO_VRAM
|
123 NOUVEAU_BO_LOW
| NOUVEAU_BO_RDWR
, 0, 0);
124 switch (fb
->zsbuf
->format
) {
125 case PIPE_FORMAT_Z24S8_UNORM
:
126 so_data(so
, NV50TCL_ZETA_FORMAT_S8Z24_UNORM
);
128 case PIPE_FORMAT_Z24X8_UNORM
:
129 so_data(so
, NV50TCL_ZETA_FORMAT_X8Z24_UNORM
);
131 case PIPE_FORMAT_S8Z24_UNORM
:
132 so_data(so
, NV50TCL_ZETA_FORMAT_Z24S8_UNORM
);
134 case PIPE_FORMAT_Z32_FLOAT
:
135 so_data(so
, NV50TCL_ZETA_FORMAT_Z32_FLOAT
);
138 NOUVEAU_ERR("AIIII unknown format %s\n",
139 util_format_name(fb
->zsbuf
->format
));
140 so_data(so
, NV50TCL_ZETA_FORMAT_S8Z24_UNORM
);
143 so_data(so
, nv50_miptree(pt
)->
144 level
[fb
->zsbuf
->level
].tile_mode
<< 4);
145 so_data(so
, 0x00000000);
147 so_method(so
, tesla
, NV50TCL_ZETA_ENABLE
, 1);
149 so_method(so
, tesla
, NV50TCL_ZETA_HORIZ
, 3);
150 so_data (so
, fb
->zsbuf
->width
);
151 so_data (so
, fb
->zsbuf
->height
);
152 so_data (so
, 0x00010001);
154 so_method(so
, tesla
, NV50TCL_ZETA_ENABLE
, 1);
158 so_method(so
, tesla
, NV50TCL_VIEWPORT_HORIZ(0), 2);
159 so_data (so
, w
<< 16);
160 so_data (so
, h
<< 16);
161 /* set window lower left corner */
162 so_method(so
, tesla
, NV50TCL_WINDOW_OFFSET_X
, 2);
165 /* set screen scissor rectangle */
166 so_method(so
, tesla
, NV50TCL_SCREEN_SCISSOR_HORIZ
, 2);
167 so_data (so
, w
<< 16);
168 so_data (so
, h
<< 16);
174 nv50_validate_samplers(struct nv50_context
*nv50
, struct nouveau_stateobj
*so
,
177 struct nouveau_grobj
*eng2d
= nv50
->screen
->eng2d
;
178 unsigned i
, j
, dw
= nv50
->sampler_nr
[p
] * 8;
182 nv50_so_init_sifc(nv50
, so
, nv50
->screen
->tsc
, NOUVEAU_BO_VRAM
,
183 p
* (32 * 8 * 4), dw
* 4);
185 so_method(so
, eng2d
, NV50_2D_SIFC_DATA
| (2 << 29), dw
);
187 for (i
= 0; i
< nv50
->sampler_nr
[p
]; ++i
) {
188 if (nv50
->sampler
[p
][i
])
189 so_datap(so
, nv50
->sampler
[p
][i
]->tsc
, 8);
191 for (j
= 0; j
< 8; ++j
) /* you get punished */
192 so_data(so
, 0); /* ... for leaving holes */
197 static struct nouveau_stateobj
*
198 validate_blend(struct nv50_context
*nv50
)
200 struct nouveau_stateobj
*so
= NULL
;
201 so_ref(nv50
->blend
->so
, &so
);
205 static struct nouveau_stateobj
*
206 validate_zsa(struct nv50_context
*nv50
)
208 struct nouveau_stateobj
*so
= NULL
;
209 so_ref(nv50
->zsa
->so
, &so
);
213 static struct nouveau_stateobj
*
214 validate_rast(struct nv50_context
*nv50
)
216 struct nouveau_stateobj
*so
= NULL
;
217 so_ref(nv50
->rasterizer
->so
, &so
);
221 static struct nouveau_stateobj
*
222 validate_blend_colour(struct nv50_context
*nv50
)
224 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
225 struct nouveau_stateobj
*so
= so_new(1, 4, 0);
227 so_method(so
, tesla
, NV50TCL_BLEND_COLOR(0), 4);
228 so_data (so
, fui(nv50
->blend_colour
.color
[0]));
229 so_data (so
, fui(nv50
->blend_colour
.color
[1]));
230 so_data (so
, fui(nv50
->blend_colour
.color
[2]));
231 so_data (so
, fui(nv50
->blend_colour
.color
[3]));
235 static struct nouveau_stateobj
*
236 validate_stencil_ref(struct nv50_context
*nv50
)
238 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
239 struct nouveau_stateobj
*so
= so
= so_new(2, 2, 0);
241 so_method(so
, tesla
, NV50TCL_STENCIL_FRONT_FUNC_REF
, 1);
242 so_data (so
, nv50
->stencil_ref
.ref_value
[0]);
243 so_method(so
, tesla
, NV50TCL_STENCIL_BACK_FUNC_REF
, 1);
244 so_data (so
, nv50
->stencil_ref
.ref_value
[1]);
248 static struct nouveau_stateobj
*
249 validate_stipple(struct nv50_context
*nv50
)
251 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
252 struct nouveau_stateobj
*so
= so_new(1, 32, 0);
255 so_method(so
, tesla
, NV50TCL_POLYGON_STIPPLE_PATTERN(0), 32);
256 for (i
= 0; i
< 32; i
++)
257 so_data(so
, util_bswap32(nv50
->stipple
.stipple
[i
]));
261 static struct nouveau_stateobj
*
262 validate_scissor(struct nv50_context
*nv50
)
264 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
265 struct pipe_scissor_state
*s
= &nv50
->scissor
;
266 struct nouveau_stateobj
*so
;
268 so
= so_new(1, 2, 0);
269 so_method(so
, tesla
, NV50TCL_SCISSOR_HORIZ(0), 2);
270 so_data (so
, (s
->maxx
<< 16) | s
->minx
);
271 so_data (so
, (s
->maxy
<< 16) | s
->miny
);
275 static struct nouveau_stateobj
*
276 validate_viewport(struct nv50_context
*nv50
)
278 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
279 struct nouveau_stateobj
*so
= so_new(5, 9, 0);
281 so_method(so
, tesla
, NV50TCL_VIEWPORT_TRANSLATE_X(0), 3);
282 so_data (so
, fui(nv50
->viewport
.translate
[0]));
283 so_data (so
, fui(nv50
->viewport
.translate
[1]));
284 so_data (so
, fui(nv50
->viewport
.translate
[2]));
285 so_method(so
, tesla
, NV50TCL_VIEWPORT_SCALE_X(0), 3);
286 so_data (so
, fui(nv50
->viewport
.scale
[0]));
287 so_data (so
, fui(nv50
->viewport
.scale
[1]));
288 so_data (so
, fui(nv50
->viewport
.scale
[2]));
290 so_method(so
, tesla
, NV50TCL_VIEWPORT_TRANSFORM_EN
, 1);
292 /* 0x0000 = remove whole primitive only (xyz)
293 * 0x1018 = remove whole primitive only (xy), clamp z
294 * 0x1080 = clip primitive (xyz)
295 * 0x1098 = clip primitive (xy), clamp z
297 so_method(so
, tesla
, NV50TCL_VIEW_VOLUME_CLIP_CTRL
, 1);
298 so_data (so
, 0x1080);
299 /* no idea what 0f90 does */
300 so_method(so
, tesla
, 0x0f90, 1);
306 static struct nouveau_stateobj
*
307 validate_sampler(struct nv50_context
*nv50
)
309 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
310 struct nouveau_stateobj
*so
;
313 for (i
= 0; i
< PIPE_SHADER_TYPES
; ++i
)
314 nr
+= nv50
->sampler_nr
[i
];
316 so
= so_new(1 + 5 * PIPE_SHADER_TYPES
,
317 1 + 19 * PIPE_SHADER_TYPES
+ nr
* 8,
318 PIPE_SHADER_TYPES
* 2);
320 nv50_validate_samplers(nv50
, so
, PIPE_SHADER_VERTEX
);
321 nv50_validate_samplers(nv50
, so
, PIPE_SHADER_FRAGMENT
);
323 so_method(so
, tesla
, 0x1334, 1); /* flush TSC */
329 static struct nouveau_stateobj
*
330 validate_vtxbuf(struct nv50_context
*nv50
)
332 struct nouveau_stateobj
*so
= NULL
;
333 so_ref(nv50
->state
.vtxbuf
, &so
);
337 static struct nouveau_stateobj
*
338 validate_vtxattr(struct nv50_context
*nv50
)
340 struct nouveau_stateobj
*so
= NULL
;
341 so_ref(nv50
->state
.vtxattr
, &so
);
345 struct state_validate
{
346 struct nouveau_stateobj
*(*func
)(struct nv50_context
*nv50
);
348 } validate_list
[] = {
349 { validate_fb
, NV50_NEW_FRAMEBUFFER
},
350 { validate_blend
, NV50_NEW_BLEND
},
351 { validate_zsa
, NV50_NEW_ZSA
},
352 { nv50_vertprog_validate
, NV50_NEW_VERTPROG
| NV50_NEW_VERTPROG_CB
},
353 { nv50_fragprog_validate
, NV50_NEW_FRAGPROG
| NV50_NEW_FRAGPROG_CB
},
354 { nv50_geomprog_validate
, NV50_NEW_GEOMPROG
| NV50_NEW_GEOMPROG_CB
},
355 { nv50_fp_linkage_validate
, NV50_NEW_VERTPROG
| NV50_NEW_GEOMPROG
|
356 NV50_NEW_FRAGPROG
| NV50_NEW_RASTERIZER
},
357 { nv50_gp_linkage_validate
, NV50_NEW_VERTPROG
| NV50_NEW_GEOMPROG
},
358 { validate_rast
, NV50_NEW_RASTERIZER
},
359 { validate_blend_colour
, NV50_NEW_BLEND_COLOUR
},
360 { validate_stencil_ref
, NV50_NEW_STENCIL_REF
},
361 { validate_stipple
, NV50_NEW_STIPPLE
},
362 { validate_scissor
, NV50_NEW_SCISSOR
},
363 { validate_viewport
, NV50_NEW_VIEWPORT
},
364 { validate_sampler
, NV50_NEW_SAMPLER
},
365 { nv50_tex_validate
, NV50_NEW_TEXTURE
| NV50_NEW_SAMPLER
},
366 { nv50_vbo_validate
, NV50_NEW_ARRAYS
},
367 { validate_vtxbuf
, NV50_NEW_ARRAYS
},
368 { validate_vtxattr
, NV50_NEW_ARRAYS
},
371 #define validate_list_len (sizeof(validate_list) / sizeof(validate_list[0]))
374 nv50_state_validate(struct nv50_context
*nv50
, unsigned wait_dwords
)
376 struct nouveau_channel
*chan
= nv50
->screen
->base
.channel
;
377 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
378 unsigned nr_relocs
= 128, nr_dwords
= wait_dwords
+ 128 + 4;
381 for (i
= 0; i
< validate_list_len
; i
++) {
382 struct state_validate
*validate
= &validate_list
[i
];
383 struct nouveau_stateobj
*so
;
385 if (!(nv50
->dirty
& validate
->states
))
388 so
= validate
->func(nv50
);
392 nr_dwords
+= (so
->total
+ so
->cur
);
393 nr_relocs
+= so
->cur_reloc
;
395 so_ref(so
, &nv50
->state
.hw
[i
]);
397 nv50
->state
.hw_dirty
|= (1 << i
);
401 if (nv50
->screen
->cur_ctx
!= nv50
) {
402 for (i
= 0; i
< validate_list_len
; i
++) {
403 if (!nv50
->state
.hw
[i
] ||
404 (nv50
->state
.hw_dirty
& (1 << i
)))
407 nr_dwords
+= (nv50
->state
.hw
[i
]->total
+
408 nv50
->state
.hw
[i
]->cur
);
409 nr_relocs
+= nv50
->state
.hw
[i
]->cur_reloc
;
410 nv50
->state
.hw_dirty
|= (1 << i
);
413 nv50
->screen
->cur_ctx
= nv50
;
416 ret
= MARK_RING(chan
, nr_dwords
, nr_relocs
);
418 debug_printf("MARK_RING(%d, %d) failed: %d\n",
419 nr_dwords
, nr_relocs
, ret
);
423 while (nv50
->state
.hw_dirty
) {
424 i
= ffs(nv50
->state
.hw_dirty
) - 1;
425 nv50
->state
.hw_dirty
&= ~(1 << i
);
427 so_emit(chan
, nv50
->state
.hw
[i
]);
430 /* Yes, really, we need to do this. If a buffer that is referenced
431 * on the hardware isn't part of changed state above, without doing
432 * this the kernel is given no clue that the buffer is being used
433 * still. This can cause all sorts of fun issues.
435 nv50_tex_relocs(nv50
);
436 so_emit_reloc_markers(chan
, nv50
->state
.hw
[0]); /* fb */
437 so_emit_reloc_markers(chan
, nv50
->state
.hw
[3]); /* vp */
438 so_emit_reloc_markers(chan
, nv50
->state
.hw
[4]); /* fp */
439 so_emit_reloc_markers(chan
, nv50
->state
.hw
[17]); /* vb */
440 so_emit_reloc_markers(chan
, nv50
->screen
->static_init
);
443 BEGIN_RING(chan
, tesla
, 0x142c, 1);
445 BEGIN_RING(chan
, tesla
, 0x142c, 1);
450 void nv50_so_init_sifc(struct nv50_context
*nv50
,
451 struct nouveau_stateobj
*so
,
452 struct nouveau_bo
*bo
, unsigned reloc
,
453 unsigned offset
, unsigned size
)
455 struct nouveau_grobj
*eng2d
= nv50
->screen
->eng2d
;
457 reloc
|= NOUVEAU_BO_WR
;
459 so_method(so
, eng2d
, NV50_2D_DST_FORMAT
, 2);
460 so_data (so
, NV50_2D_DST_FORMAT_R8_UNORM
);
462 so_method(so
, eng2d
, NV50_2D_DST_PITCH
, 5);
463 so_data (so
, 262144);
466 so_reloc (so
, bo
, offset
, reloc
| NOUVEAU_BO_HIGH
, 0, 0);
467 so_reloc (so
, bo
, offset
, reloc
| NOUVEAU_BO_LOW
, 0, 0);
468 so_method(so
, eng2d
, NV50_2D_SIFC_BITMAP_ENABLE
, 2);
470 so_data (so
, NV50_2D_SIFC_FORMAT_R8_UNORM
);
471 so_method(so
, eng2d
, NV50_2D_SIFC_WIDTH
, 10);