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"
24 #include "util/u_helpers.h"
25 #include "util/u_inlines.h"
26 #include "util/u_transfer.h"
27 #include "util/format_srgb.h"
29 #include "tgsi/tgsi_parse.h"
31 #include "nv50/nv50_stateobj.h"
32 #include "nv50/nv50_context.h"
33 #include "nv50/nv50_query_hw.h"
35 #include "nv50/nv50_3d.xml.h"
36 #include "nv50/nv50_texture.xml.h"
38 #include "nouveau_gldefs.h"
41 * ! pipe_sampler_state.normalized_coords is ignored - rectangle textures will
42 * use non-normalized coordinates, everything else won't
43 * (The relevant bit is in the TIC entry and not the TSC entry.)
45 * ! pipe_sampler_state.seamless_cube_map is ignored - seamless filtering is
46 * always activated on NVA0 +
47 * (Give me the global bit, otherwise it's not worth the CPU work.)
49 * ! pipe_sampler_state.border_color is not swizzled according to the texture
50 * swizzle in pipe_sampler_view
51 * (This will be ugly with indirect independent texture/sampler access,
52 * we'd have to emulate the logic in the shader. GL doesn't have that,
53 * D3D doesn't have swizzle, if we knew what we were implementing we'd be
56 * ! pipe_rasterizer_state.line_last_pixel is ignored - it is never drawn
58 * ! pipe_rasterizer_state.flatshade_first also applies to QUADS
59 * (There's a GL query for that, forcing an exception is just ridiculous.)
61 * ! pipe_rasterizer_state.sprite_coord_enable is masked with 0xff on NVC0
62 * (The hardware only has 8 slots meant for TexCoord and we have to assign
63 * in advance to maintain elegant separate shader objects.)
66 static inline uint32_t
67 nv50_colormask(unsigned mask
)
71 if (mask
& PIPE_MASK_R
)
73 if (mask
& PIPE_MASK_G
)
75 if (mask
& PIPE_MASK_B
)
77 if (mask
& PIPE_MASK_A
)
83 #define NV50_BLEND_FACTOR_CASE(a, b) \
84 case PIPE_BLENDFACTOR_##a: return NV50_BLEND_FACTOR_##b
86 static inline uint32_t
87 nv50_blend_fac(unsigned factor
)
90 NV50_BLEND_FACTOR_CASE(ONE
, ONE
);
91 NV50_BLEND_FACTOR_CASE(SRC_COLOR
, SRC_COLOR
);
92 NV50_BLEND_FACTOR_CASE(SRC_ALPHA
, SRC_ALPHA
);
93 NV50_BLEND_FACTOR_CASE(DST_ALPHA
, DST_ALPHA
);
94 NV50_BLEND_FACTOR_CASE(DST_COLOR
, DST_COLOR
);
95 NV50_BLEND_FACTOR_CASE(SRC_ALPHA_SATURATE
, SRC_ALPHA_SATURATE
);
96 NV50_BLEND_FACTOR_CASE(CONST_COLOR
, CONSTANT_COLOR
);
97 NV50_BLEND_FACTOR_CASE(CONST_ALPHA
, CONSTANT_ALPHA
);
98 NV50_BLEND_FACTOR_CASE(SRC1_COLOR
, SRC1_COLOR
);
99 NV50_BLEND_FACTOR_CASE(SRC1_ALPHA
, SRC1_ALPHA
);
100 NV50_BLEND_FACTOR_CASE(ZERO
, ZERO
);
101 NV50_BLEND_FACTOR_CASE(INV_SRC_COLOR
, ONE_MINUS_SRC_COLOR
);
102 NV50_BLEND_FACTOR_CASE(INV_SRC_ALPHA
, ONE_MINUS_SRC_ALPHA
);
103 NV50_BLEND_FACTOR_CASE(INV_DST_ALPHA
, ONE_MINUS_DST_ALPHA
);
104 NV50_BLEND_FACTOR_CASE(INV_DST_COLOR
, ONE_MINUS_DST_COLOR
);
105 NV50_BLEND_FACTOR_CASE(INV_CONST_COLOR
, ONE_MINUS_CONSTANT_COLOR
);
106 NV50_BLEND_FACTOR_CASE(INV_CONST_ALPHA
, ONE_MINUS_CONSTANT_ALPHA
);
107 NV50_BLEND_FACTOR_CASE(INV_SRC1_COLOR
, ONE_MINUS_SRC1_COLOR
);
108 NV50_BLEND_FACTOR_CASE(INV_SRC1_ALPHA
, ONE_MINUS_SRC1_ALPHA
);
110 return NV50_BLEND_FACTOR_ZERO
;
115 nv50_blend_state_create(struct pipe_context
*pipe
,
116 const struct pipe_blend_state
*cso
)
118 struct nv50_blend_stateobj
*so
= CALLOC_STRUCT(nv50_blend_stateobj
);
120 bool emit_common_func
= cso
->rt
[0].blend_enable
;
122 if (nv50_context(pipe
)->screen
->tesla
->oclass
>= NVA3_3D_CLASS
) {
123 SB_BEGIN_3D(so
, BLEND_INDEPENDENT
, 1);
124 SB_DATA (so
, cso
->independent_blend_enable
);
129 SB_BEGIN_3D(so
, COLOR_MASK_COMMON
, 1);
130 SB_DATA (so
, !cso
->independent_blend_enable
);
132 SB_BEGIN_3D(so
, BLEND_ENABLE_COMMON
, 1);
133 SB_DATA (so
, !cso
->independent_blend_enable
);
135 if (cso
->independent_blend_enable
) {
136 SB_BEGIN_3D(so
, BLEND_ENABLE(0), 8);
137 for (i
= 0; i
< 8; ++i
) {
138 SB_DATA(so
, cso
->rt
[i
].blend_enable
);
139 if (cso
->rt
[i
].blend_enable
)
140 emit_common_func
= true;
143 if (nv50_context(pipe
)->screen
->tesla
->oclass
>= NVA3_3D_CLASS
) {
144 emit_common_func
= false;
146 for (i
= 0; i
< 8; ++i
) {
147 if (!cso
->rt
[i
].blend_enable
)
149 SB_BEGIN_3D_(so
, NVA3_3D_IBLEND_EQUATION_RGB(i
), 6);
150 SB_DATA (so
, nvgl_blend_eqn(cso
->rt
[i
].rgb_func
));
151 SB_DATA (so
, nv50_blend_fac(cso
->rt
[i
].rgb_src_factor
));
152 SB_DATA (so
, nv50_blend_fac(cso
->rt
[i
].rgb_dst_factor
));
153 SB_DATA (so
, nvgl_blend_eqn(cso
->rt
[i
].alpha_func
));
154 SB_DATA (so
, nv50_blend_fac(cso
->rt
[i
].alpha_src_factor
));
155 SB_DATA (so
, nv50_blend_fac(cso
->rt
[i
].alpha_dst_factor
));
159 SB_BEGIN_3D(so
, BLEND_ENABLE(0), 1);
160 SB_DATA (so
, cso
->rt
[0].blend_enable
);
163 if (emit_common_func
) {
164 SB_BEGIN_3D(so
, BLEND_EQUATION_RGB
, 5);
165 SB_DATA (so
, nvgl_blend_eqn(cso
->rt
[0].rgb_func
));
166 SB_DATA (so
, nv50_blend_fac(cso
->rt
[0].rgb_src_factor
));
167 SB_DATA (so
, nv50_blend_fac(cso
->rt
[0].rgb_dst_factor
));
168 SB_DATA (so
, nvgl_blend_eqn(cso
->rt
[0].alpha_func
));
169 SB_DATA (so
, nv50_blend_fac(cso
->rt
[0].alpha_src_factor
));
170 SB_BEGIN_3D(so
, BLEND_FUNC_DST_ALPHA
, 1);
171 SB_DATA (so
, nv50_blend_fac(cso
->rt
[0].alpha_dst_factor
));
174 if (cso
->logicop_enable
) {
175 SB_BEGIN_3D(so
, LOGIC_OP_ENABLE
, 2);
177 SB_DATA (so
, nvgl_logicop_func(cso
->logicop_func
));
179 SB_BEGIN_3D(so
, LOGIC_OP_ENABLE
, 1);
183 if (cso
->independent_blend_enable
) {
184 SB_BEGIN_3D(so
, COLOR_MASK(0), 8);
185 for (i
= 0; i
< 8; ++i
)
186 SB_DATA(so
, nv50_colormask(cso
->rt
[i
].colormask
));
188 SB_BEGIN_3D(so
, COLOR_MASK(0), 1);
189 SB_DATA (so
, nv50_colormask(cso
->rt
[0].colormask
));
192 assert(so
->size
<= (sizeof(so
->state
) / sizeof(so
->state
[0])));
197 nv50_blend_state_bind(struct pipe_context
*pipe
, void *hwcso
)
199 struct nv50_context
*nv50
= nv50_context(pipe
);
202 nv50
->dirty
|= NV50_NEW_BLEND
;
206 nv50_blend_state_delete(struct pipe_context
*pipe
, void *hwcso
)
211 /* NOTE: ignoring line_last_pixel */
213 nv50_rasterizer_state_create(struct pipe_context
*pipe
,
214 const struct pipe_rasterizer_state
*cso
)
216 struct nv50_rasterizer_stateobj
*so
;
219 so
= CALLOC_STRUCT(nv50_rasterizer_stateobj
);
224 #ifndef NV50_SCISSORS_CLIPPING
225 for (int i
= 0; i
< NV50_MAX_VIEWPORTS
; i
++) {
226 SB_BEGIN_3D(so
, SCISSOR_ENABLE(i
), 1);
227 SB_DATA (so
, cso
->scissor
);
231 SB_BEGIN_3D(so
, SHADE_MODEL
, 1);
232 SB_DATA (so
, cso
->flatshade
? NV50_3D_SHADE_MODEL_FLAT
:
233 NV50_3D_SHADE_MODEL_SMOOTH
);
234 SB_BEGIN_3D(so
, PROVOKING_VERTEX_LAST
, 1);
235 SB_DATA (so
, !cso
->flatshade_first
);
236 SB_BEGIN_3D(so
, VERTEX_TWO_SIDE_ENABLE
, 1);
237 SB_DATA (so
, cso
->light_twoside
);
239 SB_BEGIN_3D(so
, FRAG_COLOR_CLAMP_EN
, 1);
240 SB_DATA (so
, cso
->clamp_fragment_color
? 0x11111111 : 0x00000000);
242 SB_BEGIN_3D(so
, MULTISAMPLE_ENABLE
, 1);
243 SB_DATA (so
, cso
->multisample
);
245 SB_BEGIN_3D(so
, LINE_WIDTH
, 1);
246 SB_DATA (so
, fui(cso
->line_width
));
247 SB_BEGIN_3D(so
, LINE_SMOOTH_ENABLE
, 1);
248 SB_DATA (so
, cso
->line_smooth
);
250 SB_BEGIN_3D(so
, LINE_STIPPLE_ENABLE
, 1);
251 if (cso
->line_stipple_enable
) {
253 SB_BEGIN_3D(so
, LINE_STIPPLE
, 1);
254 SB_DATA (so
, (cso
->line_stipple_pattern
<< 8) |
255 cso
->line_stipple_factor
);
260 if (!cso
->point_size_per_vertex
) {
261 SB_BEGIN_3D(so
, POINT_SIZE
, 1);
262 SB_DATA (so
, fui(cso
->point_size
));
264 SB_BEGIN_3D(so
, POINT_SPRITE_ENABLE
, 1);
265 SB_DATA (so
, cso
->point_quad_rasterization
);
266 SB_BEGIN_3D(so
, POINT_SMOOTH_ENABLE
, 1);
267 SB_DATA (so
, cso
->point_smooth
);
269 SB_BEGIN_3D(so
, POLYGON_MODE_FRONT
, 3);
270 SB_DATA (so
, nvgl_polygon_mode(cso
->fill_front
));
271 SB_DATA (so
, nvgl_polygon_mode(cso
->fill_back
));
272 SB_DATA (so
, cso
->poly_smooth
);
274 SB_BEGIN_3D(so
, CULL_FACE_ENABLE
, 3);
275 SB_DATA (so
, cso
->cull_face
!= PIPE_FACE_NONE
);
276 SB_DATA (so
, cso
->front_ccw
? NV50_3D_FRONT_FACE_CCW
:
277 NV50_3D_FRONT_FACE_CW
);
278 switch (cso
->cull_face
) {
279 case PIPE_FACE_FRONT_AND_BACK
:
280 SB_DATA(so
, NV50_3D_CULL_FACE_FRONT_AND_BACK
);
282 case PIPE_FACE_FRONT
:
283 SB_DATA(so
, NV50_3D_CULL_FACE_FRONT
);
287 SB_DATA(so
, NV50_3D_CULL_FACE_BACK
);
291 SB_BEGIN_3D(so
, POLYGON_STIPPLE_ENABLE
, 1);
292 SB_DATA (so
, cso
->poly_stipple_enable
);
293 SB_BEGIN_3D(so
, POLYGON_OFFSET_POINT_ENABLE
, 3);
294 SB_DATA (so
, cso
->offset_point
);
295 SB_DATA (so
, cso
->offset_line
);
296 SB_DATA (so
, cso
->offset_tri
);
298 if (cso
->offset_point
|| cso
->offset_line
|| cso
->offset_tri
) {
299 SB_BEGIN_3D(so
, POLYGON_OFFSET_FACTOR
, 1);
300 SB_DATA (so
, fui(cso
->offset_scale
));
301 SB_BEGIN_3D(so
, POLYGON_OFFSET_UNITS
, 1);
302 SB_DATA (so
, fui(cso
->offset_units
* 2.0f
));
303 SB_BEGIN_3D(so
, POLYGON_OFFSET_CLAMP
, 1);
304 SB_DATA (so
, fui(cso
->offset_clamp
));
307 if (cso
->depth_clip
) {
311 NV50_3D_VIEW_VOLUME_CLIP_CTRL_DEPTH_CLAMP_NEAR
|
312 NV50_3D_VIEW_VOLUME_CLIP_CTRL_DEPTH_CLAMP_FAR
|
313 NV50_3D_VIEW_VOLUME_CLIP_CTRL_UNK12_UNK1
;
315 #ifndef NV50_SCISSORS_CLIPPING
317 NV50_3D_VIEW_VOLUME_CLIP_CTRL_UNK7
|
318 NV50_3D_VIEW_VOLUME_CLIP_CTRL_UNK12_UNK1
;
320 SB_BEGIN_3D(so
, VIEW_VOLUME_CLIP_CTRL
, 1);
323 SB_BEGIN_3D(so
, DEPTH_CLIP_NEGATIVE_Z
, 1);
324 SB_DATA (so
, cso
->clip_halfz
);
326 SB_BEGIN_3D(so
, PIXEL_CENTER_INTEGER
, 1);
327 SB_DATA (so
, !cso
->half_pixel_center
);
329 assert(so
->size
<= (sizeof(so
->state
) / sizeof(so
->state
[0])));
334 nv50_rasterizer_state_bind(struct pipe_context
*pipe
, void *hwcso
)
336 struct nv50_context
*nv50
= nv50_context(pipe
);
339 nv50
->dirty
|= NV50_NEW_RASTERIZER
;
343 nv50_rasterizer_state_delete(struct pipe_context
*pipe
, void *hwcso
)
349 nv50_zsa_state_create(struct pipe_context
*pipe
,
350 const struct pipe_depth_stencil_alpha_state
*cso
)
352 struct nv50_zsa_stateobj
*so
= CALLOC_STRUCT(nv50_zsa_stateobj
);
356 SB_BEGIN_3D(so
, DEPTH_WRITE_ENABLE
, 1);
357 SB_DATA (so
, cso
->depth
.writemask
);
358 SB_BEGIN_3D(so
, DEPTH_TEST_ENABLE
, 1);
359 if (cso
->depth
.enabled
) {
361 SB_BEGIN_3D(so
, DEPTH_TEST_FUNC
, 1);
362 SB_DATA (so
, nvgl_comparison_op(cso
->depth
.func
));
367 SB_BEGIN_3D(so
, DEPTH_BOUNDS_EN
, 1);
368 if (cso
->depth
.bounds_test
) {
370 SB_BEGIN_3D(so
, DEPTH_BOUNDS(0), 2);
371 SB_DATA (so
, fui(cso
->depth
.bounds_min
));
372 SB_DATA (so
, fui(cso
->depth
.bounds_max
));
377 if (cso
->stencil
[0].enabled
) {
378 SB_BEGIN_3D(so
, STENCIL_ENABLE
, 5);
380 SB_DATA (so
, nvgl_stencil_op(cso
->stencil
[0].fail_op
));
381 SB_DATA (so
, nvgl_stencil_op(cso
->stencil
[0].zfail_op
));
382 SB_DATA (so
, nvgl_stencil_op(cso
->stencil
[0].zpass_op
));
383 SB_DATA (so
, nvgl_comparison_op(cso
->stencil
[0].func
));
384 SB_BEGIN_3D(so
, STENCIL_FRONT_MASK
, 2);
385 SB_DATA (so
, cso
->stencil
[0].writemask
);
386 SB_DATA (so
, cso
->stencil
[0].valuemask
);
388 SB_BEGIN_3D(so
, STENCIL_ENABLE
, 1);
392 if (cso
->stencil
[1].enabled
) {
393 assert(cso
->stencil
[0].enabled
);
394 SB_BEGIN_3D(so
, STENCIL_TWO_SIDE_ENABLE
, 5);
396 SB_DATA (so
, nvgl_stencil_op(cso
->stencil
[1].fail_op
));
397 SB_DATA (so
, nvgl_stencil_op(cso
->stencil
[1].zfail_op
));
398 SB_DATA (so
, nvgl_stencil_op(cso
->stencil
[1].zpass_op
));
399 SB_DATA (so
, nvgl_comparison_op(cso
->stencil
[1].func
));
400 SB_BEGIN_3D(so
, STENCIL_BACK_MASK
, 2);
401 SB_DATA (so
, cso
->stencil
[1].writemask
);
402 SB_DATA (so
, cso
->stencil
[1].valuemask
);
404 SB_BEGIN_3D(so
, STENCIL_TWO_SIDE_ENABLE
, 1);
408 SB_BEGIN_3D(so
, ALPHA_TEST_ENABLE
, 1);
409 if (cso
->alpha
.enabled
) {
411 SB_BEGIN_3D(so
, ALPHA_TEST_REF
, 2);
412 SB_DATA (so
, fui(cso
->alpha
.ref_value
));
413 SB_DATA (so
, nvgl_comparison_op(cso
->alpha
.func
));
418 assert(so
->size
<= (sizeof(so
->state
) / sizeof(so
->state
[0])));
423 nv50_zsa_state_bind(struct pipe_context
*pipe
, void *hwcso
)
425 struct nv50_context
*nv50
= nv50_context(pipe
);
428 nv50
->dirty
|= NV50_NEW_ZSA
;
432 nv50_zsa_state_delete(struct pipe_context
*pipe
, void *hwcso
)
437 /* ====================== SAMPLERS AND TEXTURES ================================
440 #define NV50_TSC_WRAP_CASE(n) \
441 case PIPE_TEX_WRAP_##n: return NV50_TSC_WRAP_##n
443 static inline unsigned
444 nv50_tsc_wrap_mode(unsigned wrap
)
447 NV50_TSC_WRAP_CASE(REPEAT
);
448 NV50_TSC_WRAP_CASE(MIRROR_REPEAT
);
449 NV50_TSC_WRAP_CASE(CLAMP_TO_EDGE
);
450 NV50_TSC_WRAP_CASE(CLAMP_TO_BORDER
);
451 NV50_TSC_WRAP_CASE(CLAMP
);
452 NV50_TSC_WRAP_CASE(MIRROR_CLAMP_TO_EDGE
);
453 NV50_TSC_WRAP_CASE(MIRROR_CLAMP_TO_BORDER
);
454 NV50_TSC_WRAP_CASE(MIRROR_CLAMP
);
456 NOUVEAU_ERR("unknown wrap mode: %d\n", wrap
);
457 return NV50_TSC_WRAP_REPEAT
;
462 nv50_sampler_state_create(struct pipe_context
*pipe
,
463 const struct pipe_sampler_state
*cso
)
465 struct nv50_tsc_entry
*so
= MALLOC_STRUCT(nv50_tsc_entry
);
470 so
->tsc
[0] = (0x00026000 |
471 (nv50_tsc_wrap_mode(cso
->wrap_s
) << 0) |
472 (nv50_tsc_wrap_mode(cso
->wrap_t
) << 3) |
473 (nv50_tsc_wrap_mode(cso
->wrap_r
) << 6));
475 switch (cso
->mag_img_filter
) {
476 case PIPE_TEX_FILTER_LINEAR
:
477 so
->tsc
[1] = NV50_TSC_1_MAGF_LINEAR
;
479 case PIPE_TEX_FILTER_NEAREST
:
481 so
->tsc
[1] = NV50_TSC_1_MAGF_NEAREST
;
485 switch (cso
->min_img_filter
) {
486 case PIPE_TEX_FILTER_LINEAR
:
487 so
->tsc
[1] |= NV50_TSC_1_MINF_LINEAR
;
489 case PIPE_TEX_FILTER_NEAREST
:
491 so
->tsc
[1] |= NV50_TSC_1_MINF_NEAREST
;
495 switch (cso
->min_mip_filter
) {
496 case PIPE_TEX_MIPFILTER_LINEAR
:
497 so
->tsc
[1] |= NV50_TSC_1_MIPF_LINEAR
;
499 case PIPE_TEX_MIPFILTER_NEAREST
:
500 so
->tsc
[1] |= NV50_TSC_1_MIPF_NEAREST
;
502 case PIPE_TEX_MIPFILTER_NONE
:
504 so
->tsc
[1] |= NV50_TSC_1_MIPF_NONE
;
508 if (nouveau_screen(pipe
->screen
)->class_3d
>= NVE4_3D_CLASS
) {
509 if (cso
->seamless_cube_map
)
510 so
->tsc
[1] |= NVE4_TSC_1_CUBE_SEAMLESS
;
511 if (!cso
->normalized_coords
)
512 so
->tsc
[1] |= NVE4_TSC_1_FORCE_NONNORMALIZED_COORDS
;
515 if (cso
->max_anisotropy
>= 16)
516 so
->tsc
[0] |= (7 << 20);
518 if (cso
->max_anisotropy
>= 12)
519 so
->tsc
[0] |= (6 << 20);
521 so
->tsc
[0] |= (cso
->max_anisotropy
>> 1) << 20;
523 if (cso
->max_anisotropy
>= 4)
524 so
->tsc
[1] |= NV50_TSC_1_UNKN_ANISO_35
;
526 if (cso
->max_anisotropy
>= 2)
527 so
->tsc
[1] |= NV50_TSC_1_UNKN_ANISO_15
;
530 if (cso
->compare_mode
== PIPE_TEX_COMPARE_R_TO_TEXTURE
) {
531 /* NOTE: must be deactivated for non-shadow textures */
532 so
->tsc
[0] |= (1 << 9);
533 so
->tsc
[0] |= (nvgl_comparison_op(cso
->compare_func
) & 0x7) << 10;
536 f
[0] = CLAMP(cso
->lod_bias
, -16.0f
, 15.0f
);
537 so
->tsc
[1] |= ((int)(f
[0] * 256.0f
) & 0x1fff) << 12;
539 f
[0] = CLAMP(cso
->min_lod
, 0.0f
, 15.0f
);
540 f
[1] = CLAMP(cso
->max_lod
, 0.0f
, 15.0f
);
542 (((int)(f
[1] * 256.0f
) & 0xfff) << 12) | ((int)(f
[0] * 256.0f
) & 0xfff);
545 util_format_linear_float_to_srgb_8unorm(cso
->border_color
.f
[0]) << 24;
547 util_format_linear_float_to_srgb_8unorm(cso
->border_color
.f
[1]) << 12;
549 util_format_linear_float_to_srgb_8unorm(cso
->border_color
.f
[2]) << 20;
551 so
->tsc
[4] = fui(cso
->border_color
.f
[0]);
552 so
->tsc
[5] = fui(cso
->border_color
.f
[1]);
553 so
->tsc
[6] = fui(cso
->border_color
.f
[2]);
554 so
->tsc
[7] = fui(cso
->border_color
.f
[3]);
560 nv50_sampler_state_delete(struct pipe_context
*pipe
, void *hwcso
)
564 for (s
= 0; s
< 3; ++s
) {
565 assert(nv50_context(pipe
)->num_samplers
[s
] <= PIPE_MAX_SAMPLERS
);
566 for (i
= 0; i
< nv50_context(pipe
)->num_samplers
[s
]; ++i
)
567 if (nv50_context(pipe
)->samplers
[s
][i
] == hwcso
)
568 nv50_context(pipe
)->samplers
[s
][i
] = NULL
;
571 nv50_screen_tsc_free(nv50_context(pipe
)->screen
, nv50_tsc_entry(hwcso
));
577 nv50_stage_sampler_states_bind(struct nv50_context
*nv50
, int s
,
578 unsigned nr
, void **hwcso
)
582 assert(nr
<= PIPE_MAX_SAMPLERS
);
583 for (i
= 0; i
< nr
; ++i
) {
584 struct nv50_tsc_entry
*old
= nv50
->samplers
[s
][i
];
586 nv50
->samplers
[s
][i
] = nv50_tsc_entry(hwcso
[i
]);
588 nv50_screen_tsc_unlock(nv50
->screen
, old
);
590 assert(nv50
->num_samplers
[s
] <= PIPE_MAX_SAMPLERS
);
591 for (; i
< nv50
->num_samplers
[s
]; ++i
) {
592 if (nv50
->samplers
[s
][i
]) {
593 nv50_screen_tsc_unlock(nv50
->screen
, nv50
->samplers
[s
][i
]);
594 nv50
->samplers
[s
][i
] = NULL
;
598 nv50
->num_samplers
[s
] = nr
;
600 nv50
->dirty
|= NV50_NEW_SAMPLERS
;
604 nv50_vp_sampler_states_bind(struct pipe_context
*pipe
, unsigned nr
, void **s
)
606 nv50_stage_sampler_states_bind(nv50_context(pipe
), 0, nr
, s
);
610 nv50_fp_sampler_states_bind(struct pipe_context
*pipe
, unsigned nr
, void **s
)
612 nv50_stage_sampler_states_bind(nv50_context(pipe
), 2, nr
, s
);
616 nv50_gp_sampler_states_bind(struct pipe_context
*pipe
, unsigned nr
, void **s
)
618 nv50_stage_sampler_states_bind(nv50_context(pipe
), 1, nr
, s
);
622 nv50_bind_sampler_states(struct pipe_context
*pipe
,
623 unsigned shader
, unsigned start
,
624 unsigned num_samplers
, void **samplers
)
628 case PIPE_SHADER_VERTEX
:
629 nv50_vp_sampler_states_bind(pipe
, num_samplers
, samplers
);
631 case PIPE_SHADER_GEOMETRY
:
632 nv50_gp_sampler_states_bind(pipe
, num_samplers
, samplers
);
634 case PIPE_SHADER_FRAGMENT
:
635 nv50_fp_sampler_states_bind(pipe
, num_samplers
, samplers
);
642 /* NOTE: only called when not referenced anywhere, won't be bound */
644 nv50_sampler_view_destroy(struct pipe_context
*pipe
,
645 struct pipe_sampler_view
*view
)
647 pipe_resource_reference(&view
->texture
, NULL
);
649 nv50_screen_tic_free(nv50_context(pipe
)->screen
, nv50_tic_entry(view
));
651 FREE(nv50_tic_entry(view
));
655 nv50_stage_set_sampler_views(struct nv50_context
*nv50
, int s
,
657 struct pipe_sampler_view
**views
)
661 assert(nr
<= PIPE_MAX_SAMPLERS
);
662 for (i
= 0; i
< nr
; ++i
) {
663 struct nv50_tic_entry
*old
= nv50_tic_entry(nv50
->textures
[s
][i
]);
665 nv50_screen_tic_unlock(nv50
->screen
, old
);
667 pipe_sampler_view_reference(&nv50
->textures
[s
][i
], views
[i
]);
670 assert(nv50
->num_textures
[s
] <= PIPE_MAX_SAMPLERS
);
671 for (i
= nr
; i
< nv50
->num_textures
[s
]; ++i
) {
672 struct nv50_tic_entry
*old
= nv50_tic_entry(nv50
->textures
[s
][i
]);
675 nv50_screen_tic_unlock(nv50
->screen
, old
);
677 pipe_sampler_view_reference(&nv50
->textures
[s
][i
], NULL
);
680 nv50
->num_textures
[s
] = nr
;
682 nouveau_bufctx_reset(nv50
->bufctx_3d
, NV50_BIND_TEXTURES
);
684 nv50
->dirty
|= NV50_NEW_TEXTURES
;
688 nv50_set_sampler_views(struct pipe_context
*pipe
, unsigned shader
,
689 unsigned start
, unsigned nr
,
690 struct pipe_sampler_view
**views
)
694 case PIPE_SHADER_VERTEX
:
695 nv50_stage_set_sampler_views(nv50_context(pipe
), 0, nr
, views
);
697 case PIPE_SHADER_GEOMETRY
:
698 nv50_stage_set_sampler_views(nv50_context(pipe
), 1, nr
, views
);
700 case PIPE_SHADER_FRAGMENT
:
701 nv50_stage_set_sampler_views(nv50_context(pipe
), 2, nr
, views
);
710 /* ============================= SHADERS =======================================
714 nv50_sp_state_create(struct pipe_context
*pipe
,
715 const struct pipe_shader_state
*cso
, unsigned type
)
717 struct nv50_program
*prog
;
719 prog
= CALLOC_STRUCT(nv50_program
);
724 prog
->pipe
.tokens
= tgsi_dup_tokens(cso
->tokens
);
726 if (cso
->stream_output
.num_outputs
)
727 prog
->pipe
.stream_output
= cso
->stream_output
;
729 prog
->translated
= nv50_program_translate(
730 prog
, nv50_context(pipe
)->screen
->base
.device
->chipset
);
736 nv50_sp_state_delete(struct pipe_context
*pipe
, void *hwcso
)
738 struct nv50_program
*prog
= (struct nv50_program
*)hwcso
;
740 nv50_program_destroy(nv50_context(pipe
), prog
);
742 FREE((void *)prog
->pipe
.tokens
);
747 nv50_vp_state_create(struct pipe_context
*pipe
,
748 const struct pipe_shader_state
*cso
)
750 return nv50_sp_state_create(pipe
, cso
, PIPE_SHADER_VERTEX
);
754 nv50_vp_state_bind(struct pipe_context
*pipe
, void *hwcso
)
756 struct nv50_context
*nv50
= nv50_context(pipe
);
758 nv50
->vertprog
= hwcso
;
759 nv50
->dirty
|= NV50_NEW_VERTPROG
;
763 nv50_fp_state_create(struct pipe_context
*pipe
,
764 const struct pipe_shader_state
*cso
)
766 return nv50_sp_state_create(pipe
, cso
, PIPE_SHADER_FRAGMENT
);
770 nv50_fp_state_bind(struct pipe_context
*pipe
, void *hwcso
)
772 struct nv50_context
*nv50
= nv50_context(pipe
);
774 nv50
->fragprog
= hwcso
;
775 nv50
->dirty
|= NV50_NEW_FRAGPROG
;
779 nv50_gp_state_create(struct pipe_context
*pipe
,
780 const struct pipe_shader_state
*cso
)
782 return nv50_sp_state_create(pipe
, cso
, PIPE_SHADER_GEOMETRY
);
786 nv50_gp_state_bind(struct pipe_context
*pipe
, void *hwcso
)
788 struct nv50_context
*nv50
= nv50_context(pipe
);
790 nv50
->gmtyprog
= hwcso
;
791 nv50
->dirty
|= NV50_NEW_GMTYPROG
;
795 nv50_set_constant_buffer(struct pipe_context
*pipe
, uint shader
, uint index
,
796 struct pipe_constant_buffer
*cb
)
798 struct nv50_context
*nv50
= nv50_context(pipe
);
799 struct pipe_resource
*res
= cb
? cb
->buffer
: NULL
;
800 const unsigned s
= nv50_context_shader_stage(shader
);
801 const unsigned i
= index
;
803 if (shader
== PIPE_SHADER_COMPUTE
)
806 assert(i
< NV50_MAX_PIPE_CONSTBUFS
);
807 if (nv50
->constbuf
[s
][i
].user
)
808 nv50
->constbuf
[s
][i
].u
.buf
= NULL
;
810 if (nv50
->constbuf
[s
][i
].u
.buf
)
811 nouveau_bufctx_reset(nv50
->bufctx_3d
, NV50_BIND_CB(s
, i
));
813 pipe_resource_reference(&nv50
->constbuf
[s
][i
].u
.buf
, res
);
815 nv50
->constbuf
[s
][i
].user
= (cb
&& cb
->user_buffer
) ? true : false;
816 if (nv50
->constbuf
[s
][i
].user
) {
817 nv50
->constbuf
[s
][i
].u
.data
= cb
->user_buffer
;
818 nv50
->constbuf
[s
][i
].size
= MIN2(cb
->buffer_size
, 0x10000);
819 nv50
->constbuf_valid
[s
] |= 1 << i
;
822 nv50
->constbuf
[s
][i
].offset
= cb
->buffer_offset
;
823 nv50
->constbuf
[s
][i
].size
= MIN2(align(cb
->buffer_size
, 0x100), 0x10000);
824 nv50
->constbuf_valid
[s
] |= 1 << i
;
826 nv50
->constbuf_valid
[s
] &= ~(1 << i
);
828 nv50
->constbuf_dirty
[s
] |= 1 << i
;
830 nv50
->dirty
|= NV50_NEW_CONSTBUF
;
833 /* =============================================================================
837 nv50_set_blend_color(struct pipe_context
*pipe
,
838 const struct pipe_blend_color
*bcol
)
840 struct nv50_context
*nv50
= nv50_context(pipe
);
842 nv50
->blend_colour
= *bcol
;
843 nv50
->dirty
|= NV50_NEW_BLEND_COLOUR
;
847 nv50_set_stencil_ref(struct pipe_context
*pipe
,
848 const struct pipe_stencil_ref
*sr
)
850 struct nv50_context
*nv50
= nv50_context(pipe
);
852 nv50
->stencil_ref
= *sr
;
853 nv50
->dirty
|= NV50_NEW_STENCIL_REF
;
857 nv50_set_clip_state(struct pipe_context
*pipe
,
858 const struct pipe_clip_state
*clip
)
860 struct nv50_context
*nv50
= nv50_context(pipe
);
862 memcpy(nv50
->clip
.ucp
, clip
->ucp
, sizeof(clip
->ucp
));
864 nv50
->dirty
|= NV50_NEW_CLIP
;
868 nv50_set_sample_mask(struct pipe_context
*pipe
, unsigned sample_mask
)
870 struct nv50_context
*nv50
= nv50_context(pipe
);
872 nv50
->sample_mask
= sample_mask
;
873 nv50
->dirty
|= NV50_NEW_SAMPLE_MASK
;
877 nv50_set_min_samples(struct pipe_context
*pipe
, unsigned min_samples
)
879 struct nv50_context
*nv50
= nv50_context(pipe
);
881 if (nv50
->min_samples
!= min_samples
) {
882 nv50
->min_samples
= min_samples
;
883 nv50
->dirty
|= NV50_NEW_MIN_SAMPLES
;
888 nv50_set_framebuffer_state(struct pipe_context
*pipe
,
889 const struct pipe_framebuffer_state
*fb
)
891 struct nv50_context
*nv50
= nv50_context(pipe
);
894 nouveau_bufctx_reset(nv50
->bufctx_3d
, NV50_BIND_FB
);
896 for (i
= 0; i
< fb
->nr_cbufs
; ++i
)
897 pipe_surface_reference(&nv50
->framebuffer
.cbufs
[i
], fb
->cbufs
[i
]);
898 for (; i
< nv50
->framebuffer
.nr_cbufs
; ++i
)
899 pipe_surface_reference(&nv50
->framebuffer
.cbufs
[i
], NULL
);
901 nv50
->framebuffer
.nr_cbufs
= fb
->nr_cbufs
;
903 nv50
->framebuffer
.width
= fb
->width
;
904 nv50
->framebuffer
.height
= fb
->height
;
906 pipe_surface_reference(&nv50
->framebuffer
.zsbuf
, fb
->zsbuf
);
908 nv50
->dirty
|= NV50_NEW_FRAMEBUFFER
;
912 nv50_set_polygon_stipple(struct pipe_context
*pipe
,
913 const struct pipe_poly_stipple
*stipple
)
915 struct nv50_context
*nv50
= nv50_context(pipe
);
917 nv50
->stipple
= *stipple
;
918 nv50
->dirty
|= NV50_NEW_STIPPLE
;
922 nv50_set_scissor_states(struct pipe_context
*pipe
,
924 unsigned num_scissors
,
925 const struct pipe_scissor_state
*scissor
)
927 struct nv50_context
*nv50
= nv50_context(pipe
);
930 assert(start_slot
+ num_scissors
<= NV50_MAX_VIEWPORTS
);
931 for (i
= 0; i
< num_scissors
; i
++) {
932 if (!memcmp(&nv50
->scissors
[start_slot
+ i
], &scissor
[i
], sizeof(*scissor
)))
934 nv50
->scissors
[start_slot
+ i
] = scissor
[i
];
935 nv50
->scissors_dirty
|= 1 << (start_slot
+ i
);
936 nv50
->dirty
|= NV50_NEW_SCISSOR
;
941 nv50_set_viewport_states(struct pipe_context
*pipe
,
943 unsigned num_viewports
,
944 const struct pipe_viewport_state
*vpt
)
946 struct nv50_context
*nv50
= nv50_context(pipe
);
949 assert(start_slot
+ num_viewports
<= NV50_MAX_VIEWPORTS
);
950 for (i
= 0; i
< num_viewports
; i
++) {
951 if (!memcmp(&nv50
->viewports
[start_slot
+ i
], &vpt
[i
], sizeof(*vpt
)))
953 nv50
->viewports
[start_slot
+ i
] = vpt
[i
];
954 nv50
->viewports_dirty
|= 1 << (start_slot
+ i
);
955 nv50
->dirty
|= NV50_NEW_VIEWPORT
;
960 nv50_set_vertex_buffers(struct pipe_context
*pipe
,
961 unsigned start_slot
, unsigned count
,
962 const struct pipe_vertex_buffer
*vb
)
964 struct nv50_context
*nv50
= nv50_context(pipe
);
967 util_set_vertex_buffers_count(nv50
->vtxbuf
, &nv50
->num_vtxbufs
, vb
,
971 nv50
->vbo_user
&= ~(((1ull << count
) - 1) << start_slot
);
972 nv50
->vbo_constant
&= ~(((1ull << count
) - 1) << start_slot
);
976 for (i
= 0; i
< count
; ++i
) {
977 unsigned dst_index
= start_slot
+ i
;
979 if (!vb
[i
].buffer
&& vb
[i
].user_buffer
) {
980 nv50
->vbo_user
|= 1 << dst_index
;
982 nv50
->vbo_constant
|= 1 << dst_index
;
984 nv50
->vbo_constant
&= ~(1 << dst_index
);
986 nv50
->vbo_user
&= ~(1 << dst_index
);
987 nv50
->vbo_constant
&= ~(1 << dst_index
);
991 nouveau_bufctx_reset(nv50
->bufctx_3d
, NV50_BIND_VERTEX
);
993 nv50
->dirty
|= NV50_NEW_ARRAYS
;
997 nv50_set_index_buffer(struct pipe_context
*pipe
,
998 const struct pipe_index_buffer
*ib
)
1000 struct nv50_context
*nv50
= nv50_context(pipe
);
1002 if (nv50
->idxbuf
.buffer
)
1003 nouveau_bufctx_reset(nv50
->bufctx_3d
, NV50_BIND_INDEX
);
1006 pipe_resource_reference(&nv50
->idxbuf
.buffer
, ib
->buffer
);
1007 nv50
->idxbuf
.index_size
= ib
->index_size
;
1009 nv50
->idxbuf
.offset
= ib
->offset
;
1010 BCTX_REFN(nv50
->bufctx_3d
, INDEX
, nv04_resource(ib
->buffer
), RD
);
1012 nv50
->idxbuf
.user_buffer
= ib
->user_buffer
;
1015 pipe_resource_reference(&nv50
->idxbuf
.buffer
, NULL
);
1020 nv50_vertex_state_bind(struct pipe_context
*pipe
, void *hwcso
)
1022 struct nv50_context
*nv50
= nv50_context(pipe
);
1024 nv50
->vertex
= hwcso
;
1025 nv50
->dirty
|= NV50_NEW_VERTEX
;
1028 static struct pipe_stream_output_target
*
1029 nv50_so_target_create(struct pipe_context
*pipe
,
1030 struct pipe_resource
*res
,
1031 unsigned offset
, unsigned size
)
1033 struct nv04_resource
*buf
= (struct nv04_resource
*)res
;
1034 struct nv50_so_target
*targ
= MALLOC_STRUCT(nv50_so_target
);
1038 if (nouveau_context(pipe
)->screen
->class_3d
>= NVA0_3D_CLASS
) {
1039 targ
->pq
= pipe
->create_query(pipe
,
1040 NVA0_HW_QUERY_STREAM_OUTPUT_BUFFER_OFFSET
, 0);
1050 targ
->pipe
.buffer_size
= size
;
1051 targ
->pipe
.buffer_offset
= offset
;
1052 targ
->pipe
.context
= pipe
;
1053 targ
->pipe
.buffer
= NULL
;
1054 pipe_resource_reference(&targ
->pipe
.buffer
, res
);
1055 pipe_reference_init(&targ
->pipe
.reference
, 1);
1057 assert(buf
->base
.target
== PIPE_BUFFER
);
1058 util_range_add(&buf
->valid_buffer_range
, offset
, offset
+ size
);
1064 nva0_so_target_save_offset(struct pipe_context
*pipe
,
1065 struct pipe_stream_output_target
*ptarg
,
1066 unsigned index
, bool serialize
)
1068 struct nv50_so_target
*targ
= nv50_so_target(ptarg
);
1071 struct nouveau_pushbuf
*push
= nv50_context(pipe
)->base
.pushbuf
;
1072 PUSH_SPACE(push
, 2);
1073 BEGIN_NV04(push
, SUBC_3D(NV50_GRAPH_SERIALIZE
), 1);
1074 PUSH_DATA (push
, 0);
1077 nv50_query(targ
->pq
)->index
= index
;
1078 pipe
->end_query(pipe
, targ
->pq
);
1082 nv50_so_target_destroy(struct pipe_context
*pipe
,
1083 struct pipe_stream_output_target
*ptarg
)
1085 struct nv50_so_target
*targ
= nv50_so_target(ptarg
);
1087 pipe
->destroy_query(pipe
, targ
->pq
);
1088 pipe_resource_reference(&targ
->pipe
.buffer
, NULL
);
1093 nv50_set_stream_output_targets(struct pipe_context
*pipe
,
1094 unsigned num_targets
,
1095 struct pipe_stream_output_target
**targets
,
1096 const unsigned *offsets
)
1098 struct nv50_context
*nv50
= nv50_context(pipe
);
1100 bool serialize
= true;
1101 const bool can_resume
= nv50
->screen
->base
.class_3d
>= NVA0_3D_CLASS
;
1103 assert(num_targets
<= 4);
1105 for (i
= 0; i
< num_targets
; ++i
) {
1106 const bool changed
= nv50
->so_target
[i
] != targets
[i
];
1107 const bool append
= (offsets
[i
] == (unsigned)-1);
1108 if (!changed
&& append
)
1110 nv50
->so_targets_dirty
|= 1 << i
;
1112 if (can_resume
&& changed
&& nv50
->so_target
[i
]) {
1113 nva0_so_target_save_offset(pipe
, nv50
->so_target
[i
], i
, serialize
);
1117 if (targets
[i
] && !append
)
1118 nv50_so_target(targets
[i
])->clean
= true;
1120 pipe_so_target_reference(&nv50
->so_target
[i
], targets
[i
]);
1122 for (; i
< nv50
->num_so_targets
; ++i
) {
1123 if (can_resume
&& nv50
->so_target
[i
]) {
1124 nva0_so_target_save_offset(pipe
, nv50
->so_target
[i
], i
, serialize
);
1127 pipe_so_target_reference(&nv50
->so_target
[i
], NULL
);
1128 nv50
->so_targets_dirty
|= 1 << i
;
1130 nv50
->num_so_targets
= num_targets
;
1132 if (nv50
->so_targets_dirty
)
1133 nv50
->dirty
|= NV50_NEW_STRMOUT
;
1137 nv50_init_state_functions(struct nv50_context
*nv50
)
1139 struct pipe_context
*pipe
= &nv50
->base
.pipe
;
1141 pipe
->create_blend_state
= nv50_blend_state_create
;
1142 pipe
->bind_blend_state
= nv50_blend_state_bind
;
1143 pipe
->delete_blend_state
= nv50_blend_state_delete
;
1145 pipe
->create_rasterizer_state
= nv50_rasterizer_state_create
;
1146 pipe
->bind_rasterizer_state
= nv50_rasterizer_state_bind
;
1147 pipe
->delete_rasterizer_state
= nv50_rasterizer_state_delete
;
1149 pipe
->create_depth_stencil_alpha_state
= nv50_zsa_state_create
;
1150 pipe
->bind_depth_stencil_alpha_state
= nv50_zsa_state_bind
;
1151 pipe
->delete_depth_stencil_alpha_state
= nv50_zsa_state_delete
;
1153 pipe
->create_sampler_state
= nv50_sampler_state_create
;
1154 pipe
->delete_sampler_state
= nv50_sampler_state_delete
;
1155 pipe
->bind_sampler_states
= nv50_bind_sampler_states
;
1157 pipe
->create_sampler_view
= nv50_create_sampler_view
;
1158 pipe
->sampler_view_destroy
= nv50_sampler_view_destroy
;
1159 pipe
->set_sampler_views
= nv50_set_sampler_views
;
1161 pipe
->create_vs_state
= nv50_vp_state_create
;
1162 pipe
->create_fs_state
= nv50_fp_state_create
;
1163 pipe
->create_gs_state
= nv50_gp_state_create
;
1164 pipe
->bind_vs_state
= nv50_vp_state_bind
;
1165 pipe
->bind_fs_state
= nv50_fp_state_bind
;
1166 pipe
->bind_gs_state
= nv50_gp_state_bind
;
1167 pipe
->delete_vs_state
= nv50_sp_state_delete
;
1168 pipe
->delete_fs_state
= nv50_sp_state_delete
;
1169 pipe
->delete_gs_state
= nv50_sp_state_delete
;
1171 pipe
->set_blend_color
= nv50_set_blend_color
;
1172 pipe
->set_stencil_ref
= nv50_set_stencil_ref
;
1173 pipe
->set_clip_state
= nv50_set_clip_state
;
1174 pipe
->set_sample_mask
= nv50_set_sample_mask
;
1175 pipe
->set_min_samples
= nv50_set_min_samples
;
1176 pipe
->set_constant_buffer
= nv50_set_constant_buffer
;
1177 pipe
->set_framebuffer_state
= nv50_set_framebuffer_state
;
1178 pipe
->set_polygon_stipple
= nv50_set_polygon_stipple
;
1179 pipe
->set_scissor_states
= nv50_set_scissor_states
;
1180 pipe
->set_viewport_states
= nv50_set_viewport_states
;
1182 pipe
->create_vertex_elements_state
= nv50_vertex_state_create
;
1183 pipe
->delete_vertex_elements_state
= nv50_vertex_state_delete
;
1184 pipe
->bind_vertex_elements_state
= nv50_vertex_state_bind
;
1186 pipe
->set_vertex_buffers
= nv50_set_vertex_buffers
;
1187 pipe
->set_index_buffer
= nv50_set_index_buffer
;
1189 pipe
->create_stream_output_target
= nv50_so_target_create
;
1190 pipe
->stream_output_target_destroy
= nv50_so_target_destroy
;
1191 pipe
->set_stream_output_targets
= nv50_set_stream_output_targets
;
1193 nv50
->sample_mask
= ~0;
1194 nv50
->min_samples
= 1;