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 "state_llvm.h"
34 #include "gallivm/lp_bld_tgsi.h"
35 #include "util/u_format.h"
37 #include "util/u_memory.h"
38 #include "util/u_inlines.h"
39 #include "util/u_helpers.h"
40 #include "util/u_framebuffer.h"
42 #include "swr_state.h"
43 #include "swr_context.h"
44 #include "swr_context_llvm.h"
45 #include "swr_screen.h"
46 #include "swr_resource.h"
47 #include "swr_tex_sample.h"
48 #include "swr_scratch.h"
49 #include "swr_shader.h"
50 #include "swr_fence.h"
52 /* These should be pulled out into separate files as necessary
53 * Just initializing everything here to get going. */
56 swr_create_blend_state(struct pipe_context
*pipe
,
57 const struct pipe_blend_state
*blend
)
59 struct swr_blend_state
*state
= CALLOC_STRUCT(swr_blend_state
);
61 memcpy(&state
->pipe
, blend
, sizeof(*blend
));
63 struct pipe_blend_state
*pipe_blend
= &state
->pipe
;
66 target
< std::min(SWR_NUM_RENDERTARGETS
, PIPE_MAX_COLOR_BUFS
);
69 struct pipe_rt_blend_state
*rt_blend
= &pipe_blend
->rt
[target
];
70 SWR_RENDER_TARGET_BLEND_STATE
&blendState
=
71 state
->blendState
.renderTarget
[target
];
72 RENDER_TARGET_BLEND_COMPILE_STATE
&compileState
=
73 state
->compileState
[target
];
75 if (target
!= 0 && !pipe_blend
->independent_blend_enable
) {
77 &state
->compileState
[0],
78 sizeof(RENDER_TARGET_BLEND_COMPILE_STATE
));
82 compileState
.blendEnable
= rt_blend
->blend_enable
;
83 if (compileState
.blendEnable
) {
84 compileState
.sourceAlphaBlendFactor
=
85 swr_convert_blend_factor(rt_blend
->alpha_src_factor
);
86 compileState
.destAlphaBlendFactor
=
87 swr_convert_blend_factor(rt_blend
->alpha_dst_factor
);
88 compileState
.sourceBlendFactor
=
89 swr_convert_blend_factor(rt_blend
->rgb_src_factor
);
90 compileState
.destBlendFactor
=
91 swr_convert_blend_factor(rt_blend
->rgb_dst_factor
);
93 compileState
.colorBlendFunc
=
94 swr_convert_blend_func(rt_blend
->rgb_func
);
95 compileState
.alphaBlendFunc
=
96 swr_convert_blend_func(rt_blend
->alpha_func
);
98 compileState
.logicOpEnable
= state
->pipe
.logicop_enable
;
99 if (compileState
.logicOpEnable
) {
100 compileState
.logicOpFunc
=
101 swr_convert_logic_op(state
->pipe
.logicop_func
);
104 blendState
.writeDisableRed
=
105 (rt_blend
->colormask
& PIPE_MASK_R
) ? 0 : 1;
106 blendState
.writeDisableGreen
=
107 (rt_blend
->colormask
& PIPE_MASK_G
) ? 0 : 1;
108 blendState
.writeDisableBlue
=
109 (rt_blend
->colormask
& PIPE_MASK_B
) ? 0 : 1;
110 blendState
.writeDisableAlpha
=
111 (rt_blend
->colormask
& PIPE_MASK_A
) ? 0 : 1;
113 if (rt_blend
->colormask
== 0)
114 compileState
.blendEnable
= false;
121 swr_bind_blend_state(struct pipe_context
*pipe
, void *blend
)
123 struct swr_context
*ctx
= swr_context(pipe
);
125 if (ctx
->blend
== blend
)
128 ctx
->blend
= (swr_blend_state
*)blend
;
130 ctx
->dirty
|= SWR_NEW_BLEND
;
134 swr_delete_blend_state(struct pipe_context
*pipe
, void *blend
)
140 swr_set_blend_color(struct pipe_context
*pipe
,
141 const struct pipe_blend_color
*color
)
143 struct swr_context
*ctx
= swr_context(pipe
);
145 ctx
->blend_color
= *color
;
147 ctx
->dirty
|= SWR_NEW_BLEND
;
151 swr_set_stencil_ref(struct pipe_context
*pipe
,
152 const struct pipe_stencil_ref
*ref
)
154 struct swr_context
*ctx
= swr_context(pipe
);
156 ctx
->stencil_ref
= *ref
;
158 ctx
->dirty
|= SWR_NEW_DEPTH_STENCIL_ALPHA
;
162 swr_create_depth_stencil_state(
163 struct pipe_context
*pipe
,
164 const struct pipe_depth_stencil_alpha_state
*depth_stencil
)
166 struct pipe_depth_stencil_alpha_state
*state
;
168 state
= (pipe_depth_stencil_alpha_state
*)mem_dup(depth_stencil
,
169 sizeof *depth_stencil
);
175 swr_bind_depth_stencil_state(struct pipe_context
*pipe
, void *depth_stencil
)
177 struct swr_context
*ctx
= swr_context(pipe
);
179 if (ctx
->depth_stencil
== (pipe_depth_stencil_alpha_state
*)depth_stencil
)
182 ctx
->depth_stencil
= (pipe_depth_stencil_alpha_state
*)depth_stencil
;
184 ctx
->dirty
|= SWR_NEW_DEPTH_STENCIL_ALPHA
;
188 swr_delete_depth_stencil_state(struct pipe_context
*pipe
, void *depth
)
195 swr_create_rasterizer_state(struct pipe_context
*pipe
,
196 const struct pipe_rasterizer_state
*rast
)
198 struct pipe_rasterizer_state
*state
;
199 state
= (pipe_rasterizer_state
*)mem_dup(rast
, sizeof *rast
);
205 swr_bind_rasterizer_state(struct pipe_context
*pipe
, void *handle
)
207 struct swr_context
*ctx
= swr_context(pipe
);
208 const struct pipe_rasterizer_state
*rasterizer
=
209 (const struct pipe_rasterizer_state
*)handle
;
211 if (ctx
->rasterizer
== (pipe_rasterizer_state
*)rasterizer
)
214 ctx
->rasterizer
= (pipe_rasterizer_state
*)rasterizer
;
216 ctx
->dirty
|= SWR_NEW_RASTERIZER
;
220 swr_delete_rasterizer_state(struct pipe_context
*pipe
, void *rasterizer
)
227 swr_create_sampler_state(struct pipe_context
*pipe
,
228 const struct pipe_sampler_state
*sampler
)
230 struct pipe_sampler_state
*state
=
231 (pipe_sampler_state
*)mem_dup(sampler
, sizeof *sampler
);
237 swr_bind_sampler_states(struct pipe_context
*pipe
,
238 enum pipe_shader_type shader
,
243 struct swr_context
*ctx
= swr_context(pipe
);
246 assert(shader
< PIPE_SHADER_TYPES
);
247 assert(start
+ num
<= ARRAY_SIZE(ctx
->samplers
[shader
]));
249 /* set the new samplers */
250 ctx
->num_samplers
[shader
] = num
;
251 for (i
= 0; i
< num
; i
++) {
252 ctx
->samplers
[shader
][start
+ i
] = (pipe_sampler_state
*)samplers
[i
];
255 ctx
->dirty
|= SWR_NEW_SAMPLER
;
259 swr_delete_sampler_state(struct pipe_context
*pipe
, void *sampler
)
265 static struct pipe_sampler_view
*
266 swr_create_sampler_view(struct pipe_context
*pipe
,
267 struct pipe_resource
*texture
,
268 const struct pipe_sampler_view
*templ
)
270 struct pipe_sampler_view
*view
= CALLOC_STRUCT(pipe_sampler_view
);
274 view
->reference
.count
= 1;
275 view
->texture
= NULL
;
276 pipe_resource_reference(&view
->texture
, texture
);
277 view
->context
= pipe
;
284 swr_set_sampler_views(struct pipe_context
*pipe
,
285 enum pipe_shader_type shader
,
288 struct pipe_sampler_view
**views
)
290 struct swr_context
*ctx
= swr_context(pipe
);
293 assert(num
<= PIPE_MAX_SHADER_SAMPLER_VIEWS
);
295 assert(shader
< PIPE_SHADER_TYPES
);
296 assert(start
+ num
<= ARRAY_SIZE(ctx
->sampler_views
[shader
]));
298 /* set the new sampler views */
299 ctx
->num_sampler_views
[shader
] = num
;
300 for (i
= 0; i
< num
; i
++) {
301 /* Note: we're using pipe_sampler_view_release() here to work around
302 * a possible crash when the old view belongs to another context that
303 * was already destroyed.
305 pipe_sampler_view_release(pipe
, &ctx
->sampler_views
[shader
][start
+ i
]);
306 pipe_sampler_view_reference(&ctx
->sampler_views
[shader
][start
+ i
],
310 ctx
->dirty
|= SWR_NEW_SAMPLER_VIEW
;
314 swr_sampler_view_destroy(struct pipe_context
*pipe
,
315 struct pipe_sampler_view
*view
)
317 pipe_resource_reference(&view
->texture
, NULL
);
322 swr_create_vs_state(struct pipe_context
*pipe
,
323 const struct pipe_shader_state
*vs
)
325 struct swr_vertex_shader
*swr_vs
= new swr_vertex_shader
;
329 swr_vs
->pipe
.tokens
= tgsi_dup_tokens(vs
->tokens
);
330 swr_vs
->pipe
.stream_output
= vs
->stream_output
;
332 lp_build_tgsi_info(vs
->tokens
, &swr_vs
->info
);
334 swr_vs
->soState
= {0};
336 if (swr_vs
->pipe
.stream_output
.num_outputs
) {
337 pipe_stream_output_info
*stream_output
= &swr_vs
->pipe
.stream_output
;
339 swr_vs
->soState
.soEnable
= true;
340 // soState.rasterizerDisable set on state dirty
341 // soState.streamToRasterizer not used
343 for (uint32_t i
= 0; i
< stream_output
->num_outputs
; i
++) {
344 swr_vs
->soState
.streamMasks
[stream_output
->output
[i
].stream
] |=
345 1 << (stream_output
->output
[i
].register_index
- 1);
347 for (uint32_t i
= 0; i
< MAX_SO_STREAMS
; i
++) {
348 swr_vs
->soState
.streamNumEntries
[i
] =
349 _mm_popcnt_u32(swr_vs
->soState
.streamMasks
[i
]);
357 swr_bind_vs_state(struct pipe_context
*pipe
, void *vs
)
359 struct swr_context
*ctx
= swr_context(pipe
);
364 ctx
->vs
= (swr_vertex_shader
*)vs
;
365 ctx
->dirty
|= SWR_NEW_VS
;
369 swr_delete_vs_state(struct pipe_context
*pipe
, void *vs
)
371 struct swr_vertex_shader
*swr_vs
= (swr_vertex_shader
*)vs
;
372 FREE((void *)swr_vs
->pipe
.tokens
);
377 swr_create_fs_state(struct pipe_context
*pipe
,
378 const struct pipe_shader_state
*fs
)
380 struct swr_fragment_shader
*swr_fs
= new swr_fragment_shader
;
384 swr_fs
->pipe
.tokens
= tgsi_dup_tokens(fs
->tokens
);
386 lp_build_tgsi_info(fs
->tokens
, &swr_fs
->info
);
393 swr_bind_fs_state(struct pipe_context
*pipe
, void *fs
)
395 struct swr_context
*ctx
= swr_context(pipe
);
400 ctx
->fs
= (swr_fragment_shader
*)fs
;
401 ctx
->dirty
|= SWR_NEW_FS
;
405 swr_delete_fs_state(struct pipe_context
*pipe
, void *fs
)
407 struct swr_fragment_shader
*swr_fs
= (swr_fragment_shader
*)fs
;
408 FREE((void *)swr_fs
->pipe
.tokens
);
414 swr_set_constant_buffer(struct pipe_context
*pipe
,
417 const struct pipe_constant_buffer
*cb
)
419 struct swr_context
*ctx
= swr_context(pipe
);
420 struct pipe_resource
*constants
= cb
? cb
->buffer
: NULL
;
422 assert(shader
< PIPE_SHADER_TYPES
);
423 assert(index
< ARRAY_SIZE(ctx
->constants
[shader
]));
425 /* note: reference counting */
426 util_copy_constant_buffer(&ctx
->constants
[shader
][index
], cb
);
428 if (shader
== PIPE_SHADER_VERTEX
|| shader
== PIPE_SHADER_GEOMETRY
) {
429 ctx
->dirty
|= SWR_NEW_VSCONSTANTS
;
430 } else if (shader
== PIPE_SHADER_FRAGMENT
) {
431 ctx
->dirty
|= SWR_NEW_FSCONSTANTS
;
434 if (cb
&& cb
->user_buffer
) {
435 pipe_resource_reference(&constants
, NULL
);
441 swr_create_vertex_elements_state(struct pipe_context
*pipe
,
442 unsigned num_elements
,
443 const struct pipe_vertex_element
*attribs
)
445 struct swr_vertex_element_state
*velems
;
446 assert(num_elements
<= PIPE_MAX_ATTRIBS
);
447 velems
= CALLOC_STRUCT(swr_vertex_element_state
);
449 velems
->fsState
.numAttribs
= num_elements
;
450 for (unsigned i
= 0; i
< num_elements
; i
++) {
451 // XXX: we should do this keyed on the VS usage info
453 const struct util_format_description
*desc
=
454 util_format_description(attribs
[i
].src_format
);
456 velems
->fsState
.layout
[i
].AlignedByteOffset
= attribs
[i
].src_offset
;
457 velems
->fsState
.layout
[i
].Format
=
458 mesa_to_swr_format(attribs
[i
].src_format
);
459 velems
->fsState
.layout
[i
].StreamIndex
=
460 attribs
[i
].vertex_buffer_index
;
461 velems
->fsState
.layout
[i
].InstanceEnable
=
462 attribs
[i
].instance_divisor
!= 0;
463 velems
->fsState
.layout
[i
].ComponentControl0
=
464 desc
->channel
[0].type
!= UTIL_FORMAT_TYPE_VOID
465 ? ComponentControl::StoreSrc
466 : ComponentControl::Store0
;
467 velems
->fsState
.layout
[i
].ComponentControl1
=
468 desc
->channel
[1].type
!= UTIL_FORMAT_TYPE_VOID
469 ? ComponentControl::StoreSrc
470 : ComponentControl::Store0
;
471 velems
->fsState
.layout
[i
].ComponentControl2
=
472 desc
->channel
[2].type
!= UTIL_FORMAT_TYPE_VOID
473 ? ComponentControl::StoreSrc
474 : ComponentControl::Store0
;
475 velems
->fsState
.layout
[i
].ComponentControl3
=
476 desc
->channel
[3].type
!= UTIL_FORMAT_TYPE_VOID
477 ? ComponentControl::StoreSrc
478 : ComponentControl::Store1Fp
;
479 velems
->fsState
.layout
[i
].ComponentPacking
= ComponentEnable::XYZW
;
480 velems
->fsState
.layout
[i
].InstanceDataStepRate
=
481 attribs
[i
].instance_divisor
;
483 /* Calculate the pitch of each stream */
484 const SWR_FORMAT_INFO
&swr_desc
= GetFormatInfo(
485 mesa_to_swr_format(attribs
[i
].src_format
));
486 velems
->stream_pitch
[attribs
[i
].vertex_buffer_index
] += swr_desc
.Bpp
;
494 swr_bind_vertex_elements_state(struct pipe_context
*pipe
, void *velems
)
496 struct swr_context
*ctx
= swr_context(pipe
);
497 struct swr_vertex_element_state
*swr_velems
=
498 (struct swr_vertex_element_state
*)velems
;
500 ctx
->velems
= swr_velems
;
501 ctx
->dirty
|= SWR_NEW_VERTEX
;
505 swr_delete_vertex_elements_state(struct pipe_context
*pipe
, void *velems
)
507 /* XXX Need to destroy fetch shader? */
513 swr_set_vertex_buffers(struct pipe_context
*pipe
,
515 unsigned num_elements
,
516 const struct pipe_vertex_buffer
*buffers
)
518 struct swr_context
*ctx
= swr_context(pipe
);
520 assert(num_elements
<= PIPE_MAX_ATTRIBS
);
522 util_set_vertex_buffers_count(ctx
->vertex_buffer
,
523 &ctx
->num_vertex_buffers
,
528 ctx
->dirty
|= SWR_NEW_VERTEX
;
533 swr_set_index_buffer(struct pipe_context
*pipe
,
534 const struct pipe_index_buffer
*ib
)
536 struct swr_context
*ctx
= swr_context(pipe
);
539 memcpy(&ctx
->index_buffer
, ib
, sizeof(ctx
->index_buffer
));
541 memset(&ctx
->index_buffer
, 0, sizeof(ctx
->index_buffer
));
543 ctx
->dirty
|= SWR_NEW_VERTEX
;
547 swr_set_polygon_stipple(struct pipe_context
*pipe
,
548 const struct pipe_poly_stipple
*stipple
)
550 struct swr_context
*ctx
= swr_context(pipe
);
552 ctx
->poly_stipple
= *stipple
; /* struct copy */
553 ctx
->dirty
|= SWR_NEW_STIPPLE
;
557 swr_set_clip_state(struct pipe_context
*pipe
,
558 const struct pipe_clip_state
*clip
)
560 struct swr_context
*ctx
= swr_context(pipe
);
563 /* XXX Unimplemented, but prevents crash */
565 ctx
->dirty
|= SWR_NEW_CLIP
;
570 swr_set_scissor_states(struct pipe_context
*pipe
,
572 unsigned num_viewports
,
573 const struct pipe_scissor_state
*scissor
)
575 struct swr_context
*ctx
= swr_context(pipe
);
577 ctx
->scissor
= *scissor
;
578 ctx
->swr_scissor
.xmin
= scissor
->minx
;
579 ctx
->swr_scissor
.xmax
= scissor
->maxx
;
580 ctx
->swr_scissor
.ymin
= scissor
->miny
;
581 ctx
->swr_scissor
.ymax
= scissor
->maxy
;
582 ctx
->dirty
|= SWR_NEW_SCISSOR
;
586 swr_set_viewport_states(struct pipe_context
*pipe
,
588 unsigned num_viewports
,
589 const struct pipe_viewport_state
*vpt
)
591 struct swr_context
*ctx
= swr_context(pipe
);
593 ctx
->viewport
= *vpt
;
594 ctx
->dirty
|= SWR_NEW_VIEWPORT
;
599 swr_set_framebuffer_state(struct pipe_context
*pipe
,
600 const struct pipe_framebuffer_state
*fb
)
602 struct swr_context
*ctx
= swr_context(pipe
);
604 boolean changed
= !util_framebuffer_state_equal(&ctx
->framebuffer
, fb
);
606 assert(fb
->width
<= KNOB_GUARDBAND_WIDTH
);
607 assert(fb
->height
<= KNOB_GUARDBAND_HEIGHT
);
611 for (i
= 0; i
< fb
->nr_cbufs
; ++i
)
612 pipe_surface_reference(&ctx
->framebuffer
.cbufs
[i
], fb
->cbufs
[i
]);
613 for (; i
< ctx
->framebuffer
.nr_cbufs
; ++i
)
614 pipe_surface_reference(&ctx
->framebuffer
.cbufs
[i
], NULL
);
616 ctx
->framebuffer
.nr_cbufs
= fb
->nr_cbufs
;
618 ctx
->framebuffer
.width
= fb
->width
;
619 ctx
->framebuffer
.height
= fb
->height
;
621 pipe_surface_reference(&ctx
->framebuffer
.zsbuf
, fb
->zsbuf
);
623 ctx
->dirty
|= SWR_NEW_FRAMEBUFFER
;
629 swr_set_sample_mask(struct pipe_context
*pipe
, unsigned sample_mask
)
631 struct swr_context
*ctx
= swr_context(pipe
);
633 if (sample_mask
!= ctx
->sample_mask
) {
634 ctx
->sample_mask
= sample_mask
;
635 ctx
->dirty
|= SWR_NEW_RASTERIZER
;
640 * Update resource in-use status
641 * All resources bound to color or depth targets marked as WRITE resources.
642 * VBO Vertex/index buffers and texture views marked as READ resources.
645 swr_update_resource_status(struct pipe_context
*pipe
,
646 const struct pipe_draw_info
*p_draw_info
)
648 struct swr_context
*ctx
= swr_context(pipe
);
649 struct pipe_framebuffer_state
*fb
= &ctx
->framebuffer
;
651 /* colorbuffer targets */
653 for (uint32_t i
= 0; i
< fb
->nr_cbufs
; ++i
)
655 swr_resource_write(fb
->cbufs
[i
]->texture
);
657 /* depth/stencil target */
659 swr_resource_write(fb
->zsbuf
->texture
);
661 /* VBO vertex buffers */
662 for (uint32_t i
= 0; i
< ctx
->num_vertex_buffers
; i
++) {
663 struct pipe_vertex_buffer
*vb
= &ctx
->vertex_buffer
[i
];
664 if (!vb
->user_buffer
)
665 swr_resource_read(vb
->buffer
);
668 /* VBO index buffer */
669 if (p_draw_info
&& p_draw_info
->indexed
) {
670 struct pipe_index_buffer
*ib
= &ctx
->index_buffer
;
671 if (!ib
->user_buffer
)
672 swr_resource_read(ib
->buffer
);
675 /* texture sampler views */
676 for (uint32_t i
= 0; i
< PIPE_MAX_SHADER_SAMPLER_VIEWS
; i
++) {
677 struct pipe_sampler_view
*view
=
678 ctx
->sampler_views
[PIPE_SHADER_FRAGMENT
][i
];
680 swr_resource_read(view
->texture
);
685 swr_update_texture_state(struct swr_context
*ctx
,
686 unsigned shader_type
,
687 unsigned num_sampler_views
,
688 swr_jit_texture
*textures
)
690 for (unsigned i
= 0; i
< num_sampler_views
; i
++) {
691 struct pipe_sampler_view
*view
=
692 ctx
->sampler_views
[shader_type
][i
];
695 struct pipe_resource
*res
= view
->texture
;
696 struct swr_resource
*swr_res
= swr_resource(res
);
697 struct swr_jit_texture
*jit_tex
= &textures
[i
];
698 memset(jit_tex
, 0, sizeof(*jit_tex
));
699 jit_tex
->width
= res
->width0
;
700 jit_tex
->height
= res
->height0
;
701 jit_tex
->depth
= res
->depth0
;
702 jit_tex
->first_level
= view
->u
.tex
.first_level
;
703 jit_tex
->last_level
= view
->u
.tex
.last_level
;
704 jit_tex
->base_ptr
= swr_res
->swr
.pBaseAddress
;
706 for (unsigned level
= jit_tex
->first_level
;
707 level
<= jit_tex
->last_level
;
709 jit_tex
->row_stride
[level
] = swr_res
->row_stride
[level
];
710 jit_tex
->img_stride
[level
] = swr_res
->img_stride
[level
];
711 jit_tex
->mip_offsets
[level
] = swr_res
->mip_offsets
[level
];
718 swr_update_sampler_state(struct swr_context
*ctx
,
719 unsigned shader_type
,
720 unsigned num_samplers
,
721 swr_jit_sampler
*samplers
)
723 for (unsigned i
= 0; i
< num_samplers
; i
++) {
724 const struct pipe_sampler_state
*sampler
=
725 ctx
->samplers
[shader_type
][i
];
728 samplers
[i
].min_lod
= sampler
->min_lod
;
729 samplers
[i
].max_lod
= sampler
->max_lod
;
730 samplers
[i
].lod_bias
= sampler
->lod_bias
;
731 COPY_4V(samplers
[i
].border_color
, sampler
->border_color
.f
);
737 swr_update_constants(struct swr_context
*ctx
, enum pipe_shader_type shaderType
)
739 swr_draw_context
*pDC
= &ctx
->swrDC
;
741 const float **constant
;
742 uint32_t *num_constants
;
743 struct swr_scratch_space
*scratch
;
745 switch (shaderType
) {
746 case PIPE_SHADER_VERTEX
:
747 constant
= pDC
->constantVS
;
748 num_constants
= pDC
->num_constantsVS
;
749 scratch
= &ctx
->scratch
->vs_constants
;
751 case PIPE_SHADER_FRAGMENT
:
752 constant
= pDC
->constantFS
;
753 num_constants
= pDC
->num_constantsFS
;
754 scratch
= &ctx
->scratch
->fs_constants
;
757 debug_printf("Unsupported shader type constants\n");
761 for (UINT i
= 0; i
< PIPE_MAX_CONSTANT_BUFFERS
; i
++) {
762 const pipe_constant_buffer
*cb
= &ctx
->constants
[shaderType
][i
];
763 num_constants
[i
] = cb
->buffer_size
;
766 (const float *)(swr_resource_data(cb
->buffer
) +
769 /* Need to copy these constants to scratch space */
770 if (cb
->user_buffer
&& cb
->buffer_size
) {
772 ((const uint8_t *)cb
->user_buffer
+ cb
->buffer_offset
);
773 uint32_t size
= AlignUp(cb
->buffer_size
, 4);
774 ptr
= swr_copy_to_scratch_space(ctx
, scratch
, ptr
, size
);
775 constant
[i
] = (const float *)ptr
;
782 swr_update_derived(struct pipe_context
*pipe
,
783 const struct pipe_draw_info
*p_draw_info
)
785 struct swr_context
*ctx
= swr_context(pipe
);
786 struct swr_screen
*screen
= swr_screen(ctx
->pipe
.screen
);
788 /* Update screen->pipe to current pipe context. */
789 if (screen
->pipe
!= pipe
)
792 /* Any state that requires dirty flags to be re-triggered sets this mask */
793 /* For example, user_buffer vertex and index buffers. */
794 unsigned post_update_dirty_flags
= 0;
797 if (ctx
->dirty
& SWR_NEW_FRAMEBUFFER
) {
798 struct pipe_framebuffer_state
*fb
= &ctx
->framebuffer
;
799 SWR_SURFACE_STATE
*new_attachment
[SWR_NUM_ATTACHMENTS
] = {0};
802 /* colorbuffer targets */
804 for (i
= 0; i
< fb
->nr_cbufs
; ++i
)
806 struct swr_resource
*colorBuffer
=
807 swr_resource(fb
->cbufs
[i
]->texture
);
808 new_attachment
[SWR_ATTACHMENT_COLOR0
+ i
] = &colorBuffer
->swr
;
811 /* depth/stencil target */
813 struct swr_resource
*depthStencilBuffer
=
814 swr_resource(fb
->zsbuf
->texture
);
815 if (depthStencilBuffer
->has_depth
) {
816 new_attachment
[SWR_ATTACHMENT_DEPTH
] = &depthStencilBuffer
->swr
;
818 if (depthStencilBuffer
->has_stencil
)
819 new_attachment
[SWR_ATTACHMENT_STENCIL
] =
820 &depthStencilBuffer
->secondary
;
822 } else if (depthStencilBuffer
->has_stencil
)
823 new_attachment
[SWR_ATTACHMENT_STENCIL
] = &depthStencilBuffer
->swr
;
826 /* Make the attachment updates */
827 swr_draw_context
*pDC
= &ctx
->swrDC
;
828 SWR_SURFACE_STATE
*renderTargets
= pDC
->renderTargets
;
829 unsigned need_fence
= FALSE
;
830 for (i
= 0; i
< SWR_NUM_ATTACHMENTS
; i
++) {
831 void *new_base
= nullptr;
832 if (new_attachment
[i
])
833 new_base
= new_attachment
[i
]->pBaseAddress
;
835 /* StoreTile for changed target */
836 if (renderTargets
[i
].pBaseAddress
!= new_base
) {
837 if (renderTargets
[i
].pBaseAddress
) {
838 /* If changing attachment to a new target, mark tiles as
839 * INVALID so they are reloaded from surface.
840 * If detaching attachment, mark tiles as RESOLVED so core
841 * won't try to load from non-existent target. */
842 enum SWR_TILE_STATE post_state
= (new_attachment
[i
]
843 ? SWR_TILE_INVALID
: SWR_TILE_RESOLVED
);
844 swr_store_render_target(pipe
, i
, post_state
);
849 /* Make new attachment */
850 if (new_attachment
[i
])
851 renderTargets
[i
] = *new_attachment
[i
];
853 if (renderTargets
[i
].pBaseAddress
)
854 renderTargets
[i
] = {0};
858 /* This fence ensures any attachment changes are resolved before the
861 swr_fence_submit(ctx
, screen
->flush_fence
);
865 if (ctx
->dirty
& (SWR_NEW_RASTERIZER
|
866 SWR_NEW_VS
| // clipping
867 SWR_NEW_FRAMEBUFFER
)) {
868 pipe_rasterizer_state
*rasterizer
= ctx
->rasterizer
;
869 pipe_framebuffer_state
*fb
= &ctx
->framebuffer
;
871 SWR_RASTSTATE
*rastState
= &ctx
->derived
.rastState
;
872 rastState
->cullMode
= swr_convert_cull_mode(rasterizer
->cull_face
);
873 rastState
->frontWinding
= rasterizer
->front_ccw
874 ? SWR_FRONTWINDING_CCW
875 : SWR_FRONTWINDING_CW
;
876 rastState
->scissorEnable
= rasterizer
->scissor
;
877 rastState
->pointSize
= rasterizer
->point_size
> 0.0f
878 ? rasterizer
->point_size
880 rastState
->lineWidth
= rasterizer
->line_width
> 0.0f
881 ? rasterizer
->line_width
884 rastState
->pointParam
= rasterizer
->point_size_per_vertex
;
886 rastState
->pointSpriteEnable
= rasterizer
->sprite_coord_enable
;
887 rastState
->pointSpriteTopOrigin
=
888 rasterizer
->sprite_coord_mode
== PIPE_SPRITE_COORD_UPPER_LEFT
;
890 /* XXX TODO: Add multisample */
891 rastState
->msaaRastEnable
= false;
892 rastState
->rastMode
= SWR_MSAA_RASTMODE_OFF_PIXEL
;
893 rastState
->sampleCount
= SWR_MULTISAMPLE_1X
;
894 rastState
->forcedSampleCount
= false;
896 bool do_offset
= false;
897 switch (rasterizer
->fill_front
) {
898 case PIPE_POLYGON_MODE_FILL
:
899 do_offset
= rasterizer
->offset_tri
;
901 case PIPE_POLYGON_MODE_LINE
:
902 do_offset
= rasterizer
->offset_line
;
904 case PIPE_POLYGON_MODE_POINT
:
905 do_offset
= rasterizer
->offset_point
;
910 rastState
->depthBias
= rasterizer
->offset_units
;
911 rastState
->slopeScaledDepthBias
= rasterizer
->offset_scale
;
912 rastState
->depthBiasClamp
= rasterizer
->offset_clamp
;
914 rastState
->depthBias
= 0;
915 rastState
->slopeScaledDepthBias
= 0;
916 rastState
->depthBiasClamp
= 0;
918 struct pipe_surface
*zb
= fb
->zsbuf
;
919 if (zb
&& swr_resource(zb
->texture
)->has_depth
)
920 rastState
->depthFormat
= swr_resource(zb
->texture
)->swr
.format
;
922 rastState
->depthClipEnable
= rasterizer
->depth_clip
;
924 rastState
->clipDistanceMask
=
925 ctx
->vs
->info
.base
.num_written_clipdistance
?
926 ctx
->vs
->info
.base
.clipdist_writemask
& rasterizer
->clip_plane_enable
:
927 rasterizer
->clip_plane_enable
;
929 rastState
->cullDistanceMask
=
930 ctx
->vs
->info
.base
.culldist_writemask
<< ctx
->vs
->info
.base
.num_written_clipdistance
;
932 SwrSetRastState(ctx
->swrContext
, rastState
);
936 if (ctx
->dirty
& SWR_NEW_SCISSOR
) {
937 SwrSetScissorRects(ctx
->swrContext
, 1, &ctx
->swr_scissor
);
941 if (ctx
->dirty
& (SWR_NEW_VIEWPORT
| SWR_NEW_FRAMEBUFFER
942 | SWR_NEW_RASTERIZER
)) {
943 pipe_viewport_state
*state
= &ctx
->viewport
;
944 pipe_framebuffer_state
*fb
= &ctx
->framebuffer
;
945 pipe_rasterizer_state
*rasterizer
= ctx
->rasterizer
;
947 SWR_VIEWPORT
*vp
= &ctx
->derived
.vp
;
948 SWR_VIEWPORT_MATRICES
*vpm
= &ctx
->derived
.vpm
;
950 vp
->x
= state
->translate
[0] - state
->scale
[0];
951 vp
->width
= state
->translate
[0] + state
->scale
[0];
952 vp
->y
= state
->translate
[1] - fabs(state
->scale
[1]);
953 vp
->height
= state
->translate
[1] + fabs(state
->scale
[1]);
954 if (rasterizer
->clip_halfz
== 0) {
955 vp
->minZ
= state
->translate
[2] - state
->scale
[2];
956 vp
->maxZ
= state
->translate
[2] + state
->scale
[2];
958 vp
->minZ
= state
->translate
[2];
959 vp
->maxZ
= state
->translate
[2] + state
->scale
[2];
962 vpm
->m00
[0] = state
->scale
[0];
963 vpm
->m11
[0] = state
->scale
[1];
964 vpm
->m22
[0] = state
->scale
[2];
965 vpm
->m30
[0] = state
->translate
[0];
966 vpm
->m31
[0] = state
->translate
[1];
967 vpm
->m32
[0] = state
->translate
[2];
969 /* Now that the matrix is calculated, clip the view coords to screen
970 * size. OpenGL allows for -ve x,y in the viewport. */
971 vp
->x
= std::max(vp
->x
, 0.0f
);
972 vp
->y
= std::max(vp
->y
, 0.0f
);
973 vp
->width
= std::min(vp
->width
, (float)fb
->width
);
974 vp
->height
= std::min(vp
->height
, (float)fb
->height
);
976 SwrSetViewports(ctx
->swrContext
, 1, vp
, vpm
);
979 /* Set vertex & index buffers */
980 /* (using draw info if called by swr_draw_vbo) */
981 if (ctx
->dirty
& SWR_NEW_VERTEX
) {
982 uint32_t size
, pitch
, max_vertex
, partial_inbounds
;
983 const uint8_t *p_data
;
985 /* If being called by swr_draw_vbo, copy draw details */
986 struct pipe_draw_info info
= {0};
991 SWR_VERTEX_BUFFER_STATE swrVertexBuffers
[PIPE_MAX_ATTRIBS
];
992 for (UINT i
= 0; i
< ctx
->num_vertex_buffers
; i
++) {
993 struct pipe_vertex_buffer
*vb
= &ctx
->vertex_buffer
[i
];
996 if (!vb
->user_buffer
) {
998 * size is based on buffer->width0 rather than info.max_index
999 * to prevent having to validate VBO on each draw */
1000 size
= vb
->buffer
->width0
;
1001 max_vertex
= size
/ pitch
;
1002 partial_inbounds
= size
% pitch
;
1004 p_data
= swr_resource_data(vb
->buffer
) + vb
->buffer_offset
;
1007 * client memory is one-time use, re-trigger SWR_NEW_VERTEX to
1008 * revalidate on each draw */
1009 post_update_dirty_flags
|= SWR_NEW_VERTEX
;
1012 size
= (info
.max_index
- info
.min_index
+ 1) * pitch
;
1014 /* pitch = 0, means constant value
1015 * set size to 1 vertex */
1016 size
= ctx
->velems
->stream_pitch
[i
];
1019 max_vertex
= info
.max_index
+ 1;
1020 partial_inbounds
= 0;
1022 /* Copy only needed vertices to scratch space */
1023 size
= AlignUp(size
, 4);
1024 const void *ptr
= (const uint8_t *) vb
->user_buffer
1025 + info
.min_index
* pitch
;
1026 ptr
= swr_copy_to_scratch_space(
1027 ctx
, &ctx
->scratch
->vertex_buffer
, ptr
, size
);
1028 p_data
= (const uint8_t *)ptr
- info
.min_index
* pitch
;
1031 swrVertexBuffers
[i
] = {0};
1032 swrVertexBuffers
[i
].index
= i
;
1033 swrVertexBuffers
[i
].pitch
= pitch
;
1034 swrVertexBuffers
[i
].pData
= p_data
;
1035 swrVertexBuffers
[i
].size
= size
;
1036 swrVertexBuffers
[i
].maxVertex
= max_vertex
;
1037 swrVertexBuffers
[i
].partialInboundsSize
= partial_inbounds
;
1040 SwrSetVertexBuffers(
1041 ctx
->swrContext
, ctx
->num_vertex_buffers
, swrVertexBuffers
);
1043 /* index buffer, if required (info passed in by swr_draw_vbo) */
1044 SWR_FORMAT index_type
= R32_UINT
; /* Default for non-indexed draws */
1046 struct pipe_index_buffer
*ib
= &ctx
->index_buffer
;
1048 pitch
= ib
->index_size
? ib
->index_size
: sizeof(uint32_t);
1049 index_type
= swr_convert_index_type(pitch
);
1051 if (!ib
->user_buffer
) {
1053 * size is based on buffer->width0 rather than info.count
1054 * to prevent having to validate VBO on each draw */
1055 size
= ib
->buffer
->width0
;
1056 p_data
= swr_resource_data(ib
->buffer
) + ib
->offset
;
1059 * client memory is one-time use, re-trigger SWR_NEW_VERTEX to
1060 * revalidate on each draw */
1061 post_update_dirty_flags
|= SWR_NEW_VERTEX
;
1063 size
= info
.count
* pitch
;
1064 size
= AlignUp(size
, 4);
1066 /* Copy indices to scratch space */
1067 const void *ptr
= ib
->user_buffer
;
1068 ptr
= swr_copy_to_scratch_space(
1069 ctx
, &ctx
->scratch
->index_buffer
, ptr
, size
);
1070 p_data
= (const uint8_t *)ptr
;
1073 SWR_INDEX_BUFFER_STATE swrIndexBuffer
;
1074 swrIndexBuffer
.format
= swr_convert_index_type(ib
->index_size
);
1075 swrIndexBuffer
.pIndices
= p_data
;
1076 swrIndexBuffer
.size
= size
;
1078 SwrSetIndexBuffer(ctx
->swrContext
, &swrIndexBuffer
);
1081 struct swr_vertex_element_state
*velems
= ctx
->velems
;
1082 if (velems
&& velems
->fsState
.indexType
!= index_type
) {
1083 velems
->fsFunc
= NULL
;
1084 velems
->fsState
.indexType
= index_type
;
1089 if (ctx
->dirty
& (SWR_NEW_VS
|
1090 SWR_NEW_RASTERIZER
| // for clip planes
1092 SWR_NEW_SAMPLER_VIEW
|
1093 SWR_NEW_FRAMEBUFFER
)) {
1095 swr_generate_vs_key(key
, ctx
, ctx
->vs
);
1096 auto search
= ctx
->vs
->map
.find(key
);
1097 PFN_VERTEX_FUNC func
;
1098 if (search
!= ctx
->vs
->map
.end()) {
1099 func
= search
->second
->shader
;
1101 func
= swr_compile_vs(ctx
, key
);
1103 SwrSetVertexFunc(ctx
->swrContext
, func
);
1105 /* JIT sampler state */
1106 if (ctx
->dirty
& SWR_NEW_SAMPLER
) {
1107 swr_update_sampler_state(ctx
,
1110 ctx
->swrDC
.samplersVS
);
1113 /* JIT sampler view state */
1114 if (ctx
->dirty
& (SWR_NEW_SAMPLER_VIEW
| SWR_NEW_FRAMEBUFFER
)) {
1115 swr_update_texture_state(ctx
,
1117 key
.nr_sampler_views
,
1118 ctx
->swrDC
.texturesVS
);
1122 /* FragmentShader */
1123 if (ctx
->dirty
& (SWR_NEW_FS
| SWR_NEW_SAMPLER
| SWR_NEW_SAMPLER_VIEW
1124 | SWR_NEW_RASTERIZER
| SWR_NEW_FRAMEBUFFER
)) {
1126 swr_generate_fs_key(key
, ctx
, ctx
->fs
);
1127 auto search
= ctx
->fs
->map
.find(key
);
1128 PFN_PIXEL_KERNEL func
;
1129 if (search
!= ctx
->fs
->map
.end()) {
1130 func
= search
->second
->shader
;
1132 func
= swr_compile_fs(ctx
, key
);
1134 SWR_PS_STATE psState
= {0};
1135 psState
.pfnPixelShader
= func
;
1136 psState
.killsPixel
= ctx
->fs
->info
.base
.uses_kill
;
1137 psState
.inputCoverage
= SWR_INPUT_COVERAGE_NORMAL
;
1138 psState
.writesODepth
= ctx
->fs
->info
.base
.writes_z
;
1139 psState
.usesSourceDepth
= ctx
->fs
->info
.base
.reads_z
;
1140 psState
.shadingRate
= SWR_SHADING_RATE_PIXEL
; // XXX
1141 psState
.numRenderTargets
= ctx
->framebuffer
.nr_cbufs
;
1142 psState
.posOffset
= SWR_PS_POSITION_SAMPLE_NONE
; // XXX msaa
1143 uint32_t barycentricsMask
= 0;
1145 // when we switch to mesa-master
1146 if (ctx
->fs
->info
.base
.uses_persp_center
||
1147 ctx
->fs
->info
.base
.uses_linear_center
)
1148 barycentricsMask
|= SWR_BARYCENTRIC_PER_PIXEL_MASK
;
1149 if (ctx
->fs
->info
.base
.uses_persp_centroid
||
1150 ctx
->fs
->info
.base
.uses_linear_centroid
)
1151 barycentricsMask
|= SWR_BARYCENTRIC_CENTROID_MASK
;
1152 if (ctx
->fs
->info
.base
.uses_persp_sample
||
1153 ctx
->fs
->info
.base
.uses_linear_sample
)
1154 barycentricsMask
|= SWR_BARYCENTRIC_PER_SAMPLE_MASK
;
1156 for (unsigned i
= 0; i
< ctx
->fs
->info
.base
.num_inputs
; i
++) {
1157 switch (ctx
->fs
->info
.base
.input_interpolate_loc
[i
]) {
1158 case TGSI_INTERPOLATE_LOC_CENTER
:
1159 barycentricsMask
|= SWR_BARYCENTRIC_PER_PIXEL_MASK
;
1161 case TGSI_INTERPOLATE_LOC_CENTROID
:
1162 barycentricsMask
|= SWR_BARYCENTRIC_CENTROID_MASK
;
1164 case TGSI_INTERPOLATE_LOC_SAMPLE
:
1165 barycentricsMask
|= SWR_BARYCENTRIC_PER_SAMPLE_MASK
;
1170 psState
.barycentricsMask
= barycentricsMask
;
1171 psState
.usesUAV
= false; // XXX
1172 psState
.forceEarlyZ
= false;
1173 SwrSetPixelShaderState(ctx
->swrContext
, &psState
);
1175 /* JIT sampler state */
1176 if (ctx
->dirty
& SWR_NEW_SAMPLER
) {
1177 swr_update_sampler_state(ctx
,
1178 PIPE_SHADER_FRAGMENT
,
1180 ctx
->swrDC
.samplersFS
);
1183 /* JIT sampler view state */
1184 if (ctx
->dirty
& (SWR_NEW_SAMPLER_VIEW
| SWR_NEW_FRAMEBUFFER
)) {
1185 swr_update_texture_state(ctx
,
1186 PIPE_SHADER_FRAGMENT
,
1187 key
.nr_sampler_views
,
1188 ctx
->swrDC
.texturesFS
);
1193 /* VertexShader Constants */
1194 if (ctx
->dirty
& SWR_NEW_VSCONSTANTS
) {
1195 swr_update_constants(ctx
, PIPE_SHADER_VERTEX
);
1198 /* FragmentShader Constants */
1199 if (ctx
->dirty
& SWR_NEW_FSCONSTANTS
) {
1200 swr_update_constants(ctx
, PIPE_SHADER_FRAGMENT
);
1203 /* Depth/stencil state */
1204 if (ctx
->dirty
& (SWR_NEW_DEPTH_STENCIL_ALPHA
| SWR_NEW_FRAMEBUFFER
)) {
1205 struct pipe_depth_state
*depth
= &(ctx
->depth_stencil
->depth
);
1206 struct pipe_stencil_state
*stencil
= ctx
->depth_stencil
->stencil
;
1207 SWR_DEPTH_STENCIL_STATE depthStencilState
= {{0}};
1209 /* XXX, incomplete. Need to flesh out stencil & alpha test state
1210 struct pipe_stencil_state *front_stencil =
1211 ctx->depth_stencil.stencil[0];
1212 struct pipe_stencil_state *back_stencil = ctx->depth_stencil.stencil[1];
1213 struct pipe_alpha_state alpha;
1215 if (stencil
[0].enabled
) {
1216 depthStencilState
.stencilWriteEnable
= 1;
1217 depthStencilState
.stencilTestEnable
= 1;
1218 depthStencilState
.stencilTestFunc
=
1219 swr_convert_depth_func(stencil
[0].func
);
1221 depthStencilState
.stencilPassDepthPassOp
=
1222 swr_convert_stencil_op(stencil
[0].zpass_op
);
1223 depthStencilState
.stencilPassDepthFailOp
=
1224 swr_convert_stencil_op(stencil
[0].zfail_op
);
1225 depthStencilState
.stencilFailOp
=
1226 swr_convert_stencil_op(stencil
[0].fail_op
);
1227 depthStencilState
.stencilWriteMask
= stencil
[0].writemask
;
1228 depthStencilState
.stencilTestMask
= stencil
[0].valuemask
;
1229 depthStencilState
.stencilRefValue
= ctx
->stencil_ref
.ref_value
[0];
1231 if (stencil
[1].enabled
) {
1232 depthStencilState
.doubleSidedStencilTestEnable
= 1;
1234 depthStencilState
.backfaceStencilTestFunc
=
1235 swr_convert_depth_func(stencil
[1].func
);
1237 depthStencilState
.backfaceStencilPassDepthPassOp
=
1238 swr_convert_stencil_op(stencil
[1].zpass_op
);
1239 depthStencilState
.backfaceStencilPassDepthFailOp
=
1240 swr_convert_stencil_op(stencil
[1].zfail_op
);
1241 depthStencilState
.backfaceStencilFailOp
=
1242 swr_convert_stencil_op(stencil
[1].fail_op
);
1243 depthStencilState
.backfaceStencilWriteMask
= stencil
[1].writemask
;
1244 depthStencilState
.backfaceStencilTestMask
= stencil
[1].valuemask
;
1246 depthStencilState
.backfaceStencilRefValue
=
1247 ctx
->stencil_ref
.ref_value
[1];
1250 depthStencilState
.depthTestEnable
= depth
->enabled
;
1251 depthStencilState
.depthTestFunc
= swr_convert_depth_func(depth
->func
);
1252 depthStencilState
.depthWriteEnable
= depth
->writemask
;
1253 SwrSetDepthStencilState(ctx
->swrContext
, &depthStencilState
);
1257 if (ctx
->dirty
& (SWR_NEW_BLEND
|
1258 SWR_NEW_FRAMEBUFFER
|
1259 SWR_NEW_DEPTH_STENCIL_ALPHA
)) {
1260 struct pipe_framebuffer_state
*fb
= &ctx
->framebuffer
;
1262 SWR_BLEND_STATE blendState
;
1263 memcpy(&blendState
, &ctx
->blend
->blendState
, sizeof(blendState
));
1264 blendState
.constantColor
[0] = ctx
->blend_color
.color
[0];
1265 blendState
.constantColor
[1] = ctx
->blend_color
.color
[1];
1266 blendState
.constantColor
[2] = ctx
->blend_color
.color
[2];
1267 blendState
.constantColor
[3] = ctx
->blend_color
.color
[3];
1268 blendState
.alphaTestReference
=
1269 *((uint32_t*)&ctx
->depth_stencil
->alpha
.ref_value
);
1272 blendState
.sampleMask
= 0;
1273 blendState
.sampleCount
= SWR_MULTISAMPLE_1X
;
1275 /* If there are no color buffers bound, disable writes on RT0
1277 if (fb
->nr_cbufs
== 0) {
1278 blendState
.renderTarget
[0].writeDisableRed
= 1;
1279 blendState
.renderTarget
[0].writeDisableGreen
= 1;
1280 blendState
.renderTarget
[0].writeDisableBlue
= 1;
1281 blendState
.renderTarget
[0].writeDisableAlpha
= 1;
1282 SwrSetBlendFunc(ctx
->swrContext
, 0, NULL
);
1285 for (int target
= 0;
1286 target
< std::min(SWR_NUM_RENDERTARGETS
,
1287 PIPE_MAX_COLOR_BUFS
);
1289 if (!fb
->cbufs
[target
])
1292 struct swr_resource
*colorBuffer
=
1293 swr_resource(fb
->cbufs
[target
]->texture
);
1295 BLEND_COMPILE_STATE compileState
;
1296 memset(&compileState
, 0, sizeof(compileState
));
1297 compileState
.format
= colorBuffer
->swr
.format
;
1298 memcpy(&compileState
.blendState
,
1299 &ctx
->blend
->compileState
[target
],
1300 sizeof(compileState
.blendState
));
1302 if (compileState
.blendState
.blendEnable
== false &&
1303 compileState
.blendState
.logicOpEnable
== false) {
1304 SwrSetBlendFunc(ctx
->swrContext
, target
, NULL
);
1308 compileState
.desc
.alphaTestEnable
=
1309 ctx
->depth_stencil
->alpha
.enabled
;
1310 compileState
.desc
.independentAlphaBlendEnable
=
1311 ctx
->blend
->pipe
.independent_blend_enable
;
1312 compileState
.desc
.alphaToCoverageEnable
=
1313 ctx
->blend
->pipe
.alpha_to_coverage
;
1314 compileState
.desc
.sampleMaskEnable
= 0; // XXX
1315 compileState
.desc
.numSamples
= 1; // XXX
1317 compileState
.alphaTestFunction
=
1318 swr_convert_depth_func(ctx
->depth_stencil
->alpha
.func
);
1319 compileState
.alphaTestFormat
= ALPHA_TEST_FLOAT32
; // xxx
1321 PFN_BLEND_JIT_FUNC func
= NULL
;
1322 auto search
= ctx
->blendJIT
->find(compileState
);
1323 if (search
!= ctx
->blendJIT
->end()) {
1324 func
= search
->second
;
1326 HANDLE hJitMgr
= screen
->hJitMgr
;
1327 func
= JitCompileBlend(hJitMgr
, compileState
);
1328 debug_printf("BLEND shader %p\n", func
);
1329 assert(func
&& "Error: BlendShader = NULL");
1331 ctx
->blendJIT
->insert(std::make_pair(compileState
, func
));
1333 SwrSetBlendFunc(ctx
->swrContext
, target
, func
);
1336 SwrSetBlendState(ctx
->swrContext
, &blendState
);
1339 if (ctx
->dirty
& SWR_NEW_STIPPLE
) {
1340 /* XXX What to do with this one??? SWR doesn't stipple */
1343 if (ctx
->dirty
& (SWR_NEW_VS
| SWR_NEW_SO
| SWR_NEW_RASTERIZER
)) {
1344 ctx
->vs
->soState
.rasterizerDisable
=
1345 ctx
->rasterizer
->rasterizer_discard
;
1346 SwrSetSoState(ctx
->swrContext
, &ctx
->vs
->soState
);
1348 pipe_stream_output_info
*stream_output
= &ctx
->vs
->pipe
.stream_output
;
1350 for (uint32_t i
= 0; i
< ctx
->num_so_targets
; i
++) {
1351 SWR_STREAMOUT_BUFFER buffer
= {0};
1352 if (!ctx
->so_targets
[i
])
1354 buffer
.enable
= true;
1356 (uint32_t *)swr_resource_data(ctx
->so_targets
[i
]->buffer
);
1357 buffer
.bufferSize
= ctx
->so_targets
[i
]->buffer_size
>> 2;
1358 buffer
.pitch
= stream_output
->stride
[i
];
1359 buffer
.streamOffset
= ctx
->so_targets
[i
]->buffer_offset
>> 2;
1361 SwrSetSoBuffers(ctx
->swrContext
, &buffer
, i
);
1365 if (ctx
->dirty
& SWR_NEW_CLIP
) {
1366 // shader exporting clip distances overrides all user clip planes
1367 if (ctx
->rasterizer
->clip_plane_enable
&&
1368 !ctx
->vs
->info
.base
.num_written_clipdistance
)
1370 swr_draw_context
*pDC
= &ctx
->swrDC
;
1371 memcpy(pDC
->userClipPlanes
,
1373 sizeof(pDC
->userClipPlanes
));
1377 // set up backend state
1378 SWR_BACKEND_STATE backendState
= {0};
1379 backendState
.numAttributes
=
1380 ctx
->vs
->info
.base
.num_outputs
- 1 +
1381 (ctx
->rasterizer
->sprite_coord_enable
? 1 : 0);
1382 for (unsigned i
= 0; i
< backendState
.numAttributes
; i
++)
1383 backendState
.numComponents
[i
] = 4;
1384 backendState
.constantInterpolationMask
=
1385 ctx
->rasterizer
->flatshade
?
1386 ctx
->fs
->flatConstantMask
:
1387 ctx
->fs
->constantMask
;
1388 backendState
.pointSpriteTexCoordMask
= ctx
->fs
->pointSpriteMask
;
1390 SwrSetBackendState(ctx
->swrContext
, &backendState
);
1392 /* Ensure that any in-progress attachment change StoreTiles finish */
1393 if (swr_is_fence_pending(screen
->flush_fence
))
1394 swr_fence_finish(pipe
->screen
, NULL
, screen
->flush_fence
, 0);
1396 /* Finally, update the in-use status of all resources involved in draw */
1397 swr_update_resource_status(pipe
, p_draw_info
);
1399 ctx
->dirty
= post_update_dirty_flags
;
1403 static struct pipe_stream_output_target
*
1404 swr_create_so_target(struct pipe_context
*pipe
,
1405 struct pipe_resource
*buffer
,
1406 unsigned buffer_offset
,
1407 unsigned buffer_size
)
1409 struct pipe_stream_output_target
*target
;
1411 target
= CALLOC_STRUCT(pipe_stream_output_target
);
1415 target
->context
= pipe
;
1416 target
->reference
.count
= 1;
1417 pipe_resource_reference(&target
->buffer
, buffer
);
1418 target
->buffer_offset
= buffer_offset
;
1419 target
->buffer_size
= buffer_size
;
1424 swr_destroy_so_target(struct pipe_context
*pipe
,
1425 struct pipe_stream_output_target
*target
)
1427 pipe_resource_reference(&target
->buffer
, NULL
);
1432 swr_set_so_targets(struct pipe_context
*pipe
,
1433 unsigned num_targets
,
1434 struct pipe_stream_output_target
**targets
,
1435 const unsigned *offsets
)
1437 struct swr_context
*swr
= swr_context(pipe
);
1440 assert(num_targets
< MAX_SO_STREAMS
);
1442 for (i
= 0; i
< num_targets
; i
++) {
1443 pipe_so_target_reference(
1444 (struct pipe_stream_output_target
**)&swr
->so_targets
[i
],
1448 for (/* fall-through */; i
< swr
->num_so_targets
; i
++) {
1449 pipe_so_target_reference(
1450 (struct pipe_stream_output_target
**)&swr
->so_targets
[i
], NULL
);
1453 swr
->num_so_targets
= num_targets
;
1455 swr
->dirty
= SWR_NEW_SO
;
1460 swr_state_init(struct pipe_context
*pipe
)
1462 pipe
->create_blend_state
= swr_create_blend_state
;
1463 pipe
->bind_blend_state
= swr_bind_blend_state
;
1464 pipe
->delete_blend_state
= swr_delete_blend_state
;
1466 pipe
->create_depth_stencil_alpha_state
= swr_create_depth_stencil_state
;
1467 pipe
->bind_depth_stencil_alpha_state
= swr_bind_depth_stencil_state
;
1468 pipe
->delete_depth_stencil_alpha_state
= swr_delete_depth_stencil_state
;
1470 pipe
->create_rasterizer_state
= swr_create_rasterizer_state
;
1471 pipe
->bind_rasterizer_state
= swr_bind_rasterizer_state
;
1472 pipe
->delete_rasterizer_state
= swr_delete_rasterizer_state
;
1474 pipe
->create_sampler_state
= swr_create_sampler_state
;
1475 pipe
->bind_sampler_states
= swr_bind_sampler_states
;
1476 pipe
->delete_sampler_state
= swr_delete_sampler_state
;
1478 pipe
->create_sampler_view
= swr_create_sampler_view
;
1479 pipe
->set_sampler_views
= swr_set_sampler_views
;
1480 pipe
->sampler_view_destroy
= swr_sampler_view_destroy
;
1482 pipe
->create_vs_state
= swr_create_vs_state
;
1483 pipe
->bind_vs_state
= swr_bind_vs_state
;
1484 pipe
->delete_vs_state
= swr_delete_vs_state
;
1486 pipe
->create_fs_state
= swr_create_fs_state
;
1487 pipe
->bind_fs_state
= swr_bind_fs_state
;
1488 pipe
->delete_fs_state
= swr_delete_fs_state
;
1490 pipe
->set_constant_buffer
= swr_set_constant_buffer
;
1492 pipe
->create_vertex_elements_state
= swr_create_vertex_elements_state
;
1493 pipe
->bind_vertex_elements_state
= swr_bind_vertex_elements_state
;
1494 pipe
->delete_vertex_elements_state
= swr_delete_vertex_elements_state
;
1496 pipe
->set_vertex_buffers
= swr_set_vertex_buffers
;
1497 pipe
->set_index_buffer
= swr_set_index_buffer
;
1499 pipe
->set_polygon_stipple
= swr_set_polygon_stipple
;
1500 pipe
->set_clip_state
= swr_set_clip_state
;
1501 pipe
->set_scissor_states
= swr_set_scissor_states
;
1502 pipe
->set_viewport_states
= swr_set_viewport_states
;
1504 pipe
->set_framebuffer_state
= swr_set_framebuffer_state
;
1506 pipe
->set_blend_color
= swr_set_blend_color
;
1507 pipe
->set_stencil_ref
= swr_set_stencil_ref
;
1509 pipe
->set_sample_mask
= swr_set_sample_mask
;
1511 pipe
->create_stream_output_target
= swr_create_so_target
;
1512 pipe
->stream_output_target_destroy
= swr_destroy_so_target
;
1513 pipe
->set_stream_output_targets
= swr_set_so_targets
;