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 #include "common/os.h"
26 #include "JitManager.h"
27 #include "state_llvm.h"
29 #include "gallivm/lp_bld_tgsi.h"
30 #include "util/u_format.h"
32 #include "util/u_memory.h"
33 #include "util/u_inlines.h"
34 #include "util/u_helpers.h"
35 #include "util/u_framebuffer.h"
37 #include "swr_state.h"
38 #include "swr_context.h"
39 #include "swr_context_llvm.h"
40 #include "swr_screen.h"
41 #include "swr_resource.h"
42 #include "swr_tex_sample.h"
43 #include "swr_scratch.h"
44 #include "swr_shader.h"
45 #include "swr_fence.h"
47 /* These should be pulled out into separate files as necessary
48 * Just initializing everything here to get going. */
51 swr_create_blend_state(struct pipe_context
*pipe
,
52 const struct pipe_blend_state
*blend
)
54 struct swr_blend_state
*state
= CALLOC_STRUCT(swr_blend_state
);
56 memcpy(&state
->pipe
, blend
, sizeof(*blend
));
58 struct pipe_blend_state
*pipe_blend
= &state
->pipe
;
61 target
< std::min(SWR_NUM_RENDERTARGETS
, PIPE_MAX_COLOR_BUFS
);
64 struct pipe_rt_blend_state
*rt_blend
= &pipe_blend
->rt
[target
];
65 SWR_RENDER_TARGET_BLEND_STATE
&blendState
=
66 state
->blendState
.renderTarget
[target
];
67 RENDER_TARGET_BLEND_COMPILE_STATE
&compileState
=
68 state
->compileState
[target
];
70 if (target
!= 0 && !pipe_blend
->independent_blend_enable
) {
72 &state
->compileState
[0],
73 sizeof(RENDER_TARGET_BLEND_COMPILE_STATE
));
77 compileState
.blendEnable
= rt_blend
->blend_enable
;
78 if (compileState
.blendEnable
) {
79 compileState
.sourceAlphaBlendFactor
=
80 swr_convert_blend_factor(rt_blend
->alpha_src_factor
);
81 compileState
.destAlphaBlendFactor
=
82 swr_convert_blend_factor(rt_blend
->alpha_dst_factor
);
83 compileState
.sourceBlendFactor
=
84 swr_convert_blend_factor(rt_blend
->rgb_src_factor
);
85 compileState
.destBlendFactor
=
86 swr_convert_blend_factor(rt_blend
->rgb_dst_factor
);
88 compileState
.colorBlendFunc
=
89 swr_convert_blend_func(rt_blend
->rgb_func
);
90 compileState
.alphaBlendFunc
=
91 swr_convert_blend_func(rt_blend
->alpha_func
);
93 compileState
.logicOpEnable
= state
->pipe
.logicop_enable
;
94 if (compileState
.logicOpEnable
) {
95 compileState
.logicOpFunc
=
96 swr_convert_logic_op(state
->pipe
.logicop_func
);
99 blendState
.writeDisableRed
=
100 (rt_blend
->colormask
& PIPE_MASK_R
) ? 0 : 1;
101 blendState
.writeDisableGreen
=
102 (rt_blend
->colormask
& PIPE_MASK_G
) ? 0 : 1;
103 blendState
.writeDisableBlue
=
104 (rt_blend
->colormask
& PIPE_MASK_B
) ? 0 : 1;
105 blendState
.writeDisableAlpha
=
106 (rt_blend
->colormask
& PIPE_MASK_A
) ? 0 : 1;
108 if (rt_blend
->colormask
== 0)
109 compileState
.blendEnable
= false;
116 swr_bind_blend_state(struct pipe_context
*pipe
, void *blend
)
118 struct swr_context
*ctx
= swr_context(pipe
);
120 if (ctx
->blend
== blend
)
123 ctx
->blend
= (swr_blend_state
*)blend
;
125 ctx
->dirty
|= SWR_NEW_BLEND
;
129 swr_delete_blend_state(struct pipe_context
*pipe
, void *blend
)
135 swr_set_blend_color(struct pipe_context
*pipe
,
136 const struct pipe_blend_color
*color
)
138 struct swr_context
*ctx
= swr_context(pipe
);
140 ctx
->blend_color
= *color
;
142 ctx
->dirty
|= SWR_NEW_BLEND
;
146 swr_set_stencil_ref(struct pipe_context
*pipe
,
147 const struct pipe_stencil_ref
*ref
)
149 struct swr_context
*ctx
= swr_context(pipe
);
151 ctx
->stencil_ref
= *ref
;
153 ctx
->dirty
|= SWR_NEW_DEPTH_STENCIL_ALPHA
;
157 swr_create_depth_stencil_state(
158 struct pipe_context
*pipe
,
159 const struct pipe_depth_stencil_alpha_state
*depth_stencil
)
161 struct pipe_depth_stencil_alpha_state
*state
;
163 state
= (pipe_depth_stencil_alpha_state
*)mem_dup(depth_stencil
,
164 sizeof *depth_stencil
);
170 swr_bind_depth_stencil_state(struct pipe_context
*pipe
, void *depth_stencil
)
172 struct swr_context
*ctx
= swr_context(pipe
);
174 if (ctx
->depth_stencil
== (pipe_depth_stencil_alpha_state
*)depth_stencil
)
177 ctx
->depth_stencil
= (pipe_depth_stencil_alpha_state
*)depth_stencil
;
179 ctx
->dirty
|= SWR_NEW_DEPTH_STENCIL_ALPHA
;
183 swr_delete_depth_stencil_state(struct pipe_context
*pipe
, void *depth
)
190 swr_create_rasterizer_state(struct pipe_context
*pipe
,
191 const struct pipe_rasterizer_state
*rast
)
193 struct pipe_rasterizer_state
*state
;
194 state
= (pipe_rasterizer_state
*)mem_dup(rast
, sizeof *rast
);
200 swr_bind_rasterizer_state(struct pipe_context
*pipe
, void *handle
)
202 struct swr_context
*ctx
= swr_context(pipe
);
203 const struct pipe_rasterizer_state
*rasterizer
=
204 (const struct pipe_rasterizer_state
*)handle
;
206 if (ctx
->rasterizer
== (pipe_rasterizer_state
*)rasterizer
)
209 ctx
->rasterizer
= (pipe_rasterizer_state
*)rasterizer
;
211 ctx
->dirty
|= SWR_NEW_RASTERIZER
;
215 swr_delete_rasterizer_state(struct pipe_context
*pipe
, void *rasterizer
)
222 swr_create_sampler_state(struct pipe_context
*pipe
,
223 const struct pipe_sampler_state
*sampler
)
225 struct pipe_sampler_state
*state
=
226 (pipe_sampler_state
*)mem_dup(sampler
, sizeof *sampler
);
232 swr_bind_sampler_states(struct pipe_context
*pipe
,
238 struct swr_context
*ctx
= swr_context(pipe
);
241 assert(shader
< PIPE_SHADER_TYPES
);
242 assert(start
+ num
<= Elements(ctx
->samplers
[shader
]));
244 /* set the new samplers */
245 ctx
->num_samplers
[shader
] = num
;
246 for (i
= 0; i
< num
; i
++) {
247 ctx
->samplers
[shader
][start
+ i
] = (pipe_sampler_state
*)samplers
[i
];
250 ctx
->dirty
|= SWR_NEW_SAMPLER
;
254 swr_delete_sampler_state(struct pipe_context
*pipe
, void *sampler
)
260 static struct pipe_sampler_view
*
261 swr_create_sampler_view(struct pipe_context
*pipe
,
262 struct pipe_resource
*texture
,
263 const struct pipe_sampler_view
*templ
)
265 struct pipe_sampler_view
*view
= CALLOC_STRUCT(pipe_sampler_view
);
269 view
->reference
.count
= 1;
270 view
->texture
= NULL
;
271 pipe_resource_reference(&view
->texture
, texture
);
272 view
->context
= pipe
;
279 swr_set_sampler_views(struct pipe_context
*pipe
,
283 struct pipe_sampler_view
**views
)
285 struct swr_context
*ctx
= swr_context(pipe
);
288 assert(num
<= PIPE_MAX_SHADER_SAMPLER_VIEWS
);
290 assert(shader
< PIPE_SHADER_TYPES
);
291 assert(start
+ num
<= Elements(ctx
->sampler_views
[shader
]));
293 /* set the new sampler views */
294 ctx
->num_sampler_views
[shader
] = num
;
295 for (i
= 0; i
< num
; i
++) {
296 /* Note: we're using pipe_sampler_view_release() here to work around
297 * a possible crash when the old view belongs to another context that
298 * was already destroyed.
300 pipe_sampler_view_release(pipe
, &ctx
->sampler_views
[shader
][start
+ i
]);
301 pipe_sampler_view_reference(&ctx
->sampler_views
[shader
][start
+ i
],
305 ctx
->dirty
|= SWR_NEW_SAMPLER_VIEW
;
309 swr_sampler_view_destroy(struct pipe_context
*pipe
,
310 struct pipe_sampler_view
*view
)
312 pipe_resource_reference(&view
->texture
, NULL
);
317 swr_create_vs_state(struct pipe_context
*pipe
,
318 const struct pipe_shader_state
*vs
)
320 struct swr_vertex_shader
*swr_vs
=
321 (swr_vertex_shader
*)CALLOC_STRUCT(swr_vertex_shader
);
325 swr_vs
->pipe
.tokens
= tgsi_dup_tokens(vs
->tokens
);
326 swr_vs
->pipe
.stream_output
= vs
->stream_output
;
328 lp_build_tgsi_info(vs
->tokens
, &swr_vs
->info
);
330 swr_vs
->func
= swr_compile_vs(pipe
, swr_vs
);
332 swr_vs
->soState
= {0};
334 if (swr_vs
->pipe
.stream_output
.num_outputs
) {
335 pipe_stream_output_info
*stream_output
= &swr_vs
->pipe
.stream_output
;
337 swr_vs
->soState
.soEnable
= true;
338 // soState.rasterizerDisable set on state dirty
339 // soState.streamToRasterizer not used
341 for (uint32_t i
= 0; i
< stream_output
->num_outputs
; i
++) {
342 swr_vs
->soState
.streamMasks
[stream_output
->output
[i
].stream
] |=
343 1 << (stream_output
->output
[i
].register_index
- 1);
345 for (uint32_t i
= 0; i
< MAX_SO_STREAMS
; i
++) {
346 swr_vs
->soState
.streamNumEntries
[i
] =
347 _mm_popcnt_u32(swr_vs
->soState
.streamMasks
[i
]);
355 swr_bind_vs_state(struct pipe_context
*pipe
, void *vs
)
357 struct swr_context
*ctx
= swr_context(pipe
);
362 ctx
->vs
= (swr_vertex_shader
*)vs
;
363 ctx
->dirty
|= SWR_NEW_VS
;
367 swr_delete_vs_state(struct pipe_context
*pipe
, void *vs
)
369 struct swr_vertex_shader
*swr_vs
= (swr_vertex_shader
*)vs
;
370 FREE((void *)swr_vs
->pipe
.tokens
);
375 swr_create_fs_state(struct pipe_context
*pipe
,
376 const struct pipe_shader_state
*fs
)
378 struct swr_fragment_shader
*swr_fs
= new swr_fragment_shader
;
382 swr_fs
->pipe
.tokens
= tgsi_dup_tokens(fs
->tokens
);
384 lp_build_tgsi_info(fs
->tokens
, &swr_fs
->info
);
391 swr_bind_fs_state(struct pipe_context
*pipe
, void *fs
)
393 struct swr_context
*ctx
= swr_context(pipe
);
398 ctx
->fs
= (swr_fragment_shader
*)fs
;
399 ctx
->dirty
|= SWR_NEW_FS
;
403 swr_delete_fs_state(struct pipe_context
*pipe
, void *fs
)
405 struct swr_fragment_shader
*swr_fs
= (swr_fragment_shader
*)fs
;
406 FREE((void *)swr_fs
->pipe
.tokens
);
412 swr_set_constant_buffer(struct pipe_context
*pipe
,
415 struct pipe_constant_buffer
*cb
)
417 struct swr_context
*ctx
= swr_context(pipe
);
418 struct pipe_resource
*constants
= cb
? cb
->buffer
: NULL
;
420 assert(shader
< PIPE_SHADER_TYPES
);
421 assert(index
< Elements(ctx
->constants
[shader
]));
423 /* note: reference counting */
424 util_copy_constant_buffer(&ctx
->constants
[shader
][index
], cb
);
426 if (shader
== PIPE_SHADER_VERTEX
|| shader
== PIPE_SHADER_GEOMETRY
) {
427 ctx
->dirty
|= SWR_NEW_VSCONSTANTS
;
428 } else if (shader
== PIPE_SHADER_FRAGMENT
) {
429 ctx
->dirty
|= SWR_NEW_FSCONSTANTS
;
432 if (cb
&& cb
->user_buffer
) {
433 pipe_resource_reference(&constants
, NULL
);
439 swr_create_vertex_elements_state(struct pipe_context
*pipe
,
440 unsigned num_elements
,
441 const struct pipe_vertex_element
*attribs
)
443 struct swr_vertex_element_state
*velems
;
444 assert(num_elements
<= PIPE_MAX_ATTRIBS
);
445 velems
= CALLOC_STRUCT(swr_vertex_element_state
);
447 velems
->fsState
.numAttribs
= num_elements
;
448 for (unsigned i
= 0; i
< num_elements
; i
++) {
449 // XXX: we should do this keyed on the VS usage info
451 const struct util_format_description
*desc
=
452 util_format_description(attribs
[i
].src_format
);
454 velems
->fsState
.layout
[i
].AlignedByteOffset
= attribs
[i
].src_offset
;
455 velems
->fsState
.layout
[i
].Format
=
456 mesa_to_swr_format(attribs
[i
].src_format
);
457 velems
->fsState
.layout
[i
].StreamIndex
=
458 attribs
[i
].vertex_buffer_index
;
459 velems
->fsState
.layout
[i
].InstanceEnable
=
460 attribs
[i
].instance_divisor
!= 0;
461 velems
->fsState
.layout
[i
].ComponentControl0
=
462 desc
->channel
[0].type
!= UTIL_FORMAT_TYPE_VOID
463 ? ComponentControl::StoreSrc
464 : ComponentControl::Store0
;
465 velems
->fsState
.layout
[i
].ComponentControl1
=
466 desc
->channel
[1].type
!= UTIL_FORMAT_TYPE_VOID
467 ? ComponentControl::StoreSrc
468 : ComponentControl::Store0
;
469 velems
->fsState
.layout
[i
].ComponentControl2
=
470 desc
->channel
[2].type
!= UTIL_FORMAT_TYPE_VOID
471 ? ComponentControl::StoreSrc
472 : ComponentControl::Store0
;
473 velems
->fsState
.layout
[i
].ComponentControl3
=
474 desc
->channel
[3].type
!= UTIL_FORMAT_TYPE_VOID
475 ? ComponentControl::StoreSrc
476 : ComponentControl::Store1Fp
;
477 velems
->fsState
.layout
[i
].ComponentPacking
= ComponentEnable::XYZW
;
478 velems
->fsState
.layout
[i
].InstanceDataStepRate
=
479 attribs
[i
].instance_divisor
;
481 /* Calculate the pitch of each stream */
482 const SWR_FORMAT_INFO
&swr_desc
= GetFormatInfo(
483 mesa_to_swr_format(attribs
[i
].src_format
));
484 velems
->stream_pitch
[attribs
[i
].vertex_buffer_index
] += swr_desc
.Bpp
;
492 swr_bind_vertex_elements_state(struct pipe_context
*pipe
, void *velems
)
494 struct swr_context
*ctx
= swr_context(pipe
);
495 struct swr_vertex_element_state
*swr_velems
=
496 (struct swr_vertex_element_state
*)velems
;
498 ctx
->velems
= swr_velems
;
499 ctx
->dirty
|= SWR_NEW_VERTEX
;
503 swr_delete_vertex_elements_state(struct pipe_context
*pipe
, void *velems
)
505 /* XXX Need to destroy fetch shader? */
511 swr_set_vertex_buffers(struct pipe_context
*pipe
,
513 unsigned num_elements
,
514 const struct pipe_vertex_buffer
*buffers
)
516 struct swr_context
*ctx
= swr_context(pipe
);
518 assert(num_elements
<= PIPE_MAX_ATTRIBS
);
520 util_set_vertex_buffers_count(ctx
->vertex_buffer
,
521 &ctx
->num_vertex_buffers
,
526 ctx
->dirty
|= SWR_NEW_VERTEX
;
531 swr_set_index_buffer(struct pipe_context
*pipe
,
532 const struct pipe_index_buffer
*ib
)
534 struct swr_context
*ctx
= swr_context(pipe
);
537 memcpy(&ctx
->index_buffer
, ib
, sizeof(ctx
->index_buffer
));
539 memset(&ctx
->index_buffer
, 0, sizeof(ctx
->index_buffer
));
541 ctx
->dirty
|= SWR_NEW_VERTEX
;
545 swr_set_polygon_stipple(struct pipe_context
*pipe
,
546 const struct pipe_poly_stipple
*stipple
)
548 struct swr_context
*ctx
= swr_context(pipe
);
550 ctx
->poly_stipple
= *stipple
; /* struct copy */
551 ctx
->dirty
|= SWR_NEW_STIPPLE
;
555 swr_set_clip_state(struct pipe_context
*pipe
,
556 const struct pipe_clip_state
*clip
)
558 struct swr_context
*ctx
= swr_context(pipe
);
561 /* XXX Unimplemented, but prevents crash */
563 ctx
->dirty
|= SWR_NEW_CLIP
;
568 swr_set_scissor_states(struct pipe_context
*pipe
,
570 unsigned num_viewports
,
571 const struct pipe_scissor_state
*scissor
)
573 struct swr_context
*ctx
= swr_context(pipe
);
575 ctx
->scissor
= *scissor
;
576 ctx
->dirty
|= SWR_NEW_SCISSOR
;
580 swr_set_viewport_states(struct pipe_context
*pipe
,
582 unsigned num_viewports
,
583 const struct pipe_viewport_state
*vpt
)
585 struct swr_context
*ctx
= swr_context(pipe
);
587 ctx
->viewport
= *vpt
;
588 ctx
->dirty
|= SWR_NEW_VIEWPORT
;
593 swr_set_framebuffer_state(struct pipe_context
*pipe
,
594 const struct pipe_framebuffer_state
*fb
)
596 struct swr_context
*ctx
= swr_context(pipe
);
598 boolean changed
= !util_framebuffer_state_equal(&ctx
->framebuffer
, fb
);
600 assert(fb
->width
<= KNOB_GUARDBAND_WIDTH
);
601 assert(fb
->height
<= KNOB_GUARDBAND_HEIGHT
);
605 for (i
= 0; i
< fb
->nr_cbufs
; ++i
)
606 pipe_surface_reference(&ctx
->framebuffer
.cbufs
[i
], fb
->cbufs
[i
]);
607 for (; i
< ctx
->framebuffer
.nr_cbufs
; ++i
)
608 pipe_surface_reference(&ctx
->framebuffer
.cbufs
[i
], NULL
);
610 ctx
->framebuffer
.nr_cbufs
= fb
->nr_cbufs
;
612 ctx
->framebuffer
.width
= fb
->width
;
613 ctx
->framebuffer
.height
= fb
->height
;
615 pipe_surface_reference(&ctx
->framebuffer
.zsbuf
, fb
->zsbuf
);
617 ctx
->dirty
|= SWR_NEW_FRAMEBUFFER
;
623 swr_set_sample_mask(struct pipe_context
*pipe
, unsigned sample_mask
)
625 struct swr_context
*ctx
= swr_context(pipe
);
627 if (sample_mask
!= ctx
->sample_mask
) {
628 ctx
->sample_mask
= sample_mask
;
629 ctx
->dirty
|= SWR_NEW_RASTERIZER
;
634 * Update resource in-use status
635 * All resources bound to color or depth targets marked as WRITE resources.
636 * VBO Vertex/index buffers and texture views marked as READ resources.
639 swr_update_resource_status(struct pipe_context
*pipe
,
640 const struct pipe_draw_info
*p_draw_info
)
642 struct swr_context
*ctx
= swr_context(pipe
);
643 struct pipe_framebuffer_state
*fb
= &ctx
->framebuffer
;
645 /* colorbuffer targets */
647 for (uint32_t i
= 0; i
< fb
->nr_cbufs
; ++i
)
649 swr_resource_write(pipe
, swr_resource(fb
->cbufs
[i
]->texture
));
651 /* depth/stencil target */
653 swr_resource_write(pipe
, swr_resource(fb
->zsbuf
->texture
));
655 /* VBO vertex buffers */
656 for (uint32_t i
= 0; i
< ctx
->num_vertex_buffers
; i
++) {
657 struct pipe_vertex_buffer
*vb
= &ctx
->vertex_buffer
[i
];
658 if (!vb
->user_buffer
)
659 swr_resource_read(pipe
, swr_resource(vb
->buffer
));
662 /* VBO index buffer */
663 if (p_draw_info
&& p_draw_info
->indexed
) {
664 struct pipe_index_buffer
*ib
= &ctx
->index_buffer
;
665 if (!ib
->user_buffer
)
666 swr_resource_read(pipe
, swr_resource(ib
->buffer
));
669 /* texture sampler views */
670 for (uint32_t i
= 0; i
< PIPE_MAX_SHADER_SAMPLER_VIEWS
; i
++) {
671 struct pipe_sampler_view
*view
=
672 ctx
->sampler_views
[PIPE_SHADER_FRAGMENT
][i
];
674 swr_resource_read(pipe
, swr_resource(view
->texture
));
679 swr_update_derived(struct pipe_context
*pipe
,
680 const struct pipe_draw_info
*p_draw_info
)
682 struct swr_context
*ctx
= swr_context(pipe
);
683 struct swr_screen
*screen
= swr_screen(ctx
->pipe
.screen
);
685 /* Any state that requires dirty flags to be re-triggered sets this mask */
686 /* For example, user_buffer vertex and index buffers. */
687 unsigned post_update_dirty_flags
= 0;
690 if (ctx
->dirty
& SWR_NEW_FRAMEBUFFER
) {
691 struct pipe_framebuffer_state
*fb
= &ctx
->framebuffer
;
692 SWR_SURFACE_STATE
*new_attachment
[SWR_NUM_ATTACHMENTS
] = {0};
695 /* colorbuffer targets */
697 for (i
= 0; i
< fb
->nr_cbufs
; ++i
)
699 struct swr_resource
*colorBuffer
=
700 swr_resource(fb
->cbufs
[i
]->texture
);
701 new_attachment
[SWR_ATTACHMENT_COLOR0
+ i
] = &colorBuffer
->swr
;
704 /* depth/stencil target */
706 struct swr_resource
*depthStencilBuffer
=
707 swr_resource(fb
->zsbuf
->texture
);
708 if (depthStencilBuffer
->has_depth
) {
709 new_attachment
[SWR_ATTACHMENT_DEPTH
] = &depthStencilBuffer
->swr
;
711 if (depthStencilBuffer
->has_stencil
)
712 new_attachment
[SWR_ATTACHMENT_STENCIL
] =
713 &depthStencilBuffer
->secondary
;
715 } else if (depthStencilBuffer
->has_stencil
)
716 new_attachment
[SWR_ATTACHMENT_STENCIL
] = &depthStencilBuffer
->swr
;
719 /* Make the attachment updates */
720 swr_draw_context
*pDC
= &ctx
->swrDC
;
721 SWR_SURFACE_STATE
*renderTargets
= pDC
->renderTargets
;
722 unsigned need_fence
= FALSE
;
723 for (i
= 0; i
< SWR_NUM_ATTACHMENTS
; i
++) {
724 void *new_base
= nullptr;
725 if (new_attachment
[i
])
726 new_base
= new_attachment
[i
]->pBaseAddress
;
728 /* StoreTile for changed target */
729 if (renderTargets
[i
].pBaseAddress
!= new_base
) {
730 if (renderTargets
[i
].pBaseAddress
) {
731 /* If changing attachment to a new target, mark tiles as
732 * INVALID so they are reloaded from surface.
733 * If detaching attachment, mark tiles as RESOLVED so core
734 * won't try to load from non-existent target. */
735 enum SWR_TILE_STATE post_state
= (new_attachment
[i
]
736 ? SWR_TILE_INVALID
: SWR_TILE_RESOLVED
);
737 swr_store_render_target(pipe
, i
, post_state
);
742 /* Make new attachment */
743 if (new_attachment
[i
])
744 renderTargets
[i
] = *new_attachment
[i
];
746 if (renderTargets
[i
].pBaseAddress
)
747 renderTargets
[i
] = {0};
751 /* This fence ensures any attachment changes are resolved before the
754 swr_fence_submit(ctx
, screen
->flush_fence
);
758 if (ctx
->dirty
& (SWR_NEW_RASTERIZER
| SWR_NEW_FRAMEBUFFER
)) {
759 pipe_rasterizer_state
*rasterizer
= ctx
->rasterizer
;
760 pipe_framebuffer_state
*fb
= &ctx
->framebuffer
;
762 SWR_RASTSTATE
*rastState
= &ctx
->derived
.rastState
;
763 rastState
->cullMode
= swr_convert_cull_mode(rasterizer
->cull_face
);
764 rastState
->frontWinding
= rasterizer
->front_ccw
765 ? SWR_FRONTWINDING_CCW
766 : SWR_FRONTWINDING_CW
;
767 rastState
->scissorEnable
= rasterizer
->scissor
;
768 rastState
->pointSize
= rasterizer
->point_size
> 0.0f
769 ? rasterizer
->point_size
771 rastState
->lineWidth
= rasterizer
->line_width
> 0.0f
772 ? rasterizer
->line_width
775 rastState
->pointParam
= rasterizer
->point_size_per_vertex
;
777 rastState
->pointSpriteEnable
= rasterizer
->sprite_coord_enable
;
778 rastState
->pointSpriteTopOrigin
=
779 rasterizer
->sprite_coord_mode
== PIPE_SPRITE_COORD_UPPER_LEFT
;
781 /* XXX TODO: Add multisample */
782 rastState
->msaaRastEnable
= false;
783 rastState
->rastMode
= SWR_MSAA_RASTMODE_OFF_PIXEL
;
784 rastState
->sampleCount
= SWR_MULTISAMPLE_1X
;
785 rastState
->bForcedSampleCount
= false;
787 bool do_offset
= false;
788 switch (rasterizer
->fill_front
) {
789 case PIPE_POLYGON_MODE_FILL
:
790 do_offset
= rasterizer
->offset_tri
;
792 case PIPE_POLYGON_MODE_LINE
:
793 do_offset
= rasterizer
->offset_line
;
795 case PIPE_POLYGON_MODE_POINT
:
796 do_offset
= rasterizer
->offset_point
;
801 rastState
->depthBias
= rasterizer
->offset_units
;
802 rastState
->slopeScaledDepthBias
= rasterizer
->offset_scale
;
803 rastState
->depthBiasClamp
= rasterizer
->offset_clamp
;
805 rastState
->depthBias
= 0;
806 rastState
->slopeScaledDepthBias
= 0;
807 rastState
->depthBiasClamp
= 0;
809 struct pipe_surface
*zb
= fb
->zsbuf
;
810 if (zb
&& swr_resource(zb
->texture
)->has_depth
)
811 rastState
->depthFormat
= swr_resource(zb
->texture
)->swr
.format
;
813 rastState
->depthClipEnable
= rasterizer
->depth_clip
;
815 SwrSetRastState(ctx
->swrContext
, rastState
);
819 if (ctx
->dirty
& SWR_NEW_SCISSOR
) {
820 pipe_scissor_state
*scissor
= &ctx
->scissor
;
821 BBOX
bbox(scissor
->miny
, scissor
->maxy
,
822 scissor
->minx
, scissor
->maxx
);
823 SwrSetScissorRects(ctx
->swrContext
, 1, &bbox
);
827 if (ctx
->dirty
& (SWR_NEW_VIEWPORT
| SWR_NEW_FRAMEBUFFER
828 | SWR_NEW_RASTERIZER
)) {
829 pipe_viewport_state
*state
= &ctx
->viewport
;
830 pipe_framebuffer_state
*fb
= &ctx
->framebuffer
;
831 pipe_rasterizer_state
*rasterizer
= ctx
->rasterizer
;
833 SWR_VIEWPORT
*vp
= &ctx
->derived
.vp
;
834 SWR_VIEWPORT_MATRIX
*vpm
= &ctx
->derived
.vpm
;
836 vp
->x
= state
->translate
[0] - state
->scale
[0];
837 vp
->width
= state
->translate
[0] + state
->scale
[0];
838 vp
->y
= state
->translate
[1] - fabs(state
->scale
[1]);
839 vp
->height
= state
->translate
[1] + fabs(state
->scale
[1]);
840 if (rasterizer
->clip_halfz
== 0) {
841 vp
->minZ
= state
->translate
[2] - state
->scale
[2];
842 vp
->maxZ
= state
->translate
[2] + state
->scale
[2];
844 vp
->minZ
= state
->translate
[2];
845 vp
->maxZ
= state
->translate
[2] + state
->scale
[2];
848 vpm
->m00
= state
->scale
[0];
849 vpm
->m11
= state
->scale
[1];
850 vpm
->m22
= state
->scale
[2];
851 vpm
->m30
= state
->translate
[0];
852 vpm
->m31
= state
->translate
[1];
853 vpm
->m32
= state
->translate
[2];
855 /* Now that the matrix is calculated, clip the view coords to screen
856 * size. OpenGL allows for -ve x,y in the viewport. */
857 vp
->x
= std::max(vp
->x
, 0.0f
);
858 vp
->y
= std::max(vp
->y
, 0.0f
);
859 vp
->width
= std::min(vp
->width
, (float)fb
->width
);
860 vp
->height
= std::min(vp
->height
, (float)fb
->height
);
862 SwrSetViewports(ctx
->swrContext
, 1, vp
, vpm
);
865 /* Set vertex & index buffers */
866 /* (using draw info if called by swr_draw_vbo) */
867 if (ctx
->dirty
& SWR_NEW_VERTEX
) {
868 uint32_t size
, pitch
, max_vertex
, partial_inbounds
;
869 const uint8_t *p_data
;
871 /* If being called by swr_draw_vbo, copy draw details */
872 struct pipe_draw_info info
= {0};
877 SWR_VERTEX_BUFFER_STATE swrVertexBuffers
[PIPE_MAX_ATTRIBS
];
878 for (UINT i
= 0; i
< ctx
->num_vertex_buffers
; i
++) {
879 struct pipe_vertex_buffer
*vb
= &ctx
->vertex_buffer
[i
];
882 if (!vb
->user_buffer
) {
884 * size is based on buffer->width0 rather than info.max_index
885 * to prevent having to validate VBO on each draw */
886 size
= vb
->buffer
->width0
;
887 max_vertex
= size
/ pitch
;
888 partial_inbounds
= size
% pitch
;
890 p_data
= (const uint8_t *)swr_resource_data(vb
->buffer
)
894 * client memory is one-time use, re-trigger SWR_NEW_VERTEX to
895 * revalidate on each draw */
896 post_update_dirty_flags
|= SWR_NEW_VERTEX
;
899 size
= (info
.max_index
- info
.min_index
+ 1) * pitch
;
901 /* pitch = 0, means constant value
902 * set size to 1 vertex */
903 size
= ctx
->velems
->stream_pitch
[i
];
906 max_vertex
= info
.max_index
+ 1;
907 partial_inbounds
= 0;
909 /* Copy only needed vertices to scratch space */
910 size
= AlignUp(size
, 4);
911 const void *ptr
= (const uint8_t *) vb
->user_buffer
912 + info
.min_index
* pitch
;
913 ptr
= swr_copy_to_scratch_space(
914 ctx
, &ctx
->scratch
->vertex_buffer
, ptr
, size
);
915 p_data
= (const uint8_t *)ptr
- info
.min_index
* pitch
;
918 swrVertexBuffers
[i
] = {0};
919 swrVertexBuffers
[i
].index
= i
;
920 swrVertexBuffers
[i
].pitch
= pitch
;
921 swrVertexBuffers
[i
].pData
= p_data
;
922 swrVertexBuffers
[i
].size
= size
;
923 swrVertexBuffers
[i
].maxVertex
= max_vertex
;
924 swrVertexBuffers
[i
].partialInboundsSize
= partial_inbounds
;
928 ctx
->swrContext
, ctx
->num_vertex_buffers
, swrVertexBuffers
);
930 /* index buffer, if required (info passed in by swr_draw_vbo) */
931 SWR_FORMAT index_type
= R32_UINT
; /* Default for non-indexed draws */
933 struct pipe_index_buffer
*ib
= &ctx
->index_buffer
;
935 pitch
= ib
->index_size
? ib
->index_size
: sizeof(uint32_t);
936 index_type
= swr_convert_index_type(pitch
);
938 if (!ib
->user_buffer
) {
940 * size is based on buffer->width0 rather than info.count
941 * to prevent having to validate VBO on each draw */
942 size
= ib
->buffer
->width0
;
944 (const uint8_t *)swr_resource_data(ib
->buffer
) + ib
->offset
;
947 * client memory is one-time use, re-trigger SWR_NEW_VERTEX to
948 * revalidate on each draw */
949 post_update_dirty_flags
|= SWR_NEW_VERTEX
;
951 size
= info
.count
* pitch
;
952 size
= AlignUp(size
, 4);
954 /* Copy indices to scratch space */
955 const void *ptr
= ib
->user_buffer
;
956 ptr
= swr_copy_to_scratch_space(
957 ctx
, &ctx
->scratch
->index_buffer
, ptr
, size
);
958 p_data
= (const uint8_t *)ptr
;
961 SWR_INDEX_BUFFER_STATE swrIndexBuffer
;
962 swrIndexBuffer
.format
= swr_convert_index_type(ib
->index_size
);
963 swrIndexBuffer
.pIndices
= p_data
;
964 swrIndexBuffer
.size
= size
;
966 SwrSetIndexBuffer(ctx
->swrContext
, &swrIndexBuffer
);
969 struct swr_vertex_element_state
*velems
= ctx
->velems
;
970 if (velems
&& velems
->fsState
.indexType
!= index_type
) {
971 velems
->fsFunc
= NULL
;
972 velems
->fsState
.indexType
= index_type
;
977 if (ctx
->dirty
& (SWR_NEW_VS
| SWR_NEW_FRAMEBUFFER
)) {
978 SwrSetVertexFunc(ctx
->swrContext
, ctx
->vs
->func
);
982 if (ctx
->dirty
& (SWR_NEW_FS
| SWR_NEW_SAMPLER
| SWR_NEW_SAMPLER_VIEW
983 | SWR_NEW_RASTERIZER
| SWR_NEW_FRAMEBUFFER
)) {
984 memset(&key
, 0, sizeof(key
));
985 swr_generate_fs_key(key
, ctx
, ctx
->fs
);
986 auto search
= ctx
->fs
->map
.find(key
);
987 PFN_PIXEL_KERNEL func
;
988 if (search
!= ctx
->fs
->map
.end()) {
989 func
= search
->second
;
991 func
= swr_compile_fs(ctx
, key
);
992 ctx
->fs
->map
.insert(std::make_pair(key
, func
));
994 SWR_PS_STATE psState
= {0};
995 psState
.pfnPixelShader
= func
;
996 psState
.killsPixel
= ctx
->fs
->info
.base
.uses_kill
;
997 psState
.inputCoverage
= SWR_INPUT_COVERAGE_NORMAL
;
998 psState
.writesODepth
= ctx
->fs
->info
.base
.writes_z
;
999 psState
.usesSourceDepth
= ctx
->fs
->info
.base
.reads_z
;
1000 psState
.shadingRate
= SWR_SHADING_RATE_PIXEL
; // XXX
1001 psState
.numRenderTargets
= ctx
->framebuffer
.nr_cbufs
;
1002 psState
.posOffset
= SWR_PS_POSITION_SAMPLE_NONE
; // XXX msaa
1003 uint32_t barycentricsMask
= 0;
1005 // when we switch to mesa-master
1006 if (ctx
->fs
->info
.base
.uses_persp_center
||
1007 ctx
->fs
->info
.base
.uses_linear_center
)
1008 barycentricsMask
|= SWR_BARYCENTRIC_PER_PIXEL_MASK
;
1009 if (ctx
->fs
->info
.base
.uses_persp_centroid
||
1010 ctx
->fs
->info
.base
.uses_linear_centroid
)
1011 barycentricsMask
|= SWR_BARYCENTRIC_CENTROID_MASK
;
1012 if (ctx
->fs
->info
.base
.uses_persp_sample
||
1013 ctx
->fs
->info
.base
.uses_linear_sample
)
1014 barycentricsMask
|= SWR_BARYCENTRIC_PER_SAMPLE_MASK
;
1016 for (unsigned i
= 0; i
< ctx
->fs
->info
.base
.num_inputs
; i
++) {
1017 switch (ctx
->fs
->info
.base
.input_interpolate_loc
[i
]) {
1018 case TGSI_INTERPOLATE_LOC_CENTER
:
1019 barycentricsMask
|= SWR_BARYCENTRIC_PER_PIXEL_MASK
;
1021 case TGSI_INTERPOLATE_LOC_CENTROID
:
1022 barycentricsMask
|= SWR_BARYCENTRIC_CENTROID_MASK
;
1024 case TGSI_INTERPOLATE_LOC_SAMPLE
:
1025 barycentricsMask
|= SWR_BARYCENTRIC_PER_SAMPLE_MASK
;
1030 psState
.barycentricsMask
= barycentricsMask
;
1031 psState
.usesUAV
= false; // XXX
1032 psState
.forceEarlyZ
= false;
1033 SwrSetPixelShaderState(ctx
->swrContext
, &psState
);
1036 /* JIT sampler state */
1037 if (ctx
->dirty
& SWR_NEW_SAMPLER
) {
1038 swr_draw_context
*pDC
= &ctx
->swrDC
;
1040 for (unsigned i
= 0; i
< key
.nr_samplers
; i
++) {
1041 const struct pipe_sampler_state
*sampler
=
1042 ctx
->samplers
[PIPE_SHADER_FRAGMENT
][i
];
1045 pDC
->samplersFS
[i
].min_lod
= sampler
->min_lod
;
1046 pDC
->samplersFS
[i
].max_lod
= sampler
->max_lod
;
1047 pDC
->samplersFS
[i
].lod_bias
= sampler
->lod_bias
;
1048 COPY_4V(pDC
->samplersFS
[i
].border_color
, sampler
->border_color
.f
);
1053 /* JIT sampler view state */
1054 if (ctx
->dirty
& (SWR_NEW_SAMPLER_VIEW
| SWR_NEW_FRAMEBUFFER
)) {
1055 swr_draw_context
*pDC
= &ctx
->swrDC
;
1057 for (unsigned i
= 0; i
< key
.nr_sampler_views
; i
++) {
1058 struct pipe_sampler_view
*view
=
1059 ctx
->sampler_views
[PIPE_SHADER_FRAGMENT
][i
];
1062 struct pipe_resource
*res
= view
->texture
;
1063 struct swr_resource
*swr_res
= swr_resource(res
);
1064 struct swr_jit_texture
*jit_tex
= &pDC
->texturesFS
[i
];
1065 memset(jit_tex
, 0, sizeof(*jit_tex
));
1066 jit_tex
->width
= res
->width0
;
1067 jit_tex
->height
= res
->height0
;
1068 jit_tex
->depth
= res
->depth0
;
1069 jit_tex
->first_level
= view
->u
.tex
.first_level
;
1070 jit_tex
->last_level
= view
->u
.tex
.last_level
;
1071 jit_tex
->base_ptr
= swr_res
->swr
.pBaseAddress
;
1073 for (unsigned level
= jit_tex
->first_level
;
1074 level
<= jit_tex
->last_level
;
1076 jit_tex
->row_stride
[level
] = swr_res
->row_stride
[level
];
1077 jit_tex
->img_stride
[level
] = swr_res
->img_stride
[level
];
1078 jit_tex
->mip_offsets
[level
] = swr_res
->mip_offsets
[level
];
1084 /* VertexShader Constants */
1085 if (ctx
->dirty
& SWR_NEW_VSCONSTANTS
) {
1086 swr_draw_context
*pDC
= &ctx
->swrDC
;
1088 for (UINT i
= 0; i
< PIPE_MAX_CONSTANT_BUFFERS
; i
++) {
1089 const pipe_constant_buffer
*cb
=
1090 &ctx
->constants
[PIPE_SHADER_VERTEX
][i
];
1091 pDC
->num_constantsVS
[i
] = cb
->buffer_size
;
1093 pDC
->constantVS
[i
] =
1094 (const float *)((const uint8_t *)cb
->buffer
+ cb
->buffer_offset
);
1096 /* Need to copy these constants to scratch space */
1097 if (cb
->user_buffer
&& cb
->buffer_size
) {
1099 ((const uint8_t *)cb
->user_buffer
+ cb
->buffer_offset
);
1100 uint32_t size
= AlignUp(cb
->buffer_size
, 4);
1101 ptr
= swr_copy_to_scratch_space(
1102 ctx
, &ctx
->scratch
->vs_constants
, ptr
, size
);
1103 pDC
->constantVS
[i
] = (const float *)ptr
;
1109 /* FragmentShader Constants */
1110 if (ctx
->dirty
& SWR_NEW_FSCONSTANTS
) {
1111 swr_draw_context
*pDC
= &ctx
->swrDC
;
1113 for (UINT i
= 0; i
< PIPE_MAX_CONSTANT_BUFFERS
; i
++) {
1114 const pipe_constant_buffer
*cb
=
1115 &ctx
->constants
[PIPE_SHADER_FRAGMENT
][i
];
1116 pDC
->num_constantsFS
[i
] = cb
->buffer_size
;
1118 pDC
->constantFS
[i
] =
1119 (const float *)((const uint8_t *)cb
->buffer
+ cb
->buffer_offset
);
1121 /* Need to copy these constants to scratch space */
1122 if (cb
->user_buffer
&& cb
->buffer_size
) {
1124 ((const uint8_t *)cb
->user_buffer
+ cb
->buffer_offset
);
1125 uint32_t size
= AlignUp(cb
->buffer_size
, 4);
1126 ptr
= swr_copy_to_scratch_space(
1127 ctx
, &ctx
->scratch
->fs_constants
, ptr
, size
);
1128 pDC
->constantFS
[i
] = (const float *)ptr
;
1134 /* Depth/stencil state */
1135 if (ctx
->dirty
& (SWR_NEW_DEPTH_STENCIL_ALPHA
| SWR_NEW_FRAMEBUFFER
)) {
1136 struct pipe_depth_state
*depth
= &(ctx
->depth_stencil
->depth
);
1137 struct pipe_stencil_state
*stencil
= ctx
->depth_stencil
->stencil
;
1138 SWR_DEPTH_STENCIL_STATE depthStencilState
= {{0}};
1140 /* XXX, incomplete. Need to flesh out stencil & alpha test state
1141 struct pipe_stencil_state *front_stencil =
1142 ctx->depth_stencil.stencil[0];
1143 struct pipe_stencil_state *back_stencil = ctx->depth_stencil.stencil[1];
1144 struct pipe_alpha_state alpha;
1146 if (stencil
[0].enabled
) {
1147 depthStencilState
.stencilWriteEnable
= 1;
1148 depthStencilState
.stencilTestEnable
= 1;
1149 depthStencilState
.stencilTestFunc
=
1150 swr_convert_depth_func(stencil
[0].func
);
1152 depthStencilState
.stencilPassDepthPassOp
=
1153 swr_convert_stencil_op(stencil
[0].zpass_op
);
1154 depthStencilState
.stencilPassDepthFailOp
=
1155 swr_convert_stencil_op(stencil
[0].zfail_op
);
1156 depthStencilState
.stencilFailOp
=
1157 swr_convert_stencil_op(stencil
[0].fail_op
);
1158 depthStencilState
.stencilWriteMask
= stencil
[0].writemask
;
1159 depthStencilState
.stencilTestMask
= stencil
[0].valuemask
;
1160 depthStencilState
.stencilRefValue
= ctx
->stencil_ref
.ref_value
[0];
1162 if (stencil
[1].enabled
) {
1163 depthStencilState
.doubleSidedStencilTestEnable
= 1;
1165 depthStencilState
.backfaceStencilTestFunc
=
1166 swr_convert_depth_func(stencil
[1].func
);
1168 depthStencilState
.backfaceStencilPassDepthPassOp
=
1169 swr_convert_stencil_op(stencil
[1].zpass_op
);
1170 depthStencilState
.backfaceStencilPassDepthFailOp
=
1171 swr_convert_stencil_op(stencil
[1].zfail_op
);
1172 depthStencilState
.backfaceStencilFailOp
=
1173 swr_convert_stencil_op(stencil
[1].fail_op
);
1174 depthStencilState
.backfaceStencilWriteMask
= stencil
[1].writemask
;
1175 depthStencilState
.backfaceStencilTestMask
= stencil
[1].valuemask
;
1177 depthStencilState
.backfaceStencilRefValue
=
1178 ctx
->stencil_ref
.ref_value
[1];
1181 depthStencilState
.depthTestEnable
= depth
->enabled
;
1182 depthStencilState
.depthTestFunc
= swr_convert_depth_func(depth
->func
);
1183 depthStencilState
.depthWriteEnable
= depth
->writemask
;
1184 SwrSetDepthStencilState(ctx
->swrContext
, &depthStencilState
);
1188 if (ctx
->dirty
& (SWR_NEW_BLEND
|
1189 SWR_NEW_FRAMEBUFFER
|
1190 SWR_NEW_DEPTH_STENCIL_ALPHA
)) {
1191 struct pipe_framebuffer_state
*fb
= &ctx
->framebuffer
;
1193 SWR_BLEND_STATE blendState
;
1194 memcpy(&blendState
, &ctx
->blend
->blendState
, sizeof(blendState
));
1195 blendState
.constantColor
[0] = ctx
->blend_color
.color
[0];
1196 blendState
.constantColor
[1] = ctx
->blend_color
.color
[1];
1197 blendState
.constantColor
[2] = ctx
->blend_color
.color
[2];
1198 blendState
.constantColor
[3] = ctx
->blend_color
.color
[3];
1199 blendState
.alphaTestReference
=
1200 *((uint32_t*)&ctx
->depth_stencil
->alpha
.ref_value
);
1203 blendState
.sampleMask
= 0;
1204 blendState
.sampleCount
= SWR_MULTISAMPLE_1X
;
1206 /* If there are no color buffers bound, disable writes on RT0
1208 if (fb
->nr_cbufs
== 0) {
1209 blendState
.renderTarget
[0].writeDisableRed
= 1;
1210 blendState
.renderTarget
[0].writeDisableGreen
= 1;
1211 blendState
.renderTarget
[0].writeDisableBlue
= 1;
1212 blendState
.renderTarget
[0].writeDisableAlpha
= 1;
1213 SwrSetBlendFunc(ctx
->swrContext
, 0, NULL
);
1216 for (int target
= 0;
1217 target
< std::min(SWR_NUM_RENDERTARGETS
,
1218 PIPE_MAX_COLOR_BUFS
);
1220 if (!fb
->cbufs
[target
])
1223 struct swr_resource
*colorBuffer
=
1224 swr_resource(fb
->cbufs
[target
]->texture
);
1226 BLEND_COMPILE_STATE compileState
;
1227 memset(&compileState
, 0, sizeof(compileState
));
1228 compileState
.format
= colorBuffer
->swr
.format
;
1229 memcpy(&compileState
.blendState
,
1230 &ctx
->blend
->compileState
[target
],
1231 sizeof(compileState
.blendState
));
1233 if (compileState
.blendState
.blendEnable
== false &&
1234 compileState
.blendState
.logicOpEnable
== false) {
1235 SwrSetBlendFunc(ctx
->swrContext
, target
, NULL
);
1239 compileState
.desc
.alphaTestEnable
=
1240 ctx
->depth_stencil
->alpha
.enabled
;
1241 compileState
.desc
.independentAlphaBlendEnable
=
1242 ctx
->blend
->pipe
.independent_blend_enable
;
1243 compileState
.desc
.alphaToCoverageEnable
=
1244 ctx
->blend
->pipe
.alpha_to_coverage
;
1245 compileState
.desc
.sampleMaskEnable
= 0; // XXX
1246 compileState
.desc
.numSamples
= 1; // XXX
1248 compileState
.alphaTestFunction
=
1249 swr_convert_depth_func(ctx
->depth_stencil
->alpha
.func
);
1250 compileState
.alphaTestFormat
= ALPHA_TEST_FLOAT32
; // xxx
1252 PFN_BLEND_JIT_FUNC func
= NULL
;
1253 auto search
= ctx
->blendJIT
->find(compileState
);
1254 if (search
!= ctx
->blendJIT
->end()) {
1255 func
= search
->second
;
1257 HANDLE hJitMgr
= screen
->hJitMgr
;
1258 func
= JitCompileBlend(hJitMgr
, compileState
);
1259 debug_printf("BLEND shader %p\n", func
);
1260 assert(func
&& "Error: BlendShader = NULL");
1262 ctx
->blendJIT
->insert(std::make_pair(compileState
, func
));
1264 SwrSetBlendFunc(ctx
->swrContext
, target
, func
);
1267 SwrSetBlendState(ctx
->swrContext
, &blendState
);
1270 if (ctx
->dirty
& SWR_NEW_STIPPLE
) {
1271 /* XXX What to do with this one??? SWR doesn't stipple */
1274 if (ctx
->dirty
& (SWR_NEW_VS
| SWR_NEW_SO
| SWR_NEW_RASTERIZER
)) {
1275 ctx
->vs
->soState
.rasterizerDisable
=
1276 ctx
->rasterizer
->rasterizer_discard
;
1277 SwrSetSoState(ctx
->swrContext
, &ctx
->vs
->soState
);
1279 pipe_stream_output_info
*stream_output
= &ctx
->vs
->pipe
.stream_output
;
1281 for (uint32_t i
= 0; i
< ctx
->num_so_targets
; i
++) {
1282 SWR_STREAMOUT_BUFFER buffer
= {0};
1283 if (!ctx
->so_targets
[i
])
1285 buffer
.enable
= true;
1287 (uint32_t *)swr_resource_data(ctx
->so_targets
[i
]->buffer
);
1288 buffer
.bufferSize
= ctx
->so_targets
[i
]->buffer_size
>> 2;
1289 buffer
.pitch
= stream_output
->stride
[i
];
1290 buffer
.streamOffset
= ctx
->so_targets
[i
]->buffer_offset
>> 2;
1292 SwrSetSoBuffers(ctx
->swrContext
, &buffer
, i
);
1296 uint32_t linkage
= ctx
->vs
->linkageMask
;
1297 if (ctx
->rasterizer
->sprite_coord_enable
)
1298 linkage
|= (1 << ctx
->vs
->info
.base
.num_outputs
);
1300 SwrSetLinkage(ctx
->swrContext
, linkage
, NULL
);
1302 // set up frontend state
1303 SWR_FRONTEND_STATE feState
= {0};
1304 SwrSetFrontendState(ctx
->swrContext
, &feState
);
1306 // set up backend state
1307 SWR_BACKEND_STATE backendState
= {0};
1308 backendState
.numAttributes
= 1;
1309 backendState
.numComponents
[0] = 4;
1310 backendState
.constantInterpolationMask
= ctx
->fs
->constantMask
;
1311 backendState
.pointSpriteTexCoordMask
= ctx
->fs
->pointSpriteMask
;
1313 SwrSetBackendState(ctx
->swrContext
, &backendState
);
1315 /* Ensure that any in-progress attachment change StoreTiles finish */
1316 if (swr_is_fence_pending(screen
->flush_fence
))
1317 swr_fence_finish(pipe
->screen
, screen
->flush_fence
, 0);
1319 /* Finally, update the in-use status of all resources involved in draw */
1320 swr_update_resource_status(pipe
, p_draw_info
);
1322 ctx
->dirty
= post_update_dirty_flags
;
1326 static struct pipe_stream_output_target
*
1327 swr_create_so_target(struct pipe_context
*pipe
,
1328 struct pipe_resource
*buffer
,
1329 unsigned buffer_offset
,
1330 unsigned buffer_size
)
1332 struct pipe_stream_output_target
*target
;
1334 target
= CALLOC_STRUCT(pipe_stream_output_target
);
1338 target
->context
= pipe
;
1339 target
->reference
.count
= 1;
1340 pipe_resource_reference(&target
->buffer
, buffer
);
1341 target
->buffer_offset
= buffer_offset
;
1342 target
->buffer_size
= buffer_size
;
1347 swr_destroy_so_target(struct pipe_context
*pipe
,
1348 struct pipe_stream_output_target
*target
)
1350 pipe_resource_reference(&target
->buffer
, NULL
);
1355 swr_set_so_targets(struct pipe_context
*pipe
,
1356 unsigned num_targets
,
1357 struct pipe_stream_output_target
**targets
,
1358 const unsigned *offsets
)
1360 struct swr_context
*swr
= swr_context(pipe
);
1363 assert(num_targets
< MAX_SO_STREAMS
);
1365 for (i
= 0; i
< num_targets
; i
++) {
1366 pipe_so_target_reference(
1367 (struct pipe_stream_output_target
**)&swr
->so_targets
[i
],
1371 for (/* fall-through */; i
< swr
->num_so_targets
; i
++) {
1372 pipe_so_target_reference(
1373 (struct pipe_stream_output_target
**)&swr
->so_targets
[i
], NULL
);
1376 swr
->num_so_targets
= num_targets
;
1378 swr
->dirty
= SWR_NEW_SO
;
1383 swr_state_init(struct pipe_context
*pipe
)
1385 pipe
->create_blend_state
= swr_create_blend_state
;
1386 pipe
->bind_blend_state
= swr_bind_blend_state
;
1387 pipe
->delete_blend_state
= swr_delete_blend_state
;
1389 pipe
->create_depth_stencil_alpha_state
= swr_create_depth_stencil_state
;
1390 pipe
->bind_depth_stencil_alpha_state
= swr_bind_depth_stencil_state
;
1391 pipe
->delete_depth_stencil_alpha_state
= swr_delete_depth_stencil_state
;
1393 pipe
->create_rasterizer_state
= swr_create_rasterizer_state
;
1394 pipe
->bind_rasterizer_state
= swr_bind_rasterizer_state
;
1395 pipe
->delete_rasterizer_state
= swr_delete_rasterizer_state
;
1397 pipe
->create_sampler_state
= swr_create_sampler_state
;
1398 pipe
->bind_sampler_states
= swr_bind_sampler_states
;
1399 pipe
->delete_sampler_state
= swr_delete_sampler_state
;
1401 pipe
->create_sampler_view
= swr_create_sampler_view
;
1402 pipe
->set_sampler_views
= swr_set_sampler_views
;
1403 pipe
->sampler_view_destroy
= swr_sampler_view_destroy
;
1405 pipe
->create_vs_state
= swr_create_vs_state
;
1406 pipe
->bind_vs_state
= swr_bind_vs_state
;
1407 pipe
->delete_vs_state
= swr_delete_vs_state
;
1409 pipe
->create_fs_state
= swr_create_fs_state
;
1410 pipe
->bind_fs_state
= swr_bind_fs_state
;
1411 pipe
->delete_fs_state
= swr_delete_fs_state
;
1413 pipe
->set_constant_buffer
= swr_set_constant_buffer
;
1415 pipe
->create_vertex_elements_state
= swr_create_vertex_elements_state
;
1416 pipe
->bind_vertex_elements_state
= swr_bind_vertex_elements_state
;
1417 pipe
->delete_vertex_elements_state
= swr_delete_vertex_elements_state
;
1419 pipe
->set_vertex_buffers
= swr_set_vertex_buffers
;
1420 pipe
->set_index_buffer
= swr_set_index_buffer
;
1422 pipe
->set_polygon_stipple
= swr_set_polygon_stipple
;
1423 pipe
->set_clip_state
= swr_set_clip_state
;
1424 pipe
->set_scissor_states
= swr_set_scissor_states
;
1425 pipe
->set_viewport_states
= swr_set_viewport_states
;
1427 pipe
->set_framebuffer_state
= swr_set_framebuffer_state
;
1429 pipe
->set_blend_color
= swr_set_blend_color
;
1430 pipe
->set_stencil_ref
= swr_set_stencil_ref
;
1432 pipe
->set_sample_mask
= swr_set_sample_mask
;
1434 pipe
->create_stream_output_target
= swr_create_so_target
;
1435 pipe
->stream_output_target_destroy
= swr_destroy_so_target
;
1436 pipe
->set_stream_output_targets
= swr_set_so_targets
;