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 boolean 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 if (cso
->stencil
[0].enabled
) {
377 SB_BEGIN_3D(so
, STENCIL_ENABLE
, 5);
379 SB_DATA (so
, nvgl_stencil_op(cso
->stencil
[0].fail_op
));
380 SB_DATA (so
, nvgl_stencil_op(cso
->stencil
[0].zfail_op
));
381 SB_DATA (so
, nvgl_stencil_op(cso
->stencil
[0].zpass_op
));
382 SB_DATA (so
, nvgl_comparison_op(cso
->stencil
[0].func
));
383 SB_BEGIN_3D(so
, STENCIL_FRONT_MASK
, 2);
384 SB_DATA (so
, cso
->stencil
[0].writemask
);
385 SB_DATA (so
, cso
->stencil
[0].valuemask
);
387 SB_BEGIN_3D(so
, STENCIL_ENABLE
, 1);
391 if (cso
->stencil
[1].enabled
) {
392 assert(cso
->stencil
[0].enabled
);
393 SB_BEGIN_3D(so
, STENCIL_TWO_SIDE_ENABLE
, 5);
395 SB_DATA (so
, nvgl_stencil_op(cso
->stencil
[1].fail_op
));
396 SB_DATA (so
, nvgl_stencil_op(cso
->stencil
[1].zfail_op
));
397 SB_DATA (so
, nvgl_stencil_op(cso
->stencil
[1].zpass_op
));
398 SB_DATA (so
, nvgl_comparison_op(cso
->stencil
[1].func
));
399 SB_BEGIN_3D(so
, STENCIL_BACK_MASK
, 2);
400 SB_DATA (so
, cso
->stencil
[1].writemask
);
401 SB_DATA (so
, cso
->stencil
[1].valuemask
);
403 SB_BEGIN_3D(so
, STENCIL_TWO_SIDE_ENABLE
, 1);
407 SB_BEGIN_3D(so
, ALPHA_TEST_ENABLE
, 1);
408 if (cso
->alpha
.enabled
) {
410 SB_BEGIN_3D(so
, ALPHA_TEST_REF
, 2);
411 SB_DATA (so
, fui(cso
->alpha
.ref_value
));
412 SB_DATA (so
, nvgl_comparison_op(cso
->alpha
.func
));
417 assert(so
->size
<= (sizeof(so
->state
) / sizeof(so
->state
[0])));
422 nv50_zsa_state_bind(struct pipe_context
*pipe
, void *hwcso
)
424 struct nv50_context
*nv50
= nv50_context(pipe
);
427 nv50
->dirty
|= NV50_NEW_ZSA
;
431 nv50_zsa_state_delete(struct pipe_context
*pipe
, void *hwcso
)
436 /* ====================== SAMPLERS AND TEXTURES ================================
439 #define NV50_TSC_WRAP_CASE(n) \
440 case PIPE_TEX_WRAP_##n: return NV50_TSC_WRAP_##n
442 static INLINE
unsigned
443 nv50_tsc_wrap_mode(unsigned wrap
)
446 NV50_TSC_WRAP_CASE(REPEAT
);
447 NV50_TSC_WRAP_CASE(MIRROR_REPEAT
);
448 NV50_TSC_WRAP_CASE(CLAMP_TO_EDGE
);
449 NV50_TSC_WRAP_CASE(CLAMP_TO_BORDER
);
450 NV50_TSC_WRAP_CASE(CLAMP
);
451 NV50_TSC_WRAP_CASE(MIRROR_CLAMP_TO_EDGE
);
452 NV50_TSC_WRAP_CASE(MIRROR_CLAMP_TO_BORDER
);
453 NV50_TSC_WRAP_CASE(MIRROR_CLAMP
);
455 NOUVEAU_ERR("unknown wrap mode: %d\n", wrap
);
456 return NV50_TSC_WRAP_REPEAT
;
461 nv50_sampler_state_create(struct pipe_context
*pipe
,
462 const struct pipe_sampler_state
*cso
)
464 struct nv50_tsc_entry
*so
= MALLOC_STRUCT(nv50_tsc_entry
);
469 so
->tsc
[0] = (0x00026000 |
470 (nv50_tsc_wrap_mode(cso
->wrap_s
) << 0) |
471 (nv50_tsc_wrap_mode(cso
->wrap_t
) << 3) |
472 (nv50_tsc_wrap_mode(cso
->wrap_r
) << 6));
474 switch (cso
->mag_img_filter
) {
475 case PIPE_TEX_FILTER_LINEAR
:
476 so
->tsc
[1] = NV50_TSC_1_MAGF_LINEAR
;
478 case PIPE_TEX_FILTER_NEAREST
:
480 so
->tsc
[1] = NV50_TSC_1_MAGF_NEAREST
;
484 switch (cso
->min_img_filter
) {
485 case PIPE_TEX_FILTER_LINEAR
:
486 so
->tsc
[1] |= NV50_TSC_1_MINF_LINEAR
;
488 case PIPE_TEX_FILTER_NEAREST
:
490 so
->tsc
[1] |= NV50_TSC_1_MINF_NEAREST
;
494 switch (cso
->min_mip_filter
) {
495 case PIPE_TEX_MIPFILTER_LINEAR
:
496 so
->tsc
[1] |= NV50_TSC_1_MIPF_LINEAR
;
498 case PIPE_TEX_MIPFILTER_NEAREST
:
499 so
->tsc
[1] |= NV50_TSC_1_MIPF_NEAREST
;
501 case PIPE_TEX_MIPFILTER_NONE
:
503 so
->tsc
[1] |= NV50_TSC_1_MIPF_NONE
;
507 if (nouveau_screen(pipe
->screen
)->class_3d
>= NVE4_3D_CLASS
) {
508 if (cso
->seamless_cube_map
)
509 so
->tsc
[1] |= NVE4_TSC_1_CUBE_SEAMLESS
;
510 if (!cso
->normalized_coords
)
511 so
->tsc
[1] |= NVE4_TSC_1_FORCE_NONNORMALIZED_COORDS
;
514 if (cso
->max_anisotropy
>= 16)
515 so
->tsc
[0] |= (7 << 20);
517 if (cso
->max_anisotropy
>= 12)
518 so
->tsc
[0] |= (6 << 20);
520 so
->tsc
[0] |= (cso
->max_anisotropy
>> 1) << 20;
522 if (cso
->max_anisotropy
>= 4)
523 so
->tsc
[1] |= NV50_TSC_1_UNKN_ANISO_35
;
525 if (cso
->max_anisotropy
>= 2)
526 so
->tsc
[1] |= NV50_TSC_1_UNKN_ANISO_15
;
529 if (cso
->compare_mode
== PIPE_TEX_COMPARE_R_TO_TEXTURE
) {
530 /* NOTE: must be deactivated for non-shadow textures */
531 so
->tsc
[0] |= (1 << 9);
532 so
->tsc
[0] |= (nvgl_comparison_op(cso
->compare_func
) & 0x7) << 10;
535 f
[0] = CLAMP(cso
->lod_bias
, -16.0f
, 15.0f
);
536 so
->tsc
[1] |= ((int)(f
[0] * 256.0f
) & 0x1fff) << 12;
538 f
[0] = CLAMP(cso
->min_lod
, 0.0f
, 15.0f
);
539 f
[1] = CLAMP(cso
->max_lod
, 0.0f
, 15.0f
);
541 (((int)(f
[1] * 256.0f
) & 0xfff) << 12) | ((int)(f
[0] * 256.0f
) & 0xfff);
544 util_format_linear_float_to_srgb_8unorm(cso
->border_color
.f
[0]) << 24;
546 util_format_linear_float_to_srgb_8unorm(cso
->border_color
.f
[1]) << 12;
548 util_format_linear_float_to_srgb_8unorm(cso
->border_color
.f
[2]) << 20;
550 so
->tsc
[4] = fui(cso
->border_color
.f
[0]);
551 so
->tsc
[5] = fui(cso
->border_color
.f
[1]);
552 so
->tsc
[6] = fui(cso
->border_color
.f
[2]);
553 so
->tsc
[7] = fui(cso
->border_color
.f
[3]);
559 nv50_sampler_state_delete(struct pipe_context
*pipe
, void *hwcso
)
563 for (s
= 0; s
< 3; ++s
) {
564 assert(nv50_context(pipe
)->num_samplers
[s
] <= PIPE_MAX_SAMPLERS
);
565 for (i
= 0; i
< nv50_context(pipe
)->num_samplers
[s
]; ++i
)
566 if (nv50_context(pipe
)->samplers
[s
][i
] == hwcso
)
567 nv50_context(pipe
)->samplers
[s
][i
] = NULL
;
570 nv50_screen_tsc_free(nv50_context(pipe
)->screen
, nv50_tsc_entry(hwcso
));
576 nv50_stage_sampler_states_bind(struct nv50_context
*nv50
, int s
,
577 unsigned nr
, void **hwcso
)
581 assert(nr
<= PIPE_MAX_SAMPLERS
);
582 for (i
= 0; i
< nr
; ++i
) {
583 struct nv50_tsc_entry
*old
= nv50
->samplers
[s
][i
];
585 nv50
->samplers
[s
][i
] = nv50_tsc_entry(hwcso
[i
]);
587 nv50_screen_tsc_unlock(nv50
->screen
, old
);
589 assert(nv50
->num_samplers
[s
] <= PIPE_MAX_SAMPLERS
);
590 for (; i
< nv50
->num_samplers
[s
]; ++i
) {
591 if (nv50
->samplers
[s
][i
]) {
592 nv50_screen_tsc_unlock(nv50
->screen
, nv50
->samplers
[s
][i
]);
593 nv50
->samplers
[s
][i
] = NULL
;
597 nv50
->num_samplers
[s
] = nr
;
599 nv50
->dirty
|= NV50_NEW_SAMPLERS
;
603 nv50_vp_sampler_states_bind(struct pipe_context
*pipe
, unsigned nr
, void **s
)
605 nv50_stage_sampler_states_bind(nv50_context(pipe
), 0, nr
, s
);
609 nv50_fp_sampler_states_bind(struct pipe_context
*pipe
, unsigned nr
, void **s
)
611 nv50_stage_sampler_states_bind(nv50_context(pipe
), 2, nr
, s
);
615 nv50_gp_sampler_states_bind(struct pipe_context
*pipe
, unsigned nr
, void **s
)
617 nv50_stage_sampler_states_bind(nv50_context(pipe
), 1, nr
, s
);
621 nv50_bind_sampler_states(struct pipe_context
*pipe
,
622 unsigned shader
, unsigned start
,
623 unsigned num_samplers
, void **samplers
)
627 case PIPE_SHADER_VERTEX
:
628 nv50_vp_sampler_states_bind(pipe
, num_samplers
, samplers
);
630 case PIPE_SHADER_GEOMETRY
:
631 nv50_gp_sampler_states_bind(pipe
, num_samplers
, samplers
);
633 case PIPE_SHADER_FRAGMENT
:
634 nv50_fp_sampler_states_bind(pipe
, num_samplers
, samplers
);
641 /* NOTE: only called when not referenced anywhere, won't be bound */
643 nv50_sampler_view_destroy(struct pipe_context
*pipe
,
644 struct pipe_sampler_view
*view
)
646 pipe_resource_reference(&view
->texture
, NULL
);
648 nv50_screen_tic_free(nv50_context(pipe
)->screen
, nv50_tic_entry(view
));
650 FREE(nv50_tic_entry(view
));
654 nv50_stage_set_sampler_views(struct nv50_context
*nv50
, int s
,
656 struct pipe_sampler_view
**views
)
660 assert(nr
<= PIPE_MAX_SAMPLERS
);
661 for (i
= 0; i
< nr
; ++i
) {
662 struct nv50_tic_entry
*old
= nv50_tic_entry(nv50
->textures
[s
][i
]);
664 nv50_screen_tic_unlock(nv50
->screen
, old
);
666 pipe_sampler_view_reference(&nv50
->textures
[s
][i
], views
[i
]);
669 assert(nv50
->num_textures
[s
] <= PIPE_MAX_SAMPLERS
);
670 for (i
= nr
; i
< nv50
->num_textures
[s
]; ++i
) {
671 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
], NULL
);
679 nv50
->num_textures
[s
] = nr
;
681 nouveau_bufctx_reset(nv50
->bufctx_3d
, NV50_BIND_TEXTURES
);
683 nv50
->dirty
|= NV50_NEW_TEXTURES
;
687 nv50_set_sampler_views(struct pipe_context
*pipe
, unsigned shader
,
688 unsigned start
, unsigned nr
,
689 struct pipe_sampler_view
**views
)
693 case PIPE_SHADER_VERTEX
:
694 nv50_stage_set_sampler_views(nv50_context(pipe
), 0, nr
, views
);
696 case PIPE_SHADER_GEOMETRY
:
697 nv50_stage_set_sampler_views(nv50_context(pipe
), 1, nr
, views
);
699 case PIPE_SHADER_FRAGMENT
:
700 nv50_stage_set_sampler_views(nv50_context(pipe
), 2, nr
, views
);
709 /* ============================= SHADERS =======================================
713 nv50_sp_state_create(struct pipe_context
*pipe
,
714 const struct pipe_shader_state
*cso
, unsigned type
)
716 struct nv50_program
*prog
;
718 prog
= CALLOC_STRUCT(nv50_program
);
723 prog
->pipe
.tokens
= tgsi_dup_tokens(cso
->tokens
);
725 if (cso
->stream_output
.num_outputs
)
726 prog
->pipe
.stream_output
= cso
->stream_output
;
732 nv50_sp_state_delete(struct pipe_context
*pipe
, void *hwcso
)
734 struct nv50_program
*prog
= (struct nv50_program
*)hwcso
;
736 nv50_program_destroy(nv50_context(pipe
), prog
);
738 FREE((void *)prog
->pipe
.tokens
);
743 nv50_vp_state_create(struct pipe_context
*pipe
,
744 const struct pipe_shader_state
*cso
)
746 return nv50_sp_state_create(pipe
, cso
, PIPE_SHADER_VERTEX
);
750 nv50_vp_state_bind(struct pipe_context
*pipe
, void *hwcso
)
752 struct nv50_context
*nv50
= nv50_context(pipe
);
754 nv50
->vertprog
= hwcso
;
755 nv50
->dirty
|= NV50_NEW_VERTPROG
;
759 nv50_fp_state_create(struct pipe_context
*pipe
,
760 const struct pipe_shader_state
*cso
)
762 return nv50_sp_state_create(pipe
, cso
, PIPE_SHADER_FRAGMENT
);
766 nv50_fp_state_bind(struct pipe_context
*pipe
, void *hwcso
)
768 struct nv50_context
*nv50
= nv50_context(pipe
);
770 nv50
->fragprog
= hwcso
;
771 nv50
->dirty
|= NV50_NEW_FRAGPROG
;
775 nv50_gp_state_create(struct pipe_context
*pipe
,
776 const struct pipe_shader_state
*cso
)
778 return nv50_sp_state_create(pipe
, cso
, PIPE_SHADER_GEOMETRY
);
782 nv50_gp_state_bind(struct pipe_context
*pipe
, void *hwcso
)
784 struct nv50_context
*nv50
= nv50_context(pipe
);
786 nv50
->gmtyprog
= hwcso
;
787 nv50
->dirty
|= NV50_NEW_GMTYPROG
;
791 nv50_set_constant_buffer(struct pipe_context
*pipe
, uint shader
, uint index
,
792 struct pipe_constant_buffer
*cb
)
794 struct nv50_context
*nv50
= nv50_context(pipe
);
795 struct pipe_resource
*res
= cb
? cb
->buffer
: NULL
;
796 const unsigned s
= nv50_context_shader_stage(shader
);
797 const unsigned i
= index
;
799 if (shader
== PIPE_SHADER_COMPUTE
)
802 assert(i
< NV50_MAX_PIPE_CONSTBUFS
);
803 if (nv50
->constbuf
[s
][i
].user
)
804 nv50
->constbuf
[s
][i
].u
.buf
= NULL
;
806 if (nv50
->constbuf
[s
][i
].u
.buf
)
807 nouveau_bufctx_reset(nv50
->bufctx_3d
, NV50_BIND_CB(s
, i
));
809 pipe_resource_reference(&nv50
->constbuf
[s
][i
].u
.buf
, res
);
811 nv50
->constbuf
[s
][i
].user
= (cb
&& cb
->user_buffer
) ? TRUE
: FALSE
;
812 if (nv50
->constbuf
[s
][i
].user
) {
813 nv50
->constbuf
[s
][i
].u
.data
= cb
->user_buffer
;
814 nv50
->constbuf
[s
][i
].size
= cb
->buffer_size
;
815 nv50
->constbuf_valid
[s
] |= 1 << i
;
818 nv50
->constbuf
[s
][i
].offset
= cb
->buffer_offset
;
819 nv50
->constbuf
[s
][i
].size
= align(cb
->buffer_size
, 0x100);
820 nv50
->constbuf_valid
[s
] |= 1 << i
;
822 nv50
->constbuf_valid
[s
] &= ~(1 << i
);
824 nv50
->constbuf_dirty
[s
] |= 1 << i
;
826 nv50
->dirty
|= NV50_NEW_CONSTBUF
;
829 /* =============================================================================
833 nv50_set_blend_color(struct pipe_context
*pipe
,
834 const struct pipe_blend_color
*bcol
)
836 struct nv50_context
*nv50
= nv50_context(pipe
);
838 nv50
->blend_colour
= *bcol
;
839 nv50
->dirty
|= NV50_NEW_BLEND_COLOUR
;
843 nv50_set_stencil_ref(struct pipe_context
*pipe
,
844 const struct pipe_stencil_ref
*sr
)
846 struct nv50_context
*nv50
= nv50_context(pipe
);
848 nv50
->stencil_ref
= *sr
;
849 nv50
->dirty
|= NV50_NEW_STENCIL_REF
;
853 nv50_set_clip_state(struct pipe_context
*pipe
,
854 const struct pipe_clip_state
*clip
)
856 struct nv50_context
*nv50
= nv50_context(pipe
);
858 memcpy(nv50
->clip
.ucp
, clip
->ucp
, sizeof(clip
->ucp
));
860 nv50
->dirty
|= NV50_NEW_CLIP
;
864 nv50_set_sample_mask(struct pipe_context
*pipe
, unsigned sample_mask
)
866 struct nv50_context
*nv50
= nv50_context(pipe
);
868 nv50
->sample_mask
= sample_mask
;
869 nv50
->dirty
|= NV50_NEW_SAMPLE_MASK
;
873 nv50_set_min_samples(struct pipe_context
*pipe
, unsigned min_samples
)
875 struct nv50_context
*nv50
= nv50_context(pipe
);
877 if (nv50
->min_samples
!= min_samples
) {
878 nv50
->min_samples
= min_samples
;
879 nv50
->dirty
|= NV50_NEW_MIN_SAMPLES
;
884 nv50_set_framebuffer_state(struct pipe_context
*pipe
,
885 const struct pipe_framebuffer_state
*fb
)
887 struct nv50_context
*nv50
= nv50_context(pipe
);
890 nouveau_bufctx_reset(nv50
->bufctx_3d
, NV50_BIND_FB
);
892 for (i
= 0; i
< fb
->nr_cbufs
; ++i
)
893 pipe_surface_reference(&nv50
->framebuffer
.cbufs
[i
], fb
->cbufs
[i
]);
894 for (; i
< nv50
->framebuffer
.nr_cbufs
; ++i
)
895 pipe_surface_reference(&nv50
->framebuffer
.cbufs
[i
], NULL
);
897 nv50
->framebuffer
.nr_cbufs
= fb
->nr_cbufs
;
899 nv50
->framebuffer
.width
= fb
->width
;
900 nv50
->framebuffer
.height
= fb
->height
;
902 pipe_surface_reference(&nv50
->framebuffer
.zsbuf
, fb
->zsbuf
);
904 nv50
->dirty
|= NV50_NEW_FRAMEBUFFER
;
908 nv50_set_polygon_stipple(struct pipe_context
*pipe
,
909 const struct pipe_poly_stipple
*stipple
)
911 struct nv50_context
*nv50
= nv50_context(pipe
);
913 nv50
->stipple
= *stipple
;
914 nv50
->dirty
|= NV50_NEW_STIPPLE
;
918 nv50_set_scissor_states(struct pipe_context
*pipe
,
920 unsigned num_scissors
,
921 const struct pipe_scissor_state
*scissor
)
923 struct nv50_context
*nv50
= nv50_context(pipe
);
926 assert(start_slot
+ num_scissors
<= NV50_MAX_VIEWPORTS
);
927 for (i
= 0; i
< num_scissors
; i
++) {
928 if (!memcmp(&nv50
->scissors
[start_slot
+ i
], &scissor
[i
], sizeof(*scissor
)))
930 nv50
->scissors
[start_slot
+ i
] = scissor
[i
];
931 nv50
->scissors_dirty
|= 1 << (start_slot
+ i
);
932 nv50
->dirty
|= NV50_NEW_SCISSOR
;
937 nv50_set_viewport_states(struct pipe_context
*pipe
,
939 unsigned num_viewports
,
940 const struct pipe_viewport_state
*vpt
)
942 struct nv50_context
*nv50
= nv50_context(pipe
);
945 assert(start_slot
+ num_viewports
<= NV50_MAX_VIEWPORTS
);
946 for (i
= 0; i
< num_viewports
; i
++) {
947 if (!memcmp(&nv50
->viewports
[start_slot
+ i
], &vpt
[i
], sizeof(*vpt
)))
949 nv50
->viewports
[start_slot
+ i
] = vpt
[i
];
950 nv50
->viewports_dirty
|= 1 << (start_slot
+ i
);
951 nv50
->dirty
|= NV50_NEW_VIEWPORT
;
956 nv50_set_vertex_buffers(struct pipe_context
*pipe
,
957 unsigned start_slot
, unsigned count
,
958 const struct pipe_vertex_buffer
*vb
)
960 struct nv50_context
*nv50
= nv50_context(pipe
);
963 util_set_vertex_buffers_count(nv50
->vtxbuf
, &nv50
->num_vtxbufs
, vb
,
967 nv50
->vbo_user
&= ~(((1ull << count
) - 1) << start_slot
);
968 nv50
->vbo_constant
&= ~(((1ull << count
) - 1) << start_slot
);
972 for (i
= 0; i
< count
; ++i
) {
973 unsigned dst_index
= start_slot
+ i
;
975 if (!vb
[i
].buffer
&& vb
[i
].user_buffer
) {
976 nv50
->vbo_user
|= 1 << dst_index
;
978 nv50
->vbo_constant
|= 1 << dst_index
;
980 nv50
->vbo_constant
&= ~(1 << dst_index
);
982 nv50
->vbo_user
&= ~(1 << dst_index
);
983 nv50
->vbo_constant
&= ~(1 << dst_index
);
987 nouveau_bufctx_reset(nv50
->bufctx_3d
, NV50_BIND_VERTEX
);
989 nv50
->dirty
|= NV50_NEW_ARRAYS
;
993 nv50_set_index_buffer(struct pipe_context
*pipe
,
994 const struct pipe_index_buffer
*ib
)
996 struct nv50_context
*nv50
= nv50_context(pipe
);
998 if (nv50
->idxbuf
.buffer
)
999 nouveau_bufctx_reset(nv50
->bufctx_3d
, NV50_BIND_INDEX
);
1002 pipe_resource_reference(&nv50
->idxbuf
.buffer
, ib
->buffer
);
1003 nv50
->idxbuf
.index_size
= ib
->index_size
;
1005 nv50
->idxbuf
.offset
= ib
->offset
;
1006 BCTX_REFN(nv50
->bufctx_3d
, INDEX
, nv04_resource(ib
->buffer
), RD
);
1008 nv50
->idxbuf
.user_buffer
= ib
->user_buffer
;
1011 pipe_resource_reference(&nv50
->idxbuf
.buffer
, NULL
);
1016 nv50_vertex_state_bind(struct pipe_context
*pipe
, void *hwcso
)
1018 struct nv50_context
*nv50
= nv50_context(pipe
);
1020 nv50
->vertex
= hwcso
;
1021 nv50
->dirty
|= NV50_NEW_VERTEX
;
1024 static struct pipe_stream_output_target
*
1025 nv50_so_target_create(struct pipe_context
*pipe
,
1026 struct pipe_resource
*res
,
1027 unsigned offset
, unsigned size
)
1029 struct nv04_resource
*buf
= (struct nv04_resource
*)res
;
1030 struct nv50_so_target
*targ
= MALLOC_STRUCT(nv50_so_target
);
1034 if (nouveau_context(pipe
)->screen
->class_3d
>= NVA0_3D_CLASS
) {
1035 targ
->pq
= pipe
->create_query(pipe
,
1036 NVA0_QUERY_STREAM_OUTPUT_BUFFER_OFFSET
, 0);
1046 targ
->pipe
.buffer_size
= size
;
1047 targ
->pipe
.buffer_offset
= offset
;
1048 targ
->pipe
.context
= pipe
;
1049 targ
->pipe
.buffer
= NULL
;
1050 pipe_resource_reference(&targ
->pipe
.buffer
, res
);
1051 pipe_reference_init(&targ
->pipe
.reference
, 1);
1053 assert(buf
->base
.target
== PIPE_BUFFER
);
1054 util_range_add(&buf
->valid_buffer_range
, offset
, offset
+ size
);
1060 nv50_so_target_destroy(struct pipe_context
*pipe
,
1061 struct pipe_stream_output_target
*ptarg
)
1063 struct nv50_so_target
*targ
= nv50_so_target(ptarg
);
1065 pipe
->destroy_query(pipe
, targ
->pq
);
1066 pipe_resource_reference(&targ
->pipe
.buffer
, NULL
);
1071 nv50_set_stream_output_targets(struct pipe_context
*pipe
,
1072 unsigned num_targets
,
1073 struct pipe_stream_output_target
**targets
,
1074 const unsigned *offsets
)
1076 struct nv50_context
*nv50
= nv50_context(pipe
);
1078 boolean serialize
= TRUE
;
1079 const boolean can_resume
= nv50
->screen
->base
.class_3d
>= NVA0_3D_CLASS
;
1081 assert(num_targets
<= 4);
1083 for (i
= 0; i
< num_targets
; ++i
) {
1084 const boolean changed
= nv50
->so_target
[i
] != targets
[i
];
1085 const boolean append
= (offsets
[i
] == (unsigned)-1);
1086 if (!changed
&& append
)
1088 nv50
->so_targets_dirty
|= 1 << i
;
1090 if (can_resume
&& changed
&& nv50
->so_target
[i
]) {
1091 nva0_so_target_save_offset(pipe
, nv50
->so_target
[i
], i
, serialize
);
1095 if (targets
[i
] && !append
)
1096 nv50_so_target(targets
[i
])->clean
= TRUE
;
1098 pipe_so_target_reference(&nv50
->so_target
[i
], targets
[i
]);
1100 for (; i
< nv50
->num_so_targets
; ++i
) {
1101 if (can_resume
&& nv50
->so_target
[i
]) {
1102 nva0_so_target_save_offset(pipe
, nv50
->so_target
[i
], i
, serialize
);
1105 pipe_so_target_reference(&nv50
->so_target
[i
], NULL
);
1106 nv50
->so_targets_dirty
|= 1 << i
;
1108 nv50
->num_so_targets
= num_targets
;
1110 if (nv50
->so_targets_dirty
)
1111 nv50
->dirty
|= NV50_NEW_STRMOUT
;
1115 nv50_init_state_functions(struct nv50_context
*nv50
)
1117 struct pipe_context
*pipe
= &nv50
->base
.pipe
;
1119 pipe
->create_blend_state
= nv50_blend_state_create
;
1120 pipe
->bind_blend_state
= nv50_blend_state_bind
;
1121 pipe
->delete_blend_state
= nv50_blend_state_delete
;
1123 pipe
->create_rasterizer_state
= nv50_rasterizer_state_create
;
1124 pipe
->bind_rasterizer_state
= nv50_rasterizer_state_bind
;
1125 pipe
->delete_rasterizer_state
= nv50_rasterizer_state_delete
;
1127 pipe
->create_depth_stencil_alpha_state
= nv50_zsa_state_create
;
1128 pipe
->bind_depth_stencil_alpha_state
= nv50_zsa_state_bind
;
1129 pipe
->delete_depth_stencil_alpha_state
= nv50_zsa_state_delete
;
1131 pipe
->create_sampler_state
= nv50_sampler_state_create
;
1132 pipe
->delete_sampler_state
= nv50_sampler_state_delete
;
1133 pipe
->bind_sampler_states
= nv50_bind_sampler_states
;
1135 pipe
->create_sampler_view
= nv50_create_sampler_view
;
1136 pipe
->sampler_view_destroy
= nv50_sampler_view_destroy
;
1137 pipe
->set_sampler_views
= nv50_set_sampler_views
;
1139 pipe
->create_vs_state
= nv50_vp_state_create
;
1140 pipe
->create_fs_state
= nv50_fp_state_create
;
1141 pipe
->create_gs_state
= nv50_gp_state_create
;
1142 pipe
->bind_vs_state
= nv50_vp_state_bind
;
1143 pipe
->bind_fs_state
= nv50_fp_state_bind
;
1144 pipe
->bind_gs_state
= nv50_gp_state_bind
;
1145 pipe
->delete_vs_state
= nv50_sp_state_delete
;
1146 pipe
->delete_fs_state
= nv50_sp_state_delete
;
1147 pipe
->delete_gs_state
= nv50_sp_state_delete
;
1149 pipe
->set_blend_color
= nv50_set_blend_color
;
1150 pipe
->set_stencil_ref
= nv50_set_stencil_ref
;
1151 pipe
->set_clip_state
= nv50_set_clip_state
;
1152 pipe
->set_sample_mask
= nv50_set_sample_mask
;
1153 pipe
->set_min_samples
= nv50_set_min_samples
;
1154 pipe
->set_constant_buffer
= nv50_set_constant_buffer
;
1155 pipe
->set_framebuffer_state
= nv50_set_framebuffer_state
;
1156 pipe
->set_polygon_stipple
= nv50_set_polygon_stipple
;
1157 pipe
->set_scissor_states
= nv50_set_scissor_states
;
1158 pipe
->set_viewport_states
= nv50_set_viewport_states
;
1160 pipe
->create_vertex_elements_state
= nv50_vertex_state_create
;
1161 pipe
->delete_vertex_elements_state
= nv50_vertex_state_delete
;
1162 pipe
->bind_vertex_elements_state
= nv50_vertex_state_bind
;
1164 pipe
->set_vertex_buffers
= nv50_set_vertex_buffers
;
1165 pipe
->set_index_buffer
= nv50_set_index_buffer
;
1167 pipe
->create_stream_output_target
= nv50_so_target_create
;
1168 pipe
->stream_output_target_destroy
= nv50_so_target_destroy
;
1169 pipe
->set_stream_output_targets
= nv50_set_stream_output_targets
;
1171 nv50
->sample_mask
= ~0;
1172 nv50
->min_samples
= 1;