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"
34 #include "nv50/nv50_3d.xml.h"
35 #include "nv50/nv50_texture.xml.h"
37 #include "nouveau_gldefs.h"
40 * ! pipe_sampler_state.normalized_coords is ignored - rectangle textures will
41 * use non-normalized coordinates, everything else won't
42 * (The relevant bit is in the TIC entry and not the TSC entry.)
44 * ! pipe_sampler_state.seamless_cube_map is ignored - seamless filtering is
45 * always activated on NVA0 +
46 * (Give me the global bit, otherwise it's not worth the CPU work.)
48 * ! pipe_sampler_state.border_color is not swizzled according to the texture
49 * swizzle in pipe_sampler_view
50 * (This will be ugly with indirect independent texture/sampler access,
51 * we'd have to emulate the logic in the shader. GL doesn't have that,
52 * D3D doesn't have swizzle, if we knew what we were implementing we'd be
55 * ! pipe_rasterizer_state.line_last_pixel is ignored - it is never drawn
57 * ! pipe_rasterizer_state.flatshade_first also applies to QUADS
58 * (There's a GL query for that, forcing an exception is just ridiculous.)
60 * ! pipe_rasterizer_state.sprite_coord_enable is masked with 0xff on NVC0
61 * (The hardware only has 8 slots meant for TexCoord and we have to assign
62 * in advance to maintain elegant separate shader objects.)
65 static inline uint32_t
66 nv50_colormask(unsigned mask
)
70 if (mask
& PIPE_MASK_R
)
72 if (mask
& PIPE_MASK_G
)
74 if (mask
& PIPE_MASK_B
)
76 if (mask
& PIPE_MASK_A
)
82 #define NV50_BLEND_FACTOR_CASE(a, b) \
83 case PIPE_BLENDFACTOR_##a: return NV50_BLEND_FACTOR_##b
85 static inline uint32_t
86 nv50_blend_fac(unsigned factor
)
89 NV50_BLEND_FACTOR_CASE(ONE
, ONE
);
90 NV50_BLEND_FACTOR_CASE(SRC_COLOR
, SRC_COLOR
);
91 NV50_BLEND_FACTOR_CASE(SRC_ALPHA
, SRC_ALPHA
);
92 NV50_BLEND_FACTOR_CASE(DST_ALPHA
, DST_ALPHA
);
93 NV50_BLEND_FACTOR_CASE(DST_COLOR
, DST_COLOR
);
94 NV50_BLEND_FACTOR_CASE(SRC_ALPHA_SATURATE
, SRC_ALPHA_SATURATE
);
95 NV50_BLEND_FACTOR_CASE(CONST_COLOR
, CONSTANT_COLOR
);
96 NV50_BLEND_FACTOR_CASE(CONST_ALPHA
, CONSTANT_ALPHA
);
97 NV50_BLEND_FACTOR_CASE(SRC1_COLOR
, SRC1_COLOR
);
98 NV50_BLEND_FACTOR_CASE(SRC1_ALPHA
, SRC1_ALPHA
);
99 NV50_BLEND_FACTOR_CASE(ZERO
, ZERO
);
100 NV50_BLEND_FACTOR_CASE(INV_SRC_COLOR
, ONE_MINUS_SRC_COLOR
);
101 NV50_BLEND_FACTOR_CASE(INV_SRC_ALPHA
, ONE_MINUS_SRC_ALPHA
);
102 NV50_BLEND_FACTOR_CASE(INV_DST_ALPHA
, ONE_MINUS_DST_ALPHA
);
103 NV50_BLEND_FACTOR_CASE(INV_DST_COLOR
, ONE_MINUS_DST_COLOR
);
104 NV50_BLEND_FACTOR_CASE(INV_CONST_COLOR
, ONE_MINUS_CONSTANT_COLOR
);
105 NV50_BLEND_FACTOR_CASE(INV_CONST_ALPHA
, ONE_MINUS_CONSTANT_ALPHA
);
106 NV50_BLEND_FACTOR_CASE(INV_SRC1_COLOR
, ONE_MINUS_SRC1_COLOR
);
107 NV50_BLEND_FACTOR_CASE(INV_SRC1_ALPHA
, ONE_MINUS_SRC1_ALPHA
);
109 return NV50_BLEND_FACTOR_ZERO
;
114 nv50_blend_state_create(struct pipe_context
*pipe
,
115 const struct pipe_blend_state
*cso
)
117 struct nv50_blend_stateobj
*so
= CALLOC_STRUCT(nv50_blend_stateobj
);
119 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
));
193 if (cso
->alpha_to_coverage
)
194 ms
|= NV50_3D_MULTISAMPLE_CTRL_ALPHA_TO_COVERAGE
;
195 if (cso
->alpha_to_one
)
196 ms
|= NV50_3D_MULTISAMPLE_CTRL_ALPHA_TO_ONE
;
198 SB_BEGIN_3D(so
, MULTISAMPLE_CTRL
, 1);
201 assert(so
->size
<= (sizeof(so
->state
) / sizeof(so
->state
[0])));
206 nv50_blend_state_bind(struct pipe_context
*pipe
, void *hwcso
)
208 struct nv50_context
*nv50
= nv50_context(pipe
);
211 nv50
->dirty
|= NV50_NEW_BLEND
;
215 nv50_blend_state_delete(struct pipe_context
*pipe
, void *hwcso
)
220 /* NOTE: ignoring line_last_pixel */
222 nv50_rasterizer_state_create(struct pipe_context
*pipe
,
223 const struct pipe_rasterizer_state
*cso
)
225 struct nv50_rasterizer_stateobj
*so
;
228 so
= CALLOC_STRUCT(nv50_rasterizer_stateobj
);
233 #ifndef NV50_SCISSORS_CLIPPING
234 for (int i
= 0; i
< NV50_MAX_VIEWPORTS
; i
++) {
235 SB_BEGIN_3D(so
, SCISSOR_ENABLE(i
), 1);
236 SB_DATA (so
, cso
->scissor
);
240 SB_BEGIN_3D(so
, SHADE_MODEL
, 1);
241 SB_DATA (so
, cso
->flatshade
? NV50_3D_SHADE_MODEL_FLAT
:
242 NV50_3D_SHADE_MODEL_SMOOTH
);
243 SB_BEGIN_3D(so
, PROVOKING_VERTEX_LAST
, 1);
244 SB_DATA (so
, !cso
->flatshade_first
);
245 SB_BEGIN_3D(so
, VERTEX_TWO_SIDE_ENABLE
, 1);
246 SB_DATA (so
, cso
->light_twoside
);
248 SB_BEGIN_3D(so
, FRAG_COLOR_CLAMP_EN
, 1);
249 SB_DATA (so
, cso
->clamp_fragment_color
? 0x11111111 : 0x00000000);
251 SB_BEGIN_3D(so
, MULTISAMPLE_ENABLE
, 1);
252 SB_DATA (so
, cso
->multisample
);
254 SB_BEGIN_3D(so
, LINE_WIDTH
, 1);
255 SB_DATA (so
, fui(cso
->line_width
));
256 SB_BEGIN_3D(so
, LINE_SMOOTH_ENABLE
, 1);
257 SB_DATA (so
, cso
->line_smooth
);
259 SB_BEGIN_3D(so
, LINE_STIPPLE_ENABLE
, 1);
260 if (cso
->line_stipple_enable
) {
262 SB_BEGIN_3D(so
, LINE_STIPPLE
, 1);
263 SB_DATA (so
, (cso
->line_stipple_pattern
<< 8) |
264 cso
->line_stipple_factor
);
269 if (!cso
->point_size_per_vertex
) {
270 SB_BEGIN_3D(so
, POINT_SIZE
, 1);
271 SB_DATA (so
, fui(cso
->point_size
));
273 SB_BEGIN_3D(so
, POINT_SPRITE_ENABLE
, 1);
274 SB_DATA (so
, cso
->point_quad_rasterization
);
275 SB_BEGIN_3D(so
, POINT_SMOOTH_ENABLE
, 1);
276 SB_DATA (so
, cso
->point_smooth
);
278 SB_BEGIN_3D(so
, POLYGON_MODE_FRONT
, 3);
279 SB_DATA (so
, nvgl_polygon_mode(cso
->fill_front
));
280 SB_DATA (so
, nvgl_polygon_mode(cso
->fill_back
));
281 SB_DATA (so
, cso
->poly_smooth
);
283 SB_BEGIN_3D(so
, CULL_FACE_ENABLE
, 3);
284 SB_DATA (so
, cso
->cull_face
!= PIPE_FACE_NONE
);
285 SB_DATA (so
, cso
->front_ccw
? NV50_3D_FRONT_FACE_CCW
:
286 NV50_3D_FRONT_FACE_CW
);
287 switch (cso
->cull_face
) {
288 case PIPE_FACE_FRONT_AND_BACK
:
289 SB_DATA(so
, NV50_3D_CULL_FACE_FRONT_AND_BACK
);
291 case PIPE_FACE_FRONT
:
292 SB_DATA(so
, NV50_3D_CULL_FACE_FRONT
);
296 SB_DATA(so
, NV50_3D_CULL_FACE_BACK
);
300 SB_BEGIN_3D(so
, POLYGON_STIPPLE_ENABLE
, 1);
301 SB_DATA (so
, cso
->poly_stipple_enable
);
302 SB_BEGIN_3D(so
, POLYGON_OFFSET_POINT_ENABLE
, 3);
303 SB_DATA (so
, cso
->offset_point
);
304 SB_DATA (so
, cso
->offset_line
);
305 SB_DATA (so
, cso
->offset_tri
);
307 if (cso
->offset_point
|| cso
->offset_line
|| cso
->offset_tri
) {
308 SB_BEGIN_3D(so
, POLYGON_OFFSET_FACTOR
, 1);
309 SB_DATA (so
, fui(cso
->offset_scale
));
310 SB_BEGIN_3D(so
, POLYGON_OFFSET_UNITS
, 1);
311 SB_DATA (so
, fui(cso
->offset_units
* 2.0f
));
312 SB_BEGIN_3D(so
, POLYGON_OFFSET_CLAMP
, 1);
313 SB_DATA (so
, fui(cso
->offset_clamp
));
316 if (cso
->depth_clip
) {
320 NV50_3D_VIEW_VOLUME_CLIP_CTRL_DEPTH_CLAMP_NEAR
|
321 NV50_3D_VIEW_VOLUME_CLIP_CTRL_DEPTH_CLAMP_FAR
|
322 NV50_3D_VIEW_VOLUME_CLIP_CTRL_UNK12_UNK1
;
324 #ifndef NV50_SCISSORS_CLIPPING
326 NV50_3D_VIEW_VOLUME_CLIP_CTRL_UNK7
|
327 NV50_3D_VIEW_VOLUME_CLIP_CTRL_UNK12_UNK1
;
329 SB_BEGIN_3D(so
, VIEW_VOLUME_CLIP_CTRL
, 1);
332 SB_BEGIN_3D(so
, DEPTH_CLIP_NEGATIVE_Z
, 1);
333 SB_DATA (so
, cso
->clip_halfz
);
335 SB_BEGIN_3D(so
, PIXEL_CENTER_INTEGER
, 1);
336 SB_DATA (so
, !cso
->half_pixel_center
);
338 assert(so
->size
<= (sizeof(so
->state
) / sizeof(so
->state
[0])));
343 nv50_rasterizer_state_bind(struct pipe_context
*pipe
, void *hwcso
)
345 struct nv50_context
*nv50
= nv50_context(pipe
);
348 nv50
->dirty
|= NV50_NEW_RASTERIZER
;
352 nv50_rasterizer_state_delete(struct pipe_context
*pipe
, void *hwcso
)
358 nv50_zsa_state_create(struct pipe_context
*pipe
,
359 const struct pipe_depth_stencil_alpha_state
*cso
)
361 struct nv50_zsa_stateobj
*so
= CALLOC_STRUCT(nv50_zsa_stateobj
);
365 SB_BEGIN_3D(so
, DEPTH_WRITE_ENABLE
, 1);
366 SB_DATA (so
, cso
->depth
.writemask
);
367 SB_BEGIN_3D(so
, DEPTH_TEST_ENABLE
, 1);
368 if (cso
->depth
.enabled
) {
370 SB_BEGIN_3D(so
, DEPTH_TEST_FUNC
, 1);
371 SB_DATA (so
, nvgl_comparison_op(cso
->depth
.func
));
376 SB_BEGIN_3D(so
, DEPTH_BOUNDS_EN
, 1);
377 if (cso
->depth
.bounds_test
) {
379 SB_BEGIN_3D(so
, DEPTH_BOUNDS(0), 2);
380 SB_DATA (so
, fui(cso
->depth
.bounds_min
));
381 SB_DATA (so
, fui(cso
->depth
.bounds_max
));
386 if (cso
->stencil
[0].enabled
) {
387 SB_BEGIN_3D(so
, STENCIL_ENABLE
, 5);
389 SB_DATA (so
, nvgl_stencil_op(cso
->stencil
[0].fail_op
));
390 SB_DATA (so
, nvgl_stencil_op(cso
->stencil
[0].zfail_op
));
391 SB_DATA (so
, nvgl_stencil_op(cso
->stencil
[0].zpass_op
));
392 SB_DATA (so
, nvgl_comparison_op(cso
->stencil
[0].func
));
393 SB_BEGIN_3D(so
, STENCIL_FRONT_MASK
, 2);
394 SB_DATA (so
, cso
->stencil
[0].writemask
);
395 SB_DATA (so
, cso
->stencil
[0].valuemask
);
397 SB_BEGIN_3D(so
, STENCIL_ENABLE
, 1);
401 if (cso
->stencil
[1].enabled
) {
402 assert(cso
->stencil
[0].enabled
);
403 SB_BEGIN_3D(so
, STENCIL_TWO_SIDE_ENABLE
, 5);
405 SB_DATA (so
, nvgl_stencil_op(cso
->stencil
[1].fail_op
));
406 SB_DATA (so
, nvgl_stencil_op(cso
->stencil
[1].zfail_op
));
407 SB_DATA (so
, nvgl_stencil_op(cso
->stencil
[1].zpass_op
));
408 SB_DATA (so
, nvgl_comparison_op(cso
->stencil
[1].func
));
409 SB_BEGIN_3D(so
, STENCIL_BACK_MASK
, 2);
410 SB_DATA (so
, cso
->stencil
[1].writemask
);
411 SB_DATA (so
, cso
->stencil
[1].valuemask
);
413 SB_BEGIN_3D(so
, STENCIL_TWO_SIDE_ENABLE
, 1);
417 SB_BEGIN_3D(so
, ALPHA_TEST_ENABLE
, 1);
418 if (cso
->alpha
.enabled
) {
420 SB_BEGIN_3D(so
, ALPHA_TEST_REF
, 2);
421 SB_DATA (so
, fui(cso
->alpha
.ref_value
));
422 SB_DATA (so
, nvgl_comparison_op(cso
->alpha
.func
));
427 assert(so
->size
<= (sizeof(so
->state
) / sizeof(so
->state
[0])));
432 nv50_zsa_state_bind(struct pipe_context
*pipe
, void *hwcso
)
434 struct nv50_context
*nv50
= nv50_context(pipe
);
437 nv50
->dirty
|= NV50_NEW_ZSA
;
441 nv50_zsa_state_delete(struct pipe_context
*pipe
, void *hwcso
)
446 /* ====================== SAMPLERS AND TEXTURES ================================
449 #define NV50_TSC_WRAP_CASE(n) \
450 case PIPE_TEX_WRAP_##n: return NV50_TSC_WRAP_##n
452 static inline unsigned
453 nv50_tsc_wrap_mode(unsigned wrap
)
456 NV50_TSC_WRAP_CASE(REPEAT
);
457 NV50_TSC_WRAP_CASE(MIRROR_REPEAT
);
458 NV50_TSC_WRAP_CASE(CLAMP_TO_EDGE
);
459 NV50_TSC_WRAP_CASE(CLAMP_TO_BORDER
);
460 NV50_TSC_WRAP_CASE(CLAMP
);
461 NV50_TSC_WRAP_CASE(MIRROR_CLAMP_TO_EDGE
);
462 NV50_TSC_WRAP_CASE(MIRROR_CLAMP_TO_BORDER
);
463 NV50_TSC_WRAP_CASE(MIRROR_CLAMP
);
465 NOUVEAU_ERR("unknown wrap mode: %d\n", wrap
);
466 return NV50_TSC_WRAP_REPEAT
;
471 nv50_sampler_state_create(struct pipe_context
*pipe
,
472 const struct pipe_sampler_state
*cso
)
474 struct nv50_tsc_entry
*so
= MALLOC_STRUCT(nv50_tsc_entry
);
479 so
->tsc
[0] = (0x00026000 |
480 (nv50_tsc_wrap_mode(cso
->wrap_s
) << 0) |
481 (nv50_tsc_wrap_mode(cso
->wrap_t
) << 3) |
482 (nv50_tsc_wrap_mode(cso
->wrap_r
) << 6));
484 switch (cso
->mag_img_filter
) {
485 case PIPE_TEX_FILTER_LINEAR
:
486 so
->tsc
[1] = NV50_TSC_1_MAGF_LINEAR
;
488 case PIPE_TEX_FILTER_NEAREST
:
490 so
->tsc
[1] = NV50_TSC_1_MAGF_NEAREST
;
494 switch (cso
->min_img_filter
) {
495 case PIPE_TEX_FILTER_LINEAR
:
496 so
->tsc
[1] |= NV50_TSC_1_MINF_LINEAR
;
498 case PIPE_TEX_FILTER_NEAREST
:
500 so
->tsc
[1] |= NV50_TSC_1_MINF_NEAREST
;
504 switch (cso
->min_mip_filter
) {
505 case PIPE_TEX_MIPFILTER_LINEAR
:
506 so
->tsc
[1] |= NV50_TSC_1_MIPF_LINEAR
;
508 case PIPE_TEX_MIPFILTER_NEAREST
:
509 so
->tsc
[1] |= NV50_TSC_1_MIPF_NEAREST
;
511 case PIPE_TEX_MIPFILTER_NONE
:
513 so
->tsc
[1] |= NV50_TSC_1_MIPF_NONE
;
517 if (nouveau_screen(pipe
->screen
)->class_3d
>= NVE4_3D_CLASS
) {
518 if (cso
->seamless_cube_map
)
519 so
->tsc
[1] |= NVE4_TSC_1_CUBE_SEAMLESS
;
520 if (!cso
->normalized_coords
)
521 so
->tsc
[1] |= NVE4_TSC_1_FORCE_NONNORMALIZED_COORDS
;
524 if (cso
->max_anisotropy
>= 16)
525 so
->tsc
[0] |= (7 << 20);
527 if (cso
->max_anisotropy
>= 12)
528 so
->tsc
[0] |= (6 << 20);
530 so
->tsc
[0] |= (cso
->max_anisotropy
>> 1) << 20;
532 if (cso
->max_anisotropy
>= 4)
533 so
->tsc
[1] |= NV50_TSC_1_UNKN_ANISO_35
;
535 if (cso
->max_anisotropy
>= 2)
536 so
->tsc
[1] |= NV50_TSC_1_UNKN_ANISO_15
;
539 if (cso
->compare_mode
== PIPE_TEX_COMPARE_R_TO_TEXTURE
) {
540 /* NOTE: must be deactivated for non-shadow textures */
541 so
->tsc
[0] |= (1 << 9);
542 so
->tsc
[0] |= (nvgl_comparison_op(cso
->compare_func
) & 0x7) << 10;
545 f
[0] = CLAMP(cso
->lod_bias
, -16.0f
, 15.0f
);
546 so
->tsc
[1] |= ((int)(f
[0] * 256.0f
) & 0x1fff) << 12;
548 f
[0] = CLAMP(cso
->min_lod
, 0.0f
, 15.0f
);
549 f
[1] = CLAMP(cso
->max_lod
, 0.0f
, 15.0f
);
551 (((int)(f
[1] * 256.0f
) & 0xfff) << 12) | ((int)(f
[0] * 256.0f
) & 0xfff);
554 util_format_linear_float_to_srgb_8unorm(cso
->border_color
.f
[0]) << 24;
556 util_format_linear_float_to_srgb_8unorm(cso
->border_color
.f
[1]) << 12;
558 util_format_linear_float_to_srgb_8unorm(cso
->border_color
.f
[2]) << 20;
560 so
->tsc
[4] = fui(cso
->border_color
.f
[0]);
561 so
->tsc
[5] = fui(cso
->border_color
.f
[1]);
562 so
->tsc
[6] = fui(cso
->border_color
.f
[2]);
563 so
->tsc
[7] = fui(cso
->border_color
.f
[3]);
569 nv50_sampler_state_delete(struct pipe_context
*pipe
, void *hwcso
)
573 for (s
= 0; s
< 3; ++s
) {
574 assert(nv50_context(pipe
)->num_samplers
[s
] <= PIPE_MAX_SAMPLERS
);
575 for (i
= 0; i
< nv50_context(pipe
)->num_samplers
[s
]; ++i
)
576 if (nv50_context(pipe
)->samplers
[s
][i
] == hwcso
)
577 nv50_context(pipe
)->samplers
[s
][i
] = NULL
;
580 nv50_screen_tsc_free(nv50_context(pipe
)->screen
, nv50_tsc_entry(hwcso
));
586 nv50_stage_sampler_states_bind(struct nv50_context
*nv50
, int s
,
587 unsigned nr
, void **hwcso
)
591 assert(nr
<= PIPE_MAX_SAMPLERS
);
592 for (i
= 0; i
< nr
; ++i
) {
593 struct nv50_tsc_entry
*old
= nv50
->samplers
[s
][i
];
595 nv50
->samplers
[s
][i
] = nv50_tsc_entry(hwcso
[i
]);
597 nv50_screen_tsc_unlock(nv50
->screen
, old
);
599 assert(nv50
->num_samplers
[s
] <= PIPE_MAX_SAMPLERS
);
600 for (; i
< nv50
->num_samplers
[s
]; ++i
) {
601 if (nv50
->samplers
[s
][i
]) {
602 nv50_screen_tsc_unlock(nv50
->screen
, nv50
->samplers
[s
][i
]);
603 nv50
->samplers
[s
][i
] = NULL
;
607 nv50
->num_samplers
[s
] = nr
;
609 nv50
->dirty
|= NV50_NEW_SAMPLERS
;
613 nv50_vp_sampler_states_bind(struct pipe_context
*pipe
, unsigned nr
, void **s
)
615 nv50_stage_sampler_states_bind(nv50_context(pipe
), 0, nr
, s
);
619 nv50_fp_sampler_states_bind(struct pipe_context
*pipe
, unsigned nr
, void **s
)
621 nv50_stage_sampler_states_bind(nv50_context(pipe
), 2, nr
, s
);
625 nv50_gp_sampler_states_bind(struct pipe_context
*pipe
, unsigned nr
, void **s
)
627 nv50_stage_sampler_states_bind(nv50_context(pipe
), 1, nr
, s
);
631 nv50_bind_sampler_states(struct pipe_context
*pipe
,
632 unsigned shader
, unsigned start
,
633 unsigned num_samplers
, void **samplers
)
637 case PIPE_SHADER_VERTEX
:
638 nv50_vp_sampler_states_bind(pipe
, num_samplers
, samplers
);
640 case PIPE_SHADER_GEOMETRY
:
641 nv50_gp_sampler_states_bind(pipe
, num_samplers
, samplers
);
643 case PIPE_SHADER_FRAGMENT
:
644 nv50_fp_sampler_states_bind(pipe
, num_samplers
, samplers
);
651 /* NOTE: only called when not referenced anywhere, won't be bound */
653 nv50_sampler_view_destroy(struct pipe_context
*pipe
,
654 struct pipe_sampler_view
*view
)
656 pipe_resource_reference(&view
->texture
, NULL
);
658 nv50_screen_tic_free(nv50_context(pipe
)->screen
, nv50_tic_entry(view
));
660 FREE(nv50_tic_entry(view
));
664 nv50_stage_set_sampler_views(struct nv50_context
*nv50
, int s
,
666 struct pipe_sampler_view
**views
)
670 assert(nr
<= PIPE_MAX_SAMPLERS
);
671 for (i
= 0; i
< nr
; ++i
) {
672 struct nv50_tic_entry
*old
= nv50_tic_entry(nv50
->textures
[s
][i
]);
674 nv50_screen_tic_unlock(nv50
->screen
, old
);
676 pipe_sampler_view_reference(&nv50
->textures
[s
][i
], views
[i
]);
679 assert(nv50
->num_textures
[s
] <= PIPE_MAX_SAMPLERS
);
680 for (i
= nr
; i
< nv50
->num_textures
[s
]; ++i
) {
681 struct nv50_tic_entry
*old
= nv50_tic_entry(nv50
->textures
[s
][i
]);
684 nv50_screen_tic_unlock(nv50
->screen
, old
);
686 pipe_sampler_view_reference(&nv50
->textures
[s
][i
], NULL
);
689 nv50
->num_textures
[s
] = nr
;
691 nouveau_bufctx_reset(nv50
->bufctx_3d
, NV50_BIND_TEXTURES
);
693 nv50
->dirty
|= NV50_NEW_TEXTURES
;
697 nv50_set_sampler_views(struct pipe_context
*pipe
, unsigned shader
,
698 unsigned start
, unsigned nr
,
699 struct pipe_sampler_view
**views
)
703 case PIPE_SHADER_VERTEX
:
704 nv50_stage_set_sampler_views(nv50_context(pipe
), 0, nr
, views
);
706 case PIPE_SHADER_GEOMETRY
:
707 nv50_stage_set_sampler_views(nv50_context(pipe
), 1, nr
, views
);
709 case PIPE_SHADER_FRAGMENT
:
710 nv50_stage_set_sampler_views(nv50_context(pipe
), 2, nr
, views
);
719 /* ============================= SHADERS =======================================
723 nv50_sp_state_create(struct pipe_context
*pipe
,
724 const struct pipe_shader_state
*cso
, unsigned type
)
726 struct nv50_program
*prog
;
728 prog
= CALLOC_STRUCT(nv50_program
);
733 prog
->pipe
.tokens
= tgsi_dup_tokens(cso
->tokens
);
735 if (cso
->stream_output
.num_outputs
)
736 prog
->pipe
.stream_output
= cso
->stream_output
;
742 nv50_sp_state_delete(struct pipe_context
*pipe
, void *hwcso
)
744 struct nv50_program
*prog
= (struct nv50_program
*)hwcso
;
746 nv50_program_destroy(nv50_context(pipe
), prog
);
748 FREE((void *)prog
->pipe
.tokens
);
753 nv50_vp_state_create(struct pipe_context
*pipe
,
754 const struct pipe_shader_state
*cso
)
756 return nv50_sp_state_create(pipe
, cso
, PIPE_SHADER_VERTEX
);
760 nv50_vp_state_bind(struct pipe_context
*pipe
, void *hwcso
)
762 struct nv50_context
*nv50
= nv50_context(pipe
);
764 nv50
->vertprog
= hwcso
;
765 nv50
->dirty
|= NV50_NEW_VERTPROG
;
769 nv50_fp_state_create(struct pipe_context
*pipe
,
770 const struct pipe_shader_state
*cso
)
772 return nv50_sp_state_create(pipe
, cso
, PIPE_SHADER_FRAGMENT
);
776 nv50_fp_state_bind(struct pipe_context
*pipe
, void *hwcso
)
778 struct nv50_context
*nv50
= nv50_context(pipe
);
780 nv50
->fragprog
= hwcso
;
781 nv50
->dirty
|= NV50_NEW_FRAGPROG
;
785 nv50_gp_state_create(struct pipe_context
*pipe
,
786 const struct pipe_shader_state
*cso
)
788 return nv50_sp_state_create(pipe
, cso
, PIPE_SHADER_GEOMETRY
);
792 nv50_gp_state_bind(struct pipe_context
*pipe
, void *hwcso
)
794 struct nv50_context
*nv50
= nv50_context(pipe
);
796 nv50
->gmtyprog
= hwcso
;
797 nv50
->dirty
|= NV50_NEW_GMTYPROG
;
801 nv50_set_constant_buffer(struct pipe_context
*pipe
, uint shader
, uint index
,
802 struct pipe_constant_buffer
*cb
)
804 struct nv50_context
*nv50
= nv50_context(pipe
);
805 struct pipe_resource
*res
= cb
? cb
->buffer
: NULL
;
806 const unsigned s
= nv50_context_shader_stage(shader
);
807 const unsigned i
= index
;
809 if (shader
== PIPE_SHADER_COMPUTE
)
812 assert(i
< NV50_MAX_PIPE_CONSTBUFS
);
813 if (nv50
->constbuf
[s
][i
].user
)
814 nv50
->constbuf
[s
][i
].u
.buf
= NULL
;
816 if (nv50
->constbuf
[s
][i
].u
.buf
)
817 nouveau_bufctx_reset(nv50
->bufctx_3d
, NV50_BIND_CB(s
, i
));
819 pipe_resource_reference(&nv50
->constbuf
[s
][i
].u
.buf
, res
);
821 nv50
->constbuf
[s
][i
].user
= (cb
&& cb
->user_buffer
) ? true : false;
822 if (nv50
->constbuf
[s
][i
].user
) {
823 nv50
->constbuf
[s
][i
].u
.data
= cb
->user_buffer
;
824 nv50
->constbuf
[s
][i
].size
= MIN2(cb
->buffer_size
, 0x10000);
825 nv50
->constbuf_valid
[s
] |= 1 << i
;
828 nv50
->constbuf
[s
][i
].offset
= cb
->buffer_offset
;
829 nv50
->constbuf
[s
][i
].size
= MIN2(align(cb
->buffer_size
, 0x100), 0x10000);
830 nv50
->constbuf_valid
[s
] |= 1 << i
;
832 nv50
->constbuf_valid
[s
] &= ~(1 << i
);
834 nv50
->constbuf_dirty
[s
] |= 1 << i
;
836 nv50
->dirty
|= NV50_NEW_CONSTBUF
;
839 /* =============================================================================
843 nv50_set_blend_color(struct pipe_context
*pipe
,
844 const struct pipe_blend_color
*bcol
)
846 struct nv50_context
*nv50
= nv50_context(pipe
);
848 nv50
->blend_colour
= *bcol
;
849 nv50
->dirty
|= NV50_NEW_BLEND_COLOUR
;
853 nv50_set_stencil_ref(struct pipe_context
*pipe
,
854 const struct pipe_stencil_ref
*sr
)
856 struct nv50_context
*nv50
= nv50_context(pipe
);
858 nv50
->stencil_ref
= *sr
;
859 nv50
->dirty
|= NV50_NEW_STENCIL_REF
;
863 nv50_set_clip_state(struct pipe_context
*pipe
,
864 const struct pipe_clip_state
*clip
)
866 struct nv50_context
*nv50
= nv50_context(pipe
);
868 memcpy(nv50
->clip
.ucp
, clip
->ucp
, sizeof(clip
->ucp
));
870 nv50
->dirty
|= NV50_NEW_CLIP
;
874 nv50_set_sample_mask(struct pipe_context
*pipe
, unsigned sample_mask
)
876 struct nv50_context
*nv50
= nv50_context(pipe
);
878 nv50
->sample_mask
= sample_mask
;
879 nv50
->dirty
|= NV50_NEW_SAMPLE_MASK
;
883 nv50_set_min_samples(struct pipe_context
*pipe
, unsigned min_samples
)
885 struct nv50_context
*nv50
= nv50_context(pipe
);
887 if (nv50
->min_samples
!= min_samples
) {
888 nv50
->min_samples
= min_samples
;
889 nv50
->dirty
|= NV50_NEW_MIN_SAMPLES
;
894 nv50_set_framebuffer_state(struct pipe_context
*pipe
,
895 const struct pipe_framebuffer_state
*fb
)
897 struct nv50_context
*nv50
= nv50_context(pipe
);
900 nouveau_bufctx_reset(nv50
->bufctx_3d
, NV50_BIND_FB
);
902 for (i
= 0; i
< fb
->nr_cbufs
; ++i
)
903 pipe_surface_reference(&nv50
->framebuffer
.cbufs
[i
], fb
->cbufs
[i
]);
904 for (; i
< nv50
->framebuffer
.nr_cbufs
; ++i
)
905 pipe_surface_reference(&nv50
->framebuffer
.cbufs
[i
], NULL
);
907 nv50
->framebuffer
.nr_cbufs
= fb
->nr_cbufs
;
909 nv50
->framebuffer
.width
= fb
->width
;
910 nv50
->framebuffer
.height
= fb
->height
;
912 pipe_surface_reference(&nv50
->framebuffer
.zsbuf
, fb
->zsbuf
);
914 nv50
->dirty
|= NV50_NEW_FRAMEBUFFER
;
918 nv50_set_polygon_stipple(struct pipe_context
*pipe
,
919 const struct pipe_poly_stipple
*stipple
)
921 struct nv50_context
*nv50
= nv50_context(pipe
);
923 nv50
->stipple
= *stipple
;
924 nv50
->dirty
|= NV50_NEW_STIPPLE
;
928 nv50_set_scissor_states(struct pipe_context
*pipe
,
930 unsigned num_scissors
,
931 const struct pipe_scissor_state
*scissor
)
933 struct nv50_context
*nv50
= nv50_context(pipe
);
936 assert(start_slot
+ num_scissors
<= NV50_MAX_VIEWPORTS
);
937 for (i
= 0; i
< num_scissors
; i
++) {
938 if (!memcmp(&nv50
->scissors
[start_slot
+ i
], &scissor
[i
], sizeof(*scissor
)))
940 nv50
->scissors
[start_slot
+ i
] = scissor
[i
];
941 nv50
->scissors_dirty
|= 1 << (start_slot
+ i
);
942 nv50
->dirty
|= NV50_NEW_SCISSOR
;
947 nv50_set_viewport_states(struct pipe_context
*pipe
,
949 unsigned num_viewports
,
950 const struct pipe_viewport_state
*vpt
)
952 struct nv50_context
*nv50
= nv50_context(pipe
);
955 assert(start_slot
+ num_viewports
<= NV50_MAX_VIEWPORTS
);
956 for (i
= 0; i
< num_viewports
; i
++) {
957 if (!memcmp(&nv50
->viewports
[start_slot
+ i
], &vpt
[i
], sizeof(*vpt
)))
959 nv50
->viewports
[start_slot
+ i
] = vpt
[i
];
960 nv50
->viewports_dirty
|= 1 << (start_slot
+ i
);
961 nv50
->dirty
|= NV50_NEW_VIEWPORT
;
966 nv50_set_vertex_buffers(struct pipe_context
*pipe
,
967 unsigned start_slot
, unsigned count
,
968 const struct pipe_vertex_buffer
*vb
)
970 struct nv50_context
*nv50
= nv50_context(pipe
);
973 util_set_vertex_buffers_count(nv50
->vtxbuf
, &nv50
->num_vtxbufs
, vb
,
977 nv50
->vbo_user
&= ~(((1ull << count
) - 1) << start_slot
);
978 nv50
->vbo_constant
&= ~(((1ull << count
) - 1) << start_slot
);
982 for (i
= 0; i
< count
; ++i
) {
983 unsigned dst_index
= start_slot
+ i
;
985 if (!vb
[i
].buffer
&& vb
[i
].user_buffer
) {
986 nv50
->vbo_user
|= 1 << dst_index
;
988 nv50
->vbo_constant
|= 1 << dst_index
;
990 nv50
->vbo_constant
&= ~(1 << dst_index
);
992 nv50
->vbo_user
&= ~(1 << dst_index
);
993 nv50
->vbo_constant
&= ~(1 << dst_index
);
997 nouveau_bufctx_reset(nv50
->bufctx_3d
, NV50_BIND_VERTEX
);
999 nv50
->dirty
|= NV50_NEW_ARRAYS
;
1003 nv50_set_index_buffer(struct pipe_context
*pipe
,
1004 const struct pipe_index_buffer
*ib
)
1006 struct nv50_context
*nv50
= nv50_context(pipe
);
1008 if (nv50
->idxbuf
.buffer
)
1009 nouveau_bufctx_reset(nv50
->bufctx_3d
, NV50_BIND_INDEX
);
1012 pipe_resource_reference(&nv50
->idxbuf
.buffer
, ib
->buffer
);
1013 nv50
->idxbuf
.index_size
= ib
->index_size
;
1015 nv50
->idxbuf
.offset
= ib
->offset
;
1016 BCTX_REFN(nv50
->bufctx_3d
, INDEX
, nv04_resource(ib
->buffer
), RD
);
1018 nv50
->idxbuf
.user_buffer
= ib
->user_buffer
;
1021 pipe_resource_reference(&nv50
->idxbuf
.buffer
, NULL
);
1026 nv50_vertex_state_bind(struct pipe_context
*pipe
, void *hwcso
)
1028 struct nv50_context
*nv50
= nv50_context(pipe
);
1030 nv50
->vertex
= hwcso
;
1031 nv50
->dirty
|= NV50_NEW_VERTEX
;
1034 static struct pipe_stream_output_target
*
1035 nv50_so_target_create(struct pipe_context
*pipe
,
1036 struct pipe_resource
*res
,
1037 unsigned offset
, unsigned size
)
1039 struct nv04_resource
*buf
= (struct nv04_resource
*)res
;
1040 struct nv50_so_target
*targ
= MALLOC_STRUCT(nv50_so_target
);
1044 if (nouveau_context(pipe
)->screen
->class_3d
>= NVA0_3D_CLASS
) {
1045 targ
->pq
= pipe
->create_query(pipe
,
1046 NVA0_QUERY_STREAM_OUTPUT_BUFFER_OFFSET
, 0);
1056 targ
->pipe
.buffer_size
= size
;
1057 targ
->pipe
.buffer_offset
= offset
;
1058 targ
->pipe
.context
= pipe
;
1059 targ
->pipe
.buffer
= NULL
;
1060 pipe_resource_reference(&targ
->pipe
.buffer
, res
);
1061 pipe_reference_init(&targ
->pipe
.reference
, 1);
1063 assert(buf
->base
.target
== PIPE_BUFFER
);
1064 util_range_add(&buf
->valid_buffer_range
, offset
, offset
+ size
);
1070 nv50_so_target_destroy(struct pipe_context
*pipe
,
1071 struct pipe_stream_output_target
*ptarg
)
1073 struct nv50_so_target
*targ
= nv50_so_target(ptarg
);
1075 pipe
->destroy_query(pipe
, targ
->pq
);
1076 pipe_resource_reference(&targ
->pipe
.buffer
, NULL
);
1081 nv50_set_stream_output_targets(struct pipe_context
*pipe
,
1082 unsigned num_targets
,
1083 struct pipe_stream_output_target
**targets
,
1084 const unsigned *offsets
)
1086 struct nv50_context
*nv50
= nv50_context(pipe
);
1088 bool serialize
= true;
1089 const bool can_resume
= nv50
->screen
->base
.class_3d
>= NVA0_3D_CLASS
;
1091 assert(num_targets
<= 4);
1093 for (i
= 0; i
< num_targets
; ++i
) {
1094 const bool changed
= nv50
->so_target
[i
] != targets
[i
];
1095 const bool append
= (offsets
[i
] == (unsigned)-1);
1096 if (!changed
&& append
)
1098 nv50
->so_targets_dirty
|= 1 << i
;
1100 if (can_resume
&& changed
&& nv50
->so_target
[i
]) {
1101 nva0_so_target_save_offset(pipe
, nv50
->so_target
[i
], i
, serialize
);
1105 if (targets
[i
] && !append
)
1106 nv50_so_target(targets
[i
])->clean
= true;
1108 pipe_so_target_reference(&nv50
->so_target
[i
], targets
[i
]);
1110 for (; i
< nv50
->num_so_targets
; ++i
) {
1111 if (can_resume
&& nv50
->so_target
[i
]) {
1112 nva0_so_target_save_offset(pipe
, nv50
->so_target
[i
], i
, serialize
);
1115 pipe_so_target_reference(&nv50
->so_target
[i
], NULL
);
1116 nv50
->so_targets_dirty
|= 1 << i
;
1118 nv50
->num_so_targets
= num_targets
;
1120 if (nv50
->so_targets_dirty
)
1121 nv50
->dirty
|= NV50_NEW_STRMOUT
;
1125 nv50_init_state_functions(struct nv50_context
*nv50
)
1127 struct pipe_context
*pipe
= &nv50
->base
.pipe
;
1129 pipe
->create_blend_state
= nv50_blend_state_create
;
1130 pipe
->bind_blend_state
= nv50_blend_state_bind
;
1131 pipe
->delete_blend_state
= nv50_blend_state_delete
;
1133 pipe
->create_rasterizer_state
= nv50_rasterizer_state_create
;
1134 pipe
->bind_rasterizer_state
= nv50_rasterizer_state_bind
;
1135 pipe
->delete_rasterizer_state
= nv50_rasterizer_state_delete
;
1137 pipe
->create_depth_stencil_alpha_state
= nv50_zsa_state_create
;
1138 pipe
->bind_depth_stencil_alpha_state
= nv50_zsa_state_bind
;
1139 pipe
->delete_depth_stencil_alpha_state
= nv50_zsa_state_delete
;
1141 pipe
->create_sampler_state
= nv50_sampler_state_create
;
1142 pipe
->delete_sampler_state
= nv50_sampler_state_delete
;
1143 pipe
->bind_sampler_states
= nv50_bind_sampler_states
;
1145 pipe
->create_sampler_view
= nv50_create_sampler_view
;
1146 pipe
->sampler_view_destroy
= nv50_sampler_view_destroy
;
1147 pipe
->set_sampler_views
= nv50_set_sampler_views
;
1149 pipe
->create_vs_state
= nv50_vp_state_create
;
1150 pipe
->create_fs_state
= nv50_fp_state_create
;
1151 pipe
->create_gs_state
= nv50_gp_state_create
;
1152 pipe
->bind_vs_state
= nv50_vp_state_bind
;
1153 pipe
->bind_fs_state
= nv50_fp_state_bind
;
1154 pipe
->bind_gs_state
= nv50_gp_state_bind
;
1155 pipe
->delete_vs_state
= nv50_sp_state_delete
;
1156 pipe
->delete_fs_state
= nv50_sp_state_delete
;
1157 pipe
->delete_gs_state
= nv50_sp_state_delete
;
1159 pipe
->set_blend_color
= nv50_set_blend_color
;
1160 pipe
->set_stencil_ref
= nv50_set_stencil_ref
;
1161 pipe
->set_clip_state
= nv50_set_clip_state
;
1162 pipe
->set_sample_mask
= nv50_set_sample_mask
;
1163 pipe
->set_min_samples
= nv50_set_min_samples
;
1164 pipe
->set_constant_buffer
= nv50_set_constant_buffer
;
1165 pipe
->set_framebuffer_state
= nv50_set_framebuffer_state
;
1166 pipe
->set_polygon_stipple
= nv50_set_polygon_stipple
;
1167 pipe
->set_scissor_states
= nv50_set_scissor_states
;
1168 pipe
->set_viewport_states
= nv50_set_viewport_states
;
1170 pipe
->create_vertex_elements_state
= nv50_vertex_state_create
;
1171 pipe
->delete_vertex_elements_state
= nv50_vertex_state_delete
;
1172 pipe
->bind_vertex_elements_state
= nv50_vertex_state_bind
;
1174 pipe
->set_vertex_buffers
= nv50_set_vertex_buffers
;
1175 pipe
->set_index_buffer
= nv50_set_index_buffer
;
1177 pipe
->create_stream_output_target
= nv50_so_target_create
;
1178 pipe
->stream_output_target_destroy
= nv50_so_target_destroy
;
1179 pipe
->set_stream_output_targets
= nv50_set_stream_output_targets
;
1181 nv50
->sample_mask
= ~0;
1182 nv50
->min_samples
= 1;