1 /****************************************************************************
2 * Copyright (C) 2015 Intel Corporation. All Rights Reserved.
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 (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 ***************************************************************************/
24 // llvm redefines DEBUG
25 #pragma push_macro("DEBUG")
27 #include "JitManager.h"
28 #pragma pop_macro("DEBUG")
30 #include "common/os.h"
32 #include "gen_state_llvm.h"
33 #include "core/multisample.h"
35 #include "gallivm/lp_bld_tgsi.h"
36 #include "util/u_format.h"
38 #include "util/u_memory.h"
39 #include "util/u_inlines.h"
40 #include "util/u_helpers.h"
41 #include "util/u_framebuffer.h"
42 #include "util/u_viewport.h"
44 #include "swr_state.h"
45 #include "swr_context.h"
46 #include "gen_swr_context_llvm.h"
47 #include "swr_screen.h"
48 #include "swr_resource.h"
49 #include "swr_tex_sample.h"
50 #include "swr_scratch.h"
51 #include "swr_shader.h"
52 #include "swr_fence.h"
54 /* These should be pulled out into separate files as necessary
55 * Just initializing everything here to get going. */
58 swr_create_blend_state(struct pipe_context
*pipe
,
59 const struct pipe_blend_state
*blend
)
61 struct swr_blend_state
*state
= CALLOC_STRUCT(swr_blend_state
);
63 memcpy(&state
->pipe
, blend
, sizeof(*blend
));
65 struct pipe_blend_state
*pipe_blend
= &state
->pipe
;
68 target
< std::min(SWR_NUM_RENDERTARGETS
, PIPE_MAX_COLOR_BUFS
);
71 struct pipe_rt_blend_state
*rt_blend
= &pipe_blend
->rt
[target
];
72 SWR_RENDER_TARGET_BLEND_STATE
&blendState
=
73 state
->blendState
.renderTarget
[target
];
74 RENDER_TARGET_BLEND_COMPILE_STATE
&compileState
=
75 state
->compileState
[target
];
77 if (target
!= 0 && !pipe_blend
->independent_blend_enable
) {
79 &state
->compileState
[0],
80 sizeof(RENDER_TARGET_BLEND_COMPILE_STATE
));
84 compileState
.blendEnable
= rt_blend
->blend_enable
;
85 if (compileState
.blendEnable
) {
86 compileState
.sourceAlphaBlendFactor
=
87 swr_convert_blend_factor(rt_blend
->alpha_src_factor
);
88 compileState
.destAlphaBlendFactor
=
89 swr_convert_blend_factor(rt_blend
->alpha_dst_factor
);
90 compileState
.sourceBlendFactor
=
91 swr_convert_blend_factor(rt_blend
->rgb_src_factor
);
92 compileState
.destBlendFactor
=
93 swr_convert_blend_factor(rt_blend
->rgb_dst_factor
);
95 compileState
.colorBlendFunc
=
96 swr_convert_blend_func(rt_blend
->rgb_func
);
97 compileState
.alphaBlendFunc
=
98 swr_convert_blend_func(rt_blend
->alpha_func
);
100 compileState
.logicOpEnable
= state
->pipe
.logicop_enable
;
101 if (compileState
.logicOpEnable
) {
102 compileState
.logicOpFunc
=
103 swr_convert_logic_op(state
->pipe
.logicop_func
);
106 blendState
.writeDisableRed
=
107 (rt_blend
->colormask
& PIPE_MASK_R
) ? 0 : 1;
108 blendState
.writeDisableGreen
=
109 (rt_blend
->colormask
& PIPE_MASK_G
) ? 0 : 1;
110 blendState
.writeDisableBlue
=
111 (rt_blend
->colormask
& PIPE_MASK_B
) ? 0 : 1;
112 blendState
.writeDisableAlpha
=
113 (rt_blend
->colormask
& PIPE_MASK_A
) ? 0 : 1;
115 if (rt_blend
->colormask
== 0)
116 compileState
.blendEnable
= false;
123 swr_bind_blend_state(struct pipe_context
*pipe
, void *blend
)
125 struct swr_context
*ctx
= swr_context(pipe
);
127 if (ctx
->blend
== blend
)
130 ctx
->blend
= (swr_blend_state
*)blend
;
132 ctx
->dirty
|= SWR_NEW_BLEND
;
136 swr_delete_blend_state(struct pipe_context
*pipe
, void *blend
)
142 swr_set_blend_color(struct pipe_context
*pipe
,
143 const struct pipe_blend_color
*color
)
145 struct swr_context
*ctx
= swr_context(pipe
);
147 ctx
->blend_color
= *color
;
149 ctx
->dirty
|= SWR_NEW_BLEND
;
153 swr_set_stencil_ref(struct pipe_context
*pipe
,
154 const struct pipe_stencil_ref
*ref
)
156 struct swr_context
*ctx
= swr_context(pipe
);
158 ctx
->stencil_ref
= *ref
;
160 ctx
->dirty
|= SWR_NEW_DEPTH_STENCIL_ALPHA
;
164 swr_create_depth_stencil_state(
165 struct pipe_context
*pipe
,
166 const struct pipe_depth_stencil_alpha_state
*depth_stencil
)
168 struct pipe_depth_stencil_alpha_state
*state
;
170 state
= (pipe_depth_stencil_alpha_state
*)mem_dup(depth_stencil
,
171 sizeof *depth_stencil
);
177 swr_bind_depth_stencil_state(struct pipe_context
*pipe
, void *depth_stencil
)
179 struct swr_context
*ctx
= swr_context(pipe
);
181 if (ctx
->depth_stencil
== (pipe_depth_stencil_alpha_state
*)depth_stencil
)
184 ctx
->depth_stencil
= (pipe_depth_stencil_alpha_state
*)depth_stencil
;
186 ctx
->dirty
|= SWR_NEW_DEPTH_STENCIL_ALPHA
;
190 swr_delete_depth_stencil_state(struct pipe_context
*pipe
, void *depth
)
197 swr_create_rasterizer_state(struct pipe_context
*pipe
,
198 const struct pipe_rasterizer_state
*rast
)
200 struct pipe_rasterizer_state
*state
;
201 state
= (pipe_rasterizer_state
*)mem_dup(rast
, sizeof *rast
);
207 swr_bind_rasterizer_state(struct pipe_context
*pipe
, void *handle
)
209 struct swr_context
*ctx
= swr_context(pipe
);
210 const struct pipe_rasterizer_state
*rasterizer
=
211 (const struct pipe_rasterizer_state
*)handle
;
213 if (ctx
->rasterizer
== (pipe_rasterizer_state
*)rasterizer
)
216 ctx
->rasterizer
= (pipe_rasterizer_state
*)rasterizer
;
218 ctx
->dirty
|= SWR_NEW_RASTERIZER
;
222 swr_delete_rasterizer_state(struct pipe_context
*pipe
, void *rasterizer
)
229 swr_create_sampler_state(struct pipe_context
*pipe
,
230 const struct pipe_sampler_state
*sampler
)
232 struct pipe_sampler_state
*state
=
233 (pipe_sampler_state
*)mem_dup(sampler
, sizeof *sampler
);
239 swr_bind_sampler_states(struct pipe_context
*pipe
,
240 enum pipe_shader_type shader
,
245 struct swr_context
*ctx
= swr_context(pipe
);
248 assert(shader
< PIPE_SHADER_TYPES
);
249 assert(start
+ num
<= ARRAY_SIZE(ctx
->samplers
[shader
]));
251 /* set the new samplers */
252 ctx
->num_samplers
[shader
] = num
;
253 for (i
= 0; i
< num
; i
++) {
254 ctx
->samplers
[shader
][start
+ i
] = (pipe_sampler_state
*)samplers
[i
];
257 ctx
->dirty
|= SWR_NEW_SAMPLER
;
261 swr_delete_sampler_state(struct pipe_context
*pipe
, void *sampler
)
267 static struct pipe_sampler_view
*
268 swr_create_sampler_view(struct pipe_context
*pipe
,
269 struct pipe_resource
*texture
,
270 const struct pipe_sampler_view
*templ
)
272 struct pipe_sampler_view
*view
= CALLOC_STRUCT(pipe_sampler_view
);
276 view
->reference
.count
= 1;
277 view
->texture
= NULL
;
278 pipe_resource_reference(&view
->texture
, texture
);
279 view
->context
= pipe
;
286 swr_set_sampler_views(struct pipe_context
*pipe
,
287 enum pipe_shader_type shader
,
290 struct pipe_sampler_view
**views
)
292 struct swr_context
*ctx
= swr_context(pipe
);
295 assert(num
<= PIPE_MAX_SHADER_SAMPLER_VIEWS
);
297 assert(shader
< PIPE_SHADER_TYPES
);
298 assert(start
+ num
<= ARRAY_SIZE(ctx
->sampler_views
[shader
]));
300 /* set the new sampler views */
301 ctx
->num_sampler_views
[shader
] = num
;
302 for (i
= 0; i
< num
; i
++) {
303 /* Note: we're using pipe_sampler_view_release() here to work around
304 * a possible crash when the old view belongs to another context that
305 * was already destroyed.
307 pipe_sampler_view_release(pipe
, &ctx
->sampler_views
[shader
][start
+ i
]);
308 pipe_sampler_view_reference(&ctx
->sampler_views
[shader
][start
+ i
],
312 ctx
->dirty
|= SWR_NEW_SAMPLER_VIEW
;
316 swr_sampler_view_destroy(struct pipe_context
*pipe
,
317 struct pipe_sampler_view
*view
)
319 pipe_resource_reference(&view
->texture
, NULL
);
324 swr_create_vs_state(struct pipe_context
*pipe
,
325 const struct pipe_shader_state
*vs
)
327 struct swr_vertex_shader
*swr_vs
= new swr_vertex_shader
;
331 swr_vs
->pipe
.tokens
= tgsi_dup_tokens(vs
->tokens
);
332 swr_vs
->pipe
.stream_output
= vs
->stream_output
;
334 lp_build_tgsi_info(vs
->tokens
, &swr_vs
->info
);
336 swr_vs
->soState
= {0};
338 if (swr_vs
->pipe
.stream_output
.num_outputs
) {
339 pipe_stream_output_info
*stream_output
= &swr_vs
->pipe
.stream_output
;
341 swr_vs
->soState
.soEnable
= true;
342 // soState.rasterizerDisable set on state dirty
343 // soState.streamToRasterizer not used
345 for (uint32_t i
= 0; i
< stream_output
->num_outputs
; i
++) {
346 swr_vs
->soState
.streamMasks
[stream_output
->output
[i
].stream
] |=
347 1 << (stream_output
->output
[i
].register_index
- 1);
349 for (uint32_t i
= 0; i
< MAX_SO_STREAMS
; i
++) {
350 swr_vs
->soState
.streamNumEntries
[i
] =
351 _mm_popcnt_u32(swr_vs
->soState
.streamMasks
[i
]);
359 swr_bind_vs_state(struct pipe_context
*pipe
, void *vs
)
361 struct swr_context
*ctx
= swr_context(pipe
);
366 ctx
->vs
= (swr_vertex_shader
*)vs
;
367 ctx
->dirty
|= SWR_NEW_VS
;
371 swr_delete_vs_state(struct pipe_context
*pipe
, void *vs
)
373 struct swr_vertex_shader
*swr_vs
= (swr_vertex_shader
*)vs
;
374 FREE((void *)swr_vs
->pipe
.tokens
);
375 struct swr_screen
*screen
= swr_screen(pipe
->screen
);
377 /* Defer deletion of vs state */
378 swr_fence_work_delete_vs(screen
->flush_fence
, swr_vs
);
382 swr_create_fs_state(struct pipe_context
*pipe
,
383 const struct pipe_shader_state
*fs
)
385 struct swr_fragment_shader
*swr_fs
= new swr_fragment_shader
;
389 swr_fs
->pipe
.tokens
= tgsi_dup_tokens(fs
->tokens
);
391 lp_build_tgsi_info(fs
->tokens
, &swr_fs
->info
);
398 swr_bind_fs_state(struct pipe_context
*pipe
, void *fs
)
400 struct swr_context
*ctx
= swr_context(pipe
);
405 ctx
->fs
= (swr_fragment_shader
*)fs
;
406 ctx
->dirty
|= SWR_NEW_FS
;
410 swr_delete_fs_state(struct pipe_context
*pipe
, void *fs
)
412 struct swr_fragment_shader
*swr_fs
= (swr_fragment_shader
*)fs
;
413 FREE((void *)swr_fs
->pipe
.tokens
);
414 struct swr_screen
*screen
= swr_screen(pipe
->screen
);
416 /* Defer deleton of fs state */
417 swr_fence_work_delete_fs(screen
->flush_fence
, swr_fs
);
421 swr_create_gs_state(struct pipe_context
*pipe
,
422 const struct pipe_shader_state
*gs
)
424 struct swr_geometry_shader
*swr_gs
= new swr_geometry_shader
;
428 swr_gs
->pipe
.tokens
= tgsi_dup_tokens(gs
->tokens
);
430 lp_build_tgsi_info(gs
->tokens
, &swr_gs
->info
);
437 swr_bind_gs_state(struct pipe_context
*pipe
, void *gs
)
439 struct swr_context
*ctx
= swr_context(pipe
);
444 ctx
->gs
= (swr_geometry_shader
*)gs
;
445 ctx
->dirty
|= SWR_NEW_GS
;
449 swr_delete_gs_state(struct pipe_context
*pipe
, void *gs
)
451 struct swr_geometry_shader
*swr_gs
= (swr_geometry_shader
*)gs
;
452 FREE((void *)swr_gs
->pipe
.tokens
);
453 struct swr_screen
*screen
= swr_screen(pipe
->screen
);
455 /* Defer deleton of fs state */
456 swr_fence_work_delete_gs(screen
->flush_fence
, swr_gs
);
460 swr_set_constant_buffer(struct pipe_context
*pipe
,
461 enum pipe_shader_type shader
,
463 const struct pipe_constant_buffer
*cb
)
465 struct swr_context
*ctx
= swr_context(pipe
);
466 struct pipe_resource
*constants
= cb
? cb
->buffer
: NULL
;
468 assert(shader
< PIPE_SHADER_TYPES
);
469 assert(index
< ARRAY_SIZE(ctx
->constants
[shader
]));
471 /* note: reference counting */
472 util_copy_constant_buffer(&ctx
->constants
[shader
][index
], cb
);
474 if (shader
== PIPE_SHADER_VERTEX
) {
475 ctx
->dirty
|= SWR_NEW_VSCONSTANTS
;
476 } else if (shader
== PIPE_SHADER_FRAGMENT
) {
477 ctx
->dirty
|= SWR_NEW_FSCONSTANTS
;
478 } else if (shader
== PIPE_SHADER_GEOMETRY
) {
479 ctx
->dirty
|= SWR_NEW_GSCONSTANTS
;
482 if (cb
&& cb
->user_buffer
) {
483 pipe_resource_reference(&constants
, NULL
);
489 swr_create_vertex_elements_state(struct pipe_context
*pipe
,
490 unsigned num_elements
,
491 const struct pipe_vertex_element
*attribs
)
493 struct swr_vertex_element_state
*velems
;
494 assert(num_elements
<= PIPE_MAX_ATTRIBS
);
495 velems
= new swr_vertex_element_state
;
497 velems
->fsState
.bVertexIDOffsetEnable
= true;
498 velems
->fsState
.numAttribs
= num_elements
;
499 for (unsigned i
= 0; i
< num_elements
; i
++) {
500 // XXX: we should do this keyed on the VS usage info
502 const struct util_format_description
*desc
=
503 util_format_description(attribs
[i
].src_format
);
505 velems
->fsState
.layout
[i
].AlignedByteOffset
= attribs
[i
].src_offset
;
506 velems
->fsState
.layout
[i
].Format
=
507 mesa_to_swr_format(attribs
[i
].src_format
);
508 velems
->fsState
.layout
[i
].StreamIndex
=
509 attribs
[i
].vertex_buffer_index
;
510 velems
->fsState
.layout
[i
].InstanceEnable
=
511 attribs
[i
].instance_divisor
!= 0;
512 velems
->fsState
.layout
[i
].ComponentControl0
=
513 desc
->channel
[0].type
!= UTIL_FORMAT_TYPE_VOID
514 ? ComponentControl::StoreSrc
515 : ComponentControl::Store0
;
516 velems
->fsState
.layout
[i
].ComponentControl1
=
517 desc
->channel
[1].type
!= UTIL_FORMAT_TYPE_VOID
518 ? ComponentControl::StoreSrc
519 : ComponentControl::Store0
;
520 velems
->fsState
.layout
[i
].ComponentControl2
=
521 desc
->channel
[2].type
!= UTIL_FORMAT_TYPE_VOID
522 ? ComponentControl::StoreSrc
523 : ComponentControl::Store0
;
524 velems
->fsState
.layout
[i
].ComponentControl3
=
525 desc
->channel
[3].type
!= UTIL_FORMAT_TYPE_VOID
526 ? ComponentControl::StoreSrc
527 : ComponentControl::Store1Fp
;
528 velems
->fsState
.layout
[i
].ComponentPacking
= ComponentEnable::XYZW
;
529 velems
->fsState
.layout
[i
].InstanceDataStepRate
=
530 attribs
[i
].instance_divisor
;
532 /* Calculate the pitch of each stream */
533 const SWR_FORMAT_INFO
&swr_desc
= GetFormatInfo(
534 mesa_to_swr_format(attribs
[i
].src_format
));
535 velems
->stream_pitch
[attribs
[i
].vertex_buffer_index
] += swr_desc
.Bpp
;
537 if (attribs
[i
].instance_divisor
!= 0) {
538 velems
->instanced_bufs
|= 1U << attribs
[i
].vertex_buffer_index
;
539 uint32_t *min_instance_div
=
540 &velems
->min_instance_div
[attribs
[i
].vertex_buffer_index
];
541 if (!*min_instance_div
||
542 attribs
[i
].instance_divisor
< *min_instance_div
)
543 *min_instance_div
= attribs
[i
].instance_divisor
;
552 swr_bind_vertex_elements_state(struct pipe_context
*pipe
, void *velems
)
554 struct swr_context
*ctx
= swr_context(pipe
);
555 struct swr_vertex_element_state
*swr_velems
=
556 (struct swr_vertex_element_state
*)velems
;
558 ctx
->velems
= swr_velems
;
559 ctx
->dirty
|= SWR_NEW_VERTEX
;
563 swr_delete_vertex_elements_state(struct pipe_context
*pipe
, void *velems
)
565 struct swr_vertex_element_state
*swr_velems
=
566 (struct swr_vertex_element_state
*) velems
;
567 /* XXX Need to destroy fetch shader? */
573 swr_set_vertex_buffers(struct pipe_context
*pipe
,
575 unsigned num_elements
,
576 const struct pipe_vertex_buffer
*buffers
)
578 struct swr_context
*ctx
= swr_context(pipe
);
580 assert(num_elements
<= PIPE_MAX_ATTRIBS
);
582 util_set_vertex_buffers_count(ctx
->vertex_buffer
,
583 &ctx
->num_vertex_buffers
,
588 ctx
->dirty
|= SWR_NEW_VERTEX
;
593 swr_set_index_buffer(struct pipe_context
*pipe
,
594 const struct pipe_index_buffer
*ib
)
596 struct swr_context
*ctx
= swr_context(pipe
);
599 memcpy(&ctx
->index_buffer
, ib
, sizeof(ctx
->index_buffer
));
601 memset(&ctx
->index_buffer
, 0, sizeof(ctx
->index_buffer
));
603 ctx
->dirty
|= SWR_NEW_VERTEX
;
607 swr_set_polygon_stipple(struct pipe_context
*pipe
,
608 const struct pipe_poly_stipple
*stipple
)
610 struct swr_context
*ctx
= swr_context(pipe
);
612 ctx
->poly_stipple
= *stipple
; /* struct copy */
613 ctx
->dirty
|= SWR_NEW_STIPPLE
;
617 swr_set_clip_state(struct pipe_context
*pipe
,
618 const struct pipe_clip_state
*clip
)
620 struct swr_context
*ctx
= swr_context(pipe
);
623 /* XXX Unimplemented, but prevents crash */
625 ctx
->dirty
|= SWR_NEW_CLIP
;
630 swr_set_scissor_states(struct pipe_context
*pipe
,
632 unsigned num_viewports
,
633 const struct pipe_scissor_state
*scissor
)
635 struct swr_context
*ctx
= swr_context(pipe
);
637 ctx
->scissor
= *scissor
;
638 ctx
->swr_scissor
.xmin
= scissor
->minx
;
639 ctx
->swr_scissor
.xmax
= scissor
->maxx
;
640 ctx
->swr_scissor
.ymin
= scissor
->miny
;
641 ctx
->swr_scissor
.ymax
= scissor
->maxy
;
642 ctx
->dirty
|= SWR_NEW_SCISSOR
;
646 swr_set_viewport_states(struct pipe_context
*pipe
,
648 unsigned num_viewports
,
649 const struct pipe_viewport_state
*vpt
)
651 struct swr_context
*ctx
= swr_context(pipe
);
653 ctx
->viewport
= *vpt
;
654 ctx
->dirty
|= SWR_NEW_VIEWPORT
;
659 swr_set_framebuffer_state(struct pipe_context
*pipe
,
660 const struct pipe_framebuffer_state
*fb
)
662 struct swr_context
*ctx
= swr_context(pipe
);
664 boolean changed
= !util_framebuffer_state_equal(&ctx
->framebuffer
, fb
);
666 assert(fb
->width
<= KNOB_GUARDBAND_WIDTH
);
667 assert(fb
->height
<= KNOB_GUARDBAND_HEIGHT
);
670 util_copy_framebuffer_state(&ctx
->framebuffer
, fb
);
672 /* 0 and 1 both indicate no msaa. Core doesn't understand 0 samples */
673 ctx
->framebuffer
.samples
= std::max((ubyte
)1, ctx
->framebuffer
.samples
);
675 ctx
->dirty
|= SWR_NEW_FRAMEBUFFER
;
681 swr_set_sample_mask(struct pipe_context
*pipe
, unsigned sample_mask
)
683 struct swr_context
*ctx
= swr_context(pipe
);
685 if (sample_mask
!= ctx
->sample_mask
) {
686 ctx
->sample_mask
= sample_mask
;
687 ctx
->dirty
|= SWR_NEW_RASTERIZER
;
692 * MSAA fixed sample position table
693 * used by update_derived and get_sample_position
694 * (integer locations on a 16x16 grid)
696 static const uint8_t swr_sample_positions
[][2] =
698 /* 2x*/ {12,12},{ 4, 4},
699 /* 4x*/ { 6, 2},{14, 6},{ 2,10},{10,14},
700 /* 8x*/ { 9, 5},{ 7,11},{13, 9},{ 5, 3},
701 { 3,13},{ 1, 7},{11,15},{15, 1},
702 /*16x*/ { 9, 9},{ 7, 5},{ 5,10},{12, 7},
703 { 3, 6},{10,13},{13,11},{11, 3},
704 { 6,14},{ 8, 1},{ 4, 2},{ 2,12},
705 { 0, 8},{15, 4},{14,15},{ 1, 0} };
708 swr_get_sample_position(struct pipe_context
*pipe
,
709 unsigned sample_count
, unsigned sample_index
,
712 /* validate sample_count */
713 sample_count
= GetNumSamples(GetSampleCount(sample_count
));
715 const uint8_t *sample
= swr_sample_positions
[sample_count
-1 + sample_index
];
716 out_value
[0] = sample
[0] / 16.0f
;
717 out_value
[1] = sample
[1] / 16.0f
;
722 * Update resource in-use status
723 * All resources bound to color or depth targets marked as WRITE resources.
724 * VBO Vertex/index buffers and texture views marked as READ resources.
727 swr_update_resource_status(struct pipe_context
*pipe
,
728 const struct pipe_draw_info
*p_draw_info
)
730 struct swr_context
*ctx
= swr_context(pipe
);
731 struct pipe_framebuffer_state
*fb
= &ctx
->framebuffer
;
733 /* colorbuffer targets */
735 for (uint32_t i
= 0; i
< fb
->nr_cbufs
; ++i
)
737 swr_resource_write(fb
->cbufs
[i
]->texture
);
739 /* depth/stencil target */
741 swr_resource_write(fb
->zsbuf
->texture
);
743 /* VBO vertex buffers */
744 for (uint32_t i
= 0; i
< ctx
->num_vertex_buffers
; i
++) {
745 struct pipe_vertex_buffer
*vb
= &ctx
->vertex_buffer
[i
];
746 if (!vb
->user_buffer
)
747 swr_resource_read(vb
->buffer
);
750 /* VBO index buffer */
751 if (p_draw_info
&& p_draw_info
->indexed
) {
752 struct pipe_index_buffer
*ib
= &ctx
->index_buffer
;
753 if (!ib
->user_buffer
)
754 swr_resource_read(ib
->buffer
);
757 /* transform feedback buffers */
758 for (uint32_t i
= 0; i
< ctx
->num_so_targets
; i
++) {
759 struct pipe_stream_output_target
*target
= ctx
->so_targets
[i
];
760 if (target
&& target
->buffer
)
761 swr_resource_write(target
->buffer
);
764 /* texture sampler views */
765 for (uint32_t j
: {PIPE_SHADER_VERTEX
, PIPE_SHADER_FRAGMENT
}) {
766 for (uint32_t i
= 0; i
< ctx
->num_sampler_views
[j
]; i
++) {
767 struct pipe_sampler_view
*view
= ctx
->sampler_views
[j
][i
];
769 swr_resource_read(view
->texture
);
773 /* constant buffers */
774 for (uint32_t j
: {PIPE_SHADER_VERTEX
, PIPE_SHADER_FRAGMENT
}) {
775 for (uint32_t i
= 0; i
< PIPE_MAX_CONSTANT_BUFFERS
; i
++) {
776 struct pipe_constant_buffer
*cb
= &ctx
->constants
[j
][i
];
778 swr_resource_read(cb
->buffer
);
784 swr_update_texture_state(struct swr_context
*ctx
,
785 enum pipe_shader_type shader_type
,
786 unsigned num_sampler_views
,
787 swr_jit_texture
*textures
)
789 for (unsigned i
= 0; i
< num_sampler_views
; i
++) {
790 struct pipe_sampler_view
*view
=
791 ctx
->sampler_views
[shader_type
][i
];
792 struct swr_jit_texture
*jit_tex
= &textures
[i
];
794 memset(jit_tex
, 0, sizeof(*jit_tex
));
796 struct pipe_resource
*res
= view
->texture
;
797 struct swr_resource
*swr_res
= swr_resource(res
);
798 SWR_SURFACE_STATE
*swr
= &swr_res
->swr
;
799 size_t *mip_offsets
= swr_res
->mip_offsets
;
800 if (swr_res
->has_depth
&& swr_res
->has_stencil
&&
801 !util_format_has_depth(util_format_description(view
->format
))) {
802 swr
= &swr_res
->secondary
;
803 mip_offsets
= swr_res
->secondary_mip_offsets
;
806 jit_tex
->width
= res
->width0
;
807 jit_tex
->height
= res
->height0
;
808 jit_tex
->base_ptr
= swr
->pBaseAddress
;
809 if (view
->target
!= PIPE_BUFFER
) {
810 jit_tex
->first_level
= view
->u
.tex
.first_level
;
811 jit_tex
->last_level
= view
->u
.tex
.last_level
;
812 if (view
->target
== PIPE_TEXTURE_3D
)
813 jit_tex
->depth
= res
->depth0
;
816 view
->u
.tex
.last_layer
- view
->u
.tex
.first_layer
+ 1;
817 jit_tex
->base_ptr
+= view
->u
.tex
.first_layer
*
818 swr
->qpitch
* swr
->pitch
;
820 unsigned view_blocksize
= util_format_get_blocksize(view
->format
);
821 jit_tex
->base_ptr
+= view
->u
.buf
.offset
;
822 jit_tex
->width
= view
->u
.buf
.size
/ view_blocksize
;
826 for (unsigned level
= jit_tex
->first_level
;
827 level
<= jit_tex
->last_level
;
829 jit_tex
->row_stride
[level
] = swr
->pitch
;
830 jit_tex
->img_stride
[level
] = swr
->qpitch
* swr
->pitch
;
831 jit_tex
->mip_offsets
[level
] = mip_offsets
[level
];
838 swr_update_sampler_state(struct swr_context
*ctx
,
839 enum pipe_shader_type shader_type
,
840 unsigned num_samplers
,
841 swr_jit_sampler
*samplers
)
843 for (unsigned i
= 0; i
< num_samplers
; i
++) {
844 const struct pipe_sampler_state
*sampler
=
845 ctx
->samplers
[shader_type
][i
];
848 samplers
[i
].min_lod
= sampler
->min_lod
;
849 samplers
[i
].max_lod
= sampler
->max_lod
;
850 samplers
[i
].lod_bias
= sampler
->lod_bias
;
851 COPY_4V(samplers
[i
].border_color
, sampler
->border_color
.f
);
857 swr_update_constants(struct swr_context
*ctx
, enum pipe_shader_type shaderType
)
859 swr_draw_context
*pDC
= &ctx
->swrDC
;
861 const float **constant
;
862 uint32_t *num_constants
;
863 struct swr_scratch_space
*scratch
;
865 switch (shaderType
) {
866 case PIPE_SHADER_VERTEX
:
867 constant
= pDC
->constantVS
;
868 num_constants
= pDC
->num_constantsVS
;
869 scratch
= &ctx
->scratch
->vs_constants
;
871 case PIPE_SHADER_FRAGMENT
:
872 constant
= pDC
->constantFS
;
873 num_constants
= pDC
->num_constantsFS
;
874 scratch
= &ctx
->scratch
->fs_constants
;
876 case PIPE_SHADER_GEOMETRY
:
877 constant
= pDC
->constantGS
;
878 num_constants
= pDC
->num_constantsGS
;
879 scratch
= &ctx
->scratch
->gs_constants
;
882 debug_printf("Unsupported shader type constants\n");
886 for (UINT i
= 0; i
< PIPE_MAX_CONSTANT_BUFFERS
; i
++) {
887 const pipe_constant_buffer
*cb
= &ctx
->constants
[shaderType
][i
];
888 num_constants
[i
] = cb
->buffer_size
;
891 (const float *)(swr_resource_data(cb
->buffer
) +
894 /* Need to copy these constants to scratch space */
895 if (cb
->user_buffer
&& cb
->buffer_size
) {
897 ((const uint8_t *)cb
->user_buffer
+ cb
->buffer_offset
);
898 uint32_t size
= AlignUp(cb
->buffer_size
, 4);
899 ptr
= swr_copy_to_scratch_space(ctx
, scratch
, ptr
, size
);
900 constant
[i
] = (const float *)ptr
;
907 swr_change_rt(struct swr_context
*ctx
,
909 const struct pipe_surface
*sf
)
911 swr_draw_context
*pDC
= &ctx
->swrDC
;
912 struct SWR_SURFACE_STATE
*rt
= &pDC
->renderTargets
[attachment
];
914 /* Do nothing if the render target hasn't changed */
915 if ((!sf
|| !sf
->texture
) && rt
->pBaseAddress
== nullptr)
918 /* Deal with disabling RT up front */
919 if (!sf
|| !sf
->texture
) {
920 /* If detaching attachment, mark tiles as RESOLVED so core
921 * won't try to load from non-existent target. */
922 swr_store_render_target(&ctx
->pipe
, attachment
, SWR_TILE_RESOLVED
);
927 const struct swr_resource
*swr
= swr_resource(sf
->texture
);
928 const SWR_SURFACE_STATE
*swr_surface
= &swr
->swr
;
929 SWR_FORMAT fmt
= mesa_to_swr_format(sf
->format
);
931 if (attachment
== SWR_ATTACHMENT_STENCIL
&& swr
->secondary
.pBaseAddress
) {
932 swr_surface
= &swr
->secondary
;
933 fmt
= swr_surface
->format
;
936 if (rt
->pBaseAddress
== swr_surface
->pBaseAddress
&&
938 rt
->lod
== sf
->u
.tex
.level
&&
939 rt
->arrayIndex
== sf
->u
.tex
.first_layer
)
942 bool need_fence
= false;
944 /* StoreTile for changed target */
945 if (rt
->pBaseAddress
) {
946 /* If changing attachment to a new target, mark tiles as
947 * INVALID so they are reloaded from surface. */
948 swr_store_render_target(&ctx
->pipe
, attachment
, SWR_TILE_INVALID
);
952 /* Make new attachment */
955 rt
->lod
= sf
->u
.tex
.level
;
956 rt
->arrayIndex
= sf
->u
.tex
.first_layer
;
962 swr_user_vbuf_range(const struct pipe_draw_info
*info
,
963 const struct swr_vertex_element_state
*velems
,
964 const struct pipe_vertex_buffer
*vb
,
970 /* FIXME: The size is too large - we don't access the full extra stride. */
972 if (velems
->instanced_bufs
& (1U << i
)) {
973 elems
= info
->instance_count
/ velems
->min_instance_div
[i
] + 1;
974 *totelems
= info
->start_instance
+ elems
;
975 *base
= info
->start_instance
* vb
->stride
;
976 *size
= elems
* vb
->stride
;
977 } else if (vb
->stride
) {
978 elems
= info
->max_index
- info
->min_index
+ 1;
979 *totelems
= info
->max_index
+ 1;
980 *base
= info
->min_index
* vb
->stride
;
981 *size
= elems
* vb
->stride
;
985 *size
= velems
->stream_pitch
[i
];
990 swr_update_derived(struct pipe_context
*pipe
,
991 const struct pipe_draw_info
*p_draw_info
)
993 struct swr_context
*ctx
= swr_context(pipe
);
994 struct swr_screen
*screen
= swr_screen(pipe
->screen
);
996 /* When called from swr_clear (p_draw_info = null), set any null
997 * state-objects to the dummy state objects to prevent nullptr dereference
998 * in validation below.
1000 * Important that this remains static for zero initialization. These
1001 * aren't meant to be proper state objects, just empty structs. They will
1002 * not be written to.
1004 * Shaders can't be part of the union since they contain std::unordered_map
1008 struct pipe_rasterizer_state rasterizer
;
1009 struct pipe_depth_stencil_alpha_state depth_stencil
;
1010 struct swr_blend_state blend
;
1012 struct swr_vertex_shader vs
;
1013 struct swr_fragment_shader fs
;
1017 if (!ctx
->rasterizer
)
1018 ctx
->rasterizer
= &swr_dummy
.state
.rasterizer
;
1019 if (!ctx
->depth_stencil
)
1020 ctx
->depth_stencil
= &swr_dummy
.state
.depth_stencil
;
1022 ctx
->blend
= &swr_dummy
.state
.blend
;
1024 ctx
->vs
= &swr_dummy
.vs
;
1026 ctx
->fs
= &swr_dummy
.fs
;
1029 /* Update screen->pipe to current pipe context. */
1030 if (screen
->pipe
!= pipe
)
1031 screen
->pipe
= pipe
;
1033 /* Any state that requires dirty flags to be re-triggered sets this mask */
1034 /* For example, user_buffer vertex and index buffers. */
1035 unsigned post_update_dirty_flags
= 0;
1037 /* Render Targets */
1038 if (ctx
->dirty
& SWR_NEW_FRAMEBUFFER
) {
1039 struct pipe_framebuffer_state
*fb
= &ctx
->framebuffer
;
1040 const struct util_format_description
*desc
= NULL
;
1041 bool need_fence
= false;
1043 /* colorbuffer targets */
1045 for (unsigned i
= 0; i
< fb
->nr_cbufs
; ++i
)
1046 need_fence
|= swr_change_rt(
1047 ctx
, SWR_ATTACHMENT_COLOR0
+ i
, fb
->cbufs
[i
]);
1049 for (unsigned i
= fb
->nr_cbufs
; i
< SWR_NUM_RENDERTARGETS
; ++i
)
1050 need_fence
|= swr_change_rt(ctx
, SWR_ATTACHMENT_COLOR0
+ i
, NULL
);
1052 /* depth/stencil target */
1054 desc
= util_format_description(fb
->zsbuf
->format
);
1055 if (fb
->zsbuf
&& util_format_has_depth(desc
))
1056 need_fence
|= swr_change_rt(ctx
, SWR_ATTACHMENT_DEPTH
, fb
->zsbuf
);
1058 need_fence
|= swr_change_rt(ctx
, SWR_ATTACHMENT_DEPTH
, NULL
);
1060 if (fb
->zsbuf
&& util_format_has_stencil(desc
))
1061 need_fence
|= swr_change_rt(ctx
, SWR_ATTACHMENT_STENCIL
, fb
->zsbuf
);
1063 need_fence
|= swr_change_rt(ctx
, SWR_ATTACHMENT_STENCIL
, NULL
);
1065 /* This fence ensures any attachment changes are resolved before the
1068 swr_fence_submit(ctx
, screen
->flush_fence
);
1072 if (ctx
->dirty
& (SWR_NEW_RASTERIZER
|
1073 SWR_NEW_VS
| // clipping
1074 SWR_NEW_FRAMEBUFFER
)) {
1075 pipe_rasterizer_state
*rasterizer
= ctx
->rasterizer
;
1076 pipe_framebuffer_state
*fb
= &ctx
->framebuffer
;
1078 SWR_RASTSTATE
*rastState
= &ctx
->derived
.rastState
;
1079 rastState
->cullMode
= swr_convert_cull_mode(rasterizer
->cull_face
);
1080 rastState
->frontWinding
= rasterizer
->front_ccw
1081 ? SWR_FRONTWINDING_CCW
1082 : SWR_FRONTWINDING_CW
;
1083 rastState
->scissorEnable
= rasterizer
->scissor
;
1084 rastState
->pointSize
= rasterizer
->point_size
> 0.0f
1085 ? rasterizer
->point_size
1087 rastState
->lineWidth
= rasterizer
->line_width
> 0.0f
1088 ? rasterizer
->line_width
1091 rastState
->pointParam
= rasterizer
->point_size_per_vertex
;
1093 rastState
->pointSpriteEnable
= rasterizer
->sprite_coord_enable
;
1094 rastState
->pointSpriteTopOrigin
=
1095 rasterizer
->sprite_coord_mode
== PIPE_SPRITE_COORD_UPPER_LEFT
;
1097 /* If SWR_MSAA_FORCE_ENABLE is set, turn msaa on */
1098 if (screen
->msaa_force_enable
&& !rasterizer
->multisample
) {
1099 /* Force enable and use the value the surface was created with */
1100 rasterizer
->multisample
= true;
1101 fb
->samples
= swr_resource(fb
->cbufs
[0]->texture
)->swr
.numSamples
;
1102 fprintf(stderr
,"msaa force enable: %d samples\n", fb
->samples
);
1105 rastState
->sampleCount
= GetSampleCount(fb
->samples
);
1106 rastState
->forcedSampleCount
= false;
1107 rastState
->bIsCenterPattern
= !rasterizer
->multisample
;
1108 rastState
->pixelLocation
= SWR_PIXEL_LOCATION_CENTER
;
1110 /* Only initialize sample positions if msaa is enabled */
1111 if (rasterizer
->multisample
) {
1112 for (uint32_t i
= 0; i
< fb
->samples
; i
++) {
1113 const uint8_t *sample
= swr_sample_positions
[fb
->samples
-1 + i
];
1114 rastState
->samplePositions
.SetXi(i
, sample
[0] << 4);
1115 rastState
->samplePositions
.SetYi(i
, sample
[1] << 4);
1116 rastState
->samplePositions
.SetX (i
, sample
[0] / 16.0f
);
1117 rastState
->samplePositions
.SetY (i
, sample
[1] / 16.0f
);
1119 rastState
->samplePositions
.PrecalcSampleData(fb
->samples
);
1122 bool do_offset
= false;
1123 switch (rasterizer
->fill_front
) {
1124 case PIPE_POLYGON_MODE_FILL
:
1125 do_offset
= rasterizer
->offset_tri
;
1127 case PIPE_POLYGON_MODE_LINE
:
1128 do_offset
= rasterizer
->offset_line
;
1130 case PIPE_POLYGON_MODE_POINT
:
1131 do_offset
= rasterizer
->offset_point
;
1136 rastState
->depthBias
= rasterizer
->offset_units
;
1137 rastState
->slopeScaledDepthBias
= rasterizer
->offset_scale
;
1138 rastState
->depthBiasClamp
= rasterizer
->offset_clamp
;
1140 rastState
->depthBias
= 0;
1141 rastState
->slopeScaledDepthBias
= 0;
1142 rastState
->depthBiasClamp
= 0;
1144 struct pipe_surface
*zb
= fb
->zsbuf
;
1145 if (zb
&& swr_resource(zb
->texture
)->has_depth
)
1146 rastState
->depthFormat
= swr_resource(zb
->texture
)->swr
.format
;
1148 rastState
->depthClipEnable
= rasterizer
->depth_clip
;
1149 rastState
->clipHalfZ
= rasterizer
->clip_halfz
;
1151 rastState
->clipDistanceMask
=
1152 ctx
->vs
->info
.base
.num_written_clipdistance
?
1153 ctx
->vs
->info
.base
.clipdist_writemask
& rasterizer
->clip_plane_enable
:
1154 rasterizer
->clip_plane_enable
;
1156 rastState
->cullDistanceMask
=
1157 ctx
->vs
->info
.base
.culldist_writemask
<< ctx
->vs
->info
.base
.num_written_clipdistance
;
1159 SwrSetRastState(ctx
->swrContext
, rastState
);
1163 if (ctx
->dirty
& SWR_NEW_SCISSOR
) {
1164 SwrSetScissorRects(ctx
->swrContext
, 1, &ctx
->swr_scissor
);
1168 if (ctx
->dirty
& (SWR_NEW_VIEWPORT
| SWR_NEW_FRAMEBUFFER
1169 | SWR_NEW_RASTERIZER
)) {
1170 pipe_viewport_state
*state
= &ctx
->viewport
;
1171 pipe_framebuffer_state
*fb
= &ctx
->framebuffer
;
1172 pipe_rasterizer_state
*rasterizer
= ctx
->rasterizer
;
1174 SWR_VIEWPORT
*vp
= &ctx
->derived
.vp
;
1175 SWR_VIEWPORT_MATRICES
*vpm
= &ctx
->derived
.vpm
;
1177 vp
->x
= state
->translate
[0] - state
->scale
[0];
1178 vp
->width
= 2 * state
->scale
[0];
1179 vp
->y
= state
->translate
[1] - fabs(state
->scale
[1]);
1180 vp
->height
= 2 * fabs(state
->scale
[1]);
1181 util_viewport_zmin_zmax(state
, rasterizer
->clip_halfz
,
1182 &vp
->minZ
, &vp
->maxZ
);
1184 vpm
->m00
[0] = state
->scale
[0];
1185 vpm
->m11
[0] = state
->scale
[1];
1186 vpm
->m22
[0] = state
->scale
[2];
1187 vpm
->m30
[0] = state
->translate
[0];
1188 vpm
->m31
[0] = state
->translate
[1];
1189 vpm
->m32
[0] = state
->translate
[2];
1191 /* Now that the matrix is calculated, clip the view coords to screen
1192 * size. OpenGL allows for -ve x,y in the viewport. */
1198 vp
->height
+= vp
->y
;
1201 vp
->width
= std::min(vp
->width
, (float)fb
->width
- vp
->x
);
1202 vp
->height
= std::min(vp
->height
, (float)fb
->height
- vp
->y
);
1204 SwrSetViewports(ctx
->swrContext
, 1, vp
, vpm
);
1207 /* Set vertex & index buffers */
1208 /* (using draw info if called by swr_draw_vbo) */
1209 if (ctx
->dirty
& SWR_NEW_VERTEX
) {
1210 uint32_t scratch_total
;
1211 uint8_t *scratch
= NULL
;
1213 /* If being called by swr_draw_vbo, copy draw details */
1214 struct pipe_draw_info info
= {0};
1216 info
= *p_draw_info
;
1218 /* We must get all the scratch space in one go */
1220 for (UINT i
= 0; i
< ctx
->num_vertex_buffers
; i
++) {
1221 struct pipe_vertex_buffer
*vb
= &ctx
->vertex_buffer
[i
];
1223 if (!vb
->user_buffer
)
1226 uint32_t elems
, base
, size
;
1227 swr_user_vbuf_range(&info
, ctx
->velems
, vb
, i
, &elems
, &base
, &size
);
1228 scratch_total
+= AlignUp(size
, 4);
1231 if (scratch_total
) {
1232 scratch
= (uint8_t *)swr_copy_to_scratch_space(
1233 ctx
, &ctx
->scratch
->vertex_buffer
, NULL
, scratch_total
);
1236 /* vertex buffers */
1237 SWR_VERTEX_BUFFER_STATE swrVertexBuffers
[PIPE_MAX_ATTRIBS
];
1238 for (UINT i
= 0; i
< ctx
->num_vertex_buffers
; i
++) {
1239 uint32_t size
, pitch
, elems
, partial_inbounds
;
1240 uint32_t min_vertex_index
;
1241 const uint8_t *p_data
;
1242 struct pipe_vertex_buffer
*vb
= &ctx
->vertex_buffer
[i
];
1245 if (!vb
->user_buffer
) {
1247 * size is based on buffer->width0 rather than info.max_index
1248 * to prevent having to validate VBO on each draw */
1249 size
= vb
->buffer
->width0
;
1250 elems
= size
/ pitch
;
1251 partial_inbounds
= size
% pitch
;
1252 min_vertex_index
= 0;
1254 p_data
= swr_resource_data(vb
->buffer
) + vb
->buffer_offset
;
1257 * client memory is one-time use, re-trigger SWR_NEW_VERTEX to
1258 * revalidate on each draw */
1259 post_update_dirty_flags
|= SWR_NEW_VERTEX
;
1262 swr_user_vbuf_range(&info
, ctx
->velems
, vb
, i
, &elems
, &base
, &size
);
1263 partial_inbounds
= 0;
1264 min_vertex_index
= info
.min_index
;
1266 /* Copy only needed vertices to scratch space */
1267 size
= AlignUp(size
, 4);
1268 const void *ptr
= (const uint8_t *) vb
->user_buffer
+ base
;
1269 memcpy(scratch
, ptr
, size
);
1272 p_data
= (const uint8_t *)ptr
- base
;
1275 swrVertexBuffers
[i
] = {0};
1276 swrVertexBuffers
[i
].index
= i
;
1277 swrVertexBuffers
[i
].pitch
= pitch
;
1278 swrVertexBuffers
[i
].pData
= p_data
;
1279 swrVertexBuffers
[i
].size
= size
;
1280 swrVertexBuffers
[i
].minVertex
= min_vertex_index
;
1281 swrVertexBuffers
[i
].maxVertex
= elems
;
1282 swrVertexBuffers
[i
].partialInboundsSize
= partial_inbounds
;
1285 SwrSetVertexBuffers(
1286 ctx
->swrContext
, ctx
->num_vertex_buffers
, swrVertexBuffers
);
1288 /* index buffer, if required (info passed in by swr_draw_vbo) */
1289 SWR_FORMAT index_type
= R32_UINT
; /* Default for non-indexed draws */
1291 const uint8_t *p_data
;
1292 uint32_t size
, pitch
;
1293 struct pipe_index_buffer
*ib
= &ctx
->index_buffer
;
1295 pitch
= ib
->index_size
? ib
->index_size
: sizeof(uint32_t);
1296 index_type
= swr_convert_index_type(pitch
);
1298 if (!ib
->user_buffer
) {
1300 * size is based on buffer->width0 rather than info.count
1301 * to prevent having to validate VBO on each draw */
1302 size
= ib
->buffer
->width0
;
1303 p_data
= swr_resource_data(ib
->buffer
) + ib
->offset
;
1306 * client memory is one-time use, re-trigger SWR_NEW_VERTEX to
1307 * revalidate on each draw */
1308 post_update_dirty_flags
|= SWR_NEW_VERTEX
;
1310 size
= info
.count
* pitch
;
1311 size
= AlignUp(size
, 4);
1313 /* Copy indices to scratch space */
1314 const void *ptr
= ib
->user_buffer
;
1315 ptr
= swr_copy_to_scratch_space(
1316 ctx
, &ctx
->scratch
->index_buffer
, ptr
, size
);
1317 p_data
= (const uint8_t *)ptr
;
1320 SWR_INDEX_BUFFER_STATE swrIndexBuffer
;
1321 swrIndexBuffer
.format
= swr_convert_index_type(ib
->index_size
);
1322 swrIndexBuffer
.pIndices
= p_data
;
1323 swrIndexBuffer
.size
= size
;
1325 SwrSetIndexBuffer(ctx
->swrContext
, &swrIndexBuffer
);
1328 struct swr_vertex_element_state
*velems
= ctx
->velems
;
1329 if (velems
&& velems
->fsState
.indexType
!= index_type
) {
1330 velems
->fsFunc
= NULL
;
1331 velems
->fsState
.indexType
= index_type
;
1335 /* GeometryShader */
1336 if (ctx
->dirty
& (SWR_NEW_GS
|
1339 SWR_NEW_SAMPLER_VIEW
)) {
1342 swr_generate_gs_key(key
, ctx
, ctx
->gs
);
1343 auto search
= ctx
->gs
->map
.find(key
);
1345 if (search
!= ctx
->gs
->map
.end()) {
1346 func
= search
->second
->shader
;
1348 func
= swr_compile_gs(ctx
, key
);
1350 SwrSetGsFunc(ctx
->swrContext
, func
);
1352 /* JIT sampler state */
1353 if (ctx
->dirty
& SWR_NEW_SAMPLER
) {
1354 swr_update_sampler_state(ctx
,
1355 PIPE_SHADER_GEOMETRY
,
1357 ctx
->swrDC
.samplersGS
);
1360 /* JIT sampler view state */
1361 if (ctx
->dirty
& (SWR_NEW_SAMPLER_VIEW
| SWR_NEW_FRAMEBUFFER
)) {
1362 swr_update_texture_state(ctx
,
1363 PIPE_SHADER_GEOMETRY
,
1364 key
.nr_sampler_views
,
1365 ctx
->swrDC
.texturesGS
);
1368 SwrSetGsState(ctx
->swrContext
, &ctx
->gs
->gsState
);
1370 SWR_GS_STATE state
= { 0 };
1371 SwrSetGsState(ctx
->swrContext
, &state
);
1372 SwrSetGsFunc(ctx
->swrContext
, NULL
);
1377 if (ctx
->dirty
& (SWR_NEW_VS
|
1378 SWR_NEW_RASTERIZER
| // for clip planes
1380 SWR_NEW_SAMPLER_VIEW
|
1381 SWR_NEW_FRAMEBUFFER
)) {
1383 swr_generate_vs_key(key
, ctx
, ctx
->vs
);
1384 auto search
= ctx
->vs
->map
.find(key
);
1385 PFN_VERTEX_FUNC func
;
1386 if (search
!= ctx
->vs
->map
.end()) {
1387 func
= search
->second
->shader
;
1389 func
= swr_compile_vs(ctx
, key
);
1391 SwrSetVertexFunc(ctx
->swrContext
, func
);
1393 /* JIT sampler state */
1394 if (ctx
->dirty
& SWR_NEW_SAMPLER
) {
1395 swr_update_sampler_state(ctx
,
1398 ctx
->swrDC
.samplersVS
);
1401 /* JIT sampler view state */
1402 if (ctx
->dirty
& (SWR_NEW_SAMPLER_VIEW
| SWR_NEW_FRAMEBUFFER
)) {
1403 swr_update_texture_state(ctx
,
1405 key
.nr_sampler_views
,
1406 ctx
->swrDC
.texturesVS
);
1410 /* FragmentShader */
1411 if (ctx
->dirty
& (SWR_NEW_FS
|
1414 SWR_NEW_RASTERIZER
|
1416 SWR_NEW_SAMPLER_VIEW
|
1417 SWR_NEW_FRAMEBUFFER
)) {
1419 swr_generate_fs_key(key
, ctx
, ctx
->fs
);
1420 auto search
= ctx
->fs
->map
.find(key
);
1421 PFN_PIXEL_KERNEL func
;
1422 if (search
!= ctx
->fs
->map
.end()) {
1423 func
= search
->second
->shader
;
1425 func
= swr_compile_fs(ctx
, key
);
1427 SWR_PS_STATE psState
= {0};
1428 psState
.pfnPixelShader
= func
;
1429 psState
.killsPixel
= ctx
->fs
->info
.base
.uses_kill
;
1430 psState
.inputCoverage
= SWR_INPUT_COVERAGE_NORMAL
;
1431 psState
.writesODepth
= ctx
->fs
->info
.base
.writes_z
;
1432 psState
.usesSourceDepth
= ctx
->fs
->info
.base
.reads_z
;
1433 psState
.shadingRate
= SWR_SHADING_RATE_PIXEL
;
1434 psState
.numRenderTargets
= ctx
->framebuffer
.nr_cbufs
;
1435 psState
.posOffset
= SWR_PS_POSITION_SAMPLE_NONE
;
1436 uint32_t barycentricsMask
= 0;
1438 // when we switch to mesa-master
1439 if (ctx
->fs
->info
.base
.uses_persp_center
||
1440 ctx
->fs
->info
.base
.uses_linear_center
)
1441 barycentricsMask
|= SWR_BARYCENTRIC_PER_PIXEL_MASK
;
1442 if (ctx
->fs
->info
.base
.uses_persp_centroid
||
1443 ctx
->fs
->info
.base
.uses_linear_centroid
)
1444 barycentricsMask
|= SWR_BARYCENTRIC_CENTROID_MASK
;
1445 if (ctx
->fs
->info
.base
.uses_persp_sample
||
1446 ctx
->fs
->info
.base
.uses_linear_sample
)
1447 barycentricsMask
|= SWR_BARYCENTRIC_PER_SAMPLE_MASK
;
1449 for (unsigned i
= 0; i
< ctx
->fs
->info
.base
.num_inputs
; i
++) {
1450 switch (ctx
->fs
->info
.base
.input_interpolate_loc
[i
]) {
1451 case TGSI_INTERPOLATE_LOC_CENTER
:
1452 barycentricsMask
|= SWR_BARYCENTRIC_PER_PIXEL_MASK
;
1454 case TGSI_INTERPOLATE_LOC_CENTROID
:
1455 barycentricsMask
|= SWR_BARYCENTRIC_CENTROID_MASK
;
1457 case TGSI_INTERPOLATE_LOC_SAMPLE
:
1458 barycentricsMask
|= SWR_BARYCENTRIC_PER_SAMPLE_MASK
;
1463 psState
.barycentricsMask
= barycentricsMask
;
1464 psState
.usesUAV
= false; // XXX
1465 psState
.forceEarlyZ
= false;
1466 SwrSetPixelShaderState(ctx
->swrContext
, &psState
);
1468 /* JIT sampler state */
1469 if (ctx
->dirty
& (SWR_NEW_SAMPLER
|
1471 swr_update_sampler_state(ctx
,
1472 PIPE_SHADER_FRAGMENT
,
1474 ctx
->swrDC
.samplersFS
);
1477 /* JIT sampler view state */
1478 if (ctx
->dirty
& (SWR_NEW_SAMPLER_VIEW
|
1479 SWR_NEW_FRAMEBUFFER
|
1481 swr_update_texture_state(ctx
,
1482 PIPE_SHADER_FRAGMENT
,
1483 key
.nr_sampler_views
,
1484 ctx
->swrDC
.texturesFS
);
1489 /* VertexShader Constants */
1490 if (ctx
->dirty
& SWR_NEW_VSCONSTANTS
) {
1491 swr_update_constants(ctx
, PIPE_SHADER_VERTEX
);
1494 /* FragmentShader Constants */
1495 if (ctx
->dirty
& SWR_NEW_FSCONSTANTS
) {
1496 swr_update_constants(ctx
, PIPE_SHADER_FRAGMENT
);
1499 /* GeometryShader Constants */
1500 if (ctx
->dirty
& SWR_NEW_GSCONSTANTS
) {
1501 swr_update_constants(ctx
, PIPE_SHADER_GEOMETRY
);
1504 /* Depth/stencil state */
1505 if (ctx
->dirty
& (SWR_NEW_DEPTH_STENCIL_ALPHA
| SWR_NEW_FRAMEBUFFER
)) {
1506 struct pipe_depth_state
*depth
= &(ctx
->depth_stencil
->depth
);
1507 struct pipe_stencil_state
*stencil
= ctx
->depth_stencil
->stencil
;
1508 SWR_DEPTH_STENCIL_STATE depthStencilState
= {{0}};
1509 SWR_DEPTH_BOUNDS_STATE depthBoundsState
= {0};
1511 /* XXX, incomplete. Need to flesh out stencil & alpha test state
1512 struct pipe_stencil_state *front_stencil =
1513 ctx->depth_stencil.stencil[0];
1514 struct pipe_stencil_state *back_stencil = ctx->depth_stencil.stencil[1];
1515 struct pipe_alpha_state alpha;
1517 if (stencil
[0].enabled
) {
1518 depthStencilState
.stencilWriteEnable
= 1;
1519 depthStencilState
.stencilTestEnable
= 1;
1520 depthStencilState
.stencilTestFunc
=
1521 swr_convert_depth_func(stencil
[0].func
);
1523 depthStencilState
.stencilPassDepthPassOp
=
1524 swr_convert_stencil_op(stencil
[0].zpass_op
);
1525 depthStencilState
.stencilPassDepthFailOp
=
1526 swr_convert_stencil_op(stencil
[0].zfail_op
);
1527 depthStencilState
.stencilFailOp
=
1528 swr_convert_stencil_op(stencil
[0].fail_op
);
1529 depthStencilState
.stencilWriteMask
= stencil
[0].writemask
;
1530 depthStencilState
.stencilTestMask
= stencil
[0].valuemask
;
1531 depthStencilState
.stencilRefValue
= ctx
->stencil_ref
.ref_value
[0];
1533 if (stencil
[1].enabled
) {
1534 depthStencilState
.doubleSidedStencilTestEnable
= 1;
1536 depthStencilState
.backfaceStencilTestFunc
=
1537 swr_convert_depth_func(stencil
[1].func
);
1539 depthStencilState
.backfaceStencilPassDepthPassOp
=
1540 swr_convert_stencil_op(stencil
[1].zpass_op
);
1541 depthStencilState
.backfaceStencilPassDepthFailOp
=
1542 swr_convert_stencil_op(stencil
[1].zfail_op
);
1543 depthStencilState
.backfaceStencilFailOp
=
1544 swr_convert_stencil_op(stencil
[1].fail_op
);
1545 depthStencilState
.backfaceStencilWriteMask
= stencil
[1].writemask
;
1546 depthStencilState
.backfaceStencilTestMask
= stencil
[1].valuemask
;
1548 depthStencilState
.backfaceStencilRefValue
=
1549 ctx
->stencil_ref
.ref_value
[1];
1552 depthStencilState
.depthTestEnable
= depth
->enabled
;
1553 depthStencilState
.depthTestFunc
= swr_convert_depth_func(depth
->func
);
1554 depthStencilState
.depthWriteEnable
= depth
->writemask
;
1555 SwrSetDepthStencilState(ctx
->swrContext
, &depthStencilState
);
1557 depthBoundsState
.depthBoundsTestEnable
= depth
->bounds_test
;
1558 depthBoundsState
.depthBoundsTestMinValue
= depth
->bounds_min
;
1559 depthBoundsState
.depthBoundsTestMaxValue
= depth
->bounds_max
;
1560 SwrSetDepthBoundsState(ctx
->swrContext
, &depthBoundsState
);
1564 if (ctx
->dirty
& (SWR_NEW_BLEND
|
1565 SWR_NEW_RASTERIZER
|
1566 SWR_NEW_FRAMEBUFFER
|
1567 SWR_NEW_DEPTH_STENCIL_ALPHA
)) {
1568 struct pipe_framebuffer_state
*fb
= &ctx
->framebuffer
;
1570 SWR_BLEND_STATE blendState
;
1571 memcpy(&blendState
, &ctx
->blend
->blendState
, sizeof(blendState
));
1572 blendState
.constantColor
[0] = ctx
->blend_color
.color
[0];
1573 blendState
.constantColor
[1] = ctx
->blend_color
.color
[1];
1574 blendState
.constantColor
[2] = ctx
->blend_color
.color
[2];
1575 blendState
.constantColor
[3] = ctx
->blend_color
.color
[3];
1576 blendState
.alphaTestReference
=
1577 *((uint32_t*)&ctx
->depth_stencil
->alpha
.ref_value
);
1579 blendState
.sampleMask
= ctx
->sample_mask
;
1580 blendState
.sampleCount
= GetSampleCount(fb
->samples
);
1582 /* If there are no color buffers bound, disable writes on RT0
1584 if (fb
->nr_cbufs
== 0) {
1585 blendState
.renderTarget
[0].writeDisableRed
= 1;
1586 blendState
.renderTarget
[0].writeDisableGreen
= 1;
1587 blendState
.renderTarget
[0].writeDisableBlue
= 1;
1588 blendState
.renderTarget
[0].writeDisableAlpha
= 1;
1589 SwrSetBlendFunc(ctx
->swrContext
, 0, NULL
);
1592 for (int target
= 0;
1593 target
< std::min(SWR_NUM_RENDERTARGETS
,
1594 PIPE_MAX_COLOR_BUFS
);
1596 if (!fb
->cbufs
[target
])
1599 struct swr_resource
*colorBuffer
=
1600 swr_resource(fb
->cbufs
[target
]->texture
);
1602 BLEND_COMPILE_STATE compileState
;
1603 memset(&compileState
, 0, sizeof(compileState
));
1604 compileState
.format
= colorBuffer
->swr
.format
;
1605 memcpy(&compileState
.blendState
,
1606 &ctx
->blend
->compileState
[target
],
1607 sizeof(compileState
.blendState
));
1609 const SWR_FORMAT_INFO
& info
= GetFormatInfo(compileState
.format
);
1610 if (compileState
.blendState
.logicOpEnable
&&
1611 ((info
.type
[0] == SWR_TYPE_FLOAT
) || info
.isSRGB
)) {
1612 compileState
.blendState
.logicOpEnable
= false;
1615 if (info
.type
[0] == SWR_TYPE_SINT
|| info
.type
[0] == SWR_TYPE_UINT
)
1616 compileState
.blendState
.blendEnable
= false;
1618 if (compileState
.blendState
.blendEnable
== false &&
1619 compileState
.blendState
.logicOpEnable
== false &&
1620 ctx
->depth_stencil
->alpha
.enabled
== 0) {
1621 SwrSetBlendFunc(ctx
->swrContext
, target
, NULL
);
1625 compileState
.desc
.alphaTestEnable
=
1626 ctx
->depth_stencil
->alpha
.enabled
;
1627 compileState
.desc
.independentAlphaBlendEnable
=
1628 (compileState
.blendState
.sourceBlendFactor
!=
1629 compileState
.blendState
.sourceAlphaBlendFactor
) ||
1630 (compileState
.blendState
.destBlendFactor
!=
1631 compileState
.blendState
.destAlphaBlendFactor
) ||
1632 (compileState
.blendState
.colorBlendFunc
!=
1633 compileState
.blendState
.alphaBlendFunc
);
1634 compileState
.desc
.alphaToCoverageEnable
=
1635 ctx
->blend
->pipe
.alpha_to_coverage
;
1636 compileState
.desc
.sampleMaskEnable
= (blendState
.sampleMask
!= 0);
1637 compileState
.desc
.numSamples
= fb
->samples
;
1639 compileState
.alphaTestFunction
=
1640 swr_convert_depth_func(ctx
->depth_stencil
->alpha
.func
);
1641 compileState
.alphaTestFormat
= ALPHA_TEST_FLOAT32
; // xxx
1643 compileState
.Canonicalize();
1645 PFN_BLEND_JIT_FUNC func
= NULL
;
1646 auto search
= ctx
->blendJIT
->find(compileState
);
1647 if (search
!= ctx
->blendJIT
->end()) {
1648 func
= search
->second
;
1650 HANDLE hJitMgr
= screen
->hJitMgr
;
1651 func
= JitCompileBlend(hJitMgr
, compileState
);
1652 debug_printf("BLEND shader %p\n", func
);
1653 assert(func
&& "Error: BlendShader = NULL");
1655 ctx
->blendJIT
->insert(std::make_pair(compileState
, func
));
1657 SwrSetBlendFunc(ctx
->swrContext
, target
, func
);
1660 SwrSetBlendState(ctx
->swrContext
, &blendState
);
1663 if (ctx
->dirty
& SWR_NEW_STIPPLE
) {
1664 /* XXX What to do with this one??? SWR doesn't stipple */
1667 if (ctx
->dirty
& (SWR_NEW_VS
| SWR_NEW_SO
| SWR_NEW_RASTERIZER
)) {
1668 ctx
->vs
->soState
.rasterizerDisable
=
1669 ctx
->rasterizer
->rasterizer_discard
;
1670 SwrSetSoState(ctx
->swrContext
, &ctx
->vs
->soState
);
1672 pipe_stream_output_info
*stream_output
= &ctx
->vs
->pipe
.stream_output
;
1674 for (uint32_t i
= 0; i
< ctx
->num_so_targets
; i
++) {
1675 SWR_STREAMOUT_BUFFER buffer
= {0};
1676 if (!ctx
->so_targets
[i
])
1678 buffer
.enable
= true;
1680 (uint32_t *)(swr_resource_data(ctx
->so_targets
[i
]->buffer
) +
1681 ctx
->so_targets
[i
]->buffer_offset
);
1682 buffer
.bufferSize
= ctx
->so_targets
[i
]->buffer_size
>> 2;
1683 buffer
.pitch
= stream_output
->stride
[i
];
1684 buffer
.streamOffset
= 0;
1686 SwrSetSoBuffers(ctx
->swrContext
, &buffer
, i
);
1690 if (ctx
->dirty
& (SWR_NEW_CLIP
| SWR_NEW_RASTERIZER
| SWR_NEW_VS
)) {
1691 // shader exporting clip distances overrides all user clip planes
1692 if (ctx
->rasterizer
->clip_plane_enable
&&
1693 !ctx
->vs
->info
.base
.num_written_clipdistance
)
1695 swr_draw_context
*pDC
= &ctx
->swrDC
;
1696 memcpy(pDC
->userClipPlanes
,
1698 sizeof(pDC
->userClipPlanes
));
1702 // set up backend state
1703 SWR_BACKEND_STATE backendState
= {0};
1704 backendState
.numAttributes
=
1705 ((ctx
->gs
? ctx
->gs
->info
.base
.num_outputs
: ctx
->vs
->info
.base
.num_outputs
) - 1) +
1706 (ctx
->rasterizer
->sprite_coord_enable
? 1 : 0);
1707 backendState
.numAttributes
= std::min((size_t)backendState
.numAttributes
,
1708 sizeof(backendState
.numComponents
));
1709 for (unsigned i
= 0; i
< backendState
.numAttributes
; i
++)
1710 backendState
.numComponents
[i
] = 4;
1711 backendState
.constantInterpolationMask
= ctx
->fs
->constantMask
|
1712 (ctx
->rasterizer
->flatshade
? ctx
->fs
->flatConstantMask
: 0);
1713 backendState
.pointSpriteTexCoordMask
= ctx
->fs
->pointSpriteMask
;
1715 SwrSetBackendState(ctx
->swrContext
, &backendState
);
1717 /* Ensure that any in-progress attachment change StoreTiles finish */
1718 if (swr_is_fence_pending(screen
->flush_fence
))
1719 swr_fence_finish(pipe
->screen
, NULL
, screen
->flush_fence
, 0);
1721 /* Finally, update the in-use status of all resources involved in draw */
1722 swr_update_resource_status(pipe
, p_draw_info
);
1724 ctx
->dirty
= post_update_dirty_flags
;
1728 static struct pipe_stream_output_target
*
1729 swr_create_so_target(struct pipe_context
*pipe
,
1730 struct pipe_resource
*buffer
,
1731 unsigned buffer_offset
,
1732 unsigned buffer_size
)
1734 struct pipe_stream_output_target
*target
;
1736 target
= CALLOC_STRUCT(pipe_stream_output_target
);
1740 target
->context
= pipe
;
1741 target
->reference
.count
= 1;
1742 pipe_resource_reference(&target
->buffer
, buffer
);
1743 target
->buffer_offset
= buffer_offset
;
1744 target
->buffer_size
= buffer_size
;
1749 swr_destroy_so_target(struct pipe_context
*pipe
,
1750 struct pipe_stream_output_target
*target
)
1752 pipe_resource_reference(&target
->buffer
, NULL
);
1757 swr_set_so_targets(struct pipe_context
*pipe
,
1758 unsigned num_targets
,
1759 struct pipe_stream_output_target
**targets
,
1760 const unsigned *offsets
)
1762 struct swr_context
*swr
= swr_context(pipe
);
1765 assert(num_targets
<= MAX_SO_STREAMS
);
1767 for (i
= 0; i
< num_targets
; i
++) {
1768 pipe_so_target_reference(
1769 (struct pipe_stream_output_target
**)&swr
->so_targets
[i
],
1773 for (/* fall-through */; i
< swr
->num_so_targets
; i
++) {
1774 pipe_so_target_reference(
1775 (struct pipe_stream_output_target
**)&swr
->so_targets
[i
], NULL
);
1778 swr
->num_so_targets
= num_targets
;
1780 swr
->dirty
|= SWR_NEW_SO
;
1785 swr_state_init(struct pipe_context
*pipe
)
1787 pipe
->create_blend_state
= swr_create_blend_state
;
1788 pipe
->bind_blend_state
= swr_bind_blend_state
;
1789 pipe
->delete_blend_state
= swr_delete_blend_state
;
1791 pipe
->create_depth_stencil_alpha_state
= swr_create_depth_stencil_state
;
1792 pipe
->bind_depth_stencil_alpha_state
= swr_bind_depth_stencil_state
;
1793 pipe
->delete_depth_stencil_alpha_state
= swr_delete_depth_stencil_state
;
1795 pipe
->create_rasterizer_state
= swr_create_rasterizer_state
;
1796 pipe
->bind_rasterizer_state
= swr_bind_rasterizer_state
;
1797 pipe
->delete_rasterizer_state
= swr_delete_rasterizer_state
;
1799 pipe
->create_sampler_state
= swr_create_sampler_state
;
1800 pipe
->bind_sampler_states
= swr_bind_sampler_states
;
1801 pipe
->delete_sampler_state
= swr_delete_sampler_state
;
1803 pipe
->create_sampler_view
= swr_create_sampler_view
;
1804 pipe
->set_sampler_views
= swr_set_sampler_views
;
1805 pipe
->sampler_view_destroy
= swr_sampler_view_destroy
;
1807 pipe
->create_vs_state
= swr_create_vs_state
;
1808 pipe
->bind_vs_state
= swr_bind_vs_state
;
1809 pipe
->delete_vs_state
= swr_delete_vs_state
;
1811 pipe
->create_fs_state
= swr_create_fs_state
;
1812 pipe
->bind_fs_state
= swr_bind_fs_state
;
1813 pipe
->delete_fs_state
= swr_delete_fs_state
;
1815 pipe
->create_gs_state
= swr_create_gs_state
;
1816 pipe
->bind_gs_state
= swr_bind_gs_state
;
1817 pipe
->delete_gs_state
= swr_delete_gs_state
;
1819 pipe
->set_constant_buffer
= swr_set_constant_buffer
;
1821 pipe
->create_vertex_elements_state
= swr_create_vertex_elements_state
;
1822 pipe
->bind_vertex_elements_state
= swr_bind_vertex_elements_state
;
1823 pipe
->delete_vertex_elements_state
= swr_delete_vertex_elements_state
;
1825 pipe
->set_vertex_buffers
= swr_set_vertex_buffers
;
1826 pipe
->set_index_buffer
= swr_set_index_buffer
;
1828 pipe
->set_polygon_stipple
= swr_set_polygon_stipple
;
1829 pipe
->set_clip_state
= swr_set_clip_state
;
1830 pipe
->set_scissor_states
= swr_set_scissor_states
;
1831 pipe
->set_viewport_states
= swr_set_viewport_states
;
1833 pipe
->set_framebuffer_state
= swr_set_framebuffer_state
;
1835 pipe
->set_blend_color
= swr_set_blend_color
;
1836 pipe
->set_stencil_ref
= swr_set_stencil_ref
;
1838 pipe
->set_sample_mask
= swr_set_sample_mask
;
1839 pipe
->get_sample_position
= swr_get_sample_position
;
1841 pipe
->create_stream_output_target
= swr_create_so_target
;
1842 pipe
->stream_output_target_destroy
= swr_destroy_so_target
;
1843 pipe
->set_stream_output_targets
= swr_set_so_targets
;